X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fcommand_db.c;h=fd7f4dc7a2f78ae431ebd0bf37ee52bfda574da1;hb=00a7727e37e2d80ff115f03e7971a92c66edcd96;hp=2a37b405e505326f7e6faf8293211ee44ff825d2;hpb=951248dddace9f7cadcf30700a3c3e6ad7ae2888;p=plomrogue diff --git a/src/command_db.c b/src/command_db.c index 2a37b40..fd7f4dc 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -1,34 +1,35 @@ /* command.c */ #include "command_db.h" -#include /* for malloc() */ -#include /* for FILE typedef, fopen(), fclose(), fgets() */ +#include /* for free() */ +#include /* for FILE typedef, fgets() */ #include /* for uint8_t */ #include /* for strlen(), strtok() */ -#include "main.h" /* for World struct */ -#include "rexit.h" /* for exit_err() */ -#include "misc.h" /* for textfile_sizes() */ +#include "main.h" /* for world global */ +#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose() */ +#include "misc.h" /* for try_malloc() */ +#include "rexit.h" /* exit_trouble() */ -/* Build string pointed to by "ch_ptr" from next token delimited by "delim", - * exit_err() with "err" as error message on malloc() failure. - */ -static void copy_tokenized_string(struct World * world, - char ** ch_ptr, char * delim, char * err) +/* Build string pointed to by "ch_ptr" from next token delimited by "delim". */ +static void copy_tokenized_string(char ** ch_ptr, char * delim); + + + +static void copy_tokenized_string(char ** ch_ptr, char * delim) { + char * f_name = "copy_tokenized_string()"; char * dsc_ptr = strtok(NULL, delim); - * ch_ptr = malloc(strlen(dsc_ptr) + 1); - exit_err(NULL == * ch_ptr, world, err); + * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name); memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1); } -extern uint8_t is_command_id_shortdsc(struct World * world, - uint8_t id, char * shortdsc) +extern uint8_t is_command_id_shortdsc(uint8_t id, char * shortdsc) { - struct Command * cmd_ptr = world->cmd_db->cmds; + struct Command * cmd_ptr = world.cmd_db->cmds; while (1) { if (id == cmd_ptr->id) @@ -45,9 +46,9 @@ extern uint8_t is_command_id_shortdsc(struct World * world, -extern uint8_t get_command_id(struct World * world, char * dsc_short) +extern uint8_t get_command_id(char * dsc_short) { - struct Command * cmd_ptr = world->cmd_db->cmds; + struct Command * cmd_ptr = world.cmd_db->cmds; while (1) { if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) @@ -60,9 +61,9 @@ extern uint8_t get_command_id(struct World * world, char * dsc_short) -extern char * get_command_longdsc(struct World * world, char * dsc_short) +extern char * get_command_longdsc(char * dsc_short) { - struct Command * cmd_ptr = world->cmd_db->cmds; + struct Command * cmd_ptr = world.cmd_db->cmds; while (1) { if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) @@ -75,44 +76,48 @@ extern char * get_command_longdsc(struct World * world, char * dsc_short) -extern void init_command_db(struct World * world) +extern void init_command_db() { - char * err = "Trouble in init_cmds() with fopen() on file 'commands'."; - FILE * file = fopen("config/commands", "r"); - exit_err(NULL == file, world, err); + char * f_name = "init_command_db()"; + + char * path = "config/commands"; + FILE * file = try_fopen(path, "r", f_name); uint16_t lines, linemax; - textfile_sizes(file, &linemax, &lines); - err = "Trouble in init_cmds() with malloc()."; - char * line = malloc(linemax); - exit_err(NULL == line, world, err); - struct Command * cmds = malloc(lines * sizeof(struct Command)); - exit_err(NULL == line, world, err); + uint8_t test = textfile_sizes(file, &linemax, &lines); + exit_trouble(test, f_name, "textfile_sizes()"); + char line[linemax + 1]; + + struct Command * cmds = try_malloc(lines * sizeof(struct Command), f_name); uint8_t i = 0; - while (fgets(line, linemax, file)) + while (fgets(line, linemax + 1, file)) { + if ('\n' == line[0] || 0 == line[0]) + { + break; + } cmds[i].id = atoi(strtok(line, " ")); - copy_tokenized_string(world, &cmds[i].dsc_short, " ", err); - copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err); + copy_tokenized_string(&cmds[i].dsc_short, " "); + copy_tokenized_string(&cmds[i].dsc_long, "\n"); i++; } - world->cmd_db = malloc(sizeof(struct CommandDB)); - world->cmd_db->cmds = cmds; - world->cmd_db->n = lines; - err = "Trouble in init_cmds() with fclose() on file 'commands'."; - exit_err(fclose(file), world, err); + try_fclose(file, f_name); + + world.cmd_db = try_malloc(sizeof(struct CommandDB), f_name); + world.cmd_db->cmds = cmds; + world.cmd_db->n = lines; } -extern void free_command_db(struct World * world) +extern void free_command_db() { uint8_t i = 0; - while (i < world->cmd_db->n) + while (i < world.cmd_db->n) { - free(world->cmd_db->cmds[i].dsc_short); - free(world->cmd_db->cmds[i].dsc_long); + free(world.cmd_db->cmds[i].dsc_short); + free(world.cmd_db->cmds[i].dsc_long); i++; } - free(world->cmd_db->cmds); - free(world->cmd_db); + free(world.cmd_db->cmds); + free(world.cmd_db); }