X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fmisc.c;h=697c182f7ae2b856c96e15a1727ddac84c8c46b0;hb=55178f6102ba0cba96e52b03edb226e8f7ad686c;hp=ec05accaa513ac3ba8389f6020f1c5bb96e61924;hpb=b9082c113c43afe5c6a11c2b72f845ee2f8c6aea;p=plomrogue diff --git a/src/misc.c b/src/misc.c index ec05acc..697c182 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,8 +1,11 @@ /* misc.c */ #include "misc.h" +#include /* for rename() */ +#include /* for unlink(), acess() */ #include /* for calloc(), free() */ #include /* for strlen(), strcmp(), memcpy() */ +#include /* 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 @@ -132,17 +135,31 @@ extern void turn_over(struct World * world, char action) extern void save_game(struct World * world) { - uint8_t fail; - FILE * file = fopen("savefile", "w"); - fail = write_uint32_bigendian(world->seed, file); - fail = fail | write_uint32_bigendian(world->turn, file); - fail = fail | write_uint16_bigendian(world->player->pos.y + 1, file); - fail = fail | write_uint16_bigendian(world->player->pos.x + 1, file); - fail = fail | write_uint8(world->player->hitpoints, file); - fail = fail | write_map_objects(world, world->monster, file); - fail = fail | write_map_objects(world, world->item, file); - exit_err(fail, world, "Error saving game."); - fclose(file); + char * savefile_tmp = "savefile_tmp"; + char * savefile = "savefile"; + FILE * file = fopen(savefile_tmp, "w"); + exit_err(0 == file, world, "Error saving game: " + "Unable to open new savefile for writing."); + 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, "Error saving game: " + "Trouble writing to opened new savefile."); + } + exit_err(fclose(file), world, "Error saving game: " + "Unable to close opened new savefile."); + if (!access(savefile, F_OK)) + { + exit_err(unlink(savefile), world, "Error saving game: " + "Unable to unlink old savefile."); + } + exit_err(rename(savefile_tmp, "savefile"), world, "Error saving game: " + "Unable to rename 'savefile_tmp' to 'savefile'."); } @@ -215,12 +232,10 @@ extern struct yx_uint16 find_passable_pos(struct Map * map) -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")) {