123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- ///////////////////////////////////////////////////////////////////////////////////////
- /// \file savestate.cpp
- /// \brief Help functions for serialization
- ///
- /// $Date: $
- ///
- ///////////////////////////////////////////////////////////////////////////////////////
- // ecev3 - copied from RCA branch. We assume that the state is always saved on Dec 31.
- #include "config.h"
- #include "savestate.h"
- #ifdef _MSC_VER
- #include <direct.h>
- #else
- #include <unistd.h>
- #endif
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <string.h>
- #include <sstream>
- namespace {
- // Creates a directory (the parent directory must exist)
- void make_directory(const std::string& path) {
- // Check if the directory already exists, if so just exit
- #ifdef _MSC_VER
- struct _stat buf;
- if (!_stat(path.c_str(), &buf) &&
- buf.st_mode & _S_IFDIR) {
- #else
- struct stat buf;
- if (!stat(path.c_str(), &buf) &&
- S_ISDIR(buf.st_mode)) {
- #endif
- return;
- }
- #ifdef _MSC_VER
- if (_mkdir(path.c_str())) {
- #else
- if (mkdir(path.c_str(), 0777)) {
- #endif
- if (errno != EEXIST) {
- fail("Failed to create directory %s\n%s\n",
- path.c_str(), strerror(errno));
- }
- }
- }
- // Converts an integer to a string
- std::string to_string(int d) {
- std::ostringstream os;
- os << d;
- return os.str();
- }
- }
- GuessSerializer* create_serializer(xtring state_dir,
- xtring state_name,
- int calendar_year,
- // int month,
- // int dayofmonth,
- int instance,
- int num_processes) {
- // first create the directory and its parents
- std::string full_path((char*)state_dir);
- full_path += std::string("/") + (char*)state_name + "_state_" + to_string(calendar_year);
- make_directory(full_path);
-
- // ecev3 assumption
- /*
- full_path += "/" + to_string(month+1);
- make_directory(full_path);
-
- full_path += "/" + to_string(dayofmonth+1);
- make_directory(full_path);
- */
- // now that we have a directory, we can create the serializer
- return new GuessSerializer(full_path.c_str(), instance, num_processes);
- }
- GuessDeserializer* create_deserializer(xtring state_dir,
- xtring state_name,
- int calendar_year /* ,
- int month,
- int dayofmonth*/) {
- // verify that the directory exists, otherwise we'll return a null pointer
- std::string full_path((char*)state_dir);
- full_path += std::string("/") + (char*)state_name + "_state_" + to_string(calendar_year);
- // ecev3 assumption
- /*
- full_path += "/" + to_string(month+1);
- full_path += "/" + to_string(dayofmonth+1);
- */
- #ifdef _MSC_VER
- struct _stat buf;
- if (!_stat(full_path.c_str(), &buf) &&
- buf.st_mode & _S_IFDIR) {
- #else
- struct stat buf;
- if (!stat(full_path.c_str(), &buf) &&
- S_ISDIR(buf.st_mode)) {
- #endif
- return new GuessDeserializer(full_path.c_str());
- }
- else {
- return 0;
- }
- }
|