fix: map parsing manages blank spaces better
This commit is contained in:
parent
3ab5f9cb1d
commit
39158f4890
6 changed files with 75 additions and 12 deletions
18
dev/cub.vim
18
dev/cub.vim
|
@ -1,23 +1,27 @@
|
|||
# Vim plugin to edit .cub files
|
||||
" Vim plugin to edit .cub files
|
||||
" Copy this file in ~/.vim/plugin/
|
||||
|
||||
function! CubSyntax()
|
||||
if expand('%:e') == 'cub'
|
||||
|
||||
highlight cubParameter ctermfg=blue
|
||||
syntax match cubParameter /^(NO|SO|WE|EA|F|C) .$/
|
||||
|
||||
highlight cubDefault ctermfg=white
|
||||
highlight cubDefault ctermfg=white ctermbg=red
|
||||
syntax match cubDefault /./
|
||||
|
||||
highlight cub0 ctermfg=grey
|
||||
highlight cubEmpty ctermbg=darkblue
|
||||
syntax match cubEmpty / /
|
||||
|
||||
highlight cub0 cterm=bold ctermfg=grey
|
||||
syntax match cub0 /0/
|
||||
|
||||
highlight cub1 cterm=bold ctermfg=white
|
||||
syntax match cub1 /1/
|
||||
|
||||
highlight cubP cterm=bold ctermfg=blue
|
||||
highlight cubP cterm=bold ctermfg=yellow
|
||||
syntax match cubP /[NSWE]/
|
||||
|
||||
highlight cubParameter cterm=NONE ctermfg=yellow ctermbg=black
|
||||
syntax match cubParameter /\v^(NO|SO|WE|EA|C|F).*$/
|
||||
|
||||
endif
|
||||
endfunction
|
||||
autocmd BufReadPost * call CubSyntax()
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/03 15:02:09 by mc #+# #+# */
|
||||
/* Updated: 2024/10/15 17:43:29 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/10/17 15:59:15 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -25,7 +25,8 @@ bool read_color_parameter(const char *name, t_color *dest,
|
|||
old = *stream;
|
||||
err = false;
|
||||
read_expected_string(name, stream, &err);
|
||||
read_expected_string(" ", stream, &err);
|
||||
if (!read_blank(stream))
|
||||
err = true;
|
||||
read_unsigned(&rgb[0], stream, &err);
|
||||
read_expected_string(",", stream, &err);
|
||||
read_unsigned(&rgb[1], stream, &err);
|
||||
|
@ -56,7 +57,8 @@ bool read_string_parameter(const char *name, const char **dest,
|
|||
err = false;
|
||||
r = NULL;
|
||||
read_expected_string(name, stream, &err);
|
||||
read_expected_string(" ", stream, &err);
|
||||
if (!read_blank(stream))
|
||||
err = true;
|
||||
read_until('\n', &r, stream, &err);
|
||||
read_expected_string("\n", stream, &err);
|
||||
if (!err && *dest)
|
||||
|
|
18
stream.c
18
stream.c
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 14:08:10 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/10/02 17:31:34 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/10/17 16:02:33 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -19,6 +19,7 @@ void read_expected_string(const char *str, t_stream *stream, bool *err)
|
|||
|
||||
if (*err)
|
||||
return ;
|
||||
read_blank(stream);
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
|
@ -48,6 +49,7 @@ void read_unsigned(unsigned int *dest, t_stream *stream, bool *err)
|
|||
|
||||
if (*err)
|
||||
return ;
|
||||
read_blank(stream);
|
||||
if (!is_digit(stream->str[stream->i]))
|
||||
{
|
||||
*err = true;
|
||||
|
@ -69,6 +71,7 @@ void read_until(char c, char **dest, t_stream *stream, bool *err)
|
|||
|
||||
if (*err)
|
||||
return ;
|
||||
read_blank(stream);
|
||||
len = 0;
|
||||
while (stream->str[stream->i + len] && stream->str[stream->i + len] != c)
|
||||
len++;
|
||||
|
@ -87,3 +90,16 @@ void read_until(char c, char **dest, t_stream *stream, bool *err)
|
|||
}
|
||||
(*dest)[i] = '\0';
|
||||
}
|
||||
|
||||
bool read_blank(t_stream *stream)
|
||||
{
|
||||
if (stream->str[stream->i] != ' '
|
||||
&& stream->str[stream->i] != '\t')
|
||||
return (false);
|
||||
while (
|
||||
stream->str[stream->i] == ' '
|
||||
|| stream->str[stream->i] == '\t'
|
||||
)
|
||||
stream->i++;
|
||||
return (true);
|
||||
}
|
||||
|
|
12
stream.h
12
stream.h
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/02 12:33:25 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/10/15 16:17:34 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/10/17 15:57:59 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -26,6 +26,7 @@ typedef struct s_stream
|
|||
} t_stream;
|
||||
|
||||
/// @brief Read a specific string, error if it isn't the expected string.
|
||||
/// Skip the potential blank space before the string.
|
||||
///
|
||||
/// @param str Expected string.
|
||||
/// @param stream Stream to use.
|
||||
|
@ -34,6 +35,7 @@ typedef struct s_stream
|
|||
void read_expected_string(const char *str, t_stream *stream, bool *err);
|
||||
|
||||
/// @brief Read an unsigned int (which fits the pattern /[0-9]+/).
|
||||
/// Skip the potential blank space before the string.
|
||||
///
|
||||
/// @param dest Will be set to the value of the unsigned integer.
|
||||
/// @param stream Stream to use.
|
||||
|
@ -42,6 +44,7 @@ void read_expected_string(const char *str, t_stream *stream, bool *err);
|
|||
void read_unsigned(unsigned int *dest, t_stream *stream, bool *err);
|
||||
|
||||
/// @brief Read a string until limit char or \0.
|
||||
/// Skip the potential blank space before the string.
|
||||
///
|
||||
/// @param c Limit char.
|
||||
/// @param dest Will be set to an alloc'd pointer to the read string.
|
||||
|
@ -51,4 +54,11 @@ void read_unsigned(unsigned int *dest, t_stream *stream, bool *err);
|
|||
/// If already true, the function won't do anything.
|
||||
void read_until(char c, char **dest, t_stream *stream, bool *err);
|
||||
|
||||
/// @brief Read blank space until the next non-blank character or the EOF.
|
||||
/// ' ' and '\t' are the only characters considered blank.
|
||||
///
|
||||
/// @param stream Stream to use.
|
||||
/// @return false if there was no blank space to read, true otherwise.
|
||||
bool read_blank(t_stream *stream);
|
||||
|
||||
#endif
|
||||
|
|
11
testmaps/err_nospacebeforeparameter.cub
Normal file
11
testmaps/err_nospacebeforeparameter.cub
Normal file
|
@ -0,0 +1,11 @@
|
|||
F 255,127,0
|
||||
EA eastimage
|
||||
NO theimageforthenorthwall
|
||||
C0,2,67
|
||||
SO SOUTH!!!!!!1
|
||||
WE weeeee
|
||||
1111111
|
||||
1000001
|
||||
10W0001
|
||||
1000001
|
||||
1111111
|
20
testmaps/good_reallyweirdblank.cub
Normal file
20
testmaps/good_reallyweirdblank.cub
Normal file
|
@ -0,0 +1,20 @@
|
|||
F 255, 127 ,0
|
||||
|
||||
EA eastimage
|
||||
|
||||
NO theimageforthenorthwall
|
||||
C 0, 2 , 67
|
||||
|
||||
SO SOUTH!!!!!!1
|
||||
|
||||
WE weeeee
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1111111
|
||||
1000001
|
||||
10W0001
|
||||
1000001
|
||||
1111111
|
Loading…
Add table
Reference in a new issue