home · contact · privacy
exit_err() now also prints the internal error code.
[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(), perror() */
6 #include <stdint.h> /* for uint8_t */
7 #include <ncurses.h> /* for endwin() */
8 #include <errno.h> /* for errno */
9 #include "main.h" /* for World struct */
10 #include "map.h" /* for Map struct */
11 #include "keybindings.h" /* for KeysWinData, KeyBinding structs */
12
13
14
15 /* The clean-up routine and the flag resource by which it decides what to do. */
16 static uint8_t cleanup_flags = 0x00;
17 static void cleanup(struct World * world);
18
19
20
21 static void cleanup(struct World * world)
22 {
23     if (cleanup_flags & CLEANUP_NCURSES)
24     {
25         endwin();
26     }
27     if (cleanup_flags & CLEANUP_MAP)
28     {
29         free(world->map->cells);
30     }
31     if (cleanup_flags & CLEANUP_KEYBINDINGS)
32     {
33         uint16_t key;
34         for (key = 0; key <= world->keyswindata->max; key++)
35         {
36             free(world->keybindings[key].name);
37         }
38         free(world->keybindings);
39         free(world->keyswindata);
40     }
41     if (cleanup_flags & CLEANUP_LOG)
42     {
43         free(world->log);
44     }
45 }
46
47
48
49 extern void set_cleanup_flag(enum cleanup_flag flag)
50 {
51     cleanup_flags = cleanup_flags | flag;
52 }
53
54
55
56 extern void exit_game(struct World * world)
57 {
58     cleanup(world);
59     exit(EXIT_SUCCESS);
60 }
61
62
63
64 extern void exit_err(uint8_t err, struct World * world, char * msg)
65 {
66     if (0 == err)
67     {
68         return;
69     }
70     cleanup(world);
71     if (NULL == msg)
72     {
73         msg = "Details unknown.";
74     }
75     printf("Aborted PlomRogue due to error. %s\nInternal error code: %d\n",
76            msg, err);
77     if (0 != errno)
78     {
79         perror("errno states");
80     }
81     exit(EXIT_FAILURE);
82 }