home · contact · privacy
Check client's command DB config file for errors while reading it.
[plomrogue] / src / client / misc.c
1 /* src/client/misc.c */
2
3 #include "misc.h"
4 #include <stdlib.h> /* exit() */
5 #include <ncurses.h> /* delwin() */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint32_t */
8 #include <stdio.h> /* sprintf() */
9 #include <string.h> /* strlen() */
10 #include <unistd.h> /* global optarg, getopt() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
12                                   * try_fclose_unlink_rename(),
13                                   */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
16                           * write_keybindings_to_file()
17                           */
18 #include "map_window.h" /* map_center() */
19 #include "windows.h" /* for free_winDB(), make_v_screen_and_init_win_sizes(),
20                       * read_winconf_from_file(), write_winconf_of_id_to_file(),
21                       * toggle_window()
22                       */
23 #include "world.h" /* global world */
24
25 #include "../common/try_malloc.h" /* try_malloc() */
26
27
28 extern void obey_argv(int argc, char * argv[])
29 {
30     int opt;
31     while (-1 != (opt = getopt(argc, argv, "i:")))
32     {
33         if      ('i' == opt)
34         {
35             world.path_interface_conf = optarg;
36         }
37         else
38         {
39             exit(EXIT_FAILURE);
40         }
41     }
42 }
43
44
45
46 extern void save_interface_conf()
47 {
48     char * f_name = "save_interface_conf()";
49     char * path = world.path_interface_conf;
50     char path_tmp[strlen(path) + 4 + 1];
51     sprintf(path_tmp, "%s_tmp", path);
52     FILE * file = try_fopen(path_tmp, "w", f_name);
53     char * delim = "%\n";
54     write_keybindings_to_file(file, &world.kb_global, delim);
55     write_keybindings_to_file(file, &world.kb_wingeom, delim);
56     write_keybindings_to_file(file, &world.kb_winkeys, delim);
57     write_order_wins_visible_active(file, delim);
58     uint8_t i;
59     for (i = 0; i < strlen(world.winDB.ids); i++)
60     {
61         write_winconf_of_id_to_file(file, world.winDB.ids[i], delim);
62     }
63     try_fclose_unlink_rename(file, path_tmp, path, f_name);
64 }
65
66
67
68 extern void load_interface_conf()
69 {
70     char * f_name = "load_interface_conf()";
71
72     /* Read keybindings and WincConf DB from interface config file. */
73     FILE * file = try_fopen(world.path_interface_conf, "r", f_name);
74     uint32_t linemax = textfile_sizes(file, NULL);
75     char line[linemax + 1];
76     read_keybindings_from_file(line, linemax, file, &world.kb_global);
77     read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
78     read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
79     read_order_wins_visible_active(line, linemax, file);
80     while (read_winconf_from_file(line, linemax, file));
81     try_fclose(file, f_name);
82
83     /* Build windows as defined by read interface data and toggle them on. */
84     make_v_screen_and_init_win_sizes();
85     uint8_t i;
86     char tmp_active = world.winDB.active;
87     char tmp_order[strlen(world.winDB.order) + 1];
88     sprintf(tmp_order, "%s", world.winDB.order);
89     world.winDB.order[0] = '\0';
90     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
91     world.winDB.active = tmp_active;
92
93     /* So that the interface config data and the window structs get freed. */
94     set_cleanup_flag(CLEANUP_INTERFACE);
95 }
96
97
98
99 extern void unload_interface_conf()
100 {
101     free_keybindings(world.kb_global.kbs);
102     free_keybindings(world.kb_wingeom.kbs);
103     free_keybindings(world.kb_winkeys.kbs);
104     while ('\0' != world.winDB.active)
105     {
106         toggle_window(world.winDB.active);
107     }
108     free_winDB();
109     delwin(world.winDB.v_screen);
110 }
111
112
113
114 extern void reload_interface_conf()
115 {
116     unload_interface_conf();
117     load_interface_conf();
118     map_center();
119 }
120
121
122
123 extern void nav_inventory(char dir)
124 {
125     if ('u' == dir)
126     {
127         world.player_inventory_select = world.player_inventory_select
128                                         - (world.player_inventory_select > 0);
129         return;
130     }
131     uint8_t n_elems = 0;
132     uint8_t i;
133     for (i = 0; '\0' != world.player_inventory[i]; i++)
134     {
135         n_elems = n_elems + ('\n' == world.player_inventory[i]);
136     }
137     world.player_inventory_select = world.player_inventory_select
138                                     + (world.player_inventory_select < n_elems);
139 }
140
141
142
143 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
144                         void ** ptr_old_array)
145 {
146     char * f_name = "array_append()";
147     uint32_t old_size = old_n * region_size;
148     uint32_t new_size = old_size + region_size;
149     void * new_array = try_malloc(new_size, f_name);
150     memcpy(new_array, * ptr_old_array, old_size);
151     memcpy(new_array + (old_n * region_size), new_region, region_size);
152     free(* ptr_old_array);
153     * ptr_old_array = new_array;
154 }