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/gui/fonts/font.cpp | 4 +- src/gui/theme.cpp | 5 +- src/gui/windowmanager.cpp | 3 +- src/gui/windows/chatwindow.cpp | 4 +- src/gui/windows/helpwindow.cpp | 6 +- src/render/shaders/shadersmanager.cpp | 6 +- src/resources/beingcommon.h | 7 +- src/resources/db/badgesdb.cpp | 8 ++- src/resources/db/colordb.cpp | 2 +- src/resources/db/itemdb.cpp | 4 +- src/resources/db/palettedb.cpp | 4 +- src/utils/files.cpp | 113 -------------------------------- src/utils/files.h | 19 ------ src/utils/files_unittest.cc | 19 +++--- src/utils/virtfstools.cpp | 120 ++++++++++++++++++++++++++++++++++ src/utils/virtfstools.h | 15 +++++ 16 files changed, 174 insertions(+), 165 deletions(-) (limited to 'src') diff --git a/src/gui/fonts/font.cpp b/src/gui/fonts/font.cpp index 73b91ad4c..61bb93453 100644 --- a/src/gui/fonts/font.cpp +++ b/src/gui/fonts/font.cpp @@ -75,11 +75,11 @@ #include "resources/image/image.h" #include "utils/delete2.h" -#include "utils/files.h" #include "utils/paths.h" #include "utils/sdlcheckutils.h" #include "utils/stringutils.h" #include "utils/timer.h" +#include "utils/virtfstools.h" #include "debug.h" @@ -162,7 +162,7 @@ TTF_Font *Font::openFont(const char *const name, const int size) // return nullptr; // return TTF_OpenFontIndexRW(rw, 1, size, 0); // #else - return TTF_OpenFontIndex(Files::getPath(name).c_str(), + return TTF_OpenFontIndex(VirtFs::getPath(name).c_str(), size, 0); // #endif } diff --git a/src/gui/theme.cpp b/src/gui/theme.cpp index 0c3d80a50..f77146751 100644 --- a/src/gui/theme.cpp +++ b/src/gui/theme.cpp @@ -47,6 +47,7 @@ #include "utils/dtor.h" #include "utils/files.h" #include "utils/virtfs.h" +#include "utils/virtfstools.h" #include "utils/virtlist.h" #include "debug.h" @@ -566,13 +567,13 @@ bool Theme::tryThemePath(const std::string &themeName) void Theme::fillSkinsList(StringVect &list) { - Files::getDirs(branding.getStringValue("guiThemePath"), list); + VirtFs::getDirs(branding.getStringValue("guiThemePath"), list); } void Theme::fillFontsList(StringVect &list) { VirtFs::permitLinks(true); - Files::getFiles(branding.getStringValue("fontsPath"), list); + VirtFs::getFiles(branding.getStringValue("fontsPath"), list); VirtFs::permitLinks(false); } diff --git a/src/gui/windowmanager.cpp b/src/gui/windowmanager.cpp index 4b735f058..aa61bcb09 100644 --- a/src/gui/windowmanager.cpp +++ b/src/gui/windowmanager.cpp @@ -60,6 +60,7 @@ #include "utils/files.h" #include "utils/sdlcheckutils.h" #include "utils/sdlhelper.h" +#include "utils/virtfstools.h" #ifdef __native_client__ #include "utils/naclmessages.h" #endif // __native_client__ @@ -363,7 +364,7 @@ void WindowManager::setIcon() iconFile.append(".png"); #endif // WIN32 - iconFile = Files::getPath(iconFile); + iconFile = VirtFs::getPath(iconFile); logger->log("Loading icon from file: %s", iconFile.c_str()); #ifdef WIN32 diff --git a/src/gui/windows/chatwindow.cpp b/src/gui/windows/chatwindow.cpp index e94d39dbe..094faea6d 100644 --- a/src/gui/windows/chatwindow.cpp +++ b/src/gui/windows/chatwindow.cpp @@ -71,7 +71,7 @@ #include "utils/copynpaste.h" #include "utils/delete2.h" -#include "utils/files.h" +#include "utils/virtfstools.h" #include @@ -227,7 +227,7 @@ void ChatWindow::postInit() void ChatWindow::loadCommandsFile(const std::string &name) { StringVect list; - Files::loadTextFile(name, list); + VirtFs::loadTextFile(name, list); StringVectCIter it = list.begin(); const StringVectCIter it_end = list.end(); diff --git a/src/gui/windows/helpwindow.cpp b/src/gui/windows/helpwindow.cpp index 62c341ac3..e1ba2de30 100644 --- a/src/gui/windows/helpwindow.cpp +++ b/src/gui/windows/helpwindow.cpp @@ -38,9 +38,9 @@ #include "gui/widgets/scrollarea.h" #include "utils/gettext.h" -#include "utils/files.h" #include "utils/paths.h" #include "utils/process.h" +#include "utils/virtfstools.h" #include "utils/translation/podict.h" #include "utils/translation/translationmanager.h" @@ -161,11 +161,11 @@ void HelpWindow::loadTags() helpPath = paths.getStringValue("tags"); StringVect filesVect; - Files::getFilesInDir(helpPath, filesVect, ".idx"); + VirtFs::getFilesInDir(helpPath, filesVect, ".idx"); FOR_EACH (StringVectCIter, itVect, filesVect) { StringVect lines; - Files::loadTextFile(*itVect, lines); + VirtFs::loadTextFile(*itVect, lines); FOR_EACH (StringVectCIter, it, lines) { const std::string &str = *it; diff --git a/src/render/shaders/shadersmanager.cpp b/src/render/shaders/shadersmanager.cpp index 93de709f5..d4854cfe2 100644 --- a/src/render/shaders/shadersmanager.cpp +++ b/src/render/shaders/shadersmanager.cpp @@ -25,6 +25,8 @@ #include "configuration.h" #include "logger.h" +#include "utils/virtfstools.h" + #include "render/opengl/mgl.h" #ifdef __native_client__ #include "render/opengl/naclglfunctions.h" @@ -35,8 +37,6 @@ #include "resources/loaders/shaderloader.h" -#include "utils/files.h" - #include "debug.h" ShadersManager shaders; @@ -44,7 +44,7 @@ ShadersManager shaders; Shader *ShadersManager::createShader(const unsigned int type, const std::string &fileName) { - const std::string str = Files::loadTextFileString(fileName); + const std::string str = VirtFs::loadTextFileString(fileName); const char *ptrStr = str.c_str(); GLuint shaderId = mglCreateShader(type); mglShaderSource(shaderId, 1, &ptrStr, nullptr); diff --git a/src/resources/beingcommon.h b/src/resources/beingcommon.h index 87f7b8088..2b3d8c230 100644 --- a/src/resources/beingcommon.h +++ b/src/resources/beingcommon.h @@ -21,7 +21,8 @@ #ifndef RESOURCES_BEINGCOMMON_H #define RESOURCES_BEINGCOMMON_H -#include "utils/files.h" +#include "utils/stringvector.h" +#include "utils/virtfstools.h" #include "utils/xml.h" #include "localconsts.h" @@ -33,7 +34,7 @@ struct SpriteDisplay; #define loadXmlDir(name, function) \ { \ StringVect listVect; \ - Files::getFilesInDir(paths.getStringValue( \ + VirtFs::getFilesInDir(paths.getStringValue( \ name), listVect, ".xml"); \ FOR_EACH (StringVectCIter, itVect, listVect) \ function(*itVect, SkipError_true); \ @@ -42,7 +43,7 @@ struct SpriteDisplay; #define loadXmlDir2(name, function, ext, skipError) \ { \ StringVect listVect; \ - Files::getFilesInDir(name, listVect, ext); \ + VirtFs::getFilesInDir(name, listVect, ext); \ FOR_EACH (StringVectCIter, itVect, listVect) \ function(*itVect, skipError); \ } diff --git a/src/resources/db/badgesdb.cpp b/src/resources/db/badgesdb.cpp index 9e7a7fad4..031d78945 100644 --- a/src/resources/db/badgesdb.cpp +++ b/src/resources/db/badgesdb.cpp @@ -22,7 +22,7 @@ #include "configuration.h" -#include "utils/files.h" +#include "utils/virtfstools.h" #include "utils/xmlutils.h" #include "debug.h" @@ -58,8 +58,10 @@ static void loadDB(const std::string &name, BadgesInfos &arr) name, arr, SkipError_true); StringVect listVect; - Files::getFilesInDir(paths.getStringValue( - "badgesPatchDir"), listVect, ".xml"); + VirtFs::getFilesInDir(paths.getStringValue( + "badgesPatchDir"), + listVect, + ".xml"); FOR_EACH (StringVectCIter, itVect, listVect) loadXmlFile(*itVect, name, arr, SkipError_true); } diff --git a/src/resources/db/colordb.cpp b/src/resources/db/colordb.cpp index 0d268c78a..90e1436ac 100644 --- a/src/resources/db/colordb.cpp +++ b/src/resources/db/colordb.cpp @@ -53,7 +53,7 @@ void ColorDB::load() colors, SkipError_true); StringVect list; - Files::getFilesInDir(paths.getStringValue( + VirtFs::getFilesInDir(paths.getStringValue( "hairColorPatchDir"), list, ".xml"); FOR_EACH (StringVectCIter, it2, list) loadHair(*it2, colors, SkipError_true); diff --git a/src/resources/db/itemdb.cpp b/src/resources/db/itemdb.cpp index 482ec005d..9ff9d78c6 100644 --- a/src/resources/db/itemdb.cpp +++ b/src/resources/db/itemdb.cpp @@ -42,8 +42,8 @@ #include "utils/checkutils.h" #include "utils/delete2.h" #include "utils/dtor.h" -#include "utils/files.h" #include "utils/stringmap.h" +#include "utils/virtfstools.h" #include "debug.h" @@ -202,7 +202,7 @@ void ItemDB::load() SkipError_true); StringVect list; - Files::getFilesInDir(paths.getStringValue("itemsPatchDir"), + VirtFs::getFilesInDir(paths.getStringValue("itemsPatchDir"), list, ".xml"); FOR_EACH (StringVectCIter, it, list) diff --git a/src/resources/db/palettedb.cpp b/src/resources/db/palettedb.cpp index 75d8be290..4c9c4cd76 100644 --- a/src/resources/db/palettedb.cpp +++ b/src/resources/db/palettedb.cpp @@ -23,7 +23,7 @@ #include "configuration.h" #include "utils/checkutils.h" -#include "utils/files.h" +#include "utils/virtfstools.h" #include "resources/dye/dyecolor.h" @@ -47,7 +47,7 @@ void PaletteDB::loadPalette() { mLoaded = true; StringVect lines; - Files::loadTextFile(paths.getStringValue("palettesDir") + + VirtFs::loadTextFile(paths.getStringValue("palettesDir") + paths.getStringValue("defaultPaletteFile"), lines); StringVectCIter it = lines.begin(); 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( - 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( - 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 +#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 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 @@ -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 -- cgit v1.2.3-60-g2f50