home · contact · privacy
Removed unused textfile_sizes() functionality, appropriately renamed it.
[plomrogue] / src / client / misc.c
1 /* src/client/misc.c */
2
3 #include "misc.h"
4 #include <ncurses.h> /* delwin() */
5 #include <stdint.h> /* uint8_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdlib.h> /* free(), exit() */
8 #include <string.h> /* memcpy(), strlen() */
9 #include <unistd.h> /* global optarg, getopt() */
10 #include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
12                                   * try_fclose_unlink_rename(),
13                                   */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "cleanup.h" /* set_cleanup_flag() */
17 #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
18                           * write_keybindings_to_file()
19                           */
20 #include "map.h" /* map_center() */
21 #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
22                       * read_winconf_from_file(), write_winconf_of_id_to_file(),
23                       * toggle_window()
24                       */
25 #include "world.h" /* global world */
26
27
28
29 extern void obey_argv(int argc, char * argv[])
30 {
31     int opt;
32     while (-1 != (opt = getopt(argc, argv, "i:")))
33     {
34         if      ('i' == opt)
35         {
36             world.path_interface = optarg;
37         }
38         else
39         {
40             exit(EXIT_FAILURE);
41         }
42     }
43 }
44
45
46
47 extern void save_interface_conf()
48 {
49     char * f_name = "save_interface_conf()";
50     char * path = world.path_interface;
51     char path_tmp[strlen(path) + 4 + 1];
52     sprintf(path_tmp, "%s_tmp", path);
53     FILE * file = try_fopen(path_tmp, "w", f_name);
54     write_keybindings_to_file(file, &world.kb_global);
55     write_keybindings_to_file(file, &world.kb_wingeom);
56     write_keybindings_to_file(file, &world.kb_winkeys);
57     write_order_wins_visible_active(file);
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]);
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     reset_err_try_fgets_counter();
74     FILE * file = try_fopen(world.path_interface, "r", f_name);
75     uint32_t linemax = textfile_width(file);
76     char line[linemax + 1];
77     read_keybindings_from_file(line, linemax, file, &world.kb_global);
78     read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
79     read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
80     char active_tmp;
81     char * order_tmp;
82     read_order_wins_visible_active(line, linemax, file, &order_tmp, &active_tmp);
83     while (read_winconf_from_file(line, linemax, file));
84     try_fclose(file, f_name);
85
86     /* Check that windows of all legal IDs have been initalized. The validity of
87      * this test relies on read_winconf_from_file() failing on duplicates. Only
88      * on success initialize the windows as visible, to enable safe cleaning up.
89      */
90     char * err = "Failed to initialize all expected windows.";
91     exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
92     world.winDB.active = active_tmp;
93     world.winDB.order = order_tmp;
94
95     /* Build windows as defined by read interface data and toggle them on. */
96     make_v_screen_and_init_win_sizes();
97     char tmp_active = world.winDB.active;
98     char tmp_order[strlen(world.winDB.order) + 1];
99     sprintf(tmp_order, "%s", world.winDB.order);
100     world.winDB.order[0] = '\0';
101     uint8_t i;
102     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
103     world.winDB.active = tmp_active;
104
105     /* So that the interface config data and the window structs get freed. */
106     set_cleanup_flag(CLEANUP_INTERFACE);
107 }
108
109
110
111 extern void unload_interface_conf()
112 {
113     free_keybindings(world.kb_global.kbs);
114     free_keybindings(world.kb_wingeom.kbs);
115     free_keybindings(world.kb_winkeys.kbs);
116     while ('\0' != world.winDB.active)
117     {
118         toggle_window(world.winDB.active);
119     }
120     free_winDB();
121     delwin(world.winDB.v_screen);
122 }
123
124
125
126 extern void reload_interface_conf()
127 {
128     unload_interface_conf();
129     load_interface_conf();
130     map_center();
131     world.winDB.v_screen_offset = 0;
132 }
133
134
135
136 extern void nav_inventory(char dir)
137 {
138     if ('u' == dir)
139     {
140         world.player_inventory_select = world.player_inventory_select
141                                         - (world.player_inventory_select > 0);
142         return;
143     }
144     uint8_t n_elems = 0;
145     uint8_t i;
146     for (i = 0; '\0' != world.player_inventory[i]; i++)
147     {
148         n_elems = n_elems + ('\n' == world.player_inventory[i]);
149     }
150     world.player_inventory_select = world.player_inventory_select
151                                     + (world.player_inventory_select < n_elems);
152 }
153
154
155
156 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
157                         void ** ptr_old_array)
158 {
159     char * f_name = "array_append()";
160     uint32_t old_size = old_n * region_size;
161     uint32_t new_size = old_size + region_size;
162     void * new_array = try_malloc(new_size, f_name);
163     memcpy(new_array, * ptr_old_array, old_size);
164     memcpy(new_array + (old_n * region_size), new_region, region_size);
165     free(* ptr_old_array);
166     * ptr_old_array = new_array;
167 }