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 
5 #include <fstream>
6 
7 #include <SDL.h>
8 
9 namespace fs
10 {
11 
12 std::string readFileAsString(const std::string &fileName, bool binaryMode)
13 {
14  // on Android, files cannot be accessed directly without Java. SDL implements that for us, so we use SDL RWOps until we implemented JNI File Access ourselves
15  // binary mode is ignored for now, savegames can't be loaded
16 
17  SDL_RWops *rw = SDL_RWFromFile(fileName.c_str(), "rb");
18  Sint64 res_size = SDL_RWsize(rw);
19  char *res = (char *)malloc(res_size + 1);
20 
21  if (rw == NULL)
22  {
23  LOG(LOG_ERROR) << stderr << "Couldn't open " << fileName.c_str();
24  throw CytopiaError(TRACE_INFO "Couldn't read text file " + fileName + ": " + SDL_GetError());
25  }
26  Sint64 nb_read_total = 0, nb_read = 1;
27  char *buf = res;
28 
29  while (nb_read_total < res_size && nb_read != 0)
30  {
31  nb_read = SDL_RWread(rw, buf, 1, (res_size - nb_read_total));
32  nb_read_total += nb_read;
33  buf += nb_read;
34  }
35 
36  res[nb_read_total] = '\0';
37 
38  std::string i = res;
39  SDL_RWclose(rw);
40  if (nb_read_total != res_size)
41  {
42  free(res);
43  LOG(LOG_ERROR) << "ERROR loading file " << fileName << SDL_GetError();
44  throw CytopiaError{TRACE_INFO "Couldn't read text file " + fileName + ": " + SDL_GetError()};
45  }
46 
47  return std::string(res);
48 }
49 
50 void writeStringToFile(const std::string &fileName, const std::string &stringToWrite, bool binaryMode)
51 {
52  LOG(LOG_ERROR) << "fs::writeStringToFile() not implemented!";
53 }
54 
55 std::vector<std::string> getDirectoryListing(const std::string &directory)
56 {
57  LOG(LOG_ERROR) << "fs::getDirectoryListing() not implemented!";
58 }
59 
60 std::vector<std::string> getSaveGamePaths() { LOG(LOG_ERROR) << "fs::getSaveGamePaths() not implemented!"; }
61 
62 bool fileExists(const std::string &filePath) { return true; }
63 
64 void writeStringToFileCompressed(const std::string &fileName, const std::string &stringToWrite)
65 {
66  LOG(LOG_ERROR) << "fs::writeStringToFileCompressed() not implemented!";
67 }
68 
69 void createDirectory(const std::string &dir) { LOG(LOG_ERROR) << "fs::createDirectory() not implemented!"; }
70 
72 {
73  LOG(LOG_ERROR) << "fs::createDreadCompressedFileAsStringirectory() not implemented!";
74  return "";
75 }
76 
77 std::string getBasePath() { return ""; }
78 
79 } // namespace fs
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
LOG
Definition: LOG.hxx:32
fileExists
bool fileExists(const std::string &filePath)
Check if a file (or folder) exists.
Definition: Filesystem.cxx:62
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
LOG_ERROR
@ LOG_ERROR
Definition: LOG.hxx:28
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
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
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