From d4abb0d746f17b666b6c3871e5c5e13454f49f40 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 11 May 2013 11:35:08 +0200 Subject: [PATCH] Simplified keybindings initialization, using stdlib functions, unusing math functions. --- roguelike.c | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/roguelike.c b/roguelike.c index 5203b4b..6e9a01b 100644 --- a/roguelike.c +++ b/roguelike.c @@ -2,7 +2,6 @@ #include #include #include -#include #include "windows.h" struct Map { @@ -255,43 +254,22 @@ void init_keybindings(struct World * world) { c = getc(file); if ('\n' == c) { if (c_count > linemax) - linemax = c_count; + linemax = c_count + 1; c_count = 0; lines++; } } struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); fseek(file, 0, SEEK_SET); char * command = malloc(linemax); int commcount = 0; - char * digits = malloc(3); - int digicount = 0; - int key, digimax; - int keycount = 0; - c = getc(file); - while (EOF != c) { - if ('\n' == c) { - keybindings[keycount].name = calloc(commcount + 1, sizeof(char)); - memcpy(keybindings[keycount].name, command, commcount); - keybindings[keycount].key = key; - keycount++; - digicount = 0; - commcount = 0; } - else if (-1 != digicount) { - if (' ' == c) { - key = 0; - digimax = digicount - 1; - while (digicount > 0) { - digicount--; - key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); } - digicount = -1; } - else { - digits[digicount] = c; - digicount++; } } - else { - command[commcount] = c; - commcount++; } - c = getc(file); } + char * cmdptr; + while (fgets(command, linemax, file)) { + keybindings[commcount].key = atoi(command); + cmdptr = strchr(command, ' ') + 1; + keybindings[commcount].name = malloc(strlen(cmdptr)); + memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1); + keybindings[commcount].name[strlen(cmdptr) - 1] = '\0'; + commcount++; } free(command); - free(digits); fclose(file); struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData)); keyswindata->max = lines - 1; -- 2.30.2