Skip to content

Commit

Permalink
something weird going on with the list
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 31, 2024
1 parent 3b6c2d8 commit a02ddad
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 166 deletions.
33 changes: 8 additions & 25 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -318,24 +307,22 @@ 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

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

Expand All @@ -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
8 changes: 5 additions & 3 deletions src/cub3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/parser/check_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 0 additions & 45 deletions src/parser/extra_lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
44 changes: 0 additions & 44 deletions src/parser/flag_lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
126 changes: 104 additions & 22 deletions src/parser/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
18 changes: 5 additions & 13 deletions src/parser/lexer_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit a02ddad

Please sign in to comment.