From: Christian Heller Date: Sat, 31 Aug 2013 02:30:41 +0000 (+0200) Subject: Improved error handling, more error catching, error messages. X-Git-Tag: tce~988 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/tasks?a=commitdiff_plain;h=2409e8ab623f8ef1452508b0fb4a4293692ed6a7;p=plomrogue Improved error handling, more error catching, error messages. --- diff --git a/src/command_db.c b/src/command_db.c index ba33e91..25fcd24 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -81,7 +81,8 @@ extern void init_command_db(struct World * world) FILE * file = fopen("config/commands", "r"); exit_err(NULL == file, world, err); uint16_t lines, linemax; - textfile_sizes(file, &linemax, &lines); + err = "Trouble in init_cmds() with textfile_sizes()."; + exit_err(textfile_sizes(file, &linemax, &lines), world, err); err = "Trouble in init_cmds() with malloc()."; char * line = malloc(linemax); exit_err(NULL == line, world, err); diff --git a/src/keybindings.c b/src/keybindings.c index c318ec1..3ce0e44 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -18,7 +18,8 @@ extern void init_keybindings(struct World * world) { FILE * file = fopen("config/keybindings", "r"); uint16_t lines, linemax; - textfile_sizes(file, &linemax, &lines); + char * err = "textfile_sizes() in init_keybindings() returns error."; + exit_err(textfile_sizes(file, &linemax, &lines), world, err); struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); char * command = malloc(linemax); uint16_t commcount = 0; diff --git a/src/main.c b/src/main.c index d63f587..e2dc1b5 100644 --- a/src/main.c +++ b/src/main.c @@ -203,8 +203,7 @@ int main(int argc, char *argv[]) raw(); init_keybindings(&world); set_cleanup_flag(CLEANUP_KEYBINDINGS); - char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in " - "main()."; + char * err_winmem = "Trouble with init_win_meta() in main ()."; exit_err(init_win_meta(screen, &world.wins.meta), &world, err_winmem); set_cleanup_flag(CLEANUP_WIN_META); world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win); @@ -215,8 +214,8 @@ int main(int argc, char *argv[]) set_cleanup_flag(CLEANUP_WIN_LOG); world.wins.map = init_win_from_file(&world, "Map", draw_map_win); set_cleanup_flag(CLEANUP_WIN_MAP); - sorted_wintoggle(&world); + err_winmem = "Trouble with draw_all_wins() in main()."; /* Replay mode. */ int key; diff --git a/src/map_objects.c b/src/map_objects.c index f0f002c..28d9b14 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -8,11 +8,14 @@ #include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */ #include "misc.h" /* for textfile_sizes(), find_passable_pos() */ #include "main.h" /* for World struct */ +#include "rexit.h" /* for err_exit() */ /* Return pointer to newly allocated map object struct of size "size". If first * in map object chain ("first" pointing to !0), point "start" to it. + * + * Returns NULL instead of MapObj pointer if malloc() failed. */ static struct MapObj * get_next_map_obj(void * start, char * first, size_t size, struct MapObj * map_obj); @@ -89,15 +92,20 @@ 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()."; FILE * file = fopen(filename, "r"); + exit_err(NULL == file, world, err); uint16_t linemax; - textfile_sizes (file, &linemax, NULL); + err = "Trouble in init_map_object_defs() with textfile_sizes()."; + exit_err(textfile_sizes(file, &linemax, NULL), world, err); struct MapObjDef mod; struct ItemDef id; struct MonsterDef md; 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); char * line_p; char * delim = " "; while (fgets(defline, linemax, file)) @@ -117,11 +125,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); 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); * * p_p_id = id; p_p_id = (struct ItemDef * *) * p_p_id; } @@ -129,12 +139,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); * * p_p_md = md; p_p_md = (struct MonsterDef * *) * p_p_md; } } free(defline); - fclose(file); + err = "Trouble in init_map_object_defs() with fclose()."; + exit_err(fclose(file), world, err); } @@ -200,6 +212,7 @@ 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); @@ -226,6 +239,7 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) size = sizeof(struct Item); } map_obj = get_next_map_obj(start, &first, size, map_obj); + exit_err(NULL == map_obj, world, err); map_obj->type = type; if ( read_uint16_bigendian(file, &map_obj->pos.y) || read_uint16_bigendian(file, &map_obj->pos.x)) @@ -259,6 +273,7 @@ extern void * build_map_objects(struct World * world, void * start, char def_id, 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); @@ -270,6 +285,7 @@ extern void * build_map_objects(struct World * world, void * start, char def_id, for (i = 0; i < n; i++) { mo = get_next_map_obj(start, &first, size, mo); + exit_err(NULL == mo, world, err); mo->pos = find_passable_pos(world->map); if ('i' == mod->m_or_i) { diff --git a/src/misc.c b/src/misc.c index 4db3bc3..496c585 100644 --- a/src/misc.c +++ b/src/misc.c @@ -5,7 +5,7 @@ #include /* for unlink(), acess() */ #include /* for calloc(), free() */ #include /* for strlen(), strcmp(), memcpy() */ -#include /* for uint16_t */ +#include /* for uint8_t, uint16_t */ #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */ #include "map_objects.h" /* for struct Monster, write_map_objects(), */ #include "map_object_actions.h" /* for is_passable(), move_monster() */ @@ -17,8 +17,8 @@ -extern void textfile_sizes(FILE * file, uint16_t * linemax_p, - uint16_t * n_lines_p) +extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, + uint16_t * n_lines_p) { uint16_t n_lines = 0; int c = 0; @@ -41,12 +41,16 @@ extern void textfile_sizes(FILE * file, uint16_t * linemax_p, } } } - fseek(file, 0, SEEK_SET); + if (-1 == fseek(file, 0, SEEK_SET)) + { + return 1; + } * linemax_p = linemax; if (n_lines_p) { * n_lines_p = n_lines; } + return 0; } diff --git a/src/misc.h b/src/misc.h index be43197..5c9d3c4 100644 --- a/src/misc.h +++ b/src/misc.h @@ -9,7 +9,7 @@ -#include /* for uint16_t */ +#include /* for uint8_t, uint16_t */ #include /* for FILE typedef */ #include "yx_uint16.h" /* for yx_uint16 coordinates */ struct World; @@ -19,9 +19,11 @@ struct Map; /* Learn from "file" the largest line length (pointed to by "linemax_p") and * (pointed to by "n_lines_p" if it is not set to NULL) the number of lines. + * + * Returns 0 on success, 1 on fseek() error. */ -extern void textfile_sizes(FILE * file, uint16_t * linemax_p, - uint16_t * n_lines_p); +extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, + uint16_t * n_lines_p); diff --git a/src/wincontrol.c b/src/wincontrol.c index 74c9c2a..db144d1 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -30,7 +30,8 @@ extern struct Win * init_win_from_file(struct World * world, char * w_name, free(path); exit_err(NULL == file, world, err); uint16_t linemax; - textfile_sizes(file, &linemax, NULL); + err = "Trouble in init_win_from_file() with textfile_sizes()."; + exit_err(textfile_sizes(file, &linemax, NULL), world, err); char * line = malloc(linemax); err = "Trouble in init_win_from_file() with fgets()."; exit_err(NULL == fgets(line, linemax, file), world, err); @@ -46,8 +47,9 @@ extern struct Win * init_win_from_file(struct World * world, char * w_name, exit_err(fclose(file), world, err); struct WinMeta * wmeta = world->wins.meta; - struct Win * wp; // = malloc(sizeof(struct Win)); - init_win(wmeta, &wp, title, height, width, world, f); + struct Win * wp; + err = "Trouble in init_win_from_file() with init_win()."; + exit_err(init_win(wmeta, &wp, title, height, width, world, f), world, err); free(title); return wp; } @@ -56,18 +58,16 @@ extern struct Win * init_win_from_file(struct World * world, char * w_name, extern void sorted_wintoggle(struct World * world) { - char * err = "Trouble in sorted_wintoggle() with fopen() on file " - "'config/toggle_win_order'."; + char * err = "Trouble in sorted_wintoggle() with fopen()."; FILE * file = fopen("config/windows/toggle_order", "r"); exit_err(NULL == file, world, err); uint16_t linemax; - textfile_sizes(file, &linemax, NULL); + err = "Trouble in sorted_wintoggle() with textfile_sizes()."; + exit_err(textfile_sizes(file, &linemax, NULL), world, err); char win_order[linemax]; - err = "Trouble in sorted_wintoggle() with fgets() on file " - "'config/toggle_win_order'."; + err = "Trouble in sorted_wintoggle() with fgets()."; exit_err(NULL == fgets(win_order, linemax, file), world, err); - err = "Trouble in sorted_wintoggle() with fclose() on file " - "'toggle_win_order'."; + err = "Trouble in sorted_wintoggle() with fclose()."; exit_err(fclose(file), world, err); char c;