From 9534d791fe73868c17ff3723d3eacee020a4a215 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 21 Feb 2017 17:31:48 +0300 Subject: Move other fs related files into fs directory. --- src/CMakeLists.txt | 32 ++--- src/Makefile.am | 18 +-- src/actions/actions.cpp | 3 +- src/actormanager.cpp | 3 +- src/being/being.cpp | 5 +- src/chatlogger.cpp | 3 +- src/client.cpp | 4 +- src/commandline.cpp | 3 +- src/configmanager.cpp | 6 +- src/configuration.cpp | 3 +- src/dirs.cpp | 8 +- src/dyetool/client.cpp | 4 +- src/fs/files.cpp | 268 +++++++++++++++++++++++++++++++++++++ src/fs/files.h | 68 ++++++++++ src/fs/files_unittest.cc | 167 +++++++++++++++++++++++ src/fs/mkdir.cpp | 164 +++++++++++++++++++++++ src/fs/mkdir.h | 27 ++++ src/fs/paths.cpp | 221 ++++++++++++++++++++++++++++++ src/fs/paths.h | 50 +++++++ src/fs/specialfolder.cpp | 59 ++++++++ src/fs/specialfolder.h | 33 +++++ src/fs/virtfs.cpp | 2 +- src/fs/virtfstools.cpp | 2 +- src/game.cpp | 2 +- src/gui/fonts/font.cpp | 2 +- src/gui/windows/helpwindow.cpp | 2 +- src/gui/windows/logindialog.cpp | 5 +- src/gui/windows/serverdialog.cpp | 3 +- src/gui/windows/updaterwindow.cpp | 6 +- src/maingui.cpp | 4 +- src/net/download.cpp | 6 +- src/net/ea/loginrecv.cpp | 3 +- src/net/eathena/loginrecv.cpp | 3 +- src/resources/map/map.cpp | 2 +- src/test/testmain.cpp | 3 +- src/utils/files.cpp | 273 -------------------------------------- src/utils/files.h | 68 ---------- src/utils/files_unittest.cc | 167 ----------------------- src/utils/mkdir.cpp | 164 ----------------------- src/utils/mkdir.h | 27 ---- src/utils/paths.cpp | 222 ------------------------------- src/utils/paths.h | 50 ------- src/utils/specialfolder.cpp | 59 -------- src/utils/specialfolder.h | 33 ----- 44 files changed, 1128 insertions(+), 1129 deletions(-) create mode 100644 src/fs/files.cpp create mode 100644 src/fs/files.h create mode 100644 src/fs/files_unittest.cc create mode 100644 src/fs/mkdir.cpp create mode 100644 src/fs/mkdir.h create mode 100644 src/fs/paths.cpp create mode 100644 src/fs/paths.h create mode 100644 src/fs/specialfolder.cpp create mode 100644 src/fs/specialfolder.h delete mode 100644 src/utils/files.cpp delete mode 100644 src/utils/files.h delete mode 100644 src/utils/files_unittest.cc delete mode 100644 src/utils/mkdir.cpp delete mode 100644 src/utils/mkdir.h delete mode 100644 src/utils/paths.cpp delete mode 100644 src/utils/paths.h delete mode 100644 src/utils/specialfolder.cpp delete mode 100644 src/utils/specialfolder.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca320b926..53ce3bba1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -865,8 +865,8 @@ SET(SRCS utils/dumplibs.h utils/env.cpp utils/env.h - utils/files.cpp - utils/files.h + fs/files.cpp + fs/files.h utils/fuzzer.cpp utils/fuzzer.h utils/gettext.h @@ -882,8 +882,8 @@ SET(SRCS utils/mathutils.h utils/parameters.cpp utils/parameters.h - utils/paths.cpp - utils/paths.h + fs/paths.cpp + fs/paths.h utils/perfomance.cpp utils/perfomance.h utils/debugmemoryobject.h @@ -917,8 +917,8 @@ SET(SRCS utils/mutex.h utils/naclmessages.cpp utils/naclmessages.h - utils/mkdir.cpp - utils/mkdir.h + fs/mkdir.cpp + fs/mkdir.h utils/mrand.cpp utils/mrand.h utils/xml.h @@ -1763,14 +1763,14 @@ SET(DYE_CMD_SRCS resources/sprite/spritedef.h resources/sprite/spritedisplay.h resources/sprite/spritereference.h - utils/files.cpp - utils/files.h - utils/mkdir.cpp - utils/mkdir.h + fs/files.cpp + fs/files.h + fs/mkdir.cpp + fs/mkdir.h utils/mrand.cpp utils/mrand.h - utils/paths.cpp - utils/paths.h + fs/paths.cpp + fs/paths.h utils/perfomance.cpp utils/perfomance.h fs/virtfsrwops.cpp @@ -1997,14 +1997,14 @@ SET(SRCS_TMWA IF (WIN32) SET(SRCS ${SRCS} - utils/specialfolder.cpp - utils/specialfolder.h + fs/specialfolder.cpp + fs/specialfolder.h manaplus.rc ) SET(DYE_CMD_SRCS ${DYE_CMD_SRCS} - utils/specialfolder.cpp - utils/specialfolder.h + fs/specialfolder.cpp + fs/specialfolder.h manaplus.rc ) ENDIF () diff --git a/src/Makefile.am b/src/Makefile.am index 6c5b2d4e5..1f50fa699 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -527,8 +527,8 @@ BASE_SRC += events/actionevent.h \ utils/dumplibs.h \ utils/env.cpp \ utils/env.h \ - utils/files.cpp \ - utils/files.h \ + fs/files.cpp \ + fs/files.h \ utils/fuzzer.cpp \ utils/fuzzer.h \ utils/gettext.h \ @@ -540,14 +540,14 @@ BASE_SRC += events/actionevent.h \ utils/langs.cpp \ utils/langs.h \ utils/mathutils.h \ - utils/mkdir.cpp \ - utils/mkdir.h \ + fs/mkdir.cpp \ + fs/mkdir.h \ utils/mrand.cpp \ utils/mrand.h \ utils/parameters.cpp \ utils/parameters.h \ - utils/paths.cpp \ - utils/paths.h \ + fs/paths.cpp \ + fs/paths.h \ utils/perfomance.cpp \ utils/perfomance.h \ utils/debugmemoryobject.h \ @@ -568,8 +568,8 @@ BASE_SRC += events/actionevent.h \ utils/sdlhelper.cpp \ utils/sdlhelper.h \ utils/sdlmemoryobject.h \ - utils/specialfolder.cpp \ - utils/specialfolder.h \ + fs/specialfolder.cpp \ + fs/specialfolder.h \ utils/stringmap.h \ utils/stringutils.cpp \ utils/stringutils.h \ @@ -1924,7 +1924,7 @@ manaplustests_SOURCES = ${SRC} \ utils/timer_unittest.cc \ utils/xmlutils_unittest.cc \ utils/mathutils_unittest.cc \ - utils/files_unittest.cc \ + fs/files_unittest.cc \ utils/stringutils_unittest.cc \ utils/parameters_unittest.cc \ resources/mstack_unittest.cc \ diff --git a/src/actions/actions.cpp b/src/actions/actions.cpp index 7f1e37104..b4fadf62d 100644 --- a/src/actions/actions.cpp +++ b/src/actions/actions.cpp @@ -38,6 +38,8 @@ #include "const/resources/skill.h" +#include "fs/files.h" + #include "gui/gui.h" #include "gui/popupmanager.h" #include "gui/sdlinput.h" @@ -102,7 +104,6 @@ #include "utils/chatutils.h" #include "utils/delete2.h" #include "utils/gettext.h" -#include "utils/files.h" #include "utils/timer.h" #include "utils/mathutils.h" diff --git a/src/actormanager.cpp b/src/actormanager.cpp index 9f8985d7e..35be95159 100644 --- a/src/actormanager.cpp +++ b/src/actormanager.cpp @@ -40,10 +40,11 @@ #include "gui/windows/socialwindow.h" #include "gui/windows/questswindow.h" +#include "fs/files.h" + #include "input/inputmanager.h" #include "utils/checkutils.h" -#include "utils/files.h" #include "utils/mathutils.h" #include "utils/gettext.h" diff --git a/src/being/being.cpp b/src/being/being.cpp index 9a86c26cb..1e63b9b98 100644 --- a/src/being/being.cpp +++ b/src/being/being.cpp @@ -51,7 +51,7 @@ #include "enums/resources/map/blockmask.h" -#include "particle/particleinfo.h" +#include "fs/files.h" #include "gui/gui.h" #include "gui/userpalette.h" @@ -74,6 +74,8 @@ #include "net/playerhandler.h" #include "net/serverfeatures.h" +#include "particle/particleinfo.h" + #include "resources/attack.h" #include "resources/chatobject.h" #include "resources/emoteinfo.h" @@ -110,7 +112,6 @@ #include "utils/checkutils.h" #include "utils/delete2.h" -#include "utils/files.h" #include "utils/gettext.h" #include "utils/timer.h" diff --git a/src/chatlogger.cpp b/src/chatlogger.cpp index 1f6fae035..2b420d1f3 100644 --- a/src/chatlogger.cpp +++ b/src/chatlogger.cpp @@ -34,10 +34,9 @@ #include "configuration.h" +#include "fs/mkdir.h" #include "fs/virtfs.h" -#include "utils/mkdir.h" - #include "debug.h" ChatLogger *chatLogger = nullptr; diff --git a/src/client.cpp b/src/client.cpp index e87ac65cb..bb7c8e687 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -144,7 +144,7 @@ #include "utils/gettexthelper.h" #include "utils/mrand.h" #ifdef ANDROID -#include "utils/paths.h" +#include "fs/paths.h" #endif // ANDROID #include "utils/sdlcheckutils.h" #include "utils/timer.h" @@ -163,7 +163,7 @@ #ifdef WIN32 #include -#include "utils/specialfolder.h" +#include "fs/specialfolder.h" #undef ERROR #endif // WIN32 diff --git a/src/commandline.cpp b/src/commandline.cpp index 8315d7d58..55bea5bd3 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -25,8 +25,9 @@ #include "main.h" #include "settings.h" +#include "fs/paths.h" + #include "utils/gettext.h" -#include "utils/paths.h" #include "utils/stringutils.h" #include diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 95aac7ed1..bd19d7307 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -29,12 +29,12 @@ #include "being/beingspeech.h" +#include "fs/files.h" #include "fs/virtfs.h" +#include "fs/mkdir.h" +#include "fs/paths.h" -#include "utils/files.h" #include "utils/gettext.h" -#include "utils/mkdir.h" -#include "utils/paths.h" #include "render/renderers.h" diff --git a/src/configuration.cpp b/src/configuration.cpp index 7a6f91e79..bcf461892 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -25,10 +25,11 @@ #include "logger.h" #include "variabledata.h" +#include "fs/paths.h" + #include "listeners/configlistener.h" #include "utils/delete2.h" -#include "utils/paths.h" #ifdef DEBUG_CONFIG #include "utils/stringmap.h" #endif // DEBUG_CONFIG diff --git a/src/dirs.cpp b/src/dirs.cpp index 43f27ae4c..72d108388 100644 --- a/src/dirs.cpp +++ b/src/dirs.cpp @@ -28,16 +28,16 @@ #include "main.h" #include "settings.h" +#include "fs/mkdir.h" +#include "fs/paths.h" #include "fs/virtfs.h" #include "utils/base64.h" #if defined(__native_client__) || (defined(ANDROID) && defined(USE_SDL2)) -#include "utils/files.h" +#include "fs/files.h" #endif // defined(__native_client__) || (defined(ANDROID) && // defined(USE_SDL2)) #include "utils/gettext.h" -#include "utils/mkdir.h" -#include "utils/paths.h" #ifdef ANDROID #ifdef USE_SDL2 @@ -53,7 +53,7 @@ #ifdef WIN32 #include -#include "utils/specialfolder.h" +#include "fs/specialfolder.h" #undef ERROR #endif // WIN32 diff --git a/src/dyetool/client.cpp b/src/dyetool/client.cpp index 0ee7d0c2c..6e206de7f 100644 --- a/src/dyetool/client.cpp +++ b/src/dyetool/client.cpp @@ -63,7 +63,7 @@ #include "utils/gettexthelper.h" #include "utils/mrand.h" #ifdef ANDROID -#include "utils/paths.h" +#include "fs/paths.h" #endif // ANDROID #include "utils/sdlcheckutils.h" @@ -77,7 +77,7 @@ #ifdef WIN32 #include -#include "utils/specialfolder.h" +#include "fs/specialfolder.h" #undef ERROR #endif // WIN32 diff --git a/src/fs/files.cpp b/src/fs/files.cpp new file mode 100644 index 000000000..516283fe6 --- /dev/null +++ b/src/fs/files.cpp @@ -0,0 +1,268 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fs/files.h" + +#include "logger.h" + +#include "fs/mkdir.h" +#if defined(ANDROID) || defined(__native_client__) +#include "fs/paths.h" +#include "fs/virtfs.h" +#include "fs/virtfstools.h" +#include "fs/virtlist.h" +#endif // defined(ANDROID) || defined(__native_client__) + +#include + +#include "debug.h" + +#ifdef ANDROID +void Files::extractLocale() +{ + // in future need also remove all locales in local dir + + const std::string fileName2 = std::string(getenv( + "APPDIR")).append("/locale.zip"); + VirtFs::addZipToSearchPath(fileName2, Append_false); + + const std::string localDir = std::string(getenv("APPDIR")).append("/"); + VirtList *const rootDirs = VirtFs::enumerateFiles("locale"); + FOR_EACH (StringVectCIter, i, rootDirs->names) + { + const std::string dir = std::string("locale/").append(*i); + if (VirtFs::isDirectory(dir)) + { + const std::string moFile = dir + "/LC_MESSAGES/manaplus.mo"; + if (VirtFs::exists((moFile))) + { + const std::string localFile = localDir + moFile; + const std::string localDir2 = localDir + dir + "/LC_MESSAGES"; + mkdir_r(localDir2.c_str()); + copyPhysFsFile(moFile, localFile); + } + } + } + VirtFs::freeList(rootDirs); + VirtFs::removeZipFromSearchPath(fileName2); + remove(fileName2.c_str()); +} +#endif // ANDROID + +#if defined(ANDROID) || defined(__native_client__) + +namespace +{ +#ifdef ANDROID + int mFilesCount = 0; +#endif // ANDROID + + Files::CopyFileCallbackPtr mCallbackPtr = nullptr; +} // namespace + +void Files::setCopyCallBack(Files::CopyFileCallbackPtr callback) +{ + mCallbackPtr = callback; +} + +void Files::copyPhysFsFile(const std::string &restrict inFile, + const std::string &restrict outFile) +{ + int size = 0; + void *const buf = VirtFs::loadFile(inFile, size); + FILE *const file = fopen(outFile.c_str(), "w"); + fwrite(buf, 1, size, file); + fclose(file); + free(buf); +#ifdef ANDROID + if (mCallbackPtr) + { + mCallbackPtr(mFilesCount); + mFilesCount ++; + } +#endif // ANDROID +} + +void Files::copyPhysFsDir(const std::string &restrict inDir, + const std::string &restrict outDir) +{ + mkdir_r(outDir.c_str()); + VirtList *const files = VirtFs::enumerateFiles(inDir); + FOR_EACH (StringVectCIter, i, files->names) + { + const std::string file = std::string(inDir).append("/").append(*i); + const std::string outDir2 = std::string(outDir).append("/").append(*i); + if (VirtFs::isDirectory(file)) + copyPhysFsDir(file, outDir2); + else + copyPhysFsFile(file, outDir2); + } + VirtFs::freeList(files); +} + +void Files::extractZip(const std::string &restrict zipName, + const std::string &restrict inDir, + const std::string &restrict outDir) +{ + VirtFs::addZipToSearchPath(zipName, Append_false); + copyPhysFsDir(inDir, outDir); + VirtFs::removeZipFromSearchPath(zipName); + remove(zipName.c_str()); +} + +#endif // ANDROID __native_client__ + +int Files::renameFile(const std::string &restrict srcName, + const std::string &restrict dstName) +{ +#if defined __native_client__ + FILE *srcFile = fopen(srcName.c_str(), "rb"); + if (srcFile == nullptr) + return -1; + FILE *dstFile = fopen(dstName.c_str(), "w+b"); + if (dstFile == nullptr) + { + fclose(srcFile); + return -1; + } + + const int chunkSize = 500000; + char *buf = new char[chunkSize]; + size_t sz = 0; + while ((sz = fread(buf, 1, chunkSize, srcFile))) + { + if (fwrite(buf, 1, sz, dstFile) != sz) + { + delete [] buf; + fclose(srcFile); + fclose(dstFile); + ::remove(dstName.c_str()); + return -1; + } + } + + delete [] buf; + fclose(srcFile); + fclose(dstFile); + if (!::remove(srcName.c_str())) + return 0; + + return -1; +#else // defined __native_client__ + + return ::rename(srcName.c_str(), dstName.c_str()); +#endif // defined __native_client__ +} + +int Files::copyFile(const std::string &restrict srcName, + const std::string &restrict dstName) +{ + FILE *srcFile = fopen(srcName.c_str(), "rb"); + if (srcFile == nullptr) + return -1; + FILE *dstFile = fopen(dstName.c_str(), "w+b"); + if (dstFile == nullptr) + { + fclose(srcFile); + return -1; + } + + const int chunkSize = 500000; + char *buf = new char[chunkSize]; + size_t sz = 0; + while ((sz = fread(buf, 1, chunkSize, srcFile))) + { + if (fwrite(buf, 1, sz, dstFile) != sz) + { + delete [] buf; + fclose(srcFile); + fclose(dstFile); + return -1; + } + } + + delete [] buf; + fclose(srcFile); + fclose(dstFile); + return 0; +} + +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; +} + +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 += "/"; + const struct dirent *next_file = nullptr; + DIR *const dir = opendir(path.c_str()); + + if (dir) + { + while ((next_file = readdir(dir))) + { + const std::string file = next_file->d_name; + if (file != "." && file != "..") + remove((path + file).c_str()); + } + closedir(dir); + } +} diff --git a/src/fs/files.h b/src/fs/files.h new file mode 100644 index 000000000..b91f7e9ab --- /dev/null +++ b/src/fs/files.h @@ -0,0 +1,68 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_FILES_H +#define UTILS_FILES_H + +#include "utils/stringvector.h" + +#include "localconsts.h" + +namespace Files +{ +#ifdef ANDROID + void extractLocale(); +#endif // ANDROID + +#if defined(ANDROID) || defined(__native_client__) + typedef void (*CopyFileCallbackPtr) (int cnt); + + void setCopyCallBack(CopyFileCallbackPtr callback); + + void copyPhysFsFile(const std::string &restrict inFile, + const std::string &restrict outFile); + + void copyPhysFsDir(const std::string &restrict inDir, + const std::string &restrict outDir); + + void extractZip(const std::string &restrict zipName, + const std::string &restrict inDir, + const std::string &restrict outDir); +#endif // ANDROID __native_client__ + + int renameFile(const std::string &restrict pFrom, + const std::string &restrict pTo); + + int copyFile(const std::string &restrict pFrom, + const std::string &restrict pTo); + + bool existsLocal(const std::string &path); + + bool loadTextFileLocal(const std::string &fileName, + StringVect &lines); + + void saveTextFile(std::string path, + const std::string &restrict name, + const std::string &restrict text); + + void deleteFilesInDirectory(std::string path); +} // namespace Files + +#endif // UTILS_FILES_H diff --git a/src/fs/files_unittest.cc b/src/fs/files_unittest.cc new file mode 100644 index 000000000..7b4c6ca33 --- /dev/null +++ b/src/fs/files_unittest.cc @@ -0,0 +1,167 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fs/files.h" + +#include "catch.hpp" +#include "logger.h" + +#include "fs/virtfs.h" +#include "fs/virtfstools.h" + +#include "utils/delete2.h" + +#include "resources/resourcemanager/resourcemanager.h" + +#include "debug.h" + +TEST_CASE("Files renameFile") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + const int sz = 1234567; + char *buf = new char[sz]; + for (int f = 0; f < sz; f ++) + buf[f] = f; + + const std::string name1 = "file1.test"; + const std::string name2 = "file2.test"; + FILE *file = fopen(name1.c_str(), "w+b"); + fwrite(buf, 1, sz, file); + fclose(file); + + REQUIRE(0 == Files::renameFile(name1, name2)); + char *buf2 = new char[sz]; + FILE *file2 = fopen(name2.c_str(), "rb"); + REQUIRE_FALSE(nullptr == file2); + fread(buf2, 1, sz, file2); + fclose(file2); + ::remove(name1.c_str()); + ::remove(name2.c_str()); + + for (int f = 0; f < sz; f ++) + REQUIRE(buf[f] == buf2[f]); + + delete [] buf; + delete [] buf2; + ResourceManager::deleteInstance(); + delete2(logger); +// VirtFs::deinit(); +} + +TEST_CASE("Files existsLocal") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + 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(); + delete2(logger); +// VirtFs::deinit(); +} + +TEST_CASE("Files loadTextFileString") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") == + "this is test \nfile."); + ResourceManager::deleteInstance(); + delete2(logger); +// VirtFs::deinit(); +} + +TEST_CASE("Files loadTextFile") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + StringVect lines; + VirtFs::loadTextFile("test/simplefile.txt", lines); + REQUIRE(lines.size() == 2); + REQUIRE(lines[0] == "this is test "); + REQUIRE(lines[1] == "file."); + ResourceManager::deleteInstance(); + delete2(logger); +// VirtFs::deinit(); +} + +TEST_CASE("Files saveTextFile") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + const std::string dir = VirtFs::getPath("test"); + REQUIRE(dir.size() > 0); + Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2"); + std::string data = VirtFs::loadTextFileString("test/tempfile.txt"); + ::remove((dir + "/tempfile.txt").c_str()); + REQUIRE(data == "test line\ntext line2\n"); + ResourceManager::deleteInstance(); + delete2(logger); +// VirtFs::deinit(); +} + +TEST_CASE("Files getFilesInDir") +{ + dirSeparator = "/"; + logger = new Logger(); + ResourceManager::init(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + StringVect list; + VirtFs::getFilesInDir("test", + list, + ".gpl"); + REQUIRE(list.size() == 1); + REQUIRE(list[0] == "test/palette.gpl"); + + list.clear(); + VirtFs::getFilesInDir("perserver/default", + list, + ".xml"); + REQUIRE(list.size() == 5); + REQUIRE(list[0] == "perserver/default/charcreation.xml"); + REQUIRE(list[1] == "perserver/default/deadmessages.xml"); + REQUIRE(list[2] == "perserver/default/defaultcommands.xml"); + REQUIRE(list[3] == "perserver/default/features.xml"); + REQUIRE(list[4] == "perserver/default/weapons.xml"); + ResourceManager::deleteInstance(); + delete2(logger); +// VirtFs::deinit(); +} diff --git a/src/fs/mkdir.cpp b/src/fs/mkdir.cpp new file mode 100644 index 000000000..e84ab5f28 --- /dev/null +++ b/src/fs/mkdir.cpp @@ -0,0 +1,164 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "fs/mkdir.h" + +#if defined WIN32 +#include +#include +#endif // defined WIN32 + +#include + +#include "debug.h" + +#if defined WIN32 +int mkdir_r(const char *const pathname) +{ + if (!pathname) + return -1; + + char tmp[PATH_MAX]; + char tmp2[PATH_MAX]; + char *p; + + if (strlen(pathname) >= PATH_MAX - 2) + return -1; + + strncpy(tmp, pathname, sizeof(tmp) - 1); + tmp[PATH_MAX - 1] = '\0'; + + const int len = CAST_S32(strlen(tmp)); + + if (len < 1 || len >= INT_MAX) + return -1; + + // terminate the pathname with '/' + if (tmp[len - 1] != '/') + { + tmp[len] = '/'; + tmp[len + 1] = '\0'; + } + + for (p = tmp; *p; p++) + { + if (*p == '/' || *p == '\\') + { + *p = '\0'; + // ignore a slash at the beginning of a path + if (tmp[0] == 0) + { + *p = '/'; + continue; + } + + strcpy(tmp2, tmp); + char *p2 = tmp2 + strlen(tmp2) - 1; + if (*p2 == '/' || *p2 == '\\') + *p2 = 0; + // check if the name already exists, but not as directory + struct stat statbuf; + if (!stat(tmp2, &statbuf)) + { + if (S_ISDIR(statbuf.st_mode)) + { + *p = '/'; + continue; + } + else + return -1; + } + + if (!CreateDirectory(tmp2, nullptr)) + { + // hack, hack. just assume that x: might be a drive + // letter, and try again + if (!(strlen(tmp2) == 2 && !strcmp(tmp2 + 1, ":"))) + return -1; + } + + *p = '/'; + } + } + return 0; +} +#else // WIN32 + +/// Create a directory, making leading components first if necessary +int mkdir_r(const char *const pathname) +{ + if (!pathname) + return -1; + + const size_t len = CAST_SIZE(strlen(pathname)); + char *tmp = new char[len + 2]; + char *p = nullptr; + + strcpy(tmp, pathname); + + // terminate the pathname with '/' + if (tmp[len - 1] != '/') + { + tmp[len] = '/'; + tmp[len + 1] = '\0'; + } + + for (p = tmp; *p; p++) + { + if (*p == '/') + { + *p = '\0'; + // ignore a slash at the beginning of a path + if (tmp[0] == 0) + { + *p = '/'; + continue; + } + + // check if the name already exists, but not as directory + struct stat statbuf; + if (!stat(tmp, &statbuf)) + { + if (S_ISDIR(statbuf.st_mode)) + { + *p = '/'; + continue; + } + else + { + delete []tmp; + return -1; + } + } + + if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) + { + delete []tmp; + return -1; + } + + *p = '/'; + } + } + delete []tmp; + return 0; +} +#endif // WIN32 diff --git a/src/fs/mkdir.h b/src/fs/mkdir.h new file mode 100644 index 000000000..a662daca3 --- /dev/null +++ b/src/fs/mkdir.h @@ -0,0 +1,27 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_MKDIR_H +#define UTILS_MKDIR_H + +int mkdir_r(const char *const pathname); + +#endif // UTILS_MKDIR_H diff --git a/src/fs/paths.cpp b/src/fs/paths.cpp new file mode 100644 index 000000000..affc12285 --- /dev/null +++ b/src/fs/paths.cpp @@ -0,0 +1,221 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef _MSC_VER +# include "msvc/config.h" +#elif defined(HAVE_CONFIG_H) +#include "config.h" +#endif // _MSC_VER + +#include "fs/paths.h" +#include "fs/virtfs.h" + +#include "utils/stringutils.h" + +#ifdef USE_X11 +#include "fs/files.h" +#endif // USE_X11 + +#ifdef __native_client__ +#include +#define realpath(N, R) strcpy(R, N) +#endif // __native_client__ + +#ifdef WIN32 +#include "fs/specialfolder.h" +#define realpath(N, R) _fullpath((R), (N), _MAX_PATH) +#elif defined __OpenBSD__ +#include +#elif defined __native_client__ +#include +#endif // WIN32 + +#ifdef ANDROID +#ifdef USE_SDL2 +#include +#endif // USE_SDL2 +#endif // ANDROID + +#include "debug.h" + +namespace +{ + std::string mPackageDir; +} // namespace + +std::string getRealPath(const std::string &str) +{ +#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__native_client__) + char *realPath = reinterpret_cast(calloc(PATH_MAX, sizeof(char))); + if (!realPath) + return ""; + realpath(str.c_str(), realPath); +#else // defined(__OpenBSD__) || defined(__ANDROID__) || + // defined(__native_client__) + + char *realPath = realpath(str.c_str(), nullptr); +#endif // defined(__OpenBSD__) || defined(__ANDROID__) || + // defined(__native_client__) + + if (!realPath) + return ""; + std::string path = realPath; + free(realPath); + return path; +} + +bool isRealPath(const std::string &str) +{ + return str == getRealPath(str); +} + +bool checkPath(const std::string &path) +{ + if (path.empty()) + return true; + return path.find("../") == std::string::npos + && path.find("..\\") == std::string::npos + && path.find("/..") == std::string::npos + && path.find("\\..") == std::string::npos; +} + +std::string &fixDirSeparators(std::string &str) +{ + if (dirSeparator[0] == '/') + return str; + + return replaceAll(str, "/", "\\"); +} + +std::string removeLast(const std::string &str) +{ + size_t pos2 = str.rfind('/'); + const size_t pos3 = str.rfind('\\'); + if (pos3 != std::string::npos) + { + if (pos2 == std::string::npos || pos3 > pos2) + pos2 = pos3; + } + if (pos2 != std::string::npos) + return str.substr(0, pos2); + else + return str; +} + +#ifdef WIN32 +std::string getSelfName() +{ + return "manaplus.exe"; +} + +#elif defined(__APPLE__) +std::string getSelfName() +{ + return "manaplus.exe"; +} + +#elif defined __linux__ || defined __linux +#include + +std::string getSelfName() +{ + char buf[257]; + const ssize_t sz = readlink("/proc/self/exe", buf, 256); + if (sz > 0 && sz < 256) + { + buf[sz] = 0; + return buf; + } + else + { + return ""; + } +} + +#else // WIN32 + +std::string getSelfName() +{ + return ""; +} + +#endif // WIN32 + +std::string getPicturesDir() +{ +#ifdef WIN32 + std::string dir = getSpecialFolderLocation(CSIDL_MYPICTURES); + if (dir.empty()) + dir = getSpecialFolderLocation(CSIDL_DESKTOP); + return dir; +#elif defined USE_X11 + char *xdg = getenv("XDG_CONFIG_HOME"); + std::string file; + if (!xdg) + { + file = std::string(VirtFs::getUserDir()).append( + "/.config/user-dirs.dirs"); + } + else + { + file = std::string(xdg).append("/user-dirs.dirs"); + } + + StringVect arr; + Files::loadTextFileLocal(file, arr); + FOR_EACH (StringVectCIter, it, arr) + { + std::string str = *it; + if (findCutFirst(str, "XDG_PICTURES_DIR=\"")) + { + str = str.substr(0, str.size() - 1); + // use hack to replace $HOME var. + // if in string other vars, fallback to default path + replaceAll(str, "$HOME/", VirtFs::getUserDir()); + str = getRealPath(str); + if (str.empty()) + str = std::string(VirtFs::getUserDir()).append("Desktop"); + return str; + } + } + + return std::string(VirtFs::getUserDir()).append("Desktop"); +#else // WIN32 + + return std::string(VirtFs::getUserDir()).append("Desktop"); +#endif // WIN32 +} + +#ifdef ANDROID +std::string getSdStoragePath() +{ + return getenv("DATADIR2"); +} +#endif // ANDROID + +std::string getPackageDir() +{ + return mPackageDir; +} + +void setPackageDir(const std::string &dir) +{ + mPackageDir = dir; +} diff --git a/src/fs/paths.h b/src/fs/paths.h new file mode 100644 index 000000000..fc90730f3 --- /dev/null +++ b/src/fs/paths.h @@ -0,0 +1,50 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_PATHS_H +#define UTILS_PATHS_H + +#include + +#include "localconsts.h" + +std::string getRealPath(const std::string &str) A_WARN_UNUSED; + +bool isRealPath(const std::string &str) A_WARN_UNUSED; + +bool checkPath(const std::string &path) A_WARN_UNUSED; + +std::string &fixDirSeparators(std::string &str); + +std::string removeLast(const std::string &str) A_WARN_UNUSED; + +std::string getSelfName() A_WARN_UNUSED; + +std::string getPicturesDir() A_WARN_UNUSED; + +#ifdef ANDROID +std::string getSdStoragePath() A_WARN_UNUSED; +#endif // ANDROID + +std::string getPackageDir() A_WARN_UNUSED; + +void setPackageDir(const std::string &dir); + +#endif // UTILS_PATHS_H diff --git a/src/fs/specialfolder.cpp b/src/fs/specialfolder.cpp new file mode 100644 index 000000000..ee27dab4b --- /dev/null +++ b/src/fs/specialfolder.cpp @@ -0,0 +1,59 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef WIN32 +#include "fs/specialfolder.h" +#include + +#include "debug.h" + +/* + * Retrieve the pathname of special folders on win32, or an empty string + * on error / if the folder does not exist. + * See http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx for + * a list of folder ids + */ +std::string getSpecialFolderLocation(const int folderId) +{ + std::string ret; + LPITEMIDLIST pItemIdList; + LPMALLOC pMalloc; + char szPath[_MAX_PATH]; + + // get the item ID list for folderId + HRESULT hr = SHGetSpecialFolderLocation(nullptr, folderId, &pItemIdList); + if (hr != S_OK) + return ret; + + // convert the ID list into a file system path + if (SHGetPathFromIDList(pItemIdList, szPath) == FALSE) + return ret; + + // get the IMalloc pointer and free all resources we used + hr = SHGetMalloc(&pMalloc); + pMalloc->Free(pItemIdList); + pMalloc->Release(); + + ret = szPath; + return ret; +} + +#endif // WIN32 diff --git a/src/fs/specialfolder.h b/src/fs/specialfolder.h new file mode 100644 index 000000000..f2cc93c9b --- /dev/null +++ b/src/fs/specialfolder.h @@ -0,0 +1,33 @@ +/* + * The ManaPlus Client + * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2011-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_SPECIALFOLDER_H +#define UTILS_SPECIALFOLDER_H + +#ifdef WIN32 +#include +#include + +#include "localconsts.h" +std::string getSpecialFolderLocation(const int folderId) A_WARN_UNUSED; +#endif // WIN32 + +#endif // UTILS_SPECIALFOLDER_H diff --git a/src/fs/virtfs.cpp b/src/fs/virtfs.cpp index 0db25f435..9b19890eb 100644 --- a/src/fs/virtfs.cpp +++ b/src/fs/virtfs.cpp @@ -30,7 +30,7 @@ #include #ifdef ANDROID -#include "utils/paths.h" +#include "fs/paths.h" #endif // ANDROID #include "debug.h" diff --git a/src/fs/virtfstools.cpp b/src/fs/virtfstools.cpp index 4d6c96c33..889739658 100644 --- a/src/fs/virtfstools.cpp +++ b/src/fs/virtfstools.cpp @@ -22,10 +22,10 @@ #include "logger.h" +#include "fs/paths.h" #include "fs/virtfs.h" #include "fs/virtlist.h" -#include "utils/paths.h" #include "utils/stringutils.h" #include diff --git a/src/game.cpp b/src/game.cpp index 3318c4268..9dcafad67 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -41,6 +41,7 @@ #include "enums/being/beingdirection.h" +#include "fs/mkdir.h" #include "fs/virtfs.h" #include "gui/dialogsmanager.h" @@ -122,7 +123,6 @@ #include "utils/delete2.h" #include "utils/gettext.h" -#include "utils/mkdir.h" #include "utils/sdlcheckutils.h" #include "utils/timer.h" diff --git a/src/gui/fonts/font.cpp b/src/gui/fonts/font.cpp index 877f112e0..8c148f003 100644 --- a/src/gui/fonts/font.cpp +++ b/src/gui/fonts/font.cpp @@ -66,6 +66,7 @@ #include "gui/fonts/font.h" +#include "fs/paths.h" #include "fs/virtfstools.h" #include "gui/fonts/textchunk.h" @@ -77,7 +78,6 @@ #include "resources/image/image.h" #include "utils/delete2.h" -#include "utils/paths.h" #include "utils/sdlcheckutils.h" #include "utils/stringutils.h" #include "utils/timer.h" diff --git a/src/gui/windows/helpwindow.cpp b/src/gui/windows/helpwindow.cpp index 91bdbe13e..f10933996 100644 --- a/src/gui/windows/helpwindow.cpp +++ b/src/gui/windows/helpwindow.cpp @@ -26,6 +26,7 @@ #include "enums/gui/layouttype.h" +#include "fs/paths.h" #include "fs/virtfstools.h" #include "gui/gui.h" @@ -40,7 +41,6 @@ #include "input/inputmanager.h" #include "utils/gettext.h" -#include "utils/paths.h" #include "utils/process.h" #include "utils/translation/podict.h" diff --git a/src/gui/windows/logindialog.cpp b/src/gui/windows/logindialog.cpp index dfa613ba3..731db0cc9 100644 --- a/src/gui/windows/logindialog.cpp +++ b/src/gui/windows/logindialog.cpp @@ -25,7 +25,7 @@ #include "client.h" #include "configuration.h" -#include "listeners/openurllistener.h" +#include "fs/paths.h" #include "gui/models/updatelistmodel.h" #include "gui/models/updatetypemodel.h" @@ -40,13 +40,14 @@ #include "gui/widgets/passwordfield.h" #include "gui/widgets/layoutcell.h" +#include "listeners/openurllistener.h" + #include "net/charserverhandler.h" #include "net/logindata.h" #include "net/loginhandler.h" #include "net/updatetypeoperators.h" #include "utils/delete2.h" -#include "utils/paths.h" #include "debug.h" diff --git a/src/gui/windows/serverdialog.cpp b/src/gui/windows/serverdialog.cpp index 3ccd68999..c4f65d770 100644 --- a/src/gui/windows/serverdialog.cpp +++ b/src/gui/windows/serverdialog.cpp @@ -30,6 +30,8 @@ #include "net/download.h" +#include "fs/paths.h" + #include "gui/widgets/checkbox.h" #include "gui/widgets/createwidget.h" #include "gui/widgets/desktop.h" @@ -45,7 +47,6 @@ #include "utils/delete2.h" #include "utils/gettext.h" #include "utils/langs.h" -#include "utils/paths.h" #include "debug.h" diff --git a/src/gui/windows/updaterwindow.cpp b/src/gui/windows/updaterwindow.cpp index 4d0ff9f3a..ef4b543a1 100644 --- a/src/gui/windows/updaterwindow.cpp +++ b/src/gui/windows/updaterwindow.cpp @@ -29,6 +29,9 @@ #include "enums/gui/layouttype.h" +#include "fs/files.h" +#include "fs/mkdir.h" +#include "fs/paths.h" #include "fs/virtfs.h" #include "fs/virtfstools.h" @@ -47,10 +50,7 @@ #include "resources/db/moddb.h" #include "utils/delete2.h" -#include "utils/files.h" #include "utils/gettext.h" -#include "utils/mkdir.h" -#include "utils/paths.h" #include diff --git a/src/maingui.cpp b/src/maingui.cpp index 7bf5e1290..5019daf22 100644 --- a/src/maingui.cpp +++ b/src/maingui.cpp @@ -31,8 +31,8 @@ #include "utils/delete2.h" #ifdef ANDROID -#include "utils/mkdir.h" -#include "utils/paths.h" +#include "fs/mkdir.h" +#include "fs/paths.h" #endif // ANDROID #include "utils/process.h" #include "utils/xml.h" diff --git a/src/net/download.cpp b/src/net/download.cpp index 32ee36ab2..feb92265c 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -25,15 +25,11 @@ #include "logger.h" #include "settings.h" -#include "utils/files.h" +#include "fs/files.h" #include "utils/sdlhelper.h" #include -#if defined __native_client__ -#include "utils/files.h" -#endif // defined __native_client__ - #include #include "debug.h" diff --git a/src/net/ea/loginrecv.cpp b/src/net/ea/loginrecv.cpp index f96424e6d..6c6e1bad1 100644 --- a/src/net/ea/loginrecv.cpp +++ b/src/net/ea/loginrecv.cpp @@ -27,10 +27,11 @@ #include "being/being.h" +#include "fs/paths.h" + #include "gui/windows/logindialog.h" #include "utils/gettext.h" -#include "utils/paths.h" #include "net/logindata.h" #include "net/loginhandler.h" diff --git a/src/net/eathena/loginrecv.cpp b/src/net/eathena/loginrecv.cpp index 336ca84a4..c31692b4f 100644 --- a/src/net/eathena/loginrecv.cpp +++ b/src/net/eathena/loginrecv.cpp @@ -24,6 +24,8 @@ #include "client.h" +#include "fs/paths.h" + #include "gui/windows/logindialog.h" #include "net/logindata.h" @@ -33,7 +35,6 @@ #include "net/eathena/updateprotocol.h" #include "utils/gettext.h" -#include "utils/paths.h" #include "debug.h" diff --git a/src/resources/map/map.cpp b/src/resources/map/map.cpp index 5af4e72f3..3d47e96fc 100644 --- a/src/resources/map/map.cpp +++ b/src/resources/map/map.cpp @@ -34,6 +34,7 @@ #include "enums/resources/map/blockmask.h" #include "enums/resources/map/mapitemtype.h" +#include "fs/mkdir.h" #include "fs/virtfs.h" #include "gui/userpalette.h" @@ -64,7 +65,6 @@ #include "utils/delete2.h" #include "utils/dtor.h" -#include "utils/mkdir.h" #include "utils/timer.h" #include diff --git a/src/test/testmain.cpp b/src/test/testmain.cpp index dd1829a65..c2fcc75f9 100644 --- a/src/test/testmain.cpp +++ b/src/test/testmain.cpp @@ -25,8 +25,9 @@ #include "logger.h" #include "settings.h" +#include "fs/paths.h" + #include "utils/delete2.h" -#include "utils/paths.h" #include "utils/process.h" #include "debug.h" diff --git a/src/utils/files.cpp b/src/utils/files.cpp deleted file mode 100644 index 4dd26126a..000000000 --- a/src/utils/files.cpp +++ /dev/null @@ -1,273 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2013-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "utils/files.h" - -#include "logger.h" - -#if defined(ANDROID) || defined(__native_client__) -#include "utils/mkdir.h" -#endif // defined(ANDROID) || defined(__native_client__) - -#include "utils/mkdir.h" -#if defined(ANDROID) || defined(__native_client__) -#include "fs/virtfs.h" -#include "fs/virtfstools.h" -#include "fs/virtlist.h" - -#include "utils/paths.h" -#endif // defined(ANDROID) || defined(__native_client__) - -#include - -#include "debug.h" - -#ifdef ANDROID -void Files::extractLocale() -{ - // in future need also remove all locales in local dir - - const std::string fileName2 = std::string(getenv( - "APPDIR")).append("/locale.zip"); - VirtFs::addZipToSearchPath(fileName2, Append_false); - - const std::string localDir = std::string(getenv("APPDIR")).append("/"); - VirtList *const rootDirs = VirtFs::enumerateFiles("locale"); - FOR_EACH (StringVectCIter, i, rootDirs->names) - { - const std::string dir = std::string("locale/").append(*i); - if (VirtFs::isDirectory(dir)) - { - const std::string moFile = dir + "/LC_MESSAGES/manaplus.mo"; - if (VirtFs::exists((moFile))) - { - const std::string localFile = localDir + moFile; - const std::string localDir2 = localDir + dir + "/LC_MESSAGES"; - mkdir_r(localDir2.c_str()); - copyPhysFsFile(moFile, localFile); - } - } - } - VirtFs::freeList(rootDirs); - VirtFs::removeZipFromSearchPath(fileName2); - remove(fileName2.c_str()); -} -#endif // ANDROID - -#if defined(ANDROID) || defined(__native_client__) - -namespace -{ -#ifdef ANDROID - int mFilesCount = 0; -#endif // ANDROID - - Files::CopyFileCallbackPtr mCallbackPtr = nullptr; -} // namespace - -void Files::setCopyCallBack(Files::CopyFileCallbackPtr callback) -{ - mCallbackPtr = callback; -} - -void Files::copyPhysFsFile(const std::string &restrict inFile, - const std::string &restrict outFile) -{ - int size = 0; - void *const buf = VirtFs::loadFile(inFile, size); - FILE *const file = fopen(outFile.c_str(), "w"); - fwrite(buf, 1, size, file); - fclose(file); - free(buf); -#ifdef ANDROID - if (mCallbackPtr) - { - mCallbackPtr(mFilesCount); - mFilesCount ++; - } -#endif // ANDROID -} - -void Files::copyPhysFsDir(const std::string &restrict inDir, - const std::string &restrict outDir) -{ - mkdir_r(outDir.c_str()); - VirtList *const files = VirtFs::enumerateFiles(inDir); - FOR_EACH (StringVectCIter, i, files->names) - { - const std::string file = std::string(inDir).append("/").append(*i); - const std::string outDir2 = std::string(outDir).append("/").append(*i); - if (VirtFs::isDirectory(file)) - copyPhysFsDir(file, outDir2); - else - copyPhysFsFile(file, outDir2); - } - VirtFs::freeList(files); -} - -void Files::extractZip(const std::string &restrict zipName, - const std::string &restrict inDir, - const std::string &restrict outDir) -{ - VirtFs::addZipToSearchPath(zipName, Append_false); - copyPhysFsDir(inDir, outDir); - VirtFs::removeZipFromSearchPath(zipName); - remove(zipName.c_str()); -} - -#endif // ANDROID __native_client__ - -int Files::renameFile(const std::string &restrict srcName, - const std::string &restrict dstName) -{ -#if defined __native_client__ - FILE *srcFile = fopen(srcName.c_str(), "rb"); - if (srcFile == nullptr) - return -1; - FILE *dstFile = fopen(dstName.c_str(), "w+b"); - if (dstFile == nullptr) - { - fclose(srcFile); - return -1; - } - - const int chunkSize = 500000; - char *buf = new char[chunkSize]; - size_t sz = 0; - while ((sz = fread(buf, 1, chunkSize, srcFile))) - { - if (fwrite(buf, 1, sz, dstFile) != sz) - { - delete [] buf; - fclose(srcFile); - fclose(dstFile); - ::remove(dstName.c_str()); - return -1; - } - } - - delete [] buf; - fclose(srcFile); - fclose(dstFile); - if (!::remove(srcName.c_str())) - return 0; - - return -1; -#else // defined __native_client__ - - return ::rename(srcName.c_str(), dstName.c_str()); -#endif // defined __native_client__ -} - -int Files::copyFile(const std::string &restrict srcName, - const std::string &restrict dstName) -{ - FILE *srcFile = fopen(srcName.c_str(), "rb"); - if (srcFile == nullptr) - return -1; - FILE *dstFile = fopen(dstName.c_str(), "w+b"); - if (dstFile == nullptr) - { - fclose(srcFile); - return -1; - } - - const int chunkSize = 500000; - char *buf = new char[chunkSize]; - size_t sz = 0; - while ((sz = fread(buf, 1, chunkSize, srcFile))) - { - if (fwrite(buf, 1, sz, dstFile) != sz) - { - delete [] buf; - fclose(srcFile); - fclose(dstFile); - return -1; - } - } - - delete [] buf; - fclose(srcFile); - fclose(dstFile); - return 0; -} - -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; -} - -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 += "/"; - const struct dirent *next_file = nullptr; - DIR *const dir = opendir(path.c_str()); - - if (dir) - { - while ((next_file = readdir(dir))) - { - const std::string file = next_file->d_name; - if (file != "." && file != "..") - remove((path + file).c_str()); - } - closedir(dir); - } -} diff --git a/src/utils/files.h b/src/utils/files.h deleted file mode 100644 index b91f7e9ab..000000000 --- a/src/utils/files.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2013-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef UTILS_FILES_H -#define UTILS_FILES_H - -#include "utils/stringvector.h" - -#include "localconsts.h" - -namespace Files -{ -#ifdef ANDROID - void extractLocale(); -#endif // ANDROID - -#if defined(ANDROID) || defined(__native_client__) - typedef void (*CopyFileCallbackPtr) (int cnt); - - void setCopyCallBack(CopyFileCallbackPtr callback); - - void copyPhysFsFile(const std::string &restrict inFile, - const std::string &restrict outFile); - - void copyPhysFsDir(const std::string &restrict inDir, - const std::string &restrict outDir); - - void extractZip(const std::string &restrict zipName, - const std::string &restrict inDir, - const std::string &restrict outDir); -#endif // ANDROID __native_client__ - - int renameFile(const std::string &restrict pFrom, - const std::string &restrict pTo); - - int copyFile(const std::string &restrict pFrom, - const std::string &restrict pTo); - - bool existsLocal(const std::string &path); - - bool loadTextFileLocal(const std::string &fileName, - StringVect &lines); - - void saveTextFile(std::string path, - const std::string &restrict name, - const std::string &restrict text); - - void deleteFilesInDirectory(std::string path); -} // namespace Files - -#endif // UTILS_FILES_H diff --git a/src/utils/files_unittest.cc b/src/utils/files_unittest.cc deleted file mode 100644 index 1012221f2..000000000 --- a/src/utils/files_unittest.cc +++ /dev/null @@ -1,167 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2013-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "utils/files.h" - -#include "catch.hpp" -#include "logger.h" - -#include "fs/virtfs.h" -#include "fs/virtfstools.h" - -#include "utils/delete2.h" - -#include "resources/resourcemanager/resourcemanager.h" - -#include "debug.h" - -TEST_CASE("Files renameFile") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - - const int sz = 1234567; - char *buf = new char[sz]; - for (int f = 0; f < sz; f ++) - buf[f] = f; - - const std::string name1 = "file1.test"; - const std::string name2 = "file2.test"; - FILE *file = fopen(name1.c_str(), "w+b"); - fwrite(buf, 1, sz, file); - fclose(file); - - REQUIRE(0 == Files::renameFile(name1, name2)); - char *buf2 = new char[sz]; - FILE *file2 = fopen(name2.c_str(), "rb"); - REQUIRE_FALSE(nullptr == file2); - fread(buf2, 1, sz, file2); - fclose(file2); - ::remove(name1.c_str()); - ::remove(name2.c_str()); - - for (int f = 0; f < sz; f ++) - REQUIRE(buf[f] == buf2[f]); - - delete [] buf; - delete [] buf2; - ResourceManager::deleteInstance(); - delete2(logger); -// VirtFs::deinit(); -} - -TEST_CASE("Files existsLocal") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - 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(); - delete2(logger); -// VirtFs::deinit(); -} - -TEST_CASE("Files loadTextFileString") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") == - "this is test \nfile."); - ResourceManager::deleteInstance(); - delete2(logger); -// VirtFs::deinit(); -} - -TEST_CASE("Files loadTextFile") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - - StringVect lines; - VirtFs::loadTextFile("test/simplefile.txt", lines); - REQUIRE(lines.size() == 2); - REQUIRE(lines[0] == "this is test "); - REQUIRE(lines[1] == "file."); - ResourceManager::deleteInstance(); - delete2(logger); -// VirtFs::deinit(); -} - -TEST_CASE("Files saveTextFile") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - - const std::string dir = VirtFs::getPath("test"); - REQUIRE(dir.size() > 0); - Files::saveTextFile(dir, "tempfile.txt", "test line\ntext line2"); - std::string data = VirtFs::loadTextFileString("test/tempfile.txt"); - ::remove((dir + "/tempfile.txt").c_str()); - REQUIRE(data == "test line\ntext line2\n"); - ResourceManager::deleteInstance(); - delete2(logger); -// VirtFs::deinit(); -} - -TEST_CASE("Files getFilesInDir") -{ - dirSeparator = "/"; - logger = new Logger(); - ResourceManager::init(); - VirtFs::addDirToSearchPath("data", Append_false); - VirtFs::addDirToSearchPath("../data", Append_false); - - StringVect list; - VirtFs::getFilesInDir("test", - list, - ".gpl"); - REQUIRE(list.size() == 1); - REQUIRE(list[0] == "test/palette.gpl"); - - list.clear(); - VirtFs::getFilesInDir("perserver/default", - list, - ".xml"); - REQUIRE(list.size() == 5); - REQUIRE(list[0] == "perserver/default/charcreation.xml"); - REQUIRE(list[1] == "perserver/default/deadmessages.xml"); - REQUIRE(list[2] == "perserver/default/defaultcommands.xml"); - REQUIRE(list[3] == "perserver/default/features.xml"); - REQUIRE(list[4] == "perserver/default/weapons.xml"); - ResourceManager::deleteInstance(); - delete2(logger); -// VirtFs::deinit(); -} diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp deleted file mode 100644 index a3cd52dc0..000000000 --- a/src/utils/mkdir.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "utils/mkdir.h" - -#if defined WIN32 -#include -#include -#endif // defined WIN32 - -#include - -#include "debug.h" - -#if defined WIN32 -int mkdir_r(const char *const pathname) -{ - if (!pathname) - return -1; - - char tmp[PATH_MAX]; - char tmp2[PATH_MAX]; - char *p; - - if (strlen(pathname) >= PATH_MAX - 2) - return -1; - - strncpy(tmp, pathname, sizeof(tmp) - 1); - tmp[PATH_MAX - 1] = '\0'; - - const int len = CAST_S32(strlen(tmp)); - - if (len < 1 || len >= INT_MAX) - return -1; - - // terminate the pathname with '/' - if (tmp[len - 1] != '/') - { - tmp[len] = '/'; - tmp[len + 1] = '\0'; - } - - for (p = tmp; *p; p++) - { - if (*p == '/' || *p == '\\') - { - *p = '\0'; - // ignore a slash at the beginning of a path - if (tmp[0] == 0) - { - *p = '/'; - continue; - } - - strcpy(tmp2, tmp); - char *p2 = tmp2 + strlen(tmp2) - 1; - if (*p2 == '/' || *p2 == '\\') - *p2 = 0; - // check if the name already exists, but not as directory - struct stat statbuf; - if (!stat(tmp2, &statbuf)) - { - if (S_ISDIR(statbuf.st_mode)) - { - *p = '/'; - continue; - } - else - return -1; - } - - if (!CreateDirectory(tmp2, nullptr)) - { - // hack, hack. just assume that x: might be a drive - // letter, and try again - if (!(strlen(tmp2) == 2 && !strcmp(tmp2 + 1, ":"))) - return -1; - } - - *p = '/'; - } - } - return 0; -} -#else // WIN32 - -/// Create a directory, making leading components first if necessary -int mkdir_r(const char *const pathname) -{ - if (!pathname) - return -1; - - const size_t len = CAST_SIZE(strlen(pathname)); - char *tmp = new char[len + 2]; - char *p = nullptr; - - strcpy(tmp, pathname); - - // terminate the pathname with '/' - if (tmp[len - 1] != '/') - { - tmp[len] = '/'; - tmp[len + 1] = '\0'; - } - - for (p = tmp; *p; p++) - { - if (*p == '/') - { - *p = '\0'; - // ignore a slash at the beginning of a path - if (tmp[0] == 0) - { - *p = '/'; - continue; - } - - // check if the name already exists, but not as directory - struct stat statbuf; - if (!stat(tmp, &statbuf)) - { - if (S_ISDIR(statbuf.st_mode)) - { - *p = '/'; - continue; - } - else - { - delete []tmp; - return -1; - } - } - - if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) - { - delete []tmp; - return -1; - } - - *p = '/'; - } - } - delete []tmp; - return 0; -} -#endif // WIN32 diff --git a/src/utils/mkdir.h b/src/utils/mkdir.h deleted file mode 100644 index a662daca3..000000000 --- a/src/utils/mkdir.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef UTILS_MKDIR_H -#define UTILS_MKDIR_H - -int mkdir_r(const char *const pathname); - -#endif // UTILS_MKDIR_H diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp deleted file mode 100644 index 3e5bf8829..000000000 --- a/src/utils/paths.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifdef _MSC_VER -# include "msvc/config.h" -#elif defined(HAVE_CONFIG_H) -#include "config.h" -#endif // _MSC_VER - -#include "fs/virtfs.h" - -#include "utils/paths.h" - -#include "utils/stringutils.h" - -#ifdef USE_X11 -#include "utils/files.h" -#endif // USE_X11 - -#ifdef __native_client__ -#include -#define realpath(N, R) strcpy(R, N) -#endif // __native_client__ - -#ifdef WIN32 -#include "utils/specialfolder.h" -#define realpath(N, R) _fullpath((R), (N), _MAX_PATH) -#elif defined __OpenBSD__ -#include -#elif defined __native_client__ -#include -#endif // WIN32 - -#ifdef ANDROID -#ifdef USE_SDL2 -#include -#endif // USE_SDL2 -#endif // ANDROID - -#include "debug.h" - -namespace -{ - std::string mPackageDir; -} // namespace - -std::string getRealPath(const std::string &str) -{ -#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__native_client__) - char *realPath = reinterpret_cast(calloc(PATH_MAX, sizeof(char))); - if (!realPath) - return ""; - realpath(str.c_str(), realPath); -#else // defined(__OpenBSD__) || defined(__ANDROID__) || - // defined(__native_client__) - - char *realPath = realpath(str.c_str(), nullptr); -#endif // defined(__OpenBSD__) || defined(__ANDROID__) || - // defined(__native_client__) - - if (!realPath) - return ""; - std::string path = realPath; - free(realPath); - return path; -} - -bool isRealPath(const std::string &str) -{ - return str == getRealPath(str); -} - -bool checkPath(const std::string &path) -{ - if (path.empty()) - return true; - return path.find("../") == std::string::npos - && path.find("..\\") == std::string::npos - && path.find("/..") == std::string::npos - && path.find("\\..") == std::string::npos; -} - -std::string &fixDirSeparators(std::string &str) -{ - if (dirSeparator[0] == '/') - return str; - - return replaceAll(str, "/", "\\"); -} - -std::string removeLast(const std::string &str) -{ - size_t pos2 = str.rfind('/'); - const size_t pos3 = str.rfind('\\'); - if (pos3 != std::string::npos) - { - if (pos2 == std::string::npos || pos3 > pos2) - pos2 = pos3; - } - if (pos2 != std::string::npos) - return str.substr(0, pos2); - else - return str; -} - -#ifdef WIN32 -std::string getSelfName() -{ - return "manaplus.exe"; -} - -#elif defined(__APPLE__) -std::string getSelfName() -{ - return "manaplus.exe"; -} - -#elif defined __linux__ || defined __linux -#include - -std::string getSelfName() -{ - char buf[257]; - const ssize_t sz = readlink("/proc/self/exe", buf, 256); - if (sz > 0 && sz < 256) - { - buf[sz] = 0; - return buf; - } - else - { - return ""; - } -} - -#else // WIN32 - -std::string getSelfName() -{ - return ""; -} - -#endif // WIN32 - -std::string getPicturesDir() -{ -#ifdef WIN32 - std::string dir = getSpecialFolderLocation(CSIDL_MYPICTURES); - if (dir.empty()) - dir = getSpecialFolderLocation(CSIDL_DESKTOP); - return dir; -#elif defined USE_X11 - char *xdg = getenv("XDG_CONFIG_HOME"); - std::string file; - if (!xdg) - { - file = std::string(VirtFs::getUserDir()).append( - "/.config/user-dirs.dirs"); - } - else - { - file = std::string(xdg).append("/user-dirs.dirs"); - } - - StringVect arr; - Files::loadTextFileLocal(file, arr); - FOR_EACH (StringVectCIter, it, arr) - { - std::string str = *it; - if (findCutFirst(str, "XDG_PICTURES_DIR=\"")) - { - str = str.substr(0, str.size() - 1); - // use hack to replace $HOME var. - // if in string other vars, fallback to default path - replaceAll(str, "$HOME/", VirtFs::getUserDir()); - str = getRealPath(str); - if (str.empty()) - str = std::string(VirtFs::getUserDir()).append("Desktop"); - return str; - } - } - - return std::string(VirtFs::getUserDir()).append("Desktop"); -#else // WIN32 - - return std::string(VirtFs::getUserDir()).append("Desktop"); -#endif // WIN32 -} - -#ifdef ANDROID -std::string getSdStoragePath() -{ - return getenv("DATADIR2"); -} -#endif // ANDROID - -std::string getPackageDir() -{ - return mPackageDir; -} - -void setPackageDir(const std::string &dir) -{ - mPackageDir = dir; -} diff --git a/src/utils/paths.h b/src/utils/paths.h deleted file mode 100644 index fc90730f3..000000000 --- a/src/utils/paths.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef UTILS_PATHS_H -#define UTILS_PATHS_H - -#include - -#include "localconsts.h" - -std::string getRealPath(const std::string &str) A_WARN_UNUSED; - -bool isRealPath(const std::string &str) A_WARN_UNUSED; - -bool checkPath(const std::string &path) A_WARN_UNUSED; - -std::string &fixDirSeparators(std::string &str); - -std::string removeLast(const std::string &str) A_WARN_UNUSED; - -std::string getSelfName() A_WARN_UNUSED; - -std::string getPicturesDir() A_WARN_UNUSED; - -#ifdef ANDROID -std::string getSdStoragePath() A_WARN_UNUSED; -#endif // ANDROID - -std::string getPackageDir() A_WARN_UNUSED; - -void setPackageDir(const std::string &dir); - -#endif // UTILS_PATHS_H diff --git a/src/utils/specialfolder.cpp b/src/utils/specialfolder.cpp deleted file mode 100644 index a6b748c3c..000000000 --- a/src/utils/specialfolder.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifdef WIN32 -#include "utils/specialfolder.h" -#include - -#include "debug.h" - -/* - * Retrieve the pathname of special folders on win32, or an empty string - * on error / if the folder does not exist. - * See http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx for - * a list of folder ids - */ -std::string getSpecialFolderLocation(const int folderId) -{ - std::string ret; - LPITEMIDLIST pItemIdList; - LPMALLOC pMalloc; - char szPath[_MAX_PATH]; - - // get the item ID list for folderId - HRESULT hr = SHGetSpecialFolderLocation(nullptr, folderId, &pItemIdList); - if (hr != S_OK) - return ret; - - // convert the ID list into a file system path - if (SHGetPathFromIDList(pItemIdList, szPath) == FALSE) - return ret; - - // get the IMalloc pointer and free all resources we used - hr = SHGetMalloc(&pMalloc); - pMalloc->Free(pItemIdList); - pMalloc->Release(); - - ret = szPath; - return ret; -} - -#endif // WIN32 diff --git a/src/utils/specialfolder.h b/src/utils/specialfolder.h deleted file mode 100644 index f2cc93c9b..000000000 --- a/src/utils/specialfolder.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011-2017 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef UTILS_SPECIALFOLDER_H -#define UTILS_SPECIALFOLDER_H - -#ifdef WIN32 -#include -#include - -#include "localconsts.h" -std::string getSpecialFolderLocation(const int folderId) A_WARN_UNUSED; -#endif // WIN32 - -#endif // UTILS_SPECIALFOLDER_H -- cgit v1.2.3-70-g09d2