From: Christian Heller Date: Wed, 4 Sep 2013 01:25:34 +0000 (+0200) Subject: Improved and defined more precisely textfile_sizes(). X-Git-Tag: tce~977 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/blog?a=commitdiff_plain;h=0568c1c0f6735509f2a1afea31ecb5dc28f26bf4;p=plomrogue Improved and defined more precisely textfile_sizes(). --- diff --git a/src/command_db.c b/src/command_db.c index d5af5c2..bdcdcd1 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -87,12 +87,12 @@ extern void init_command_db(struct World * world) exit_err(NULL == file, world, err_o); uint16_t lines, linemax; exit_err(textfile_sizes(file, &linemax, &lines), world, err_s); + char line[linemax + 1]; - char line[linemax]; struct Command * cmds = malloc(lines * sizeof(struct Command)); exit_err(NULL == line, world, err_m); uint8_t i = 0; - while (fgets(line, linemax, file)) + while (fgets(line, linemax + 1, file)) { cmds[i].id = atoi(strtok(line, " ")); copy_tokenized_string(world, &cmds[i].dsc_short, " ", err_m); diff --git a/src/keybindings.c b/src/keybindings.c index 121304b..8b01958 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -21,7 +21,7 @@ extern void init_keybindings(struct World * world) char * err = "textfile_sizes() in init_keybindings() returns error."; exit_err(textfile_sizes(file, &linemax, &lines), world, err); struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); - char * command = malloc(linemax); + char * command = malloc(linemax + 1); uint16_t commcount = 0; char * cmdptr; while (fgets(command, linemax + 1, file)) diff --git a/src/map_objects.c b/src/map_objects.c index 8b84ac1..f623801 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -107,10 +107,10 @@ extern void init_map_object_defs(struct World * world, char * filename) world->monster_def = 0; struct ItemDef * * p_p_id = &world->item_def; struct MonsterDef * * p_p_md = &world->monster_def; - char defline[linemax]; + char defline[linemax + 1]; char * line_p; char * delim = " "; - while (fgets(defline, linemax, file)) + while (fgets(defline, linemax + 1, file)) { mod.next = 0; mod.id = atoi(strtok(defline, delim)); diff --git a/src/misc.c b/src/misc.c index 496c585..4b25f65 100644 --- a/src/misc.c +++ b/src/misc.c @@ -20,19 +20,23 @@ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p) { - uint16_t n_lines = 0; int c = 0; - uint16_t linemax = 0; uint16_t c_count = 0; - while (EOF != c) + uint16_t n_lines = 0; + uint16_t linemax = 0; + while (1) { - c_count++; c = getc(file); + if (EOF == c) + { + break; + } + c_count++; if ('\n' == c) { if (c_count > linemax) { - linemax = c_count + 1; + linemax = c_count; } c_count = 0; if (n_lines_p) @@ -41,6 +45,11 @@ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, } } } + if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */ + { /* line / lack newline chars. */ + linemax = c_count; + } + if (-1 == fseek(file, 0, SEEK_SET)) { return 1; diff --git a/src/misc.h b/src/misc.h index 5c9d3c4..568c56d 100644 --- a/src/misc.h +++ b/src/misc.h @@ -17,10 +17,12 @@ struct Map; -/* Learn from "file" the largest line length (pointed to by "linemax_p") and - * (pointed to by "n_lines_p" if it is not set to NULL) the number of lines. +/* Learn from "file" the largest line length (pointed to by "linemax_p"; length + * includes newline chars) and (pointed to by "n_lines_p" if it is not set to + * NULL) the number of lines (= number of newline chars). * - * Returns 0 on success, 1 on fseek() error. + * Returns 0 on success, 1 on error of fseek() (called to return to initial file + * reading position). */ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p); diff --git a/src/wincontrol.c b/src/wincontrol.c index b4a8640..10a9382 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -93,23 +93,22 @@ static void init_winconf_from_file(struct World * world, char id) FILE * file = fopen(path, "r"); exit_err(NULL == file, world, err_o); free(path); - uint16_t linemax; exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); - char line[linemax]; + char line[linemax + 1]; struct WinConf * winconf = get_winconf_by_id(world, id); - exit_err(NULL == fgets(line, linemax, file), world, err_g); + exit_err(NULL == fgets(line, linemax + 1, file), world, err_g); exit_err(NULL == (winconf->title = malloc(strlen(line))), world, err_m); memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */ winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */ - exit_err(NULL == fgets(line, linemax, file), world, err_g); + exit_err(NULL == fgets(line, linemax + 1, file), world, err_g); winconf->height = atoi(line); if (0 >= winconf->height) { winconf->height_type = 1; } - exit_err(NULL == fgets(line, linemax, file), world, err_g); + exit_err(NULL == fgets(line, linemax + 1, file), world, err_g); winconf->width = atoi(line); if (0 >= winconf->width) { @@ -308,16 +307,15 @@ extern void sorted_wintoggle(struct World * world) char * path = "config/windows/toggle_order"; FILE * file = fopen(path, "r"); exit_err(NULL == file, world, err_o); - uint16_t linemax; exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); + char win_order[linemax + 1]; - char win_order[linemax]; - exit_err(NULL == fgets(win_order, linemax, file), world, err_g); + exit_err(NULL == fgets(win_order, linemax + 1, file), world, err_g); exit_err(fclose(file), world, err_c); uint8_t i = 0; - for (; i < linemax - 2; i++) + for (; i < linemax - 1; i++) { toggle_window(world->wmeta, get_win_by_id(world, win_order[i])); }