From: Christian Heller Date: Sat, 25 Jan 2014 22:06:19 +0000 (+0100) Subject: Refactored similar array append activities into array_append(). X-Git-Tag: tce~858 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/decks/todos?a=commitdiff_plain;h=3a5db435e5dc6422e559033fbdf9b64c8d8567c9;p=plomrogue Refactored similar array append activities into array_append(). --- diff --git a/src/client/command_db.c b/src/client/command_db.c index 27a3b6d..00a907b 100644 --- a/src/client/command_db.c +++ b/src/client/command_db.c @@ -14,6 +14,7 @@ #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".*/ @@ -79,13 +80,8 @@ extern void init_command_db() 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); diff --git a/src/client/misc.c b/src/client/misc.c index a8cf053..4f967df 100644 --- a/src/client/misc.c +++ b/src/client/misc.c @@ -22,6 +22,7 @@ */ #include "world.h" /* global world */ +#include "../common/try_malloc.h" /* try_malloc() */ extern void obey_argv(int argc, char * argv[]) @@ -136,3 +137,18 @@ extern void nav_inventory(char dir) 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; +} diff --git a/src/client/misc.h b/src/client/misc.h index 5fb3e71..30f1710 100644 --- a/src/client/misc.h +++ b/src/client/misc.h @@ -7,6 +7,9 @@ #ifndef MISC_H #define MISC_H +#include /* size_t */ +#include /* uint32_t */ + /* Parses command line argument -i into client configuration. */ @@ -26,6 +29,11 @@ extern void reload_interface_conf(); /* 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 diff --git a/src/client/windows.c b/src/client/windows.c index 2fd5131..ee74528 100644 --- a/src/client/windows.c +++ b/src/client/windows.c @@ -27,6 +27,7 @@ #include "keybindings.h" /* free_keybindings(), write_keybidings_to_file(), * read_keybindings_from_file() */ +#include "misc.h" /* array_append() */ #include "world.h" /* global world */ @@ -100,9 +101,6 @@ static void draw_wins(struct Win * w); 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) @@ -554,33 +552,6 @@ static void suspend_win(struct Win * w) -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) { @@ -644,7 +615,21 @@ extern uint8_t read_winconf_from_file(char * line, uint32_t linemax, 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; }