diff --git a/ion/src/simulator/shared/state_file.cpp b/ion/src/simulator/shared/state_file.cpp new file mode 100644 index 000000000..d51fde9a7 --- /dev/null +++ b/ion/src/simulator/shared/state_file.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include "journal.h" + +namespace Ion { +namespace Simulator { +namespace StateFile { + +static constexpr const char * sHeader = "NWSF"; +static constexpr int sHeaderLength = 4; +static constexpr int sVersionLength = 8; + +/* File format: * "NWSF" + "XXXXXXXX" (version) + EVENTS... */ + +static inline bool load(FILE * f) { + char buffer[sVersionLength+1]; + + // Header + buffer[sHeaderLength] = 0; + if (fread(buffer, sHeaderLength, 1, f) != 1) { + return false; + } + printf("READ\n"); + if (strcmp(buffer, sHeader) != 0) { + return false; + } + + // Software version + buffer[sVersionLength] = 0; + if (fread(buffer, sVersionLength, 1, f) != 1) { + return false; + } + if (strcmp(buffer, softwareVersion()) != 0) { + return false; + } + + // Events + Ion::Events::Journal * journal = Journal::replayJournal(); + while (fread(buffer, 1, 1, f) == 1) { + Ion::Events::Event e = Ion::Events::Event(buffer[0]); + journal->pushEvent(e); + } + Ion::Events::replayFrom(journal); + + return true; +} + +void load(const char * filename) { + FILE * f = fopen(filename, "rb"); + if (f == nullptr) { + return; + } + load(f); + fclose(f); +} + +static inline bool save(FILE * f) { + if (fwrite(sHeader, sHeaderLength, 1, f) != 1) { + return false; + } + if (fwrite(softwareVersion(), sVersionLength, 1, f) != 1) { + return false; + } + Ion::Events::Journal * journal = Journal::logJournal(); + Ion::Events::Event e; + while (!journal->isEmpty()) { + Ion::Events::Event e = journal->popEvent(); + uint8_t code = static_cast(e); + if (fwrite(&code, 1, 1, f) != 1) { + return false; + } + } + return true; +} + +void save(const char * filename) { + FILE * f = fopen(filename, "w"); + if (f == nullptr) { + return; + } + save(f); + fclose(f); +} + +} +} +} diff --git a/ion/src/simulator/shared/state_file.h b/ion/src/simulator/shared/state_file.h new file mode 100644 index 000000000..2d1686cd0 --- /dev/null +++ b/ion/src/simulator/shared/state_file.h @@ -0,0 +1,15 @@ +#ifndef ION_SIMULATOR_STATE_FILE_H +#define ION_SIMULATOR_STATE_FILE_H + +namespace Ion { +namespace Simulator { +namespace StateFile { + +void load(const char * filename); +void save(const char * filename); + +} +} +} + +#endif