home · contact · privacy
Added generic default error message to exit_err().
[plomrogue] / src / rexit.c
1 /* rexit.c */
2
3 #include "rexit.h"
4 #include <stdlib.h> /* for exit(), free(), defines EXIT_SUCESS, EXIT_FAILURE */
5 #include <stdio.h> /* for printf() */
6 #include <stdint.h> /* for uint8_t */
7 #include <ncurses.h> /* for endwin() */
8 #include "main.h" /* for World struct */
9 #include "map.h" /* for Map struct */
10 #include "keybindings.h" /* for KeysWinData, KeyBinding structs */
11
12
13
14 /* The clean-up routine and the flag resource by which it decides what to do. */
15 static uint8_t cleanup_flags = 0x00;
16 static void cleanup(struct World * world);
17
18
19
20 static void cleanup(struct World * world)
21 {
22     if (cleanup_flags & CLEANUP_NCURSES)
23     {
24         endwin();
25     }
26     if (cleanup_flags & CLEANUP_MAP)
27     {
28         free(world->map->cells);
29     }
30     if (cleanup_flags & CLEANUP_KEYBINDINGS)
31     {
32         uint16_t key;
33         for (key = 0; key <= world->keyswindata->max; key++)
34         {
35             free(world->keybindings[key].name);
36         }
37         free(world->keybindings);
38         free(world->keyswindata);
39     }
40     if (cleanup_flags & CLEANUP_LOG)
41     {
42         free(world->log);
43     }
44 }
45
46
47
48 extern void set_cleanup_flag(enum cleanup_flag flag)
49 {
50     cleanup_flags = cleanup_flags | flag;
51 }
52
53
54
55 extern void exit_game(struct World * world)
56 {
57     cleanup(world);
58     exit(EXIT_SUCCESS);
59 }
60
61
62
63 extern void exit_err(uint8_t err, struct World * world, char * msg)
64 {
65     if (0 == err)
66     {
67         return;
68     }
69     cleanup(world);
70     if (NULL == msg)
71     {
72         msg = "Some error encountered. Aborted.";
73     }
74     printf("%s\n", msg);
75     exit(EXIT_FAILURE);
76 }