summaryrefslogtreecommitdiff
path: root/src/utils/virtfstools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/virtfstools.cpp')
-rw-r--r--src/utils/virtfstools.cpp120
1 files changed, 120 insertions, 0 deletions
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