From: Christian Heller Date: Tue, 26 Nov 2013 16:20:13 +0000 (+0100) Subject: Simplified textfile_sizes() and replaced all get_linemax() calls with it. X-Git-Tag: tce~904 X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bdb.prefix%7D%7D/day?a=commitdiff_plain;h=10680a2398daf76e6a0cd261c2b247e6902f2ad0;p=plomrogue Simplified textfile_sizes() and replaced all get_linemax() calls with it. --- diff --git a/README b/README index 33a231f..5ce32d5 100644 --- a/README +++ b/README @@ -12,9 +12,8 @@ skeletons or "magic meat"--such objects can be collected, and "magic meat" can be consumed to gain hitpoints. Note that different kinds of moves take different numbers of turns to finish. -Enemies' AI is very dumb so far: Each turn, they -try to move in the (beeline) direction of the nearest enemy, so they often bump -into obstacles. +Enemies' AI is very dumb so far: Each turn, they try to move in the (beeline) +direction of the nearest enemy, so they often bump into obstacles. There is only one save file (named "savefile"), and it gets overwritten each new turn. To start over with a new world, delete it. diff --git a/src/command_db.c b/src/command_db.c index fd7f4dc..110f91f 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -82,9 +82,8 @@ extern void init_command_db() char * path = "config/commands"; FILE * file = try_fopen(path, "r", f_name); - uint16_t lines, linemax; - uint8_t test = textfile_sizes(file, &linemax, &lines); - exit_trouble(test, f_name, "textfile_sizes()"); + uint16_t lines; + uint16_t linemax = textfile_sizes(file, &lines); char line[linemax + 1]; struct Command * cmds = try_malloc(lines * sizeof(struct Command), f_name); diff --git a/src/keybindings.c b/src/keybindings.c index 29afb78..0fd2cdc 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -146,9 +146,8 @@ extern void init_keybindings(char * path, struct KeyBiData * kbd) char * f_name = "init_keybindings()"; FILE * file = try_fopen(path, "r", f_name); - uint16_t lines, linemax; - char * err = "textfile_sizes() in init_keybindings() returns error."; - exit_err(textfile_sizes(file, &linemax, &lines), err); + uint16_t lines; + uint16_t linemax = textfile_sizes(file, &lines); char command[linemax + 1]; char * cmdptr; diff --git a/src/map_object_actions.c b/src/map_object_actions.c index cdd1e97..9541214 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -12,7 +12,7 @@ #include "misc.h" /* for update_log(), try_malloc() */ #include "map.h" /* for is_passable() */ #include "main.h" /* for world global */ -#include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax() */ +#include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes() */ #include "rexit.h" /* for exit_err() */ @@ -186,7 +186,7 @@ extern void init_map_object_actions() char * path = "config/map_object_actions"; FILE * file = try_fopen(path, "r", f_name); - uint16_t linemax = get_linemax(file, f_name); + uint16_t linemax = textfile_sizes(file, NULL); char line[linemax + 1]; struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts; diff --git a/src/map_objects.c b/src/map_objects.c index 1a598cb..0407352 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -5,8 +5,8 @@ #include /* for uint8_t */ #include /* for FILE typedef */ #include /* for strchr(), strlen(), memcpy(), strtok() */ -#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose() - * [read/write]_uint[8/16/23][_bigendian]() +#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(), + * try_fgets() */ #include "misc.h" /* for try_malloc(), find_passable_pos() */ #include "main.h" /* for world global */ @@ -72,7 +72,7 @@ extern void init_map_object_defs(char * filename) { char * f_name = "init_map_object_defs()"; FILE * file = try_fopen(filename, "r", f_name); - uint16_t linemax = get_linemax(file, f_name); + uint16_t linemax = textfile_sizes(file, NULL); struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs; char * delim = " "; char line[linemax + 1]; diff --git a/src/misc.c b/src/misc.c index 1b8fe63..97c9fa6 100644 --- a/src/misc.c +++ b/src/misc.c @@ -6,7 +6,7 @@ #include /* for size_t, calloc(), free() */ #include /* for strlen(), strcmp(), memcpy() */ #include /* for uint8_t, uint16_t */ -#include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax(), +#include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes(), * try_fputc(), try_fgetc() */ #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(), @@ -283,7 +283,7 @@ extern void load_game() char * f_name = "load_game2()"; char * filename = "savefile"; FILE * file = try_fopen(filename, "r", f_name); - uint16_t linemax = get_linemax(file, f_name); + uint16_t linemax = textfile_sizes(file, NULL); char line[linemax + 1]; try_fgets(line, linemax + 1, file, f_name); world.mapseed = atoi(line); diff --git a/src/readwrite.c b/src/readwrite.c index 78f1bff..06a6344 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -105,17 +105,7 @@ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, -extern uint16_t get_linemax(FILE * file, char * f) -{ - uint16_t linemax; - exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()"); - return linemax; -} - - - -extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, - uint16_t * n_lines_p) +extern uint16_t textfile_sizes(FILE * file, uint16_t * n_lines_p) { char * f_name = "textfile_sizes()"; int c = 0; @@ -147,17 +137,12 @@ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, { /* line / lack newline chars. */ linemax = c_count; } - - if (-1 == fseek(file, 0, SEEK_SET)) - { - return 1; - } - * linemax_p = linemax; + exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()"); if (n_lines_p) { * n_lines_p = n_lines; } - return 0; + return linemax; } diff --git a/src/readwrite.h b/src/readwrite.h index 2e0ca9f..0dc47da 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -11,8 +11,8 @@ -/* Wrappers to calling from function called "f" of fopen(), fclose(), fgets() - * and fwrite() and calling exit_err() with appropriate error messages. +/* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f", + * calling exit_err() upon error with appropriate error messages. */ extern FILE * try_fopen(char * path, char * mode, char * f); extern void try_fclose(FILE * file, char * f); @@ -26,34 +26,25 @@ extern void try_fputc(uint8_t c, FILE * file, char * f); */ extern char * try_fgets(char * line, int size, FILE * file, char * f); -/* Wrappers to calling fgetc() and (try_fgetc_noeof()) treating all EOFs returns - * as errors to call exit_trouble(), or (try_fgetc()) only if ferror() says so. +/* Wrapper to calling fgetc() from function "f", treating either + * (try_fgetc_noeof()) all EOF returns as errors triggering exit_trouble(), or + * (try_fgetc()) only those accompanied by a positive ferror() result. */ extern int try_fgetc(FILE * file, char * f); extern uint8_t try_fgetc_noeof(FILE * file, char * f); /* Wrapper to successive call of fclose() from function called "f" on "file", - * then unlink() on file at "p2" if it exists, then rename() on "p1" to "p2". - * Used for handling atomic saving of files via temp files. + * then unlink() on file at path "p2" if it exists, then rename() from path "p1" + * to "p2". Used for handling atomic saving of files via temp files. */ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2, char * f); -/* Wrapper: Call textfile_sizes() from function called "f" to get max line - * length (includes newline char) of "file", exit via exit_err() with - * exit_trouble() on failure. +/* Return largest line length from "file" the largest line length (including + * newline chars) and write the number of newline chars in "file" to the memory + * pointed to by "n_lines_p" if it is not passed as NULL. */ -extern uint16_t get_linemax(FILE * file, char * f); - -/* 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 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); +extern uint16_t textfile_sizes(FILE * file, uint16_t * n_lines_p); /* Read/write via try_(fgetc|fputc)() uint32 values with defined endianness. */ extern uint32_t read_uint32_bigendian(FILE * file); diff --git a/src/wincontrol.c b/src/wincontrol.c index 6371efa..74bd4a2 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -9,7 +9,7 @@ */ #include "yx_uint16.h" /* for yx_uint16 struct */ #include "main.h" /* for world global */ -#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(), +#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(), * try_fgets(), try_fclose_unlink_rename(), try_fwrite() * try_fgetc_noeof() */ @@ -83,7 +83,7 @@ static void init_winconf_from_file(char id, struct WinConf * winconf) /* Prepare reading in file line by line into "line" array. */ FILE * file = try_fopen(path, "r", context); free(path); - uint16_t linemax = get_linemax(file, context); + uint16_t linemax = textfile_sizes(file, NULL/*, context*/); char line[linemax + 1]; /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */ @@ -396,7 +396,7 @@ extern void sorted_wintoggle_and_activate() /* Read from file order of windows to be toggled + active win selection. */ char * path = "config/windows/toggle_order_and_active"; FILE * file = try_fopen(path, "r", f_name); - uint16_t linemax = get_linemax(file, f_name); + uint16_t linemax = textfile_sizes(file, NULL); char win_order[linemax + 1]; try_fgets(win_order, linemax + 1, file, f_name); uint8_t a = try_fgetc_noeof(file, f_name);