From bb87f2911b63eaf80e49d689bf52ecf2042ae098 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Wed, 28 May 2014 20:31:09 +0300
Subject: Move from resourcemanager functions related to files into other
 files.

---
 src/utils/files.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

(limited to 'src/utils/files.cpp')

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);
+}
-- 
cgit v1.2.3-70-g09d2