#include "cleanup.h" /* set_cleanup_flag() */
#include "world.h" /* global world */
+#include "misc.h"
/* Point "ch_ptr" to next strtok() string in "line" delimited by "delim".*/
char * arg_string = strtok(NULL, delim);
cmd.arg = arg_string[0];
copy_tokenized_string(NULL, &cmd.dsc_long, "\n");
- uint32_t old_size = i * sizeof(struct Command);
- uint32_t new_size = old_size + sizeof(struct Command);
- struct Command * new_cmds = try_malloc(new_size, f_name);
- memcpy(new_cmds, world.commandDB.cmds, old_size);
- new_cmds[i] = cmd;
- free(world.commandDB.cmds);
- world.commandDB.cmds = new_cmds;
+ array_append(i, sizeof(struct Command), (void *) &cmd,
+ (void **) &world.commandDB.cmds);
i++;
}
try_fclose(file, f_name);
*/
#include "world.h" /* global world */
+#include "../common/try_malloc.h" /* try_malloc() */
extern void obey_argv(int argc, char * argv[])
world.player_inventory_select = world.player_inventory_select
+ (world.player_inventory_select < n_elems);
}
+
+
+
+extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
+ void ** ptr_old_array)
+{
+ char * f_name = "array_append()";
+ uint32_t old_size = old_n * region_size;
+ uint32_t new_size = old_size + region_size;
+ void * new_array = try_malloc(new_size, f_name);
+ memcpy(new_array, * ptr_old_array, old_size);
+ memcpy(new_array + (old_n * region_size), new_region, region_size);
+ free(* ptr_old_array);
+ * ptr_old_array = new_array;
+}
#ifndef MISC_H
#define MISC_H
+#include <stddef.h> /* size_t */
+#include <stdint.h> /* uint32_t */
+
/* Parses command line argument -i into client configuration. */
/* Move world.inventory_sel up ("dir"="u") or down (else) as far as possible. */
extern void nav_inventory(char dir);
+/* Append to array pointed to by "ptr_old_array" of "old_n" elements of
+ * "region_size" "new region".
+ */
+extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
+ void ** ptr_old_array);
#endif
#include "keybindings.h" /* free_keybindings(), write_keybidings_to_file(),
* read_keybindings_from_file()
*/
+#include "misc.h" /* array_append() */
#include "world.h" /* global world */
static void append_win(struct Win * w);
static void suspend_win(struct Win * w);
-/* Copy Win content pointed to by "win" into appendend world.winDB.wins area. */
-static void add_win_to_winDB(struct Win * win);
-
static uint8_t get_pos_in_order(char c)
-static void add_win_to_winDB(struct Win * win)
-{
- char * f_name = "add_win_to_winDB()";
- if (world.winDB.ids)
- {
- uint8_t old_ids_size = strlen(world.winDB.ids);
- char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
- sprintf(new_ids, "%s%c", world.winDB.ids, win->id);
- free(world.winDB.ids);
- world.winDB.ids = new_ids;
- uint16_t old_wins_size = old_ids_size * sizeof(struct Win);
- uint16_t new_wins_size = old_wins_size + sizeof(struct Win);
- struct Win * new_wins = try_malloc(new_wins_size, f_name);
- memcpy(new_wins, world.winDB.wins, old_wins_size);
- new_wins[old_ids_size] = *win;
- free(world.winDB.wins);
- world.winDB.wins = new_wins;
- return;
- }
- world.winDB.ids = try_malloc(2, f_name);
- sprintf(world.winDB.ids, "%c", win->id);
- world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
- world.winDB.wins[0] = *win;
-}
-
-
-
extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
uint16_t frame_size)
{
win.target_width = atoi(line);
win.target_width_type = (0 >= win.target_width);
read_keybindings_from_file(line, linemax, file, &win.kb);
- add_win_to_winDB(&win);
+ if (world.winDB.ids)
+ {
+ uint8_t old_ids_size = strlen(world.winDB.ids);
+ char * new_ids = try_malloc(old_ids_size + 1 + 1, f_name);
+ sprintf(new_ids, "%s%c", world.winDB.ids, win.id);
+ free(world.winDB.ids);
+ world.winDB.ids = new_ids;
+ array_append(old_ids_size, sizeof(struct Win), (void *) &win,
+ (void **) &world.winDB.wins);
+ return 1;
+ }
+ world.winDB.ids = try_malloc(2, f_name);
+ sprintf(world.winDB.ids, "%c", win.id);
+ world.winDB.wins = try_malloc(sizeof(struct Win), f_name);
+ world.winDB.wins[0] = win;
return 1;
}