#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()
{
while (world.turn != start_turn)
{
- action = fgetc(file);
+ action = try_fgetc(file, f_name);
if (EOF == action)
{
break;
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);
}
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);
}
#include <string.h> /* for strlen(), strcmp(), memcpy() */
#include <stdint.h> /* 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()
{
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);
+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);
-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;
}
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;
{
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;
}
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
*/
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".
#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(),
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. */