From: Christian Heller Date: Wed, 4 Sep 2013 00:44:39 +0000 (+0200) Subject: Mostly cosmetic changes to various file reading/writing functions for greater readibi... X-Git-Tag: tce~979 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/test?a=commitdiff_plain;h=b97faa0009abc699ab7bff7bcc3221b8b3104e12;p=plomrogue Mostly cosmetic changes to various file reading/writing functions for greater readibility. --- diff --git a/src/command_db.c b/src/command_db.c index 25fcd24..0d4cb94 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -77,31 +77,35 @@ 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; - err = "Trouble in init_cmds() with textfile_sizes()."; - exit_err(textfile_sizes(file, &linemax, &lines), world, err); - err = "Trouble in init_cmds() with malloc()."; + exit_err(textfile_sizes(file, &linemax, &lines), world, err_s); + char * line = malloc(linemax); - exit_err(NULL == line, world, err); + exit_err(NULL == line, world, err_m); 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++; } free(line); + 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); } diff --git a/src/map_objects.c b/src/map_objects.c index d220556..1507221 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -90,22 +90,25 @@ static uint8_t read_map_objects_monsterdata (void * start, FILE * file) extern void init_map_object_defs(struct World * world, char * filename) { - world->item_def = 0; - world->monster_def = 0; - char * err = "Trouble in init_map_object_defs() with fopen()."; + char * err_o = "Trouble in init_map_object_defs() with fopen()."; + char * err_s = "Trouble in init_map_object_defs() with textfile_sizes()."; + char * err_m = "Trouble in init_map_object_defs() with malloc()/calloc()."; + char * err_c = "Trouble in init_map_object_defs() with fclose()."; + FILE * file = fopen(filename, "r"); - exit_err(NULL == file, world, err); + exit_err(NULL == file, world, err_o); uint16_t linemax; - err = "Trouble in init_map_object_defs() with textfile_sizes()."; - exit_err(textfile_sizes(file, &linemax, NULL), world, err); + exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); + struct MapObjDef mod; struct ItemDef id; struct MonsterDef md; + world->item_def = 0; + world->monster_def = 0; struct ItemDef * * p_p_id = &world->item_def; struct MonsterDef * * p_p_md = &world->monster_def; - err = "Trouble in init_map_object_defs() with malloc() or calloc()."; char * defline = malloc(linemax); - exit_err(NULL == defline, world, err); + exit_err(NULL == defline, world, err_m); char * line_p; char * delim = " "; while (fgets(defline, linemax, file)) @@ -125,13 +128,13 @@ extern void init_map_object_defs(struct World * world, char * filename) line_p = strtok(NULL, delim); } mod.desc = calloc(strlen(line_p), sizeof(char)); - exit_err(NULL == mod.desc, world, err); + exit_err(NULL == mod.desc, world, err_m); memcpy(mod.desc, line_p, strlen(line_p) - 1); if ('i' == mod.m_or_i) { id.map_obj_def = mod; * p_p_id = malloc(sizeof(struct ItemDef)); - exit_err(NULL == p_p_id, world, err); + exit_err(NULL == p_p_id, world, err_m); * * p_p_id = id; p_p_id = (struct ItemDef * *) * p_p_id; } @@ -139,14 +142,14 @@ extern void init_map_object_defs(struct World * world, char * filename) { md.map_obj_def = mod; * p_p_md = malloc(sizeof(struct MonsterDef)); - exit_err(NULL == p_p_md, world, err); + exit_err(NULL == p_p_md, world, err_m); * * p_p_md = md; p_p_md = (struct MonsterDef * *) * p_p_md; } } + free(defline); - err = "Trouble in init_map_object_defs() with fclose()."; - exit_err(fclose(file), world, err); + exit_err(fclose(file), world, err_c); } @@ -205,6 +208,7 @@ extern uint8_t write_map_objects(struct World * world, void * start, extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) { + char * err = "Trouble in read_map_objects() with get_next_map_obj()."; struct MapObj * map_obj; struct MapObjDef * mod; size_t size; @@ -212,7 +216,6 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) char first = 1; long pos; uint16_t read_uint16 = 0; - char * err = "Trouble in read_map_objects() with get_next_map_obj()."; while (1) { pos = ftell(file); @@ -268,12 +271,12 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) extern void * build_map_objects(struct World * world, void * start, char def_id, uint8_t n) { + char * err = "Trouble in build_map_objects() with get_next_map_obj()."; uint8_t i; struct MapObj * mo; char first = 1; struct MapObjDef * mod = get_map_obj_def(world, def_id); size_t size = 0; - char * err = "Trouble in build_map_objects() with get_next_map_obj()."; if ('i' == mod->m_or_i) { size = sizeof(struct Item); diff --git a/src/wincontrol.c b/src/wincontrol.c index 2a418ed..d1a0304 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -83,49 +83,50 @@ static void create_winconf(char id, struct WinConf * wcp, static void init_winconf_from_file(struct World * world, char id) { + char * err_o = "Trouble in init_win_from_file() with fopen()."; char * err_m = "Trouble in init_win_from_file() with malloc()."; + char * err_s = "Trouble in init_win_from_file() with textfile_sizes()."; + char * err_g = "Trouble in init_win_from_file() with fgets()."; + char * err_c = "Trouble in init_win_from_file() with fclose()."; + char * path = string_prefixed_id(world, "config/windows/Win_", id); - char * err = "Trouble in init_win_from_file() with fopen()."; FILE * file = fopen(path, "r"); - exit_err(NULL == file, world, err); + exit_err(NULL == file, world, err_o); free(path); - err = "Trouble in init_win_from_file() with textfile_sizes()."; - struct WinConf * winconf = get_winconf_by_id(world, id); uint16_t linemax; - exit_err(textfile_sizes(file, &linemax, NULL), world, err); + exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); char * line = malloc(linemax); exit_err(NULL == line, world, err_m); - err = "Trouble in init_win_from_file() with fgets()."; - exit_err(NULL == fgets(line, linemax, file), world, err); - winconf->title = malloc(strlen(line)); - exit_err(NULL == winconf->title, world, err_m); + struct WinConf * winconf = get_winconf_by_id(world, id); + exit_err(NULL == fgets(line, linemax, file), world, err_g); + exit_err(NULL == (winconf->title = malloc(strlen(line))), world, err_m); memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */ winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */ - exit_err(NULL == fgets(line, linemax, file), world, err); + exit_err(NULL == fgets(line, linemax, file), world, err_g); winconf->height = atoi(line); if (0 >= winconf->height) { winconf->height_type = 1; } - exit_err(NULL == fgets(line, linemax, file), world, err); + exit_err(NULL == fgets(line, linemax, file), world, err_g); winconf->width = atoi(line); if (0 >= winconf->width) { winconf->width_type = 1; } + free(line); - err = "Trouble in init_win_from_file() with fclose()."; - exit_err(fclose(file), world, err); + exit_err(fclose(file), world, err_c); } static void init_win_from_winconf(struct World * world, char id) { - struct WinConf * winconf = get_winconf_by_id(world, id); char * err = "Trouble in init_win_from_file() with init_win()."; + struct WinConf * winconf = get_winconf_by_id(world, id); exit_err(init_win(world->wmeta, &winconf->win, winconf->title, winconf->height, winconf->width, world, winconf->draw), world, err); @@ -303,17 +304,22 @@ extern void free_wins(struct World * world) extern void sorted_wintoggle(struct World * world) { - char * err = "Trouble in sorted_wintoggle() with fopen()."; - FILE * file = fopen("config/windows/toggle_order", "r"); - exit_err(NULL == file, world, err); + char * err_o = "Trouble in sorted_wintoggle() with fopen()."; + char * err_s = "Trouble in sorted_wintoggle() with textfile_sizes()."; + char * err_g = "Trouble in sorted_wintoggle() with fgets()."; + char * err_c = "Trouble in sorted_wintoggle() with fclose()."; + + char * path = "config/windows/toggle_order"; + FILE * file = fopen(path, "r"); + exit_err(NULL == file, world, err_o); + uint16_t linemax; - err = "Trouble in sorted_wintoggle() with textfile_sizes()."; - exit_err(textfile_sizes(file, &linemax, NULL), world, err); + exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); + char win_order[linemax]; - err = "Trouble in sorted_wintoggle() with fgets()."; - exit_err(NULL == fgets(win_order, linemax, file), world, err); - err = "Trouble in sorted_wintoggle() with fclose()."; - exit_err(fclose(file), world, err); + exit_err(NULL == fgets(win_order, linemax, file), world, err_g); + exit_err(fclose(file), world, err_c); + uint8_t i = 0; for (; i < linemax - 2; i++) {