home · contact · privacy
Improved error handling, more error catching, error messages.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 31 Aug 2013 02:30:41 +0000 (04:30 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 31 Aug 2013 02:30:41 +0000 (04:30 +0200)
src/command_db.c
src/keybindings.c
src/main.c
src/map_objects.c
src/misc.c
src/misc.h
src/wincontrol.c

index ba33e912dc9df39d8b11fe26626e949f2194354c..25fcd24e90076da9ca8c5c75689c7b487525a6f3 100644 (file)
@@ -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;
     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);
     err = "Trouble in init_cmds() with malloc().";
     char * line = malloc(linemax);
     exit_err(NULL == line, world, err);
index c318ec18f49c1501c0e72640d7e01db01303350a..3ce0e440b2d319af9ff0b81d1040bc468518c10f 100644 (file)
@@ -18,7 +18,8 @@ extern void init_keybindings(struct World * world)
 {
     FILE * file = fopen("config/keybindings", "r");
     uint16_t lines, linemax;
 {
     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;
     struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
     char * command = malloc(linemax);
     uint16_t commcount = 0;
index d63f587c23955913c843c6cd2760ef2472883b71..e2dc1b5309276cd7f2097036905137264d46d0b3 100644 (file)
@@ -203,8 +203,7 @@ int main(int argc, char *argv[])
     raw();
     init_keybindings(&world);
     set_cleanup_flag(CLEANUP_KEYBINDINGS);
     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);
     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);
     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);
     sorted_wintoggle(&world);
+    err_winmem = "Trouble with draw_all_wins() in main().";
 
     /* Replay mode. */
     int key;
 
     /* Replay mode. */
     int key;
index f0f002caa31feddaaaba44d6cdef7a7a935404fa..28d9b146864e70ece8f99ebd3c0c81255c3ed1b6 100644 (file)
@@ -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 "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.
 
 
 
 /* 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);
  */
 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;
 {
     world->item_def    = 0;
     world->monster_def = 0;
+    char * err = "Trouble in init_map_object_defs() with fopen().";
     FILE * file = fopen(filename, "r");
     FILE * file = fopen(filename, "r");
+    exit_err(NULL == file, world, err);
     uint16_t linemax;
     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;
     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);
     char * defline = malloc(linemax);
+    exit_err(NULL == defline, world, err);
     char * line_p;
     char * delim = " ";
     while (fgets(defline, linemax, file))
     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));
             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));
         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;
         }
             * * 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));
         {
             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);
             * * 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 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);
     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);
             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))
         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 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);
     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);
     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)
         {
         mo->pos = find_passable_pos(world->map);
         if ('i' == mod->m_or_i)
         {
index 4db3bc319c906de93004606ffbda90955e0cdbd5..496c58549461370c8e100f28d60737f5edbd9770 100644 (file)
@@ -5,7 +5,7 @@
 #include <unistd.h> /* for unlink(), acess() */
 #include <stdlib.h> /* for calloc(), free() */
 #include <string.h> /* for strlen(), strcmp(), memcpy() */
 #include <unistd.h> /* for unlink(), acess() */
 #include <stdlib.h> /* for calloc(), free() */
 #include <string.h> /* for strlen(), strcmp(), memcpy() */
-#include <stdint.h> /* for uint16_t */
+#include <stdint.h> /* 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() */
 #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;
 {
     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;
     }
     * linemax_p = linemax;
     if (n_lines_p)
     {
         * n_lines_p = n_lines;
     }
+    return 0;
 }
 
 
 }
 
 
index be431979360ace92538ac339fbc53c55f4a94337..5c9d3c4f3244027f7ba0985aa422648d8a20236d 100644 (file)
@@ -9,7 +9,7 @@
 
 
 
 
 
 
-#include <stdint.h>    /* for uint16_t */
+#include <stdint.h>    /* for uint8_t, uint16_t */
 #include <stdio.h>     /* for FILE typedef */
 #include "yx_uint16.h" /* for yx_uint16 coordinates */
 struct World;
 #include <stdio.h>     /* 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.
 
 /* 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);
 
 
 
 
 
 
index 74c9c2a297d6fc9e0fcca3c1afdd83855cd56e2c..db144d1131664e9b666b4415b00c50071d356f27 100644 (file)
@@ -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;
     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);
     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;
     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;
 }
     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)
 {
 
 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;
     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];
     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);
     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;
     exit_err(fclose(file), world, err);
 
     char c;