X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Freadwrite.c;h=e6660ae4238ef9ed16a0ad0dfc2634c9e865b5ce;hb=e75b508724effaad1bfd6dcf337c34a368855095;hp=32553e7c8d5519556ec9559a95454236e4aae1cf;hpb=d504e5724574f9502ec203d495284738601bb6a0;p=plomrogue diff --git a/src/readwrite.c b/src/readwrite.c index 32553e7..e6660ae 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -1,6 +1,7 @@ /* readwrite.c */ #include "readwrite.h" +#include /* for size_t */ #include /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(), * sprintf(), fwrite(), ferror() */ @@ -8,7 +9,6 @@ #include /* for strlen() */ #include /* for unlink() */ #include "rexit.h" /* for exit_err(), exit_trouble() */ -#include "main.h" /* for world global */ @@ -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; } @@ -96,25 +105,16 @@ 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; 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; @@ -137,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; } @@ -156,10 +151,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; }