Loading [MathJax]/extensions/MathMenu.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Filesystem.cxx
Go to the documentation of this file.
1 #include <Filesystem.hxx>
2 #include <Settings.hxx>
3 #include <LOG.hxx>
4 #include <fstream>
5 #include "compression.hxx"
6 
7 #include <SDL.h>
8 
9 #if __has_include(<filesystem>)
10 #include <filesystem>
11 #elif __has_include(<experimental/filesystem>)
12 #include <experimental/filesystem>
13 #else
14 #pragma message("Warning: Your compiler does not support the filesystem library. Please report this incident to the Cytopia Team")
15 #endif
16 
17 namespace fs
18 {
19 #if __has_include(<filesystem>)
20 using namespace std::filesystem;
21 #elif __has_include(<experimental/filesystem>)
22 using namespace std::experimental::filesystem;
23 #endif
24 } // namespace fs
25 
26 template <typename Callback> void forEachFileType(fs::path &&path, std::string &&extension, Callback callback)
27 {
28  for (const auto &fp : fs::directory_iterator(path))
29  {
30  if (static_cast<fs::path>(fp).extension() == extension)
31  {
32  callback(fp);
33  }
34  }
35 }
36 
38 {
39  return decompressString(fs::readFileAsString(fileName, true));
40 }
41 
42 std::string fs::readFileAsString(const std::string &fileName, bool binaryMode)
43 {
44  std::ios::openmode mode;
45  if (binaryMode)
46  mode = std::ios_base::in | std::ios_base::binary;
47  else
48  {
49  mode = std::ios_base::in;
50  }
51 
52  std::ifstream stream; //(fileName, mode);
53 
54  if (fs::fileExists(fileName))
55  { // first try given path
56  stream.open(fileName, mode);
57  }
58  else if (fs::fileExists(getBasePath() + fileName))
59  { // if this doesn't work, add the basepath
60  stream.open(getBasePath() + fileName, mode);
61  }
62  else
63  {
64  throw ConfigurationError(TRACE_INFO "File " + fileName + " doesn't exist");
65  }
66 
67  if (!stream)
68  {
69  throw ConfigurationError(TRACE_INFO "Can't open file " + fileName);
70  }
71 
72  std::stringstream buffer;
73  buffer << stream.rdbuf();
74  stream.close();
75 
76  return buffer.str();
77 }
78 
79 void fs::writeStringToFileCompressed(const std::string &fileName, const std::string &stringToWrite)
80 {
81  writeStringToFile(fileName, compressString(stringToWrite), true);
82 }
83 
84 void fs::writeStringToFile(const std::string &fileName, const std::string &stringToWrite, bool binaryMode)
85 {
86  std::ios::openmode mode;
87  if (binaryMode)
88  {
89  mode = std::ios_base::out | std::ios_base::binary;
90  }
91  else
92  {
93  mode = std::ios_base::out;
94  }
95 
96  std::ofstream stream(fileName, mode);
97 
98  if (!stream)
99  {
100  throw ConfigurationError(TRACE_INFO "Could not write to file " + fileName);
101  }
102 
103  stream << stringToWrite << std::endl;
104  stream.close();
105 }
106 
107 std::vector<std::string> fs::getDirectoryListing(const std::string &directory)
108 {
109  std::vector<std::string> dirContent;
110  std::string pathToSaveFiles = getBasePath();
111  pathToSaveFiles.append(directory);
112  for (const auto &it : fs::directory_iterator(fs::path(pathToSaveFiles)))
113  {
114  dirContent.emplace_back(static_cast<path>(it).string());
115  }
116  return dirContent;
117 }
118 
119 std::vector<std::string> fs::getSaveGamePaths()
120 {
121  std::vector<std::string> saveGames;
122  forEachFileType("resources", ".cts",
123  [&saveGames](const auto &path) { saveGames.emplace_back(static_cast<fs::path>(path).string()); });
124  return saveGames;
125 }
126 
127 bool fs::fileExists(const std::string &filePath) { return fs::exists(fs::path(filePath)); }
128 
130 {
131  std::string sPath;
132 
133  char *path = SDL_GetBasePath();
134  if (path)
135  {
136  sPath = path;
137  }
138  else
139  {
140  throw CytopiaError(TRACE_INFO "SDL_GetBasePath() failed!");
141  }
142  SDL_free(path);
143 
144  return sPath;
145 }
146 
147 void fs::createDirectory(const std::string &dir)
148 {
149  if (!fs::is_directory(dir))
150  {
151  if (fs::create_directories(dir.c_str()))
152  {
153  LOG(LOG_INFO) << "Created directory" << dir;
154  }
155  }
156 }
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
compression.hxx
forEachFileType
void forEachFileType(fs::path &&path, std::string &&extension, Callback callback)
Definition: Filesystem.cxx:26
LOG
Definition: LOG.hxx:32
fileExists
bool fileExists(const std::string &filePath)
Check if a file (or folder) exists.
Definition: Filesystem.cxx:62
LOG_INFO
@ LOG_INFO
Definition: LOG.hxx:25
writeStringToFile
void writeStringToFile(const std::string &fileName, const std::string &stringToWrite, bool binaryMode)
Write a string to a file.
Definition: Filesystem.cxx:50
LOG.hxx
getSaveGamePaths
std::vector< std::string > getSaveGamePaths()
Get all savegames in the savegame directory.
Definition: Filesystem.cxx:60
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
ifstream
std::ifstream ifstream
Definition: AudioMixer.cxx:14
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
compressString
std::string compressString(const std::string &stringToCompress)
Compress a given string with zlib.
Definition: compression.hxx:17
decompressString
std::string decompressString(const std::string &compressedString)
decompresses given string
Definition: compression.hxx:71
Filesystem.hxx
Settings.hxx
createDirectory
void createDirectory(const std::string &dir)
Definition: Filesystem.cxx:69
getDirectoryListing
std::vector< std::string > getDirectoryListing(const std::string &directory)
Get a directory listing of a given directory.
Definition: Filesystem.cxx:55
string
std::string string
Definition: AudioConfig.hxx:14
CytopiaError
A generic error in Cytopia.
Definition: Exception.hxx:28
readCompressedFileAsString
std::string readCompressedFileAsString(const std::string &fileName)
Read contents from a file as string.
Definition: Filesystem.cxx:71
writeStringToFileCompressed
void writeStringToFileCompressed(const std::string &fileName, const std::string &stringToWrite)
Write a string to a file and compress it.
Definition: Filesystem.cxx:64