From: Christian Heller Date: Fri, 13 Sep 2013 14:36:32 +0000 (+0200) Subject: Made keybindings array into linked list; on the way rewrote / improved great parts... X-Git-Tag: tce~964 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/task?a=commitdiff_plain;h=140cadf8f8dc73a1756169dbfa7cb5f05e3b8b8c;p=plomrogue Made keybindings array into linked list; on the way rewrote / improved great parts of keybindings library and some directly or indirectly connected code. --- diff --git a/src/control.c b/src/control.c index e73f5a1..842b710 100644 --- a/src/control.c +++ b/src/control.c @@ -5,8 +5,8 @@ #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win, * struct WinMeta */ -#include "keybindings.h" /* for get_action_key(), save_keybindings(), - * keyswin_move_selection(), keyswin_mod_key() +#include "keybindings.h" /* for get_keycode_to_action(), save_keybindings(), + * move_keys_mod_selection(), keyswin_mod_key() */ #include "map.h" /* for map_scroll(), map_center_player(), dir enum */ #include "main.h" /* for World struct */ @@ -49,23 +49,23 @@ extern void record_control(int action, struct World * world) extern uint8_t player_control(int key, struct World * world) { - if (key == get_action_key(world->keybindings, "player_u")) + if (key == get_keycode_to_action(world->keybindings, "player_u")) { move_player(world, NORTH); } - else if (key == get_action_key(world->keybindings, "player_r")) + else if (key == get_keycode_to_action(world->keybindings, "player_r")) { move_player(world, EAST); } - else if (key == get_action_key(world->keybindings, "player_d")) + else if (key == get_keycode_to_action(world->keybindings, "player_d")) { move_player(world, SOUTH); } - else if (key == get_action_key(world->keybindings, "player_l")) + else if (key == get_keycode_to_action(world->keybindings, "player_l")) { move_player(world, WEST); } - else if (key == get_action_key(world->keybindings, "wait")) + else if (key == get_keycode_to_action(world->keybindings, "wait")) { player_wait(world); } @@ -89,119 +89,119 @@ extern uint8_t meta_control(int key, struct World * world) char * err_shift = "Trouble with shift_active_win() in meta_keys()."; char * err_resize = "Trouble with growshrink_active_window() in " "meta_keys()."; - if (key == get_action_key(world->keybindings, "quit")) + if (key == get_keycode_to_action(world->keybindings, "quit")) { return 1; } - else if (key == get_action_key(world->keybindings, "scrl_r")) + else if (key == get_keycode_to_action(world->keybindings, "scrl_r")) { scroll_pad(win_meta, '+'); } - else if (key == get_action_key(world->keybindings, "scrl_l")) + else if (key == get_keycode_to_action(world->keybindings, "scrl_l")) { scroll_pad(win_meta, '-'); } - else if (key == get_action_key(world->keybindings, "to_keywin")) + else if (key == get_keycode_to_action(world->keybindings, "to_keywin")) { exit_err(toggle_window(win_meta, win_keys), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "to_mapwin")) + else if (key == get_keycode_to_action(world->keybindings, "to_mapwin")) { exit_err(toggle_window(win_meta, win_map), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "to_infowin")) + else if (key == get_keycode_to_action(world->keybindings, "to_infowin")) { exit_err(toggle_window(win_meta, win_info), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "to_logwin")) + else if (key == get_keycode_to_action(world->keybindings, "to_logwin")) { exit_err(toggle_window(win_meta, win_log), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "cyc_win_f")) + else if (key == get_keycode_to_action(world->keybindings, "cyc_win_f")) { cycle_active_win(win_meta, 'f'); } - else if (key == get_action_key(world->keybindings, "cyc_win_b")) + else if (key == get_keycode_to_action(world->keybindings, "cyc_win_b")) { cycle_active_win(win_meta, 'b'); } - else if (key == get_action_key(world->keybindings, "shift_f")) + else if (key == get_keycode_to_action(world->keybindings, "shift_f")) { exit_err(shift_active_win(win_meta, 'f'), world, err_shift); } - else if (key == get_action_key(world->keybindings, "shift_b")) + else if (key == get_keycode_to_action(world->keybindings, "shift_b")) { exit_err(shift_active_win(win_meta, 'b'), world, err_shift); } - else if (key == get_action_key(world->keybindings, "grow_h")) + else if (key == get_keycode_to_action(world->keybindings, "grow_h")) { exit_err(growshrink_active_window(world, '*'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "shri_h")) + else if (key == get_keycode_to_action(world->keybindings, "shri_h")) { exit_err(growshrink_active_window(world, '_'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "grow_v")) + else if (key == get_keycode_to_action(world->keybindings, "grow_v")) { exit_err(growshrink_active_window(world, '+'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "shri_v")) + else if (key == get_keycode_to_action(world->keybindings, "shri_v")) { exit_err(growshrink_active_window(world, '-'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "save_keys")) + else if (key == get_keycode_to_action(world->keybindings, "save_keys")) { save_keybindings(world); } - else if (key == get_action_key(world->keybindings, "keys_u")) + else if (key == get_keycode_to_action(world->keybindings, "keys_u")) { - keyswin_move_selection(world, 'u'); + move_keys_mod_selection(world, 'u'); } - else if (key == get_action_key(world->keybindings, "keys_d")) + else if (key == get_keycode_to_action(world->keybindings, "keys_d")) { - keyswin_move_selection(world, 'd'); + move_keys_mod_selection(world, 'd'); } - else if (key == get_action_key(world->keybindings, "keys_m")) + else if (key == get_keycode_to_action(world->keybindings, "keys_m")) { keyswin_mod_key(world, win_meta); } - else if (key == get_action_key(world->keybindings, "map_u")) + else if (key == get_keycode_to_action(world->keybindings, "map_u")) { map_scroll(world->map, NORTH, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map_d")) + else if (key == get_keycode_to_action(world->keybindings, "map_d")) { map_scroll(world->map, SOUTH, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map_r")) + else if (key == get_keycode_to_action(world->keybindings, "map_r")) { map_scroll(world->map, EAST, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map_l")) + else if (key == get_keycode_to_action(world->keybindings, "map_l")) { map_scroll(world->map, WEST, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map_c")) + else if (key == get_keycode_to_action(world->keybindings, "map_c")) { map_center_player(world->map, world->player, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "reload_wins")) + else if (key == get_keycode_to_action(world->keybindings, "reload_wins")) { reload_win_config(world); } - else if (key == get_action_key(world->keybindings, "winconf")) + else if (key == get_keycode_to_action(world->keybindings, "winconf")) { toggle_winconfig(world, world->wmeta->active); } - else if (key == get_action_key(world->keybindings, "to_height_t")) + else if (key == get_keycode_to_action(world->keybindings, "to_height_t")) { toggle_win_height_type(world, world->wmeta->active); } - else if (key == get_action_key(world->keybindings, "to_width_t")) + else if (key == get_keycode_to_action(world->keybindings, "to_width_t")) { toggle_win_width_type(world, world->wmeta->active); } - else if (key == get_action_key(world->keybindings, "save_winconf")) + else if (key == get_keycode_to_action(world->keybindings, "save_winconf")) { save_win_configs(world); } diff --git a/src/draw_wins.c b/src/draw_wins.c index dacc26b..d4275d3 100644 --- a/src/draw_wins.c +++ b/src/draw_wins.c @@ -7,7 +7,7 @@ #include /* for mvwaddch() */ #include "windows.h" /* for structs Win, Frame, for draw_scroll_hint() */ #include "misc.h" /* for center_offset() */ -#include "keybindings.h" /* for struct KeyBinding, for get_keyname() */ +#include "keybindings.h" /* for struct KeyBinding, for get_name_to_keycode() */ #include "map_objects.h" /* for structs MapObj, Player */ #include "map.h" /* for Map struct */ #include "main.h" /* for World struct */ @@ -226,18 +226,19 @@ extern void draw_info_win(struct Win * win) extern void draw_keys_win(struct Win * win) { + char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win()."; struct World * world = (struct World *) win->data; - uint16_t offset, y, x; - offset = center_offset(world->keyswindata->select, world->keyswindata->max, - win->frame.size.y - 1); - uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */ + uint16_t n_keybs = get_n_of_keybs(world); + uint16_t offset = center_offset(world->keyswindata->select, n_keybs - 1, + win->frame.size.y - 1); + + uint8_t keydescwidth = 9 + 1; /* get_name_to_keycode()'s max length + \0 */ char keydesc[keydescwidth]; - char * keyname; - char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win()."; - attr_t attri; - char * cmd_dsc; - for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) + + uint16_t y, x; + for (y = 0; y <= n_keybs - 1 && y < win->frame.size.y; y++) { + if (0 == y && offset > 0) { exit_err(draw_scroll_hint(&win->frame, y, offset + 1, '^'), @@ -245,16 +246,18 @@ extern void draw_keys_win(struct Win * win) continue; } else if (win->frame.size.y == y + 1 - && 0 < world->keyswindata->max + && 0 < (n_keybs - 1) - (win->frame.size.y + offset - 1)) { exit_err(draw_scroll_hint(&win->frame, y, - world->keyswindata->max - - (offset + win->frame.size.y) + 2, 'v'), + (n_keybs - 1) + - (offset + win->frame.size.y) + 2, + 'v'), world, err_hint); continue; } - attri = 0; + + attr_t attri = 0; if (y == world->keyswindata->select - offset) { attri = A_REVERSE; @@ -263,11 +266,12 @@ extern void draw_keys_win(struct Win * win) attri = attri | A_BLINK; } } - keyname = get_keyname(world, world->keybindings[y + offset].key); + + struct KeyBinding * kb_p = get_keyb_of_n(world, y + offset); + char * keyname = get_name_to_keycode(world, kb_p->key); snprintf(keydesc, keydescwidth, "%-9s", keyname); free(keyname); - cmd_dsc = get_command_longdsc(world, - world->keybindings[y + offset].name); + char * cmd_dsc = get_command_longdsc(world, kb_p->name); for (x = 0; x < win->frame.size.x; x++) { if (x < strlen(keydesc)) diff --git a/src/keybindings.c b/src/keybindings.c index 92e4d6d..5212d5a 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -4,11 +4,11 @@ #include /* for FILE typedef*/ #include /* for free(), atoi() */ #include /* for uint16_t */ -#include /* for keycode defines in get_keyname() */ +#include /* for keycode defines in get_name_to_keycode() */ #include /* for strchr(), strlen(), strcmp(), memcpy()*/ #include "windows.h" /* for draw_all_wins() and WinMeta struct */ #include "readwrite.h" /* for texfile_sizes(), try_fopen(), try_fclose() - * try_fclose_unlink_rename() + * try_fclose_unlink_rename(), try_fwrite() */ #include "main.h" /* for World struct */ #include "rexit.h" /* for err_exit() */ @@ -16,86 +16,24 @@ -extern void init_keybindings(struct World * world) -{ - char * f_name = "init_keybindings()"; - char * path = "config/keybindings"; - FILE * file = try_fopen(path, "r", world, f_name); - uint16_t lines, linemax; - char * err = "textfile_sizes() in init_keybindings() returns error."; - exit_err(textfile_sizes(file, &linemax, &lines), world, err); - struct KeyBinding * keybindings; - keybindings = try_malloc(lines * sizeof(struct KeyBinding), world, f_name); - char command[linemax + 1]; - uint16_t commcount = 0; - char * cmdptr; - while (fgets(command, linemax + 1, file)) - { - keybindings[commcount].key = atoi(command); - cmdptr = strchr(command, ' ') + 1; - keybindings[commcount].name = try_malloc(strlen(cmdptr), world, f_name); - memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1); - keybindings[commcount].name[strlen(cmdptr) - 1] = '\0'; - commcount++; - } - try_fclose(file, world, f_name); - struct KeysWinData * keyswindata; - keyswindata = try_malloc(sizeof(struct KeysWinData), world, f_name); - keyswindata->max = lines - 1; - keyswindata->select = 0; - keyswindata->edit = 0; - world->keybindings = keybindings; - world->keyswindata = keyswindata; -} - - - -extern void save_keybindings(struct World * world) +extern uint16_t get_keycode_to_action(struct KeyBinding * kb_p, char * name) { - char * f_name = "save_keybindings()"; - struct KeysWinData * keyswindata = (struct KeysWinData *) - world->keyswindata; - struct KeyBinding * keybindings = world->keybindings; - char * path = "config/keybindings"; - char * path_tmp = "config/keybindings_tmp"; - FILE * file = try_fopen(path_tmp, "w", world, f_name); - uint16_t linemax = 0; - uint16_t i; - for (i = 0; i <= keyswindata->max; i++) + while (0 != kb_p) { - if (strlen(keybindings[i].name) > linemax) + if (0 == strcmp(kb_p->name, name)) { - linemax = strlen(keybindings[i].name); + return kb_p->key; } + kb_p = kb_p->next; } - linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */ - char line[linemax]; - for (i = 0; i <= keyswindata->max; i++) - { - snprintf(line, linemax, - "%d %s\n", keybindings[i].key, keybindings[i].name); - fwrite(line, sizeof(char), strlen(line), file); - } - try_fclose_unlink_rename(file, path_tmp, path, world, f_name); -} - - - -extern uint16_t get_action_key(struct KeyBinding * keybindings, char * name) -{ - uint16_t i = 0; - while (strcmp(keybindings[i].name, name) ) - { - i++; - } - return keybindings[i].key; + return 0; } -extern char * get_keyname(struct World * world, uint16_t keycode) +extern char * get_name_to_keycode(struct World * world, uint16_t keycode) { - char * f_name = "get_keyname()"; + char * f_name = "get_name_to_keycode()"; char * keyname = try_malloc(15, world, f_name); if (32 < keycode && keycode < 127) { @@ -175,6 +113,125 @@ extern char * get_keyname(struct World * world, uint16_t keycode) +extern uint16_t get_n_of_keybs(struct World * world) +{ + uint16_t i = 0; + struct KeyBinding * kb_p = world->keybindings; + while (1) + { + if (0 == kb_p) + { + break; + } + i++; + kb_p = kb_p->next; + } + return i; +} + + + +extern struct KeyBinding * get_keyb_of_n(struct World * world, uint16_t n) +{ + uint16_t i = 0; + struct KeyBinding * kb_p = world->keybindings; + while (1) + { + if (n == i) + { + break; + } + i++; + kb_p = kb_p->next; + } + return kb_p; +} + + + +extern void init_keybindings(struct World * world) +{ + char * f_name = "init_keybindings()"; + + char * path = "config/keybindings"; + FILE * file = try_fopen(path, "r", world, f_name); + uint16_t lines, linemax; + char * err = "textfile_sizes() in init_keybindings() returns error."; + exit_err(textfile_sizes(file, &linemax, &lines), world, err); + + char command[linemax + 1]; + char * cmdptr; + struct KeyBinding ** loc_last_ptr = &world->keybindings; + while (fgets(command, linemax + 1, file)) + { + * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), world, f_name); + struct KeyBinding * kb_p = * loc_last_ptr; + kb_p->next = 0; + kb_p->key = atoi(command); + cmdptr = strchr(command, ' ') + 1; + kb_p->name = try_malloc(strlen(cmdptr), world, f_name); + memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1); + kb_p->name[strlen(cmdptr) - 1] = '\0'; + loc_last_ptr = & kb_p->next; + } + + try_fclose(file, world, f_name); + + struct KeysWinData * keyswindata; + keyswindata = try_malloc(sizeof(struct KeysWinData), world, f_name); + keyswindata->select = 0; + keyswindata->edit = 0; + world->keyswindata = keyswindata; +} + + + +extern void save_keybindings(struct World * world) +{ + char * f_name = "save_keybindings()"; + + char * path = "config/keybindings"; + char * path_tmp = "config/keybindings_tmp"; + FILE * file = try_fopen(path_tmp, "w", world, f_name); + + uint16_t linemax = 0; + struct KeyBinding * kb_p = world->keybindings; + while (0 != kb_p) + { + if (strlen(kb_p->name) > linemax) + { + linemax = strlen(kb_p->name); + } + kb_p = kb_p->next; + } + linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */ + + char line[linemax]; + kb_p = world->keybindings; + while (0 != kb_p) + { + snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); + kb_p = kb_p->next; + } + + try_fclose_unlink_rename(file, path_tmp, path, world, f_name); +} + + + +extern void free_keybindings(struct KeyBinding * kb_start) +{ + struct KeyBinding * kb_p = kb_start->next; + if (0 != kb_p) + { + free_keybindings(kb_p); + } + free(kb_start); +} + + + extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta) { world->keyswindata->edit = 1; @@ -183,20 +240,24 @@ extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta) int key = getch(); if (key < 1000) { - world->keybindings[world->keyswindata->select].key = key; + struct KeyBinding * kb_p = get_keyb_of_n(world, + world->keyswindata->select); + kb_p->key = key; } world->keyswindata->edit = 0; } -extern void keyswin_move_selection(struct World * world, char dir) +extern void move_keys_mod_selection(struct World * world, char dir) { - if ('u' == dir && world->keyswindata->select > 0) + if ( 'u' == dir + && world->keyswindata->select > 0) { world->keyswindata->select--; } - else if ('d' == dir && world->keyswindata->select < world->keyswindata->max) + else if ( 'd' == dir + && world->keyswindata->select < get_n_of_keybs(world) - 1) { world->keyswindata->select++; } diff --git a/src/keybindings.h b/src/keybindings.h index e0fdadb..d400414 100644 --- a/src/keybindings.h +++ b/src/keybindings.h @@ -17,8 +17,9 @@ struct WinMeta; /* Individual keybinding. */ struct KeyBinding { - char * name; /* name of functionality bound to keycode */ + struct KeyBinding * next; uint16_t key; /* keycode */ + char * name; /* name of functionality bound to keycode */ }; @@ -26,26 +27,33 @@ struct KeyBinding /* Metadata used by the keybinding editing window. */ struct KeysWinData { - uint16_t max; /* index of last keybinding (= n of keybindings - 1) */ char edit; /* 1 if currently editing a keybinding, else 0 */ - uint16_t select; /* index of keybinding selected for editing */ + uint16_t select; /* list index of keybinding selected for editing */ }; -/* Read keybindings data from / write them to the file "keybindings". */ -extern void init_keybindings(struct World * world); -extern void save_keybindings(struct World * world); +/* Return keycode matched by keybinding to command of "name". */ +extern uint16_t get_keycode_to_action(struct KeyBinding * keybindings, char * name); +/* Return human-readable name (of maximum 9 chars) for "keycode" as matched by + * ncurses.h; if none is found, return "UNKNOWN". */ +extern char * get_name_to_keycode(struct World * world, uint16_t keycode); +/* Return number of keybindings in keybindings chain. */ +extern uint16_t get_n_of_keybs(struct World * world); -/* Return keycode matching a key (functionality) name. */ -extern uint16_t get_action_key(struct KeyBinding * keybindings, char * name); +/* Return "n"-th keybinding in keybindings chain. */ +extern struct KeyBinding * get_keyb_of_n(struct World * world, uint16_t n); -/* Translate keycode to readable names of max 9 chars where possible. */ -extern char * get_keyname(struct World * world, uint16_t keycode); +/* Initialize keybindings data (by reading from file "keybindings"), save it (by + * writing to the same file) and free it. + */ +extern void init_keybindings(struct World * world); +extern void save_keybindings(struct World * world); +extern void free_keybindings(struct KeyBinding * kb_start); @@ -54,12 +62,10 @@ extern char * get_keyname(struct World * world, uint16_t keycode); */ extern void keyswin_mod_key(struct World * world, struct WinMeta * win_meta); - - /* Move selection in keybinding window upwards ("dir" = "u") or downwards ("dir" * = "d") within the limits of the keybindings list length. */ -extern void keyswin_move_selection(struct World * world, char dir); +extern void move_keys_mod_selection(struct World * world, char dir); diff --git a/src/main.c b/src/main.c index 12b8ca5..a9d22f2 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(), * draw_log_win() */ -#include "keybindings.h" /* for init_keybindings(), get_action_key() */ +#include "keybindings.h" /* for init_keybindings(), get_keycode_to_action() */ #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](), * try_fopen(), try_fclose(), try_fclose_unlink_rename() */ @@ -209,7 +209,7 @@ int main(int argc, char *argv[]) draw_all_wins(world.wmeta); key = getch(); if ( EOF != action - && key == get_action_key(world.keybindings, "wait")) + && key == get_keycode_to_action(world.keybindings, "wait")) { action = getc(file); if (EOF != action) diff --git a/src/readwrite.c b/src/readwrite.c index 4b2f2a6..09faaeb 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -2,7 +2,7 @@ #include "readwrite.h" #include /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(), - * sprintf() + * sprintf(), fwrite() */ #include /* for uint8_t, uint16_t, uint32_t */ #include /* for strlen()*/ @@ -69,6 +69,16 @@ extern void try_fgets(char * line, int linemax, FILE * file, +extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, + struct World * w, char * f) +{ + char * msg = trouble_msg(w, f, "fwrite()"); + exit_err(0 == fwrite(ptr, size, nmemb, stream), w, msg); + free(msg); +} + + + extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, struct World * w, char * f) { diff --git a/src/readwrite.h b/src/readwrite.h index 5832a76..bebcdc0 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -14,13 +14,15 @@ struct World; -/* Wrappers to calling from function called "f" of fopen(), fclose() and fgets() - * and calling exit_err() with appropriate error messages. +/* Wrappers to calling from function called "f" of fopen(), fclose(), fgets() + * and fwrite() and calling exit_err() with appropriate error messages. */ extern FILE * try_fopen(char * path, char * mode, struct World * w, char * f); extern void try_fclose(FILE * file, struct World * w, char * f); extern void try_fgets(char * line, int size, FILE * file, struct World * w, char * f); +extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, + struct World * w, char * f); diff --git a/src/rexit.c b/src/rexit.c index e39a9fe..01c5727 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -33,12 +33,17 @@ static void cleanup(struct World * world) } if (cleanup_flags & CLEANUP_KEYBINDINGS) { - uint16_t key; - for (key = 0; key <= world->keyswindata->max; key++) +/* + uint16_t i = 0; + struct KeyBinding * kb_p = world->keybindings; + while (1) { + free(kb_p); free(world->keybindings[key].name); } free(world->keybindings); +*/ + free_keybindings(world->keybindings); free(world->keyswindata); } if (cleanup_flags & CLEANUP_LOG) diff --git a/src/wincontrol.c b/src/wincontrol.c index a9f8017..c88c56b 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -4,7 +4,6 @@ #include /* for free() */ #include /* for strlen(), strchr(), strstr() */ #include /* for uint8_t, uint16_t */ -#include /* for fwrite() */ #include /* for access(), unlink() */ #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(), * resize_active_win(), init_win(), free_win(), @@ -13,7 +12,7 @@ #include "yx_uint16.h" /* for yx_uint16 struct */ #include "main.h" /* for Wins struct */ #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(), - * try_fgets(), try_fclose_unlink_rename() + * try_fgets(), try_fclose_unlink_rename(), try_fwrite() */ #include "rexit.h" /* for exit_err() */ #include "main.h" /* for World, Wins structs */ @@ -158,13 +157,13 @@ extern void save_win_config(struct World * world, char id) } char line[size]; sprintf(line, "%s\n", wc->title); - fwrite(line, sizeof(char), strlen(line), file); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); sprintf(line, "%c\n", wc->draw); - fwrite(line, sizeof(char), strlen(line), file); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); sprintf(line, "%d\n", wc->height); - fwrite(line, sizeof(char), strlen(line), file); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); sprintf(line, "%d\n", wc->width); - fwrite(line, sizeof(char), strlen(line), file); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); char * path = string_prefixed_id(world, "config/windows/Win_", id); try_fclose_unlink_rename(file, path_tmp, path, world, f_name); @@ -432,7 +431,7 @@ extern void save_win_configs(struct World * world) i++; } line[i] = '\n'; - fwrite(line, sizeof(char), strlen(line), file); + try_fwrite(line, sizeof(char), strlen(line), file, world, f_name); try_fclose_unlink_rename(file, path_tmp, path, world, f_name); }