summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/files.cpp112
-rw-r--r--src/utils/files.h16
-rw-r--r--src/utils/paths.cpp4
-rw-r--r--src/utils/physfstools.cpp26
-rw-r--r--src/utils/physfstools.h2
-rw-r--r--src/utils/translation/poparser.cpp6
-rw-r--r--src/utils/translation/translationmanager.cpp4
-rw-r--r--src/utils/xml.cpp5
8 files changed, 166 insertions, 9 deletions
diff --git a/src/utils/files.cpp b/src/utils/files.cpp
index 5cf97ddba..28921a0e5 100644
--- a/src/utils/files.cpp
+++ b/src/utils/files.cpp
@@ -20,14 +20,22 @@
#include "utils/files.h"
+#include "logger.h"
+
#if defined(ANDROID) || defined(__native_client__)
#include "resources/resourcemanager.h"
#include "utils/mkdir.h"
#endif
+#include "utils/mkdir.h"
+#include "utils/paths.h"
#include "utils/physfstools.h"
+#include <dirent.h>
+#include <fstream>
+#include <sstream>
+
#include "debug.h"
#ifdef ANDROID
@@ -230,3 +238,107 @@ void Files::getFilesWithDir(const std::string &path, StringVect &list)
}
PhysFs::freeList(fonts);
}
+
+bool Files::existsLocal(const std::string &path)
+{
+ bool flg(false);
+ std::fstream file;
+ file.open(path.c_str(), std::ios::in);
+ if (file.is_open())
+ flg = true;
+ file.close();
+ return flg;
+}
+
+std::string Files::getPath(const std::string &file)
+{
+ // get the real path to the file
+ const char *const tmp = PhysFs::getRealDir(file.c_str());
+ std::string path;
+
+ // if the file is not in the search path, then its nullptr
+ if (tmp)
+ {
+ path = std::string(tmp).append(dirSeparator).append(file);
+ }
+ else
+ {
+ // if not found in search path return the default path
+ path = getPackageDir().append(dirSeparator).append(file);
+ }
+
+ return path;
+}
+
+bool Files::loadTextFile(const std::string &fileName,
+ StringVect &lines)
+{
+ int contentsLength;
+ char *fileContents = static_cast<char*>(
+ PhysFs::loadFile(fileName, contentsLength));
+
+ if (!fileContents)
+ {
+ logger->log("Couldn't load text file: %s", fileName.c_str());
+ return false;
+ }
+
+ std::istringstream iss(std::string(fileContents, contentsLength));
+ std::string line;
+
+ while (getline(iss, line))
+ lines.push_back(line);
+
+ free(fileContents);
+ return true;
+}
+
+bool Files::loadTextFileLocal(const std::string &fileName,
+ StringVect &lines)
+{
+ std::ifstream file;
+ char line[501];
+
+ file.open(fileName.c_str(), std::ios::in);
+
+ if (!file.is_open())
+ {
+ logger->log("Couldn't load text file: %s", fileName.c_str());
+ return false;
+ }
+
+ while (file.getline(line, 500))
+ lines.push_back(line);
+
+ return true;
+}
+
+void Files::saveTextFile(std::string path,
+ const std::string &restrict name,
+ const std::string &restrict text)
+{
+ if (!mkdir_r(path.c_str()))
+ {
+ std::ofstream file;
+ file.open((path.append("/").append(name)).c_str(), std::ios::out);
+ if (file.is_open())
+ file << text << std::endl;
+ file.close();
+ }
+}
+
+void Files::deleteFilesInDirectory(std::string path)
+{
+ path += "/";
+ struct dirent *next_file = nullptr;
+ DIR *const dir = opendir(path.c_str());
+
+ while ((next_file = readdir(dir)))
+ {
+ const std::string file = next_file->d_name;
+ if (file != "." && file != "..")
+ remove((path + file).c_str());
+ }
+ if (dir)
+ closedir(dir);
+}
diff --git a/src/utils/files.h b/src/utils/files.h
index 2046b8ac9..1db9993a7 100644
--- a/src/utils/files.h
+++ b/src/utils/files.h
@@ -61,6 +61,22 @@ namespace Files
void getFilesWithDir(const std::string &restrict path,
StringVect &restrict list);
+
+ bool existsLocal(const std::string &path);
+
+ std::string getPath(const std::string &file);
+
+ bool loadTextFile(const std::string &fileName,
+ StringVect &lines);
+
+ bool loadTextFileLocal(const std::string &fileName,
+ StringVect &lines);
+
+ void saveTextFile(std::string path,
+ const std::string &restrict name,
+ const std::string &restrict text);
+
+ void deleteFilesInDirectory(std::string path);
} // namespace Files
#endif // UTILS_FILES_H
diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp
index 96cd0ed29..ae576d1e4 100644
--- a/src/utils/paths.cpp
+++ b/src/utils/paths.cpp
@@ -25,6 +25,8 @@
#endif
#include "utils/paths.h"
+
+#include "utils/files.h"
#include "utils/physfstools.h"
#include "utils/stringutils.h"
@@ -174,7 +176,7 @@ std::string getPicturesDir()
}
StringVect arr;
- ResourceManager::loadTextFileLocal(file, arr);
+ Files::loadTextFileLocal(file, arr);
FOR_EACH (StringVectCIter, it, arr)
{
std::string str = *it;
diff --git a/src/utils/physfstools.cpp b/src/utils/physfstools.cpp
index 58be77f0e..1ab1aaa25 100644
--- a/src/utils/physfstools.cpp
+++ b/src/utils/physfstools.cpp
@@ -20,6 +20,8 @@
#include "utils/physfstools.h"
+#include "logger.h"
+
#include <iostream>
#include <unistd.h>
@@ -134,4 +136,28 @@ namespace PhysFs
{
return PHYSFS_mkdir(dirname);
}
+
+ void *loadFile(const std::string &fileName, int &fileSize)
+ {
+ // Attempt to open the specified file using PhysicsFS
+ PHYSFS_file *const file = PhysFs::openRead(fileName.c_str());
+
+ if (!file)
+ {
+ logger->log("Warning: Failed to load %s: %s",
+ fileName.c_str(), PHYSFS_getLastError());
+ return nullptr;
+ }
+
+ logger->log("Loaded %s/%s", PhysFs::getRealDir(fileName.c_str()),
+ fileName.c_str());
+
+ fileSize = static_cast<int>(PHYSFS_fileLength(file));
+ // Allocate memory and load the file
+ void *const buffer = calloc(fileSize, 1);
+ PHYSFS_read(file, buffer, 1, fileSize);
+ PHYSFS_close(file);
+
+ return buffer;
+ }
} // namespace PhysFs
diff --git a/src/utils/physfstools.h b/src/utils/physfstools.h
index 3667f7a16..f85dabdd6 100644
--- a/src/utils/physfstools.h
+++ b/src/utils/physfstools.h
@@ -22,6 +22,7 @@
#define UTILS_PHYSFSTOOLS_H
#include <physfs.h>
+#include <string>
namespace PhysFs
{
@@ -42,6 +43,7 @@ namespace PhysFs
bool removeFromSearchPath(const char *const oldDir);
const char *getRealDir(const char *const filename);
bool mkdir(const char *const dirName);
+ void *loadFile(const std::string &fileName, int &fileSize);
} // namespace PhysFs
extern const char *dirSeparator;
diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp
index bcb577f84..7e78ade9d 100644
--- a/src/utils/translation/poparser.cpp
+++ b/src/utils/translation/poparser.cpp
@@ -22,6 +22,7 @@
#include "resources/resourcemanager.h"
+#include "utils/physfstools.h"
#include "utils/stringutils.h"
#include "utils/translation/podict.h"
@@ -48,7 +49,7 @@ void PoParser::openFile(const std::string &name)
if (!resman)
return;
int size;
- char *buf = static_cast<char*>(resman->loadFile(getFileName(name), size));
+ char *buf = static_cast<char*>(PhysFs::loadFile(getFileName(name), size));
if (buf)
{
@@ -231,8 +232,7 @@ PoDict *PoParser::getEmptyDict()
bool PoParser::checkLang(const std::string &lang)
{
// check is po file exists
- const ResourceManager *const resman = ResourceManager::getInstance();
- return resman->exists(getFileName(lang));
+ return PhysFs::exists(getFileName(lang).c_str());
}
std::string PoParser::getFileName(const std::string &lang)
diff --git a/src/utils/translation/translationmanager.cpp b/src/utils/translation/translationmanager.cpp
index 9bbdec810..5fddee639 100644
--- a/src/utils/translation/translationmanager.cpp
+++ b/src/utils/translation/translationmanager.cpp
@@ -21,6 +21,7 @@
#include "utils/translation/translationmanager.h"
#include "utils/delete2.h"
+#include "utils/physfstools.h"
#include "utils/stringutils.h"
#include "utils/translation/podict.h"
@@ -85,9 +86,8 @@ bool TranslationManager::translateFile(const std::string &fileName,
return false;
int contentsLength;
- const ResourceManager *const resman = ResourceManager::getInstance();
char *fileContents = static_cast<char*>(
- resman->loadFile(fileName, contentsLength));
+ PhysFs::loadFile(fileName, contentsLength));
if (!fileContents)
{
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index 099457af3..3144a03b6 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -27,6 +27,7 @@
#include "resources/resourcemanager.h"
#include "utils/fuzzer.h"
+#include "utils/physfstools.h"
#include "utils/stringutils.h"
#include "utils/translation/podict.h"
@@ -91,9 +92,7 @@ namespace XML
valid = true;
if (useResman)
{
- const ResourceManager *const resman
- = ResourceManager::getInstance();
- data = static_cast<char*>(resman->loadFile(
+ data = static_cast<char*>(PhysFs::loadFile(
filename.c_str(), size));
}
else