Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Filesystem.cxx File Reference
#include "Filesystem.hxx"
#include "Settings.hxx"
#include "LOG.hxx"
#include <fstream>
#include <SDL.h>
+ Include dependency graph for Filesystem.cxx:

Go to the source code of this file.

Namespaces

 fs
 

Functions

std::string readFileAsString (const std::string &fileName, bool binaryMode)
 Read contents from a file as string. More...
 
void writeStringToFile (const std::string &fileName, const std::string &stringToWrite, bool binaryMode)
 Write a string to a file. More...
 
std::vector< std::stringgetDirectoryListing (const std::string &directory)
 Get a directory listing of a given directory. More...
 
std::vector< std::stringgetSaveGamePaths ()
 Get all savegames in the savegame directory. More...
 
bool fileExists (const std::string &filePath)
 Check if a file (or folder) exists. More...
 
void writeStringToFileCompressed (const std::string &fileName, const std::string &stringToWrite)
 Write a string to a file and compress it. More...
 
void createDirectory (const std::string &dir)
 
std::string getBasePath ()
 Get base path (where Cytopia is being run) More...
 

Function Documentation

◆ createDirectory()

void fs::createDirectory ( const std::string dir)

Definition at line 69 of file Filesystem.cxx.

69 { LOG(LOG_ERROR) << "fs::createDirectory() not implemented!"; }
+ Here is the caller graph for this function:

◆ fileExists()

bool fs::fileExists ( const std::string filePath)

Check if a file (or folder) exists.

Parameters
filePathPath to file or folder to check
Returns
true if the path exists

Definition at line 62 of file Filesystem.cxx.

62 { return true; }
+ Here is the caller graph for this function:

◆ getBasePath()

std::string fs::getBasePath ( )

Get base path (where Cytopia is being run)

A wrapper for SDL_GetBasePath, which is not run on Android because it hard-crashes the app. Memory is freed after the call.

Returns
the Base path

Definition at line 77 of file Filesystem.cxx.

77 { return ""; }
+ Here is the caller graph for this function:

◆ getDirectoryListing()

std::vector< std::string > fs::getDirectoryListing ( const std::string directory)

Get a directory listing of a given directory.

Parameters
directoryName of the folder to retrieve the directory listing from
Returns
a directory::iterator containing the folder content

Definition at line 55 of file Filesystem.cxx.

56 {
57  LOG(LOG_ERROR) << "fs::getDirectoryListing() not implemented!";
58 }

◆ getSaveGamePaths()

std::vector< std::string > fs::getSaveGamePaths ( )

Get all savegames in the savegame directory.

Returns
a std::vector containing paths with all savegames

Definition at line 60 of file Filesystem.cxx.

60 { LOG(LOG_ERROR) << "fs::getSaveGamePaths() not implemented!"; }

◆ readFileAsString()

std::string fs::readFileAsString ( const std::string fileName,
bool  binaryMode = false 
)

Read contents from a file as string.

Parameters
fileNameName of the file to read
binaryMode(optional) open the file in binary mode. (default: false)
Returns
the content of the file as string

Definition at line 12 of file Filesystem.cxx.

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 }
+ Here is the caller graph for this function:

◆ writeStringToFile()

void fs::writeStringToFile ( const std::string fileName,
const std::string stringToWrite,
bool  binaryMode = false 
)

Write a string to a file.

Parameters
fileNameName of the file to write
stringToWritestring to be written
binaryMode(optional) open the file in binary mode. (default: false)

Definition at line 50 of file Filesystem.cxx.

51 {
52  LOG(LOG_ERROR) << "fs::writeStringToFile() not implemented!";
53 }
+ Here is the caller graph for this function:

◆ writeStringToFileCompressed()

void fs::writeStringToFileCompressed ( const std::string fileName,
const std::string stringToWrite 
)

Write a string to a file and compress it.

Parameters
fileNameName of the file to write
stringToWritestring to be written

Definition at line 64 of file Filesystem.cxx.

65 {
66  LOG(LOG_ERROR) << "fs::writeStringToFileCompressed() not implemented!";
67 }
+ Here is the caller graph for this function:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
LOG
Definition: LOG.hxx:32
LOG_ERROR
@ LOG_ERROR
Definition: LOG.hxx:28
string
std::string string
Definition: AudioConfig.hxx:14
CytopiaError
A generic error in Cytopia.
Definition: Exception.hxx:28