#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()
* 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()
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))
{
/* 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
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;
{
while (world.turn != start_turn)
{
- action = getc(file);
+ action = fgetc(file);
if (EOF == action)
{
break;
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);
}
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);
}
#include <unistd.h> /* for unlink(), acess() */
#include <stdlib.h> /* for size_t, calloc(), free() */
#include <string.h> /* for strlen(), strcmp(), memcpy() */
-#include <stdint.h> /* 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 <stdint.h> /* 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()
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";
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);
}
* sprintf(), fwrite(), ferror()
*/
#include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
-#include <string.h> /* for strlen()*/
+#include <string.h> /* for strlen() */
#include <unistd.h> /* 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 ";
+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);
-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");
}
-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);
}
*/
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.
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);
#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(),
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. */
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);
}