#include <string.h> /* strlen(), memcpy(), memset() */
#include <sys/types.h> /* time_t */
#include <time.h> /* time(), nanosleep() */
-#include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
+#include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
* try_fwrite(), try_fputc(), try_fgetc()
*/
#include "../common/rexit.h" /* exit_trouble() */
try_fwrite(key, strlen(key), 1, file, f_name);
try_fputc(' ', file, f_name);
char * line = try_malloc(11, f_name);
- exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()");
+ exit_trouble(-1 == sprintf(line, "%u", value), f_name, s[S_FCN_SPRINTF]);
try_fwrite(line, strlen(line), 1, file, f_name);
free(line);
try_fputc('\n', file, f_name);
static void update_worldstate_file()
{
char * f_name = "update_worldstate_file()";
- uint16_t size = strlen(s[S_PATH_WORLDSTATE])+strlen(s[S_PATH_SUFFIX_TMP])+1;
- char * path_tmp = try_malloc(size, f_name);
- int test=sprintf(path_tmp,"%s%s",s[S_PATH_WORLDSTATE],s[S_PATH_SUFFIX_TMP]);
- exit_trouble(test < 0, f_name, "sprintf()");
- FILE * file = try_fopen(path_tmp, "w", f_name);
+ char * path_tmp;
+ FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp);
struct Thing * player = get_player();
write_value_as_line(world.turn, file);
write_value_as_line(player->lifepoints, file);
{
try_fwrite(world.log, strlen(world.log), 1, file, f_name);
}
- try_fclose_unlink_rename(file, path_tmp, s[S_PATH_WORLDSTATE], f_name);
- free(path_tmp);
+ atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp);
set_cleanup_flag(CLEANUP_WORLDSTATE);
char * dot = ".\n";;
try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
{
char * f_name = "write_value_as_line()";
char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */
- exit_trouble(sprintf(write_buf, "%u\n", value) < 0, f_name, "sprintf()");
+ exit_trouble(sprintf(write_buf, "%u\n",value) < 0, f_name,s[S_FCN_SPRINTF]);
try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
}
{
return get_message_from_queue();
}
- if (world.turn != world.last_update_turn)
+ if (world.do_update)
{
update_worldstate_file();
- world.last_update_turn = world.turn;
+ world.do_update = 0;
}
read_file_into_queue();
if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
extern void save_world()
{
char * f_name = "save_world()";
- char * path = s[S_PATH_SAVE];
- FILE * file = try_fopen(path, "w", f_name);
+ char * path_tmp;
+ FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp);
write_key_value(file, s[S_CMD_DO_FOV], 0);
try_fputc('\n', file, f_name);
write_key_value(file, s[S_CMD_SEED_MAP], world.seed_map);
write_thing(file, t);
}
write_key_value(file, s[S_CMD_DO_FOV], 1);
- try_fclose(file, f_name);
+ atomic_write_finish(file, s[S_PATH_SAVE], path_tmp);
}