From: Christian Heller Date: Tue, 26 Nov 2013 03:55:17 +0000 (+0100) Subject: Replaced entire read/write_uint*_bigendian() family in readwrite library with simpler... X-Git-Tag: tce~906 X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D/%27%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28escapeHTML%28span%5B2%5D%29%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28%27?a=commitdiff_plain;h=d504e5724574f9502ec203d495284738601bb6a0;p=plomrogue Replaced entire read/write_uint*_bigendian() family in readwrite library with simpler try_fgetc()/try_fputc() and bigendian-sorted calls to it. --- diff --git a/src/main.c b/src/main.c index 9bb983d..a8f16d4 100644 --- a/src/main.c +++ b/src/main.c @@ -10,9 +10,9 @@ #include "windows.h" /* for structs WinMeta, Win, init_win_meta(), * draw_all_wins() */ -#include "readwrite.h" /* for read_uint32_bigendian](), +#include "readwrite.h" /* for read_uint32_bigendian(), * write_uint32_bigendian(), try_fopen(), try_fclose(), - * try_fclose_unlink_rename() + * try_fclose_unlink_rename(), try_fgetc() */ #include "map_objects.h" /* for structs MapObj, init_map_object_defs(), * build_map_objects(), get_player() @@ -23,7 +23,7 @@ * load_interface_conf(), load_game(), rrand() */ #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */ -#include "rexit.h" /* for exit_game(), exit_err() */ +#include "rexit.h" /* for exit_game() */ #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */ #include "control.h" /* for control_by_id(), player_control(), * get_available_keycode_to_action() @@ -92,8 +92,6 @@ int main(int argc, char *argv[]) world.map_obj_count = 0; /* For interactive mode, try to load world state from savefile. */ - char * err_r = "Trouble loading game (in main()) / " - "reading from opened 'savefile'."; FILE * file; if (1 == world.interactive && 0 == access(savefile, F_OK)) { @@ -104,12 +102,10 @@ int main(int argc, char *argv[]) /* For non-interactive mode, try to load world state from record file. */ else { - err_r = "Trouble reading from 'record' file (read_uint32_bigendian() " - "in main())."; if (0 == world.interactive) { file = try_fopen(recordfile, "r", f_name); - exit_err(read_uint32_bigendian(file, &world.seed), err_r); + world.seed = read_uint32_bigendian(file); } /* For interactive-mode in newly started world, generate a start seed @@ -118,12 +114,8 @@ int main(int argc, char *argv[]) else { world.seed = time(NULL); - - char * err_w = "Trouble recording new seed " - "(write_uint32_bigendian() in main()) / writing to " - "file 'record_tmp'."; file = try_fopen(recordfile_tmp, "w", f_name); - exit_err(write_uint32_bigendian(world.seed, file), err_w); + write_uint32_bigendian(world.seed, file); try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name); } world.mapseed = world.seed; @@ -179,7 +171,7 @@ int main(int argc, char *argv[]) { while (world.turn != start_turn) { - action = getc(file); + action = fgetc(file); if (EOF == action) { break; @@ -187,7 +179,7 @@ int main(int argc, char *argv[]) if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - world.inventory_select = getc(file); + world.inventory_select = try_fgetc(file, f_name); } player_control_by_id(action); } @@ -205,13 +197,13 @@ int main(int argc, char *argv[]) if ( EOF != action && key == get_available_keycode_to_action("wait")) { - action = getc(file); + action = fgetc(file); if (EOF != action) { if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - world.inventory_select = getc(file); + world.inventory_select = try_fgetc(file, f_name); } player_control_by_id(action); } diff --git a/src/misc.c b/src/misc.c index c637a5d..0518aef 100644 --- a/src/misc.c +++ b/src/misc.c @@ -5,9 +5,9 @@ #include /* for unlink(), acess() */ #include /* for size_t, calloc(), free() */ #include /* for strlen(), strcmp(), memcpy() */ -#include /* for uint8_t, uint16_t, uint32_t */ -#include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](), - * try_fopen(), try_fclose(), get_linemax() +#include /* for uint8_t, uint16_t */ +#include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax(), + * try_fputc() */ #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(), * write_map_objects() @@ -195,8 +195,6 @@ extern uint16_t center_offset(uint16_t pos, uint16_t mapsize, extern void turn_over(char action) { char * f_name = "turn_over()"; - char * err_write = "Trouble in turn_over() with write_uint8() " - "writing to opened file 'record_tmp'."; char * recordfile_tmp = "record_tmp"; char * recordfile = "record"; @@ -207,15 +205,15 @@ extern void turn_over(char action) char c = fgetc(file_old); while (EOF != c) { - exit_err(write_uint8(c, file_new), err_write); + try_fputc(c, file_new, f_name); c = fgetc(file_old); } try_fclose(file_old, f_name); - exit_err(write_uint8(action, file_new), err_write); + try_fputc(action, file_new, f_name); if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - exit_err(write_uint8(world.inventory_select, file_new), err_write); + try_fputc(world.inventory_select, file_new, f_name); } try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile, f_name); } diff --git a/src/readwrite.c b/src/readwrite.c index bfbd4f6..32553e7 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -5,23 +5,13 @@ * sprintf(), fwrite(), ferror() */ #include /* for uint8_t, uint16_t, uint32_t */ -#include /* for strlen()*/ +#include /* for strlen() */ #include /* for unlink() */ #include "rexit.h" /* for exit_err(), exit_trouble() */ #include "main.h" /* for world global */ -/* Read/write "x" from/to "file" as bigendian representation of "size" bits. On - * failure, return 1, else 0. (As of of now, all extern read/write functions - * build on top of these.) Only use multiples of 8 greater or equal 32 for - * "size", so that storage inside uint32_t is possible. - */ -static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size); -static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size); - - - extern FILE * try_fopen(char * path, char * mode, char * f) { char * msg1 = "Trouble in "; @@ -46,6 +36,14 @@ extern void try_fclose(FILE * file, char * f) +extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, + char * f) +{ + exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()"); +} + + + extern char * try_fgets(char * line, int linemax, FILE * file, char * f) { char * test = fgets(line, linemax, file); @@ -55,10 +53,19 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f) -extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, - char * f) +extern uint8_t try_fgetc(FILE * file, char * f) { - exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()"); + int test = fgetc(file); + exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF"); + return (uint8_t) test; +} + + + +extern void try_fputc(uint8_t c, FILE * file, char * f) +{ + int test = fputc(c, file); + exit_trouble(EOF == test, f, "fputc() hitting EOF"); } @@ -145,68 +152,24 @@ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, -static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size) -{ - * x = 0; - int16_t bitshift = size - 8; - int test; - for (; bitshift >= 0; bitshift = bitshift - 8) - { - test = fgetc(file); - if (EOF == test) - { - return 1; - } - * x = * x + ((uint32_t) test << bitshift); - } - return 0; -} - - - -static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size) -{ - int16_t bitshift = size - 8; - for (; bitshift >= 0; bitshift = bitshift - 8) - { - if (EOF == fputc((x >> bitshift) & 0xFF, file)) - { - return 1; - } - } - return 0; -} - - - -extern uint8_t read_uint8(FILE * file, uint8_t * x) -{ - /* Since read_uintX_bigendian() works on -- and zeroes -- four bytes, direct - * work on values of fewer bytes would corrupt immediate neighbor values. - */ - uint32_t y = * x; - uint8_t err = read_uintX_bigendian(file, &y, 8); - * x = (uint8_t) y; - return err; -} - - - -extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x) -{ - return read_uintX_bigendian(file, x, 32); -} - - - -extern uint8_t write_uint8(uint8_t x, FILE * file) +extern uint32_t read_uint32_bigendian(FILE * file) { - return write_uintX_bigendian(file, x, 8); + char * f_name = "read_uint32_bigendian()"; + uint32_t x; + x = (uint32_t) try_fgetc(file, f_name) << 24; + x = x + ( (uint32_t) try_fgetc(file, f_name) << 16 ); + x = x + ( (uint32_t) try_fgetc(file, f_name) << 8 ); + x = x + (uint32_t) try_fgetc(file, f_name); + return x; } -extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file) +extern void write_uint32_bigendian(uint32_t x, FILE * file) { - return write_uintX_bigendian(file, x, 32); + char * f_name = "write_uint32_bigendian()"; + try_fputc( x >> 24, file, f_name); + try_fputc( ( x >> 16 ) & 0xFF, file, f_name); + try_fputc( ( x >> 8 ) & 0xFF, file, f_name); + try_fputc( x & 0xFF, file, f_name); } diff --git a/src/readwrite.h b/src/readwrite.h index 58bf4f2..56f34a6 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -25,6 +25,10 @@ extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, */ extern char * try_fgets(char * line, int size, FILE * file, char * f); +/* fgetc()/fputc() wrappers to catch EOF returns/upon it call exit_trouble(). */ +extern uint8_t try_fgetc(FILE * file, char * f); +extern void try_fputc(uint8_t c, 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. @@ -48,14 +52,9 @@ extern uint16_t get_linemax(FILE * file, char * f); extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p); -/* These routines for reading values "x" from / writing values to "file" ensure - * a defined endianness and consistent error codes: return 0 on success and 1 on - * fgetc()/fputc() failure. - */ -extern uint8_t read_uint8(FILE * file, uint8_t * x); -extern uint8_t read_uint32_bigendian(FILE * file, uint32_t * x); -extern uint8_t write_uint8(uint8_t x, FILE * file); -extern uint8_t write_uint32_bigendian(uint32_t x, FILE * file); +/* Read/write via try_(fgetc|fputc)() uint32 values with defined endianness. */ +extern uint32_t read_uint32_bigendian(FILE * file); +extern void write_uint32_bigendian(uint32_t x, FILE * file); diff --git a/src/wincontrol.c b/src/wincontrol.c index 4eca93b..c89f897 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -11,6 +11,7 @@ #include "main.h" /* for world global */ #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(), * try_fgets(), try_fclose_unlink_rename(), try_fwrite() + * try_fgetc() */ #include "rexit.h" /* for exit_err(), exit_trouble() */ #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_log(), @@ -398,8 +399,7 @@ extern void sorted_wintoggle_and_activate() uint16_t linemax = get_linemax(file, f_name); char win_order[linemax + 1]; try_fgets(win_order, linemax + 1, file, f_name); - uint8_t a; - exit_trouble(read_uint8(file, &a), f_name, "read_uint8()"); + uint8_t a = try_fgetc(file, f_name); try_fclose(file, f_name); /* Toggle windows and set active window selection. */ @@ -452,7 +452,7 @@ extern void save_win_configs() if (0 != world.wmeta->active) { struct WinConf * wc = get_winconf_by_win(world.wmeta->active); - write_uint8(wc->id, file); + try_fputc(wc->id, file, f_name); } try_fclose_unlink_rename(file, path_tmp, path, f_name); }