diff --git a/include/meta.h b/include/meta.h index fc880c5..7504092 100644 --- a/include/meta.h +++ b/include/meta.h @@ -181,21 +181,11 @@ typedef struct s_map { typedef struct s_flag { - char *flag; - char *content; + char *flag; + char *content; struct s_flag *next; } t_flag; - -typedef struct s_lex { - t_flag n; - t_flag s; - t_flag e; - t_flag w; - t_flag c; - t_flag f; -} t_lex; - typedef struct s_sprite { t_vec2d pos; mlx_texture_t *tex; @@ -239,8 +229,7 @@ typedef struct s_meta { t_attr attributes; const char *scene_name; char *map_element; - t_lex lexer; - t_flag *extras; + t_flag *elements; } t_meta; @@ -318,16 +307,14 @@ void sprite_sort(double *sprite_dist, int32_t *sprite_order); // lexer.c -char *extract_file(char *map_file); -bool mandatory_elements(t_lex *lex); -int lex(char *file, t_lex *lexer, t_map *map, t_flag **extras); -int lexer(t_meta *meta, char *map_file); +char *extract_file(char *map_file); +int lex(char *file, t_map *map, t_flag **elements); +int lexer(t_meta *meta, char *map_file); // lexer_utils.c -void print_lexer_mandatory(t_lex *lexer); void print_lexer_map(t_map *map); -void print_lexer_extras(t_flag **extras); +void print_lexer_elements(t_flag *elements); // map_lexer.c @@ -335,7 +322,7 @@ bool nl_only_spaces(char *file); int end_of_map(char *file); void skip_map_element(char **file, int *skip); int input_map_lexer(char *file, t_map *map); -int map_lex(char **file, t_map *map, t_lex *lexer, int *skip); +int map_lex(char **file, t_map *map, int *skip, int mandatory); // extra_lexer.c @@ -344,10 +331,6 @@ int save_extra_title(t_flag **extras, char **file); bool is_valid_extra(char *file); int lexer_input_extra(t_flag **extras, char *file, int *skip); -//flag_lexer.c - -char *get_val(char *file); -int input_lexer(t_lex *lex, char *file, int *skip); #endif diff --git a/src/cub3d.c b/src/cub3d.c index c43a422..081c8bf 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -106,10 +106,12 @@ int cub3d(int argc, char **argv) if (lexer(&meta, argv[1])) return (EXIT_FAILURE); - print_lexer_mandatory(&meta.lexer); print_lexer_map(&meta.map); - print_lexer_extras(&meta.extras); - return 0; + printf("!!!: %s\n", meta.elements->flag); + print_lexer_elements(meta.elements); + // free stuff + return (0); + if (parser(&meta, argv[1])) return(meta_free(&meta), EXIT_FAILURE); if (set_textures(&meta.attributes)) diff --git a/src/parser/check_elements.c b/src/parser/check_elements.c index 8dfcc6a..b889dea 100644 --- a/src/parser/check_elements.c +++ b/src/parser/check_elements.c @@ -15,7 +15,7 @@ // checks if the element abbreviations are valid bool is_valid_element(char *file) { - char*el[6] = {"NO", "SO", "WE", "EA", "F", "C"}; + char *el[6] = {"NO", "SO", "WE", "EA", "F", "C"}; size_t i; i = 0; diff --git a/src/parser/extra_lexer.c b/src/parser/extra_lexer.c index 04e030c..3f17bce 100644 --- a/src/parser/extra_lexer.c +++ b/src/parser/extra_lexer.c @@ -32,48 +32,3 @@ char *get_title_val(char *file) return (ft_strdup("")); } -// saves the flag of the extra (for now only sprites called "OBJ") -int save_extra_title(t_flag **extras, char **file) -{ - t_flag *new_node; - int i; - - i = 0; - new_node = malloc(sizeof(t_flag) * 1); - if (!new_node) - return (pr_err(MALL_ERR), EXIT_FAILURE); - new_node->next = NULL; - new_node->flag = get_title_val(*file); - if (*extras == NULL) - *extras = new_node; - else - { - while ((*extras)->next == NULL) - *extras = (*extras)->next; - *extras = new_node; - } - while (new_node->flag[i]) - { - i++; - (*file)++; - } - return (EXIT_SUCCESS); -} - -bool is_valid_extra(char *file) -{ - if (*file && !ft_strncmp(file, "OBJ", 3)) - return (true); - return (false); -} - -// inputs extras into the extras linked list (for now only sprites) -int lexer_input_extra(t_flag **extras, char *file, int *skip) -{ - while (*extras && (*extras)->next != NULL) - *extras = (*extras)->next; - (*extras)->content = get_val(file); - (*extras)->next = NULL; - *skip = 1; - return (EXIT_SUCCESS); -} \ No newline at end of file diff --git a/src/parser/flag_lexer.c b/src/parser/flag_lexer.c index 239bc3d..a0ce951 100644 --- a/src/parser/flag_lexer.c +++ b/src/parser/flag_lexer.c @@ -13,47 +13,3 @@ #include "meta.h" #include "parser.h" -char *get_val(char *file) -{ - int i; - char *val; - - i = 0; - skip_spaces(&file); - while (file[i] && file[i] != '\n') - i++; - if (i) - { - val = ft_substr(file, 0, i); - if (!val) - return (NULL); - return (val); - } - return (ft_strdup("")); -} - -int input_lexer(t_lex *lex, char *file, int *skip) -{ - char element[6] = {'N', 'S', 'W', 'E', 'F', 'C'}; - char** content[6] = {&lex->n.content, &lex->s.content, &lex->w.content, &lex->e.content, &lex->c.content, &lex->f.content}; - int i; - - i = 0; - *skip = 1; - skip_spaces(&file); - while (i < 6) - { - if (*file && *file == element[i]) - { - if (*content[i]) - return (pr_err(DUP_ELEMENTS)); - *content[i] = get_val(file + 2); - if (!(content[i])) - return (pr_err(MALL_ERR), EXIT_FAILURE); - if (!ft_strncmp(*content[i], "", 1)) - return (pr_err(M_PATH), EXIT_FAILURE); - } - i++; - } - return (EXIT_SUCCESS); -} \ No newline at end of file diff --git a/src/parser/lexer.c b/src/parser/lexer.c index c0d486e..bc07111 100644 --- a/src/parser/lexer.c +++ b/src/parser/lexer.c @@ -29,46 +29,128 @@ char *extract_file(char *map_file) return (file); } -bool mandatory_elements(t_lex *lex) +char *get_key(char *file) { - int i; - char* flags[6] = {lex->n.content, lex->s.content, lex->w.content, lex->e.content, lex->c.content, lex->f.content}; + int i; + char *key; i = 0; - while (i < 6) - { - if (flags[i] == NULL) - return (false); + while (file[i] != ' ') i++; + if (i) + { + key = ft_substr(file, 0, i); + if (!key) + return (pr_err(MALL_ERR), NULL); + return (key); + } + return (ft_strdup("")); +} + +char *get_val(char *file) +{ + int i; + int j; + char *val; + + i = 0; + j = 0; + while (file[j] != ' ') + j++; + while (file[j] == ' ') + j++; + i = j; + while (file[j] != '\n' && file[j]) + j++; + if (i < j) + { val = ft_substr(file, i, j); + if (!val) + return (pr_err(MALL_ERR), NULL); + return (val); + } + return (ft_strdup("")); +} + +t_flag *create_new_node(char *file) +{ + t_flag *node; + + node = malloc(sizeof(t_flag) * 1); + node->flag = get_key(file); + if (!node->flag) + return (NULL); + node->content = get_val(file); + if (!node->content) + return (free(node->flag), NULL); + node->next = NULL; + return (node); +} + +void add_to_list (t_flag **elements, t_flag *new_node) +{ + t_flag **list; + + list = elements; + if (!*list) + { + *list = new_node; + } + else + { + while ((*list)->next != NULL) + *list = (*list)->next; + (*list)->next = new_node; + } +} + +bool is_duplicate_flag(t_flag *elements, char *key) +{ + while (elements != NULL) + { + if (!ft_strncmp(elements->flag, key, ft_strlen(key))) + return (true); + elements = elements->next; } + return (false); +} + +// if key is not a duplicate and is mandatory +bool is_valid_key(t_flag **elements, t_flag *new_node) +{ + if (is_duplicate_flag(*elements, new_node->flag) && is_valid_element(new_node->flag)) + return (false); return (true); } -int lex(char *file, t_lex *lexer, t_map *map, t_flag **extras) +int lex(char *file, t_map *map, t_flag **elements) { + t_flag *new_node; int exit_code; int skip; + int mandatory = 0; while (*file) { exit_code = 0; skip = 1; skip_spaces(&file); - if (is_valid_element(file)) - { - exit_code = input_lexer(lexer, file, &skip); - } - else if (is_map_element(file) && !only_spaces(file)) - { - exit_code = map_lex(&file, map, lexer, &skip); - } - else if (is_valid_extra(file) && !save_extra_title(extras, &file)) + if (is_map_element(file) && !only_spaces(file)) { - exit_code = lexer_input_extra(extras, file, &skip); + exit_code = map_lex(&file, map, &skip, mandatory); } - else if (!only_spaces(file)) - { - exit_code = pr_err(INV_ELE); + else if (only_spaces(file)) + skip = 1; + else + { + new_node = create_new_node(file); + if (!new_node) + return (pr_err(MALL_ERR)); + if (is_valid_key(elements, new_node)) + mandatory++; + else + return (free(new_node->flag), free(new_node->content), \ + free(new_node), pr_err(DUP_ELEMENTS)); + add_to_list(elements, new_node); } if (exit_code) return (EXIT_FAILURE); @@ -87,7 +169,7 @@ int lexer(t_meta *meta, char *map_file) file = extract_file(map_file); if (!file) return(EXIT_FAILURE); - if (lex(file, &meta->lexer, &meta->map, &meta->extras)) + if (lex(file, &meta->map, &meta->elements)) return (EXIT_FAILURE); //also free everything in case of error! return (EXIT_SUCCESS); } diff --git a/src/parser/lexer_utils.c b/src/parser/lexer_utils.c index 47e3315..81cb49d 100644 --- a/src/parser/lexer_utils.c +++ b/src/parser/lexer_utils.c @@ -13,26 +13,18 @@ #include "meta.h" #include "parser.h" -void print_lexer_mandatory(t_lex *lexer) -{ - printf("NO: %s\n", lexer->n.content); - printf("SO: %s\n", lexer->s.content); - printf("EA: %s\n", lexer->e.content); - printf("WE: %s\n", lexer->w.content); - printf("F: %s\n", lexer->c.content); - printf("C: %s\n", lexer->f.content); -} void print_lexer_map(t_map *map) { printf("MAP: %s\n", map->map_element); } -void print_lexer_extras(t_flag **extras) +void print_lexer_elements(t_flag *elements) { - while (*extras) + while (elements != NULL) { - printf("OBJ: %s\n", (*extras)->content); - *extras = (*extras)->next; + printf("%s: ", elements->flag); + printf("%s\n", elements->content); + elements = elements->next; } } \ No newline at end of file diff --git a/src/parser/map_lexer.c b/src/parser/map_lexer.c index f1b9a9d..8d3a795 100644 --- a/src/parser/map_lexer.c +++ b/src/parser/map_lexer.c @@ -81,7 +81,7 @@ int input_map_lexer(char *file, t_map *map) // map_lex returns an error if 1) the map element exists already (that means there are two maps in the file) // 2) if the map element is reached before all mandatory elements are found (N, S, E, W) - wrong order -int map_lex(char **file, t_map *map, t_lex *lexer, int *skip) +int map_lex(char **file, t_map *map, int *skip, int mandatory) { int exit_code; @@ -89,7 +89,7 @@ int map_lex(char **file, t_map *map, t_lex *lexer, int *skip) { exit_code = pr_err(DUP_ELEMENTS); } - else if (mandatory_elements(lexer)) + else if (mandatory == 6) { exit_code = input_map_lexer(*file, map); } diff --git a/test_maps/simple.cub b/test_maps/simple.cub index 851ad66..a326af6 100644 --- a/test_maps/simple.cub +++ b/test_maps/simple.cub @@ -1,16 +1,13 @@ -NO texture_examples/redbrick.png -WE texture_examples/purplestone.png -EA texture_examples/purplestone.png -SO texture_examples/redbrick.png -F texture_examples/redbrick.png -C texture_examples/redbrick.png +NO texture_examples/redbrick.png +WE texture_examples/purplestone.png +EA texture_examples/purplestone.png +SO texture_examples/redbrick.png +SO texture_examples/redbrick.png +F texture_examples/redbrick.png +C texture_examples/redbrick.png 11111 10001 10E01 10001 -11111 - - - -OBJ oemwiufhwe \ No newline at end of file +11111 \ No newline at end of file