home · contact · privacy
Some code-stylistic improvements to rexit library; also moved exit_trouble() into it.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 25 Nov 2013 03:05:33 +0000 (04:05 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 25 Nov 2013 03:05:33 +0000 (04:05 +0100)
src/command_db.c
src/main.c
src/misc.c
src/misc.h
src/readwrite.c
src/rexit.c
src/rexit.h
src/wincontrol.c

index 7e60b1d3510386dd3101000acf3cc637025b3506..fd7f4dc7a2f78ae431ebd0bf37ee52bfda574da1 100644 (file)
@@ -7,7 +7,8 @@
 #include <string.h> /* for strlen(), strtok() */
 #include "main.h" /* for world global */
 #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose() */
-#include "misc.h" /* for try_malloc(), exit_trouble */
+#include "misc.h" /* for try_malloc() */
+#include "rexit.h" /* exit_trouble() */
 
 
 
index 457143847899e17e4257b68843a108fd3cdd5f3b..9bb983dcb5ff212d5e1aae9478cc2265b1c30b85 100644 (file)
@@ -41,7 +41,7 @@ int main(int argc, char *argv[])
     init_command_db();
     set_cleanup_flag(CLEANUP_COMMAND_DB);
     init_map_object_actions();
-    set_cleanup_flag(CLEANUP_MAPOBJACTS);
+    set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
 
     /* Check for corrupted savefile / recordfile savings. */
     char * recordfile = "record";
index 06c6a30a7ce6882e7f8b97ff0044ac7a731d844e..2cf9e62f936671b84faeca3eaecb4df8cb5fd267 100644 (file)
@@ -16,7 +16,7 @@
 #include "map.h" /* for Map struct, is_passable() */
 #include "main.h" /* for world global */
 #include "yx_uint16.h" /* for yx_uint16 struct */
-#include "rexit.h" /* for exit_err() */
+#include "rexit.h" /* for exit_err(), exit_trouble() */
 #include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
                          * sorted_wintoggle_and_activate()
                          */
@@ -34,21 +34,6 @@ extern uint16_t rrand()
 
 
 
-extern void exit_trouble(uint8_t test, char * parent, char * child)
-{
-    char * p1 = "Trouble in ";
-    char * p2 = " with ";
-    char * p3 = ".";
-    uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
-                    + strlen(p3) + 1;
-    char msg[size];
-    exit_err(NULL == msg, "malloc() in trouble_msg() failed.");
-    sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
-    exit_err(test, msg);
-}
-
-
-
 extern void * try_malloc(size_t size, char * f)
 {
     void * p = malloc(size);
index ddc6be6617c8b691fd06dd57365f5b1afa71b9d4..24f36b5ad6ae5d3ab15d089fe2953f428ff6dc01 100644 (file)
@@ -20,9 +20,6 @@ struct Map;
  */
 extern uint16_t rrand();
 
-/* Do exit_err() with message: "Trouble in ".parent." with ".child."." */
-extern void exit_trouble(uint8_t test, char * parent, char * child);
-
 /* Wrappers to malloc(), calloc() from function called "f" calling exit_err()
  * with trouble_msg() error message if necessary.
  */
index 531409e6e9f5c0ec09048c3ed73cb26f0ed5b33e..e5db3061cf10abb5408dfea5afffac3d2d80862e 100644 (file)
@@ -7,8 +7,7 @@
 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
 #include <string.h> /* for strlen()*/
 #include <unistd.h> /* for unlink() */
-#include "rexit.h"  /* for exit_err() */
-#include "misc.h"   /* for exit_trouble() */
+#include "rexit.h"  /* for exit_err(), exit_trouble() */
 #include "main.h"   /* for world global */
 
 
index 691cb6840f575e1caac142e49ce8f7d8e33ad6fa..f79eac7582f25f0d5c7284a47198740b35015344 100644 (file)
@@ -2,14 +2,14 @@
 
 #include "rexit.h"
 #include <stdlib.h> /* for exit(), free(), defines EXIT_SUCESS, EXIT_FAILURE */
-#include <stdio.h> /* for printf(), perror() */
+#include <stdio.h> /* for printf(), perror(), sprintf() */
 #include <stdint.h> /* for uint8_t */
+#include <string.h> /* for strlen() */
 #include <errno.h> /* for errno */
 #include "main.h" /* for world global */
-#include "map.h" /* for Map struct */
-#include "keybindings.h" /* for free_keybindings() */
+#include "map.h" /* for Map struct ("free(world.map->cells)") */
 #include "command_db.h" /* for free_command_db() */
-#include "windows.h" /* for Win struct, free_winmeta_and_endwin() */
+#include "windows.h" /* for free_winmeta_and_endwin() */
 #include "map_objects.h" /* for free_map_objects, free_map_object_defs() */
 #include "misc.h" /* for unload_interface_conf() */
 #include "map_object_actions.h" /* for free_map_object_actions() */
@@ -24,14 +24,6 @@ static void cleanup();
 
 static void cleanup()
 {
-    if (cleanup_flags & CLEANUP_MAP_OBJECTS)
-    {
-        free_map_objects(world.map_objs);
-    }
-    if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
-    {
-        free_map_object_defs(world.map_obj_defs);
-    }
     if (cleanup_flags & CLEANUP_LOG)
     {
         free(world.log);
@@ -40,21 +32,29 @@ static void cleanup()
     {
         free_command_db();
     }
-    if (cleanup_flags & CLEANUP_MAPOBJACTS)
-    {
-        free_map_object_actions(world.map_obj_acts);
-    }
     if (cleanup_flags & CLEANUP_MAP)
     {
         free(world.map->cells);
     }
-    if (cleanup_flags & CLEANUP_INTERFACE)
+    if (cleanup_flags & CLEANUP_MAP_OBJECTS)
+    {
+        free_map_objects(world.map_objs);
+    }
+    if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
     {
-        unload_interface_conf();
+        free_map_object_defs(world.map_obj_defs);
     }
-    if (cleanup_flags & CLEANUP_NCURSES)
+    if (cleanup_flags & CLEANUP_MAP_OBJECT_ACTS)
     {
-        free_winmeta_and_endwin();
+        free_map_object_actions(world.map_obj_acts);
+    }
+    if (cleanup_flags & CLEANUP_INTERFACE) /* Only cleaning-up order          */
+    {                                      /* dependency known so far:        */
+        unload_interface_conf();           /* unload_interface_conf() must    */
+    }                                      /* come before                     */
+    if (cleanup_flags & CLEANUP_NCURSES)   /* free_winmeta_and_endwin()       */
+    {                                      /* since it depends on world.wmeta */
+        free_winmeta_and_endwin();         /* for closing all windows.        */
     }
 }
 
@@ -94,3 +94,17 @@ extern void exit_err(uint8_t err, char * msg)
     }
     exit(EXIT_FAILURE);
 }
+
+
+
+extern void exit_trouble(uint8_t err, char * parent, char * child)
+{
+    char * p1 = "Trouble in ";
+    char * p2 = " with ";
+    char * p3 = ".";
+    uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
+                    + strlen(p3) + 1;
+    char msg[size];
+    sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
+    exit_err(err, msg);
+}
index ab8e8ec2aed7c53a6fbe342bd7e7c3825d419d94..1c5c59d17fc0109a8e1cb5e7f6eb8968c8cf8369 100644 (file)
@@ -7,8 +7,6 @@
 #ifndef REXIT_H
 #define REXIT_H
 
-
-
 #include <stdint.h> /* for uint8_t */
 
 
  */
 enum cleanup_flag
 {
-    CLEANUP_NCURSES         = 0x0001,
-    CLEANUP_MAP             = 0x0002,
-    CLEANUP_LOG             = 0x0004,
-    CLEANUP_COMMAND_DB      = 0x0008,
-    CLEANUP_MAPOBJACTS      = 0x0010,
-    CLEANUP_MAP_OBJECTS     = 0x0020,
-    CLEANUP_MAP_OBJECT_DEFS = 0x0040,
-    CLEANUP_INTERFACE       = 0x0080
+    CLEANUP_LOG             = 0x0001,
+    CLEANUP_COMMAND_DB      = 0x0002,
+    CLEANUP_MAP             = 0x0004,
+    CLEANUP_MAP_OBJECTS     = 0x0008,
+    CLEANUP_MAP_OBJECT_DEFS = 0x0010,
+    CLEANUP_MAP_OBJECT_ACTS = 0x0020,
+    CLEANUP_INTERFACE       = 0x0040,
+    CLEANUP_NCURSES         = 0x0080
 };
 extern void set_cleanup_flag(enum cleanup_flag flag);
 
 
 
-/* Exit orderly, clean up. */
+/* Exit orderly, clean up in an order respecting resource dependencies. */
 extern void exit_game();
 
 /* If "err" == 0, do nothing. Else, clean up and exit with an error message that
@@ -42,6 +40,9 @@ extern void exit_game();
  */
 extern void exit_err(uint8_t err, char * msg);
 
+/* Do exit_err() with "msg" as: "Trouble in ".parent." with ".child."." */
+extern void exit_trouble(uint8_t err, char * parent, char * child);
+
 
 
 #endif
index aa705ecacd89f06083eb82fca779f2777d951789..4eca93bc02e1a40777718c4180e8c2e7210ee180 100644 (file)
@@ -12,7 +12,7 @@
 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(),
                         * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
                         */
-#include "rexit.h" /* for exit_err() */
+#include "rexit.h" /* for exit_err(), exit_trouble() */
 #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_log(),
                         * draw_win_available_keybindings(),
                         * draw_win_inventory(), draw_win_keybindings_global(),
@@ -20,7 +20,7 @@
                         * draw_win_keybindings_winconf_keybindings(),
                         * draw_winconf_geometry(), draw_winconf_keybindings()
                         */
-#include "misc.h" /* for try_malloc(), exit_trouble() */
+#include "misc.h" /* for try_malloc() */
 #include "dirent.h" /* for opendir(), closedir(), readdir() */
 #include "errno.h" /* for errno */
 #include "keybindings.h" /* for KeyBinding struct, free_keybindings() */