savestate.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file savestate.cpp
  3. /// \brief Help functions for serialization
  4. ///
  5. /// $Date: $
  6. ///
  7. ///////////////////////////////////////////////////////////////////////////////////////
  8. // ecev3 - copied from RCA branch. We assume that the state is always saved on Dec 31.
  9. #include "config.h"
  10. #include "savestate.h"
  11. #ifdef _MSC_VER
  12. #include <direct.h>
  13. #else
  14. #include <unistd.h>
  15. #endif
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <sstream>
  21. namespace {
  22. // Creates a directory (the parent directory must exist)
  23. void make_directory(const std::string& path) {
  24. // Check if the directory already exists, if so just exit
  25. #ifdef _MSC_VER
  26. struct _stat buf;
  27. if (!_stat(path.c_str(), &buf) &&
  28. buf.st_mode & _S_IFDIR) {
  29. #else
  30. struct stat buf;
  31. if (!stat(path.c_str(), &buf) &&
  32. S_ISDIR(buf.st_mode)) {
  33. #endif
  34. return;
  35. }
  36. #ifdef _MSC_VER
  37. if (_mkdir(path.c_str())) {
  38. #else
  39. if (mkdir(path.c_str(), 0777)) {
  40. #endif
  41. if (errno != EEXIST) {
  42. fail("Failed to create directory %s\n%s\n",
  43. path.c_str(), strerror(errno));
  44. }
  45. }
  46. }
  47. // Converts an integer to a string
  48. std::string to_string(int d) {
  49. std::ostringstream os;
  50. os << d;
  51. return os.str();
  52. }
  53. }
  54. GuessSerializer* create_serializer(xtring state_dir,
  55. xtring state_name,
  56. int calendar_year,
  57. // int month,
  58. // int dayofmonth,
  59. int instance,
  60. int num_processes) {
  61. // first create the directory and its parents
  62. std::string full_path((char*)state_dir);
  63. full_path += std::string("/") + (char*)state_name + "_state_" + to_string(calendar_year);
  64. make_directory(full_path);
  65. // ecev3 assumption
  66. /*
  67. full_path += "/" + to_string(month+1);
  68. make_directory(full_path);
  69. full_path += "/" + to_string(dayofmonth+1);
  70. make_directory(full_path);
  71. */
  72. // now that we have a directory, we can create the serializer
  73. return new GuessSerializer(full_path.c_str(), instance, num_processes);
  74. }
  75. GuessDeserializer* create_deserializer(xtring state_dir,
  76. xtring state_name,
  77. int calendar_year /* ,
  78. int month,
  79. int dayofmonth*/) {
  80. // verify that the directory exists, otherwise we'll return a null pointer
  81. std::string full_path((char*)state_dir);
  82. full_path += std::string("/") + (char*)state_name + "_state_" + to_string(calendar_year);
  83. // ecev3 assumption
  84. /*
  85. full_path += "/" + to_string(month+1);
  86. full_path += "/" + to_string(dayofmonth+1);
  87. */
  88. #ifdef _MSC_VER
  89. struct _stat buf;
  90. if (!_stat(full_path.c_str(), &buf) &&
  91. buf.st_mode & _S_IFDIR) {
  92. #else
  93. struct stat buf;
  94. if (!stat(full_path.c_str(), &buf) &&
  95. S_ISDIR(buf.st_mode)) {
  96. #endif
  97. return new GuessDeserializer(full_path.c_str());
  98. }
  99. else {
  100. return 0;
  101. }
  102. }