home · contact · privacy
Removed unnecessary NULLings of pointers, explained kept ones.
[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
26
27 extern void obey_argv(int argc, char * argv[])
28 {
29     int opt;
30     while (-1 != (opt = getopt(argc, argv, "i:")))
31     {
32         if      ('i' == opt)
33         {
34             world.path_interface_conf = optarg;
35         }
36         else
37         {
38             exit(EXIT_FAILURE);
39         }
40     }
41 }
42
43
44
45 extern void save_interface_conf()
46 {
47     char * f_name = "save_interface_conf()";
48     char * path = world.path_interface_conf;
49     char path_tmp[strlen(path) + 4 + 1];
50     sprintf(path_tmp, "%s_tmp", path);
51     FILE * file = try_fopen(path_tmp, "w", f_name);
52     char * delim = "%\n";
53     write_keybindings_to_file(file, &world.kb_global, delim);
54     write_keybindings_to_file(file, &world.kb_wingeom, delim);
55     write_keybindings_to_file(file, &world.kb_winkeys, delim);
56     write_order_wins_visible_active(file, delim);
57     uint8_t i;
58     for (i = 0; i < strlen(world.windb.ids); i++)
59     {
60         write_winconf_of_id_to_file(file, world.windb.ids[i], delim);
61     }
62     try_fclose_unlink_rename(file, path_tmp, path, f_name);
63 }
64
65
66
67 extern void load_interface_conf()
68 {
69     char * f_name = "load_interface_conf()";
70
71     /* Read keybindings and WincConf DB from interface config file. */
72     FILE * file = try_fopen(world.path_interface_conf, "r", f_name);
73     uint32_t linemax = textfile_sizes(file, NULL);
74     char line[linemax + 1];
75     read_keybindings_from_file(line, linemax, file, &world.kb_global);
76     read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
77     read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
78     read_order_wins_visible_active(line, linemax, file);
79     while (read_winconf_from_file(line, linemax, file));
80     try_fclose(file, f_name);
81
82     /* Build windows as defined by read interface data and toggle them on. */
83     make_v_screen_and_init_win_sizes();
84     uint8_t i;
85     char tmp_active = world.windb.active;
86     char tmp_order[strlen(world.windb.order) + 1];
87     sprintf(tmp_order, "%s", world.windb.order);
88     world.windb.order[0] = '\0';
89     for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
90     world.windb.active = tmp_active;
91
92     /* So that the interface config data and the window structs get freed. */
93     set_cleanup_flag(CLEANUP_INTERFACE);
94 }
95
96
97
98 extern void unload_interface_conf()
99 {
100     free_keybindings(world.kb_global.kbs);
101     free_keybindings(world.kb_wingeom.kbs);
102     free_keybindings(world.kb_winkeys.kbs);
103     while ('\0' != world.windb.active)
104     {
105         toggle_window(world.windb.active);
106     }
107     free_windb();
108     delwin(world.windb.v_screen);
109 }
110
111
112
113 extern void reload_interface_conf()
114 {
115     unload_interface_conf();
116     load_interface_conf();
117     map_center();
118 }
119
120
121
122 extern void nav_inventory(char dir)
123 {
124     if ('u' == dir)
125     {
126         world.player_inventory_select = world.player_inventory_select
127                                         - (world.player_inventory_select > 0);
128         return;
129     }
130     uint8_t n_elems = 0;
131     uint8_t i;
132     for (i = 0; '\0' != world.player_inventory[i]; i++)
133     {
134         n_elems = n_elems + ('\n' == world.player_inventory[i]);
135     }
136     world.player_inventory_select = world.player_inventory_select
137                                     + (world.player_inventory_select < n_elems);
138 }