Parser fucking works now

This commit is contained in:
Benjamyn Love 2023-12-04 22:27:36 +11:00
parent 59e848e399
commit f4b2cf2775

View File

@ -9,24 +9,24 @@
int ID_OFFSET; int ID_OFFSET;
struct Game struct Round
{ {
int red; int red;
int green; int green;
int blue; int blue;
}; };
struct Round struct Game
{ {
struct Game games[100]; struct Round rounds[10];
int iterator; int iterator;
int id; int id;
}; };
struct Games struct Games
{ {
struct Round rounds[10]; struct Game games[100];
int interator; int iterator;
}; };
int str_to_int(char *str) int str_to_int(char *str)
@ -48,40 +48,38 @@ char *parse_cube(char *line, int *cube_num)
return token; // Cube colour return token; // Cube colour
} }
struct Round parse_round(char *line, int id) struct Round parse_round(char *line, struct Game game)
{ {
const char delim[2] = ","; const char delim[2] = ",";
char *save_ptr; char *save_ptr;
char *token; char *token;
int cube_num; int cube_num;
struct Round round; struct Round round = {-1, -1, -1};
struct Game game = {-1, -1, -1};
round.id = id;
round.iterator = 0;
token = strtok_r(line, delim, &save_ptr); token = strtok_r(line, delim, &save_ptr);
while (token != NULL) while (token != NULL)
{ {
char *colour = parse_cube(token, &cube_num); char *colour = parse_cube(token, &cube_num);
if (!strcmp(colour, "red")) if (!strcmp(colour, "red"))
{ {
game.red = cube_num; round.red = cube_num;
} }
else if (!strcmp(colour, "blue")) else if (!strcmp(colour, "blue"))
{ {
game.blue = cube_num; round.blue = cube_num;
} }
else if (!strcmp(colour, "green")) else if (!strcmp(colour, "green"))
{ {
game.green = cube_num; round.green = cube_num;
} }
token = strtok_r(NULL, delim, &save_ptr); token = strtok_r(NULL, delim, &save_ptr);
round.games[round.iterator++] = game; game.rounds[game.iterator] = round;
} }
return round; return round;
} }
void parse_game_data(char *line, struct Games games, int id) struct Game parse_game_data(char *line, struct Game game)
{ {
// printf("%s\n", line); // printf("%s\n", line);
// https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm // https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
@ -89,10 +87,11 @@ void parse_game_data(char *line, struct Games games, int id)
char *token; char *token;
char *save_ptr; char *save_ptr;
const char delim[2] = ";"; const char delim[2] = ";";
token = strtok_r(line, delim, &save_ptr); token = strtok_r(line, delim, &save_ptr);
while (token != NULL) while (token != NULL)
{ {
games.rounds[games.interator++] = parse_round(token, id); game.rounds[game.iterator++] = parse_round(token, game);
token = strtok_r(NULL, delim, &save_ptr); token = strtok_r(NULL, delim, &save_ptr);
} }
@ -117,7 +116,7 @@ void parse_game_data(char *line, struct Games games, int id)
// token2 = strtok(token, delim2); // token2 = strtok(token, delim2);
// printf("Second token is: %s\n", token2); // printf("Second token is: %s\n", token2);
struct Game tmpGame; return game;
} }
int get_id(char *ptr) int get_id(char *ptr)
@ -143,7 +142,7 @@ int get_id(char *ptr)
int main() int main()
{ {
struct Games games; struct Games games;
games.interator = 0; games.iterator = 0;
int MAX_RED = 12; int MAX_RED = 12;
int MAX_GREEN = 13; int MAX_GREEN = 13;
int MAX_BLUE = 14; int MAX_BLUE = 14;
@ -193,8 +192,8 @@ int main()
{ {
memcpy(tmpLine, &ptr[i], j); memcpy(tmpLine, &ptr[i], j);
// printf("ID: %i |", id); // printf("ID: %i |", id);
parse_game_data(tmpLine, games, id); struct Game game = {id, -1};
exit(1); games.games[games.iterator++] = parse_game_data(tmpLine, game);
i += j; i += j;
break; break;
} }