summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/files.cpp113
-rw-r--r--src/utils/files.h19
-rw-r--r--src/utils/files_unittest.cc19
-rw-r--r--src/utils/virtfstools.cpp120
-rw-r--r--src/utils/virtfstools.h15
5 files changed, 145 insertions, 141 deletions
diff --git a/src/utils/files.cpp b/src/utils/files.cpp
index b2cba4c52..87c1b7b0b 100644
--- a/src/utils/files.cpp
+++ b/src/utils/files.cpp
@@ -209,39 +209,6 @@ int Files::copyFile(const std::string &restrict srcName,
return 0;
}
-void Files::getFiles(const std::string &path, StringVect &list)
-{
- VirtList *const fonts = VirtFs::enumerateFiles(path);
- FOR_EACH (StringVectCIter, i, fonts->names)
- {
- if (!VirtFs::isDirectory(path + *i))
- list.push_back(*i);
- }
- VirtFs::freeList(fonts);
-}
-
-void Files::getDirs(const std::string &path, StringVect &list)
-{
- VirtList *const fonts = VirtFs::enumerateFiles(path);
- FOR_EACH (StringVectCIter, i, fonts->names)
- {
- if (VirtFs::isDirectory(path + *i))
- list.push_back(*i);
- }
- VirtFs::freeList(fonts);
-}
-
-void Files::getFilesWithDir(const std::string &path, StringVect &list)
-{
- VirtList *const fonts = VirtFs::enumerateFiles(path);
- FOR_EACH (StringVectCIter, i, fonts->names)
- {
- if (!VirtFs::isDirectory(path + *i))
- list.push_back(path + *i);
- }
- VirtFs::freeList(fonts);
-}
-
bool Files::existsLocal(const std::string &path)
{
bool flg(false);
@@ -253,70 +220,6 @@ bool Files::existsLocal(const std::string &path)
return flg;
}
-std::string Files::getPath(const std::string &file)
-{
- // get the real path to the file
- const char *const tmp = VirtFs::getRealDir(file);
- 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);
-#if defined __native_client__
- std::string dataZip = "/http/data.zip/";
- if (path.substr(0, dataZip.length()) == dataZip)
- path = path.replace(0, dataZip.length(), "/http/data/");
-#endif // defined __native_client__
- }
- else
- {
- // if not found in search path return the default path
- path = getPackageDir().append(dirSeparator).append(file);
- }
-
- return path;
-}
-
-std::string Files::loadTextFileString(const std::string &fileName)
-{
- int contentsLength;
- char *fileContents = static_cast<char*>(
- VirtFs::loadFile(fileName, contentsLength));
-
- if (!fileContents)
- {
- logger->log("Couldn't load text file: %s", fileName.c_str());
- return std::string();
- }
- const std::string str = std::string(fileContents, contentsLength);
- free(fileContents);
- return str;
-}
-
-bool Files::loadTextFile(const std::string &fileName,
- StringVect &lines)
-{
- int contentsLength;
- char *fileContents = static_cast<char*>(
- VirtFs::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)
{
@@ -368,19 +271,3 @@ void Files::deleteFilesInDirectory(std::string path)
closedir(dir);
}
}
-
-void Files::getFilesInDir(const std::string &dir,
- StringVect &list,
- const std::string &ext)
-{
- const std::string path = dir + "/";
- StringVect tempList;
- Files::getFilesWithDir(path, tempList);
- FOR_EACH (StringVectCIter, it, tempList)
- {
- const std::string &str = *it;
- if (findLast(str, ext))
- list.push_back(str);
- }
- std::sort(list.begin(), list.end());
-}
diff --git a/src/utils/files.h b/src/utils/files.h
index 0dd2cd739..b91f7e9ab 100644
--- a/src/utils/files.h
+++ b/src/utils/files.h
@@ -53,22 +53,8 @@ namespace Files
int copyFile(const std::string &restrict pFrom,
const std::string &restrict pTo);
- void getFiles(const std::string &path, StringVect &list);
-
- void getDirs(const std::string &path, StringVect &list);
-
- 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);
-
- std::string loadTextFileString(const std::string &fileName);
-
bool loadTextFileLocal(const std::string &fileName,
StringVect &lines);
@@ -77,11 +63,6 @@ namespace Files
const std::string &restrict text);
void deleteFilesInDirectory(std::string path);
-
- void getFilesInDir(const std::string &dir,
- StringVect &list,
- const std::string &ext);
-
} // namespace Files
#endif // UTILS_FILES_H
diff --git a/src/utils/files_unittest.cc b/src/utils/files_unittest.cc
index 71e6e89c7..ae2e69a99 100644
--- a/src/utils/files_unittest.cc
+++ b/src/utils/files_unittest.cc
@@ -24,6 +24,7 @@
#include "logger.h"
#include "utils/virtfs.h"
+#include "utils/virtfstools.h"
#include "resources/resourcemanager/resourcemanager.h"
@@ -73,9 +74,9 @@ TEST_CASE("Files existsLocal")
ResourceManager::init();
VirtFs::addDirToSearchPath("data", Append_false);
VirtFs::addDirToSearchPath("../data", Append_false);
- REQUIRE(Files::existsLocal(Files::getPath("help/about.txt")) == true);
- REQUIRE_FALSE(Files::existsLocal(Files::getPath("help/about1.txt")));
- REQUIRE_FALSE(Files::existsLocal(Files::getPath("help1/about.txt")));
+ REQUIRE(Files::existsLocal(VirtFs::getPath("help/about.txt")) == true);
+ REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help/about1.txt")));
+ REQUIRE_FALSE(Files::existsLocal(VirtFs::getPath("help1/about.txt")));
ResourceManager::deleteInstance();
// VirtFs::deinit();
}
@@ -87,7 +88,7 @@ TEST_CASE("Files loadTextFileString")
ResourceManager::init();
VirtFs::addDirToSearchPath("data", Append_false);
VirtFs::addDirToSearchPath("../data", Append_false);
- REQUIRE(Files::loadTextFileString("test/simplefile.txt") ==
+ REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") ==
"this is test \nfile.");
ResourceManager::deleteInstance();
// VirtFs::deinit();
@@ -102,7 +103,7 @@ TEST_CASE("Files loadTextFile")
VirtFs::addDirToSearchPath("../data", Append_false);
StringVect lines;
- Files::loadTextFile("test/simplefile.txt", lines);
+ VirtFs::loadTextFile("test/simplefile.txt", lines);
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] == "this is test ");
REQUIRE(lines[1] == "file.");
@@ -118,10 +119,10 @@ TEST_CASE("Files saveTextFile")
VirtFs::addDirToSearchPath("data", Append_false);
VirtFs::addDirToSearchPath("../data", Append_false);
- const std::string dir = Files::getPath("test");
+ const std::string dir = VirtFs::getPath("test");
REQUIRE(dir.size() > 0);
Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2");
- std::string data = Files::loadTextFileString("test/tempfile.txt");
+ std::string data = VirtFs::loadTextFileString("test/tempfile.txt");
::remove((dir + "/tempfile.txt").c_str());
REQUIRE(data == "test line\ntext line2\n");
ResourceManager::deleteInstance();
@@ -137,14 +138,14 @@ TEST_CASE("Files getFilesInDir")
VirtFs::addDirToSearchPath("../data", Append_false);
StringVect list;
- Files::getFilesInDir("test",
+ VirtFs::getFilesInDir("test",
list,
".gpl");
REQUIRE(list.size() == 1);
REQUIRE(list[0] == "test/palette.gpl");
list.clear();
- Files::getFilesInDir("perserver/default",
+ VirtFs::getFilesInDir("perserver/default",
list,
".xml");
REQUIRE(list.size() == 5);
diff --git a/src/utils/virtfstools.cpp b/src/utils/virtfstools.cpp
index 0b87670bf..e4b4d02cf 100644
--- a/src/utils/virtfstools.cpp
+++ b/src/utils/virtfstools.cpp
@@ -22,9 +22,14 @@
#include "logger.h"
+#include "utils/paths.h"
+#include "utils/stringutils.h"
#include "utils/virtfs.h"
#include "utils/virtlist.h"
+#include <algorithm>
+#include <sstream>
+
#include "debug.h"
namespace VirtFs
@@ -101,4 +106,119 @@ namespace VirtFs
}
VirtFs::freeList(list);
}
+
+ void getFilesWithDir(const std::string &path,
+ StringVect &list)
+ {
+ VirtList *const fonts = VirtFs::enumerateFiles(path);
+ FOR_EACH (StringVectCIter, i, fonts->names)
+ {
+ if (!VirtFs::isDirectory(path + *i))
+ list.push_back(path + *i);
+ }
+ VirtFs::freeList(fonts);
+ }
+
+ void getFilesInDir(const std::string &dir,
+ StringVect &list,
+ const std::string &ext)
+ {
+ const std::string path = dir + "/";
+ StringVect tempList;
+ VirtFs::getFilesWithDir(path, tempList);
+ FOR_EACH (StringVectCIter, it, tempList)
+ {
+ const std::string &str = *it;
+ if (findLast(str, ext))
+ list.push_back(str);
+ }
+ std::sort(list.begin(), list.end());
+ }
+
+ void getFiles(const std::string &path,
+ StringVect &list)
+ {
+ VirtList *const fonts = VirtFs::enumerateFiles(path);
+ FOR_EACH (StringVectCIter, i, fonts->names)
+ {
+ if (!VirtFs::isDirectory(path + *i))
+ list.push_back(*i);
+ }
+ VirtFs::freeList(fonts);
+ }
+
+ void getDirs(const std::string &path, StringVect &list)
+ {
+ VirtList *const fonts = VirtFs::enumerateFiles(path);
+ FOR_EACH (StringVectCIter, i, fonts->names)
+ {
+ if (VirtFs::isDirectory(path + *i))
+ list.push_back(*i);
+ }
+ VirtFs::freeList(fonts);
+ }
+
+ std::string getPath(const std::string &file)
+ {
+ // get the real path to the file
+ const char *const tmp = VirtFs::getRealDir(file);
+ 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);
+#if defined __native_client__
+ std::string dataZip = "/http/data.zip/";
+ if (path.substr(0, dataZip.length()) == dataZip)
+ path = path.replace(0, dataZip.length(), "/http/data/");
+#endif // defined __native_client__
+ }
+ else
+ {
+ // if not found in search path return the default path
+ path = getPackageDir().append(dirSeparator).append(file);
+ }
+
+ return path;
+ }
+
+ std::string loadTextFileString(const std::string &fileName)
+ {
+ int contentsLength;
+ char *fileContents = static_cast<char*>(
+ VirtFs::loadFile(fileName, contentsLength));
+
+ if (!fileContents)
+ {
+ logger->log("Couldn't load text file: %s", fileName.c_str());
+ return std::string();
+ }
+ const std::string str = std::string(fileContents, contentsLength);
+ free(fileContents);
+ return str;
+ }
+
+ bool loadTextFile(const std::string &fileName,
+ StringVect &lines)
+ {
+ int contentsLength;
+ char *fileContents = static_cast<char*>(
+ VirtFs::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;
+ }
} // namespace VirtFs
diff --git a/src/utils/virtfstools.h b/src/utils/virtfstools.h
index fccacbec1..14cf302fe 100644
--- a/src/utils/virtfstools.h
+++ b/src/utils/virtfstools.h
@@ -23,6 +23,8 @@
#include "enums/simpletypes/append.h"
+#include "utils/stringvector.h"
+
#include "localconsts.h"
#include <string>
@@ -38,6 +40,19 @@ namespace VirtFs
const Append append);
void searchAndRemoveArchives(const std::string &restrict path,
const std::string &restrict ext);
+ void getFilesInDir(const std::string &dir,
+ StringVect &list,
+ const std::string &ext);
+ void getFilesWithDir(const std::string &restrict path,
+ StringVect &restrict list);
+ void getFiles(const std::string &path,
+ StringVect &list);
+ void getDirs(const std::string &path,
+ StringVect &list);
+ std::string getPath(const std::string &file);
+ bool loadTextFile(const std::string &fileName,
+ StringVect &lines);
+ std::string loadTextFileString(const std::string &fileName);
} // namespace VirtFs
#endif // UTILS_VIRTFSTOOLS_H