/* misc.c */
#include "misc.h"
+#include <stdio.h> /* for rename() */
+#include <unistd.h> /* for unlink(), acess() */
#include <stdlib.h> /* for calloc(), free() */
#include <string.h> /* for strlen(), strcmp(), memcpy() */
+#include <stdint.h> /* for uint8_t */
#include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
* resize_active_win(), cycle_active_win(),
* shift_active_win(), struct Win, struct WinMeta
extern void turn_over(struct World * world, char action)
{
+ char * err_open = "Error recording move: "
+ "Unable to open file 'record_tmp' for appending.";
+ char * err_write = "Error recording move: "
+ "Trouble writing to opened file 'record_tmp'.";
+ char * err_close = "Error recording move: "
+ "Unable to close opened file 'record_tmp'.";
+ char * err_unl = "Error recording move: "
+ "Unable to unlink old file 'record'.";
+ char * err_move = "Error recording move: "
+ "Unable to rename file 'record_tmp' to 'record'.";
+ char * recordfile_tmp = "record_tmp";
+ char * recordfile = "record";
if (1 == world->interactive)
{
- FILE * file = fopen("record", "a");
- exit_err(write_uint8(action, file), world, "Record writing failure.");
- fclose(file);
+ FILE * file = fopen(recordfile_tmp, "a");
+ exit_err(0 == file, world, err_open);
+ exit_err(write_uint8(action, file), world, err_write);
+ exit_err(fclose(file), world, err_close);
+ exit_err(unlink(recordfile), world, err_unl);
+ exit_err(rename(recordfile_tmp, recordfile), world, err_move);
}
+
world->turn++;
rrand_seed(world->seed * world->turn);
struct Monster * monster;
extern void save_game(struct World * world)
{
- uint8_t err;
- FILE * file = fopen("savefile", "w");
- err = write_uint32_bigendian(world->seed, file);
- err = err | write_uint32_bigendian(world->turn, file);
- err = err | write_uint16_bigendian(world->player->pos.y + 1, file);
- err = err | write_uint16_bigendian(world->player->pos.x + 1, file);
- err = err | write_uint8(world->player->hitpoints, file);
- err = err | write_map_objects(world, world->monster, file);
- err = err | write_map_objects(world, world->item, file);
- exit_err(err, world, "Error saving game.");
- fclose(file);
+ char * err_open = "Error saving game: "
+ "Unable to open file 'savefile_tmp' for writing.";
+ char * err_write = "Error saving game: "
+ "Trouble writing to opened file 'savefile_tmp'.";
+ char * err_close = "Error saving game: "
+ "Unable to close opened file 'savefile_tmp'.";
+ char * err_unl = "Error saving game: "
+ "Unable to unlink old 'savefile' file.";
+ char * err_move = "Error saving game: "
+ "Unable to rename 'file savefile_tmp' to 'savefile'.";
+ char * savefile_tmp = "savefile_tmp";
+ char * savefile = "savefile";
+ FILE * file = fopen(savefile_tmp, "w");
+ exit_err(0 == file, world, err_open);
+ if ( write_uint32_bigendian(world->seed, file)
+ || write_uint32_bigendian(world->turn, file)
+ || write_uint16_bigendian(world->player->pos.y + 1, file)
+ || write_uint16_bigendian(world->player->pos.x + 1, file)
+ || write_uint8(world->player->hitpoints, file)
+ || write_map_objects(world, world->monster, file)
+ || write_map_objects(world, world->item, file))
+ {
+ exit_err(1, world, err_write);
+ }
+ exit_err(fclose(file), world, err_close);
+ if (!access(savefile, F_OK))
+ {
+ exit_err(unlink(savefile), world, err_unl);
+ }
+ exit_err(rename(savefile_tmp, savefile), world, err_move);
}
-extern unsigned char meta_keys(int key, struct World * world,
- struct WinMeta * win_meta,
- struct Win * win_keys,
- struct Win * win_map,
- struct Win * win_info,
- struct Win * win_log)
+extern uint8_t meta_keys(int key, struct World * world,
+ struct WinMeta * win_meta, struct Win * win_keys,
+ struct Win * win_map, struct Win * win_info,
+ struct Win * win_log)
{
if (key == get_action_key(world->keybindings, "quit"))
{