X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fcommand_db.c;h=d89319df70014332f1a663e6fcf3abaabec7d782;hb=550d22ec0c3f530f5d317746f3f7e75251a1de4b;hp=cbf6959e87322ff24a9a2d7e392160482450332f;hpb=58f0067336c9239750009b3f35d18031bec8f7b5;p=plomrogue diff --git a/src/command_db.c b/src/command_db.c index cbf6959..d89319d 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -1,30 +1,64 @@ /* 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 "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose() */ +#include "misc.h" /* for try_malloc() */ -/* 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(struct World * world, 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, world, 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) +{ + struct Command * cmd_ptr = world->cmd_db->cmds; + while (1) + { + if (id == cmd_ptr->id) + { + if (strcmp(shortdsc, cmd_ptr->dsc_short)) + { + return 0; + } + return 1; + } + cmd_ptr = &cmd_ptr[1]; + } +} + + + +extern uint8_t get_command_id(struct World * world, char * dsc_short) +{ + struct Command * cmd_ptr = world->cmd_db->cmds; + while (1) + { + if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) + { + return cmd_ptr->id; + } + cmd_ptr = &cmd_ptr[1]; + } +} + + + extern char * get_command_longdsc(struct World * world, char * dsc_short) { struct Command * cmd_ptr = world->cmd_db->cmds; @@ -42,29 +76,34 @@ extern char * get_command_longdsc(struct World * world, char * dsc_short) extern void init_command_db(struct World * world) { - 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 * err_s = "Trouble in init_cmds() with textfile_sizes()."; + + char * path = "config/commands"; + FILE * file = try_fopen(path, "r", world, 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); + exit_err(textfile_sizes(file, &linemax, &lines), world, err_s); + char line[linemax + 1]; + + struct Command * cmds = try_malloc(lines * sizeof(struct Command), + world, 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(world, &cmds[i].dsc_short, " "); + copy_tokenized_string(world, &cmds[i].dsc_long, "\n"); i++; } - world->cmd_db = malloc(sizeof(struct CommandDB)); + try_fclose(file, world, f_name); + + world->cmd_db = try_malloc(sizeof(struct CommandDB), world, f_name); 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); }