X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/cards/%7B%7Bcard_id%7D%7D/static/gitweb.css?a=blobdiff_plain;f=src%2Fcommand_db.c;h=d5af5c27f8bdf8f6944f384f790e931168bc3d45;hb=2f0b15e05b0df7e10a2d0bb04ce4648455d980a8;hp=2a37b405e505326f7e6faf8293211ee44ff825d2;hpb=951248dddace9f7cadcf30700a3c3e6ad7ae2888;p=plomrogue diff --git a/src/command_db.c b/src/command_db.c index 2a37b40..d5af5c2 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -77,29 +77,33 @@ 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 * err_o = "Trouble in init_cmds() with fopen() on file 'commands'."; + char * err_s = "Trouble in init_cmds() with textfile_sizes()."; + char * err_m = "Trouble in init_cmds() with malloc()."; + char * err_c = "Trouble in init_cmds() with fclose() on file 'commands'."; + + char * path = "config/commands"; + FILE * file = fopen(path, "r"); + exit_err(NULL == file, world, err_o); 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); + exit_err(textfile_sizes(file, &linemax, &lines), world, err_s); + + char line[linemax]; struct Command * cmds = malloc(lines * sizeof(struct Command)); - exit_err(NULL == line, world, err); + exit_err(NULL == line, world, err_m); uint8_t i = 0; while (fgets(line, linemax, file)) { 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, " ", err_m); + copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err_m); i++; } + exit_err(fclose(file), world, err_c); + 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); }