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