From: Christian Heller Date: Tue, 26 Nov 2013 04:35:38 +0000 (+0100) Subject: Differentiated try_fgetc() (fgetc() wrapper proper) and try_fgetc_noeof() (catches... X-Git-Tag: tce~905 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_data.id_%7D%7D?a=commitdiff_plain;h=fdb7a8eb2bc58d180d53f4085085a62bc1f2e62b;p=plomrogue Differentiated try_fgetc() (fgetc() wrapper proper) and try_fgetc_noeof() (catches EOF as error). --- diff --git a/src/main.c b/src/main.c index a8f16d4..2d09636 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 try_fgetc(), read_uint32_bigendian(), * write_uint32_bigendian(), try_fopen(), try_fclose(), - * try_fclose_unlink_rename(), try_fgetc() + * try_fclose_unlink_rename(), try_fgetc_noeof(), */ #include "map_objects.h" /* for structs MapObj, init_map_object_defs(), * build_map_objects(), get_player() @@ -171,7 +171,7 @@ int main(int argc, char *argv[]) { while (world.turn != start_turn) { - action = fgetc(file); + action = try_fgetc(file, f_name); if (EOF == action) { break; @@ -179,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 = try_fgetc(file, f_name); + world.inventory_select = try_fgetc_noeof(file, f_name); } player_control_by_id(action); } @@ -197,13 +197,13 @@ int main(int argc, char *argv[]) if ( EOF != action && key == get_available_keycode_to_action("wait")) { - action = fgetc(file); + action = try_fgetc(file, f_name); if (EOF != action) { if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - world.inventory_select = try_fgetc(file, f_name); + world.inventory_select = try_fgetc_noeof(file, f_name); } player_control_by_id(action); } diff --git a/src/misc.c b/src/misc.c index 0518aef..1b8fe63 100644 --- a/src/misc.c +++ b/src/misc.c @@ -7,7 +7,7 @@ #include /* for strlen(), strcmp(), memcpy() */ #include /* for uint8_t, uint16_t */ #include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax(), - * try_fputc() + * try_fputc(), try_fgetc() */ #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(), * write_map_objects() @@ -202,11 +202,11 @@ extern void turn_over(char action) { FILE * file_old = try_fopen(recordfile, "r", f_name); FILE * file_new = try_fopen(recordfile_tmp, "w", f_name); - char c = fgetc(file_old); + int c = try_fgetc(file_old, f_name); while (EOF != c) { - try_fputc(c, file_new, f_name); - c = fgetc(file_old); + try_fputc((uint8_t) c, file_new, f_name); + c = try_fgetc(file_old, f_name); } try_fclose(file_old, f_name); try_fputc(action, file_new, f_name); diff --git a/src/readwrite.c b/src/readwrite.c index 32553e7..78f1bff 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -44,6 +44,13 @@ extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, +extern void try_fputc(uint8_t c, FILE * file, char * f) +{ + exit_trouble(EOF == fputc(c, file), f, "fputc()"); +} + + + extern char * try_fgets(char * line, int linemax, FILE * file, char * f) { char * test = fgets(line, linemax, file); @@ -53,19 +60,21 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f) -extern uint8_t try_fgetc(FILE * file, char * f) +extern int try_fgetc(FILE * file, char * f) { int test = fgetc(file); - exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF"); - return (uint8_t) test; + exit_trouble(EOF == test && ferror(file), f, "fgetc()"); + return test; } -extern void try_fputc(uint8_t c, FILE * file, char * f) +extern uint8_t try_fgetc_noeof(FILE * file, char * f) { - int test = fputc(c, file); - exit_trouble(EOF == test, f, "fputc() hitting EOF"); + char * f_name = "try_fgetc_noeof()"; + int test = try_fgetc(file, f_name); + exit_trouble(EOF == test, f, "fgetc() unexpectedly hitting EOF"); + return (uint8_t) test; } @@ -108,13 +117,14 @@ 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) { + char * f_name = "textfile_sizes()"; int c = 0; uint16_t c_count = 0; uint16_t n_lines = 0; uint16_t linemax = 0; while (1) { - c = fgetc(file); + c = try_fgetc(file, f_name); if (EOF == c) { break; @@ -156,10 +166,10 @@ extern uint32_t read_uint32_bigendian(FILE * file) { 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); + x = (uint32_t) try_fgetc_noeof(file, f_name) << 24; + x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) << 16 ); + x = x + ( (uint32_t) try_fgetc_noeof(file, f_name) << 8 ); + x = x + (uint32_t) try_fgetc_noeof(file, f_name); return x; } diff --git a/src/readwrite.h b/src/readwrite.h index 56f34a6..2e0ca9f 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -18,6 +18,7 @@ extern FILE * try_fopen(char * path, char * mode, char * f); extern void try_fclose(FILE * file, char * f); extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, char * f); +extern void try_fputc(uint8_t c, FILE * file, char * f); /* Wrapper to calling fgets() from function called "f". The return code of * fgets() is returned unless it is NULL *and* ferror() indicates that an error @@ -25,9 +26,11 @@ 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); +/* 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. + */ +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". diff --git a/src/wincontrol.c b/src/wincontrol.c index c89f897..6371efa 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -11,7 +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() + * try_fgetc_noeof() */ #include "rexit.h" /* for exit_err(), exit_trouble() */ #include "draw_wins.h" /* for draw_win_map(), draw_win_info(), draw_win_log(), @@ -399,7 +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 = try_fgetc(file, f_name); + uint8_t a = try_fgetc_noeof(file, f_name); try_fclose(file, f_name); /* Toggle windows and set active window selection. */