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