From 56d6454ba617d1dcddbbaa21e2066a7db0caee75 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 14 Feb 2017 22:41:07 +0300 Subject: Move some virtfs replated functions from files.cpp into virtfstools.cpp --- src/utils/virtfstools.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) (limited to 'src/utils/virtfstools.cpp') 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 +#include + #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( + 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( + 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 -- cgit v1.2.3-60-g2f50