From: Christian Heller Date: Sun, 28 Apr 2013 04:03:11 +0000 (+0200) Subject: Keybindings can now be written back to keybindings file. Also made some memory alloca... X-Git-Tag: tce~1343 X-Git-Url: https://plomlompom.com/repos/task?a=commitdiff_plain;h=9db986d3c29a6e80f71c6ab6e6a5f850fc9d7d1b;p=plomrogue Keybindings can now be written back to keybindings file. Also made some memory allocations more flexible. --- diff --git a/keybindings b/keybindings index 7e3650f..b41e36f 100644 --- a/keybindings +++ b/keybindings @@ -1,4 +1,5 @@ 81 quit +83 save keys 258 keys nav down 259 keys nav up 10 keys mod diff --git a/roguelike.c b/roguelike.c index 7f7e7b2..b571edc 100644 --- a/roguelike.c +++ b/roguelike.c @@ -215,15 +215,21 @@ void init_keybindings(struct World * world) { FILE * file = fopen("keybindings", "r"); int lines = 0; int c = 0; + int linemax = 0; + int c_count = 0; while (EOF != c) { + c_count++; c = getc(file); - if ('\n' == c) - lines++; } + if ('\n' == c) { + if (c_count > linemax) + linemax = c_count; + c_count = 0; + lines++; } } struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); fseek(file, 0, SEEK_SET); - char * command = malloc(100); + char * command = malloc(linemax); char commcount = 0; - char * digits = malloc(10); + char * digits = malloc(3); char digicount = 0; int key, digimax; int keycount = 0; @@ -261,6 +267,24 @@ void init_keybindings(struct World * world) { world->keybindings = keybindings; world->keyswindata = keyswindata; } +void save_keybindings(struct World * world) { +// Write keybidings to keybindings file. + struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata; + struct KeyBinding * keybindings = world->keybindings; + FILE * file = fopen("keybindings", "w"); + int linemax = 0; + int i; + for (i = 0; i <= keyswindata->max; i++) + if (strlen(keybindings[i].name) > linemax) + linemax = strlen(keybindings[i].name); + linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte + char * line = malloc(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); } + free(line); + fclose(file); } + int main () { struct World world; init_keybindings(&world); @@ -321,6 +345,8 @@ int main () { resize_window(&win_meta, '+'); else if (key == get_action_key(world.keybindings, "shrink ver") && win_meta.active != 0) resize_window(&win_meta, '-'); + else if (key == get_action_key(world.keybindings, "save keys")) + save_keybindings(&world); else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0) { world.keyswindata->select--; draw_all_windows (&win_meta); } @@ -331,7 +357,8 @@ int main () { world.keyswindata->edit = 1; draw_all_windows (&win_meta); key = getch(); - world.keybindings[world.keyswindata->select].key = key; + if (key < 1000) // ensure maximum of three digits in key code field + world.keybindings[world.keyswindata->select].key = key; world.keyswindata->edit = 0; draw_all_windows (&win_meta); } else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0) {