diff --git a/include/meson.build b/include/meson.build index 6c0e46cc6..d19b159eb 100644 --- a/include/meson.build +++ b/include/meson.build @@ -13,19 +13,8 @@ headers = [ 'search_renderer.h', 'server.h', 'kiwixserve.h', - 'name_mapper.h' + 'name_mapper.h', + 'tools.h' ] install_headers(headers, subdir:'kiwix') - -install_headers( - 'tools/base64.h', - 'tools/networkTools.h', - 'tools/otherTools.h', - 'tools/pathTools.h', - 'tools/regexTools.h', - 'tools/stringTools.h', - 'tools/archiveTools.h', - subdir:'kiwix/tools' -) - diff --git a/include/opds_dumper.h b/include/opds_dumper.h index 230d094a1..db6026ef8 100644 --- a/include/opds_dumper.h +++ b/include/opds_dumper.h @@ -26,9 +26,6 @@ #include -#include "tools/base64.h" -#include "tools/pathTools.h" -#include "tools/regexTools.h" #include "library.h" #include "reader.h" diff --git a/include/reader.h b/include/reader.h index d288338b2..9630252c2 100644 --- a/include/reader.h +++ b/include/reader.h @@ -29,8 +29,6 @@ #include #include "common.h" #include "entry.h" -#include "tools/pathTools.h" -#include "tools/stringTools.h" using namespace std; @@ -47,20 +45,20 @@ class SuggestionItem // Temporarily making the constructor public until the code move is complete public: // Create a sugggestion item. - explicit SuggestionItem(std::string title, std::string normalizedTitle, - std::string path, std::string snippet = "") : + explicit SuggestionItem(const std::string& title, const std::string& normalizedTitle, + const std::string& path, const std::string& snippet = "") : title(title), normalizedTitle(normalizedTitle), path(path), snippet(snippet) {} public: - const std::string getTitle() {return title;} - const std::string getNormalizedTitle() {return normalizedTitle;} - const std::string getPath() {return path;} - const std::string getSnippet() {return snippet;} + const std::string& getTitle() const { return title;} + const std::string& getNormalizedTitle() const { return normalizedTitle;} + const std::string& getPath() const { return path;} + const std::string& getSnippet() const { return snippet;} - const bool hasSnippet() {return !snippet.empty();} + bool hasSnippet() const { return !snippet.empty();} // Data private: diff --git a/include/searcher.h b/include/searcher.h index 8c72953b4..f9cdb4a4c 100644 --- a/include/searcher.h +++ b/include/searcher.h @@ -29,8 +29,6 @@ #include #include #include -#include "tools/pathTools.h" -#include "tools/stringTools.h" #include diff --git a/include/tools.h b/include/tools.h new file mode 100644 index 000000000..150348929 --- /dev/null +++ b/include/tools.h @@ -0,0 +1,171 @@ +/* + * Copyright 2021 Matthieu Gautier + * + * 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 3 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef KIWIX_TOOLS_H +#define KIWIX_TOOLS_H + +#include +#include + +namespace kiwix { + +/** + * Return the current directory. + * + * @return the current directory (utf8 encoded) + */ +std::string getCurrentDirectory(); + +/** + * Return the data directory. + * + * The data directory is a directory where to put data (zim files, ...) + * It depends of the platform and it may be changed by user using environment variable. + * + * The resolution order is : + * - `KIWIX_DATA_DIR` env variable (if set). + * - On Windows : + * . `$APPDATA/kiwix` if $APPDATA is set + * . `$USERPROFILE/kiwix` if $USERPROFILE is set + * - Else : + * . `$XDG_DATA_HOME/kiwix`if $XDG_DATA_HOME is set + * . `$HOME/.local/share/kiwx` if $HOWE is set + * - current directory + * + * @return the path of the data directory (utf8 encoded) + */ +std::string getDataDirectory(); + +/** Return the path of the executable + * + * Some application may be packaged in auto extractible archive (Appimage) and the + * real executable is different of the path of the archive. + * If `realPathOnly` is true, return the path of the real executable instead of the + * archive launched by the user. + * + * @param realPathOnly If we must return the real path of the executable. + * @return the path of the executable (utf8 encoded) + */ +std::string getExecutablePath(bool realPathOnly = false); + +/** Tell if the path is a relative path. + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param path A utf8 encoded path. + * @return true if the path is relative. + */ +bool isRelativePath(const std::string& path); + +/** Append a path to another one. + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param basePath the base path. + * @param relativePath a path to add to the base path, must be a relative path. + * @return The concatenation of the paths, using the right separator. + */ +std::string appendToDirectory(const std::string& basePath, const std::string& relativePath); + +/** Remove the last element of a path. + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param path a path. + * @return The parent directory (or empty string if none). + */ +std::string removeLastPathElement(const std::string& path); + +/** Get the last element of a path. + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param path a path. + * @return The base name of the path or empty string if none (ending with a separator). + */ +std::string getLastPathElement(const std::string& path); + +/** Compute the absolute path of a relative path based on another one + * + * Equivalent to appendToDirectory followed by a normalization of the path. + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param path the base path (if empty, current directory is taken). + * @param relativePath the relative path. + * @return a absolute path. + */ +std::string computeAbsolutePath(const std::string& path, const std::string& relativePath); + +/** Compute the relative path of a path relative to another one + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate paths. + * + * @param path the base path. + * @param absolutePath the absolute path to find the relative path for. + * @return a relative path (pointing to absolutePath, relative to path). + */ +std::string computeRelativePath(const std::string& path, const std::string& absolutePath); + +/** Sleep the current thread. + * + * This function is provided as a small helper. It is probably better to use native tools. + * + * @param milliseconds The number of milliseconds to wait for. + */ +void sleep(unsigned int milliseconds); + +/** Split a string + * + * This function is provided as a small helper. It is probably better to use native tools. + * + * Assuming text = "foo:;bar;baz,oups;" + * + * split(text, ":;", true, true) => ["foo", ":", ";", "bar", ";", "baz,oups", ";"] + * split(text, ":;", true, false) => ["foo", "bar", "baz,oups"] (default) + * split(text, ":;", false, true) => ["foo", ":", "", ";", "bar", ";", "baz,oups", ";", ""] + * split(text, ":;", false, false) => ["foo", "", "bar", "baz,oups", ""] + * + * @param str The string to split. + * @param delims A string of potential delimiters. + * Each charater in the string can be a individual delimiters. + * @param dropEmpty true if empty part must be dropped from the result. + * @param keepDelim true if delimiter must be included from the result. + * @return a list of part (potentially containing delimiters) + */ +std::vector split(const std::string& str, const std::string& delims, bool dropEmpty=true, bool keepDelim = false); + +/** Convert language code from iso2 code to iso3 + * + * This function is provided as a small helper. It is probably better to use native tools + * to manipulate locales. + * + * @param a2code a iso2 code string. + * @return the corresponding iso3 code. + * @throw std::out_of_range if iso2 code is not known. + */ +std::string converta2toa3(const std::string& a2code); +} +#endif // KIWIX_TOOLS_H diff --git a/src/aria2.cpp b/src/aria2.cpp index e61bf7e1e..2bc163d83 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -6,10 +6,10 @@ #include #include #include -#include -#include -#include -#include // For AriaError +#include "tools.h" +#include "tools/pathTools.h" +#include "tools/stringTools.h" +#include "downloader.h" // For AriaError #ifdef _WIN32 # define ARIA2_CMD "aria2c.exe" diff --git a/src/book.cpp b/src/book.cpp index 02c82a735..4b2111271 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -20,10 +20,13 @@ #include "book.h" #include "reader.h" +#include "tools.h" #include "tools/base64.h" #include "tools/regexTools.h" #include "tools/networkTools.h" #include "tools/otherTools.h" +#include "tools/stringTools.h" +#include "tools/pathTools.h" #include diff --git a/src/kiwixserve.cpp b/src/kiwixserve.cpp index 639008e7a..9c55e0583 100644 --- a/src/kiwixserve.cpp +++ b/src/kiwixserve.cpp @@ -9,6 +9,7 @@ # include #endif +#include "tools.h" #include "tools/pathTools.h" #include "tools/stringTools.h" diff --git a/src/library.cpp b/src/library.cpp index c2e324ebc..3a0f0cd53 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -22,6 +22,7 @@ #include "reader.h" #include "libxml_dumper.h" +#include "tools.h" #include "tools/base64.h" #include "tools/regexTools.h" #include "tools/pathTools.h" diff --git a/src/libxml_dumper.cpp b/src/libxml_dumper.cpp index cb5892767..5a681e861 100644 --- a/src/libxml_dumper.cpp +++ b/src/libxml_dumper.cpp @@ -20,10 +20,10 @@ #include "libxml_dumper.h" #include "book.h" +#include "tools.h" #include "tools/base64.h" #include "tools/stringTools.h" #include "tools/otherTools.h" -#include "tools/pathTools.h" namespace kiwix { diff --git a/src/manager.cpp b/src/manager.cpp index 3e59cac27..c632744b2 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -19,6 +19,7 @@ #include "manager.h" +#include "tools.h" #include "tools/pathTools.h" #include diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 9d6fdfd5e..8426a34d1 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -20,11 +20,12 @@ #include "opds_dumper.h" #include "book.h" -#include "tools/otherTools.h" - #include "kiwixlib-resources.h" #include +#include "tools/stringTools.h" +#include "tools/otherTools.h" + namespace kiwix { diff --git a/src/reader.cpp b/src/reader.cpp index 20a341731..44cb312a6 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -24,6 +24,8 @@ #include #include +#include "tools.h" +#include "tools/stringTools.h" #include "tools/otherTools.h" #include "tools/archiveTools.h" diff --git a/src/search_renderer.cpp b/src/search_renderer.cpp index 66bb3b429..f3b2ac14b 100644 --- a/src/search_renderer.cpp +++ b/src/search_renderer.cpp @@ -30,7 +30,7 @@ #include #include "kiwixlib-resources.h" - +#include "tools/stringTools.h" namespace kiwix { @@ -155,4 +155,4 @@ std::string SearchRenderer::getHtml() return ss.str(); } -} \ No newline at end of file +} diff --git a/src/searcher.cpp b/src/searcher.cpp index e8b41caa1..56ffb3d4e 100644 --- a/src/searcher.cpp +++ b/src/searcher.cpp @@ -18,7 +18,6 @@ */ -#include #include "searcher.h" #include "reader.h" @@ -26,6 +25,8 @@ #include #include +#include +#include "tools/stringTools.h" #include "kiwixlib-resources.h" #define MAX_SEARCH_LEN 140 diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 0e3545028..689ba7732 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -43,7 +43,7 @@ extern "C" { #include "microhttpd_wrapper.h" } -#include "tools/otherTools.h" +#include "tools.h" #include "tools/pathTools.h" #include "tools/regexTools.h" #include "tools/stringTools.h" diff --git a/src/tools/archiveTools.cpp b/src/tools/archiveTools.cpp index 3608357ab..5d22b8ef2 100644 --- a/src/tools/archiveTools.cpp +++ b/src/tools/archiveTools.cpp @@ -17,10 +17,12 @@ * MA 02110-1301, USA. */ -#include -#include -#include -#include +#include "archiveTools.h" + +#include "tools.h" +#include "pathTools.h" +#include "otherTools.h" +#include "stringTools.h" #include #include diff --git a/include/tools/archiveTools.h b/src/tools/archiveTools.h similarity index 100% rename from include/tools/archiveTools.h rename to src/tools/archiveTools.h diff --git a/include/tools/base64.h b/src/tools/base64.h similarity index 100% rename from include/tools/base64.h rename to src/tools/base64.h diff --git a/include/tools/networkTools.h b/src/tools/networkTools.h similarity index 100% rename from include/tools/networkTools.h rename to src/tools/networkTools.h diff --git a/src/tools/otherTools.cpp b/src/tools/otherTools.cpp index eb081efb7..1afcc7d7b 100644 --- a/src/tools/otherTools.cpp +++ b/src/tools/otherTools.cpp @@ -17,7 +17,11 @@ * MA 02110-1301, USA. */ +// Implement function declared in tools.h and tools/otherTools.h +#include "tools.h" #include "tools/otherTools.h" + + #include #include diff --git a/include/tools/otherTools.h b/src/tools/otherTools.h similarity index 94% rename from include/tools/otherTools.h rename to src/tools/otherTools.h index c105d52f5..219332cdb 100644 --- a/include/tools/otherTools.h +++ b/src/tools/otherTools.h @@ -32,9 +32,7 @@ namespace pugi { namespace kiwix { - void sleep(unsigned int milliseconds); std::string nodeToString(const pugi::xml_node& node); - std::string converta2toa3(const std::string& a2code); /* * Convert all format tag string to new format diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index e7817b8e6..d66aa6f8b 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -17,7 +17,10 @@ * MA 02110-1301, USA. */ +// Implement method defined in and "tools/pathTools.h" +#include "tools.h" #include "tools/pathTools.h" + #include #ifdef __APPLE__ @@ -59,7 +62,6 @@ #define PATH_MAX 1024 #endif - #ifdef _WIN32 std::string WideToUtf8(const std::wstring& wstr) { @@ -78,7 +80,7 @@ std::wstring Utf8ToWide(const std::string& str) } #endif -bool isRelativePath(const std::string& path) +bool kiwix::isRelativePath(const std::string& path) { #ifdef _WIN32 if (path.size() < 3 ) { @@ -173,7 +175,7 @@ std::vector normalizeParts(std::vector& parts, bool ab return ret; } -std::string computeRelativePath(const std::string& path, const std::string& absolutePath) +std::string kiwix::computeRelativePath(const std::string& path, const std::string& absolutePath) { auto parts = kiwix::split(path, SEPARATOR, false); auto pathParts = normalizeParts(parts, false); @@ -198,11 +200,11 @@ std::string computeRelativePath(const std::string& path, const std::string& abso return ret; } -std::string computeAbsolutePath(const std::string& path, const std::string& relativePath) +std::string kiwix::computeAbsolutePath(const std::string& path, const std::string& relativePath) { std::string absolutePath = path; if (path.empty()) { - absolutePath = getCurrentDirectory(); + absolutePath = kiwix::getCurrentDirectory(); } auto parts = kiwix::split(absolutePath, SEPARATOR, false); @@ -215,7 +217,7 @@ std::string computeAbsolutePath(const std::string& path, const std::string& rela return ret; } -std::string removeLastPathElement(const std::string& path) +std::string kiwix::removeLastPathElement(const std::string& path) { auto parts_ = kiwix::split(path, SEPARATOR, false); auto parts = normalizeParts(parts_, false); @@ -226,7 +228,7 @@ std::string removeLastPathElement(const std::string& path) return ret; } -std::string appendToDirectory(const std::string& directoryPath, const std::string& filename) +std::string kiwix::appendToDirectory(const std::string& directoryPath, const std::string& filename) { std::string newPath = directoryPath; if (!directoryPath.empty() && directoryPath.back() != SEPARATOR[0]) { @@ -236,7 +238,7 @@ std::string appendToDirectory(const std::string& directoryPath, const std::strin return newPath; } -std::string getLastPathElement(const std::string& path) +std::string kiwix::getLastPathElement(const std::string& path) { auto parts_ = kiwix::split(path, SEPARATOR); auto parts = normalizeParts(parts_, false); @@ -366,7 +368,7 @@ bool copyFile(const std::string& sourcePath, const std::string& destPath) #endif } -std::string getExecutablePath(bool realPathOnly) +std::string kiwix::getExecutablePath(bool realPathOnly) { if (!realPathOnly) { char* cAppImage = ::getenv("APPIMAGE"); @@ -420,7 +422,7 @@ bool writeTextFile(const std::string& path, const std::string& content) return true; } -std::string getCurrentDirectory() +std::string kiwix::getCurrentDirectory() { #ifdef _WIN32 wchar_t* a_cwd = _wgetcwd(NULL, 0); @@ -434,7 +436,7 @@ std::string getCurrentDirectory() return ret; } -std::string getDataDirectory() +std::string kiwix::getDataDirectory() { // Try to get the dataDir from the `KIWIX_DATA_DIR` env var #ifdef _WIN32 @@ -524,4 +526,3 @@ std::string getMimeTypeForFile(const std::string& filename) return mimeType; } - diff --git a/include/tools/pathTools.h b/src/tools/pathTools.h similarity index 72% rename from include/tools/pathTools.h rename to src/tools/pathTools.h index 37ab6fd2b..d8450b83f 100644 --- a/include/tools/pathTools.h +++ b/src/tools/pathTools.h @@ -26,11 +26,6 @@ std::string WideToUtf8(const std::wstring& wstr); std::wstring Utf8ToWide(const std::string& str); #endif -bool isRelativePath(const std::string& path); -std::string computeAbsolutePath(const std::string& path, const std::string& relativePath); -std::string computeRelativePath(const std::string& path, const std::string& absolutePath); -std::string removeLastPathElement(const std::string& path); -std::string appendToDirectory(const std::string& directoryPath, const std::string& filename); unsigned int getFileSize(const std::string& path); std::string getFileSizeAsString(const std::string& path); @@ -39,10 +34,8 @@ bool fileExists(const std::string& path); bool makeDirectory(const std::string& path); std::string makeTmpDirectory(); bool copyFile(const std::string& sourcePath, const std::string& destPath); -std::string getLastPathElement(const std::string& path); -std::string getExecutablePath(bool realPathOnly = false); -std::string getCurrentDirectory(); -std::string getDataDirectory(); bool writeTextFile(const std::string& path, const std::string& content); std::string getMimeTypeForFile(const std::string& filename); + #endif + diff --git a/include/tools/regexTools.h b/src/tools/regexTools.h similarity index 100% rename from include/tools/regexTools.h rename to src/tools/regexTools.h diff --git a/src/tools/stringTools.cpp b/src/tools/stringTools.cpp index 55efc07f0..6d6e1d39e 100644 --- a/src/tools/stringTools.cpp +++ b/src/tools/stringTools.cpp @@ -17,9 +17,11 @@ * MA 02110-1301, USA. */ -#include +// Implement function declared in tools.h and tools/stringTools.h +#include "tools.h" +#include "tools/stringTools.h" -#include +#include "tools/pathTools.h" #include #include #include @@ -268,7 +270,7 @@ std::string kiwix::urlDecode(const std::string& value, bool component) /* Split string in a token array */ std::vector kiwix::split(const std::string& str, const std::string& delims, - bool trimEmpty, + bool dropEmpty, bool keepDelim) { std::string::size_type lastPos = 0; @@ -277,7 +279,7 @@ std::vector kiwix::split(const std::string& str, while( (pos = str.find_first_of(delims, lastPos)) < str.length() ) { auto token = str.substr(lastPos, pos - lastPos); - if (!trimEmpty || !token.empty()) { + if (!dropEmpty || !token.empty()) { tokens.push_back(token); } if (keepDelim) { @@ -287,7 +289,7 @@ std::vector kiwix::split(const std::string& str, } auto token = str.substr(lastPos); - if (!trimEmpty || !token.empty()) { + if (!dropEmpty || !token.empty()) { tokens.push_back(token); } return tokens; diff --git a/include/tools/stringTools.h b/src/tools/stringTools.h similarity index 94% rename from include/tools/stringTools.h rename to src/tools/stringTools.h index 6793c7f20..9b5cf3622 100644 --- a/include/tools/stringTools.h +++ b/src/tools/stringTools.h @@ -43,7 +43,6 @@ void loadICUExternalTables(); std::string urlEncode(const std::string& value, bool encodeReserved = false); std::string urlDecode(const std::string& value, bool component = false); -std::vector split(const std::string& str, const std::string& delims, bool trimEmpty = true, bool keepDelim = false); std::string join(const std::vector& list, const std::string& sep); std::string ucAll(const std::string& word); diff --git a/test/manager.cpp b/test/manager.cpp index c2f8f6103..09647c766 100644 --- a/test/manager.cpp +++ b/test/manager.cpp @@ -2,6 +2,7 @@ #include "../include/manager.h" #include "../include/library.h" #include "../include/book.h" +#include "../include/tools.h" #include #include @@ -13,13 +14,13 @@ TEST(ManagerTest, addBookFromPathAndGetIdTest) auto bookId = manager.addBookFromPathAndGetId("./test/example.zim"); ASSERT_NE(bookId, ""); kiwix::Book book = lib.getBookById(bookId); - EXPECT_EQ(book.getPath(), computeAbsolutePath("", "./test/example.zim")); + EXPECT_EQ(book.getPath(), kiwix::computeAbsolutePath("", "./test/example.zim")); const std::string pathToSave = "./pathToSave"; const std::string url = "url"; bookId = manager.addBookFromPathAndGetId("./test/example.zim", pathToSave, url, true); book = lib.getBookById(bookId); - auto savedPath = computeAbsolutePath(removeLastPathElement(manager.writableLibraryPath), pathToSave); + auto savedPath = kiwix::computeAbsolutePath(kiwix::removeLastPathElement(manager.writableLibraryPath), pathToSave); EXPECT_EQ(book.getPath(), savedPath); EXPECT_EQ(book.getUrl(), url); } diff --git a/test/pathTools.cpp b/test/pathTools.cpp index 64079fcc5..9047e1873 100644 --- a/test/pathTools.cpp +++ b/test/pathTools.cpp @@ -20,7 +20,8 @@ #include "gtest/gtest.h" #include #include -#include "../include/tools/pathTools.h" +#include "../include/tools.h" +#include "../src/tools/pathTools.h" #ifdef _WIN32 # define S "\\" @@ -101,61 +102,61 @@ TEST(pathTools, normalizePartsRelative) TEST(pathTools, isRelativePath) { - ASSERT_TRUE(isRelativePath("foo")); - ASSERT_TRUE(isRelativePath(P2("foo","bar"))); - ASSERT_TRUE(isRelativePath(P3(".","foo","bar"))); - ASSERT_TRUE(isRelativePath(P2("..","foo"))); - ASSERT_TRUE(isRelativePath(P4("foo","","bar",""))); - ASSERT_FALSE(isRelativePath(A1("foo"))); - ASSERT_FALSE(isRelativePath(A2("foo", "bar"))); + ASSERT_TRUE(kiwix::isRelativePath("foo")); + ASSERT_TRUE(kiwix::isRelativePath(P2("foo","bar"))); + ASSERT_TRUE(kiwix::isRelativePath(P3(".","foo","bar"))); + ASSERT_TRUE(kiwix::isRelativePath(P2("..","foo"))); + ASSERT_TRUE(kiwix::isRelativePath(P4("foo","","bar",""))); + ASSERT_FALSE(kiwix::isRelativePath(A1("foo"))); + ASSERT_FALSE(kiwix::isRelativePath(A2("foo", "bar"))); #ifdef _WIN32 - ASSERT_FALSE(isRelativePath(P2(A_SAMBA, "foo"))); - ASSERT_FALSE(isRelativePath(P3(A_SAMBA, "foo", "bar"))); + ASSERT_FALSE(kiwix::isRelativePath(P2(A_SAMBA, "foo"))); + ASSERT_FALSE(kiwix::isRelativePath(P3(A_SAMBA, "foo", "bar"))); #endif } TEST(pathTools, computeAbsolutePath) { - ASSERT_EQ(computeAbsolutePath(A2("a","b"), "foo"), + ASSERT_EQ(kiwix::computeAbsolutePath(A2("a","b"), "foo"), A3("a","b","foo")); - ASSERT_EQ(computeAbsolutePath(A3("a","b",""), "foo"), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b",""), "foo"), A3("a","b","foo")); - ASSERT_EQ(computeAbsolutePath(A2("a","b"), P2(".","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A2("a","b"), P2(".","foo")), A3("a","b","foo")); - ASSERT_EQ(computeAbsolutePath(A2("a","b"), P2("..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A2("a","b"), P2("..","foo")), A2("a","foo")); - ASSERT_EQ(computeAbsolutePath(A3("a","b",""), P2("..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b",""), P2("..","foo")), A2("a","foo")); - ASSERT_EQ(computeAbsolutePath(A5("a","b","c","d","e"), P2("..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A5("a","b","c","d","e"), P2("..","foo")), A5("a","b","c","d","foo")); - ASSERT_EQ(computeAbsolutePath(A5("a","b","c","d","e"), P5("..","..","..","g","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A5("a","b","c","d","e"), P5("..","..","..","g","foo")), A4("a","b","g","foo")); #ifdef _WIN32 - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b",""), P2("..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b",""), P2("..","foo")), P3(A_SAMBA,"a","foo")); - ASSERT_EQ(computeAbsolutePath(P6(A_SAMBA,"a","b","c","d","e"), P5("..","..","..","g","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(P6(A_SAMBA,"a","b","c","d","e"), P5("..","..","..","g","foo")), P5(A_SAMBA,"a","b","g","foo")); #endif } TEST(pathTools, computeRelativePath) { - ASSERT_EQ(computeRelativePath(A2("a","b"), A3("a","b","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A2("a","b"), A3("a","b","foo")), "foo"); - ASSERT_EQ(computeRelativePath(A3("a","b",""), A3("a","b","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A3("a","b",""), A3("a","b","foo")), "foo"); - ASSERT_EQ(computeRelativePath(A2("a","b"), A2("a","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A2("a","b"), A2("a","foo")), P2("..","foo")); - ASSERT_EQ(computeRelativePath(A3("a","b",""), A2("a","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A3("a","b",""), A2("a","foo")), P2("..","foo")); - ASSERT_EQ(computeRelativePath(A5("a","b","c","d","e"), A5("a","b","c","d","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A5("a","b","c","d","e"), A5("a","b","c","d","foo")), P2("..","foo")); - ASSERT_EQ(computeRelativePath(A5("a","b","c","d","e"), A4("a","b","g","foo")), + ASSERT_EQ(kiwix::computeRelativePath(A5("a","b","c","d","e"), A4("a","b","g","foo")), P5("..","..","..","g","foo")); #ifdef _WIN32 - ASSERT_EQ(computeRelativePath(P3(A_SAMBA,"a","b"), P3(A_SAMBA,"a","foo")), + ASSERT_EQ(kiwix::computeRelativePath(P3(A_SAMBA,"a","b"), P3(A_SAMBA,"a","foo")), P2("..","foo")); - ASSERT_EQ(computeRelativePath(P6(A_SAMBA,"a","b","c","d","e"), P5(A_SAMBA,"a","b","g","foo")), + ASSERT_EQ(kiwix::computeRelativePath(P6(A_SAMBA,"a","b","c","d","e"), P5(A_SAMBA,"a","b","g","foo")), P5("..","..","..","g","foo")); #endif @@ -163,73 +164,73 @@ TEST(pathTools, computeRelativePath) TEST(pathTools, removeLastPathElement) { - ASSERT_EQ(removeLastPathElement(P3("a","b","c")), + ASSERT_EQ(kiwix::removeLastPathElement(P3("a","b","c")), P2("a","b")); - ASSERT_EQ(removeLastPathElement(A3("a","b","c")), + ASSERT_EQ(kiwix::removeLastPathElement(A3("a","b","c")), A2("a","b")); - ASSERT_EQ(removeLastPathElement(P4("a","b","c","")), + ASSERT_EQ(kiwix::removeLastPathElement(P4("a","b","c","")), P2("a","b")); - ASSERT_EQ(removeLastPathElement(A4("a","b","c","")), + ASSERT_EQ(kiwix::removeLastPathElement(A4("a","b","c","")), A2("a","b")); } TEST(pathTools, appendToDirectory) { - ASSERT_EQ(appendToDirectory(P3("a","b","c"), "foo.xml"), + ASSERT_EQ(kiwix::appendToDirectory(P3("a","b","c"), "foo.xml"), P4("a","b","c","foo.xml")); - ASSERT_EQ(appendToDirectory(P4("a","b","c",""), "foo.xml"), + ASSERT_EQ(kiwix::appendToDirectory(P4("a","b","c",""), "foo.xml"), P4("a","b","c","foo.xml")); - ASSERT_EQ(appendToDirectory(P3("a","b","c"), P2("d","foo.xml")), + ASSERT_EQ(kiwix::appendToDirectory(P3("a","b","c"), P2("d","foo.xml")), P5("a","b","c","d","foo.xml")); - ASSERT_EQ(appendToDirectory(P4("a","b","c",""), P2("d","foo.xml")), + ASSERT_EQ(kiwix::appendToDirectory(P4("a","b","c",""), P2("d","foo.xml")), P5("a","b","c","d","foo.xml")); - ASSERT_EQ(appendToDirectory(P3("a","b","c"), P2(".","foo.xml")), + ASSERT_EQ(kiwix::appendToDirectory(P3("a","b","c"), P2(".","foo.xml")), P5("a","b","c",".","foo.xml")); - ASSERT_EQ(appendToDirectory(P4("a","b","c",""), P2(".","foo.xml")), + ASSERT_EQ(kiwix::appendToDirectory(P4("a","b","c",""), P2(".","foo.xml")), P5("a","b","c",".","foo.xml")); } TEST(pathTools, goUp) { - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), ".."), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), ".."), A2("a", "b")); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P2("..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P2("..","..")), A1("a")); #ifdef _WIN32 - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P3("..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P3("..","..","..")), "c:"); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P4("..","..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P4("..","..","..","..")), "c:"); - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), ".."), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), ".."), P3(A_SAMBA,"a", "b")); - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P2("..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P2("..","..")), P2(A_SAMBA,"a")); - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P3("..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P3("..","..","..")), A_SAMBA); - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P4("..","..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P4("..","..","..","..")), A_SAMBA); #else - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P3("..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P3("..","..","..")), "/"); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P4("..","..","..","..")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P4("..","..","..","..")), "/"); #endif - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P2("..", "foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P2("..", "foo")), A3("a", "b","foo")); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P3("..","..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P3("..","..","foo")), A2("a","foo")); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P4("..","..","..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P4("..","..","..","foo")), A1("foo")); - ASSERT_EQ(computeAbsolutePath(A3("a","b","c"), P5("..","..","..","..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(A3("a","b","c"), P5("..","..","..","..","foo")), A1("foo")); #ifdef _WIN32 - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P4("..","..","..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P4("..","..","..","foo")), P2(A_SAMBA,"foo")); - ASSERT_EQ(computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P5("..","..","..","..","foo")), + ASSERT_EQ(kiwix::computeAbsolutePath(P4(A_SAMBA,"a","b","c"), P5("..","..","..","..","foo")), P2(A_SAMBA,"foo")); #endif } @@ -241,20 +242,20 @@ TEST(pathTools, dirChange) std::string p1("c:\\a\\b\\c"); std::string p2("d:\\d\\e\\foo.xml"); { - std::string relative_path = computeRelativePath(p1, p2); + std::string relative_path = kiwix::computeRelativePath(p1, p2); ASSERT_EQ(relative_path, p2); - std::string abs_path = computeAbsolutePath(p1, relative_path); + std::string abs_path = kiwix::computeAbsolutePath(p1, relative_path); ASSERT_EQ(abs_path, p2); - ASSERT_EQ(computeAbsolutePath(p1, "..\\..\\..\\..\\..\\d:\\d\\e\\foo.xml"), p2); + ASSERT_EQ(kiwix::computeAbsolutePath(p1, "..\\..\\..\\..\\..\\d:\\d\\e\\foo.xml"), p2); } std::string ps("\\\\samba\\d\\e\\foo.xml"); { - std::string relative_path = computeRelativePath(p1, ps); + std::string relative_path = kiwix::computeRelativePath(p1, ps); ASSERT_EQ(relative_path, ps); - std::string abs_path = computeAbsolutePath(p1, relative_path); + std::string abs_path = kiwix::computeAbsolutePath(p1, relative_path); ASSERT_EQ(abs_path, ps); // I'm not sure this test is valid on windows :/ -// ASSERT_EQ(computeAbsolutePath(p1, "..\\..\\..\\..\\..\\\\samba\\d\\e\\foo.xml"), ps); +// ASSERT_EQ(kiwix::computeAbsolutePath(p1, "..\\..\\..\\..\\..\\\\samba\\d\\e\\foo.xml"), ps); } } diff --git a/test/regex.cpp b/test/regex.cpp index e70bd1670..0d83055b0 100644 --- a/test/regex.cpp +++ b/test/regex.cpp @@ -21,7 +21,7 @@ #include -#include "../include/tools/regexTools.h" +#include "../src/tools/regexTools.h" namespace { diff --git a/test/server.cpp b/test/server.cpp index 87f1d47b3..749474bbe 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -5,7 +5,7 @@ #include "../include/manager.h" #include "../include/server.h" #include "../include/name_mapper.h" - +#include "../include/tools.h" using TestContextImpl = std::vector >; struct TestContext : TestContextImpl { @@ -81,8 +81,8 @@ private: // data ZimFileServer::ZimFileServer(int serverPort, std::string libraryFilePath) : manager(&this->library) { - if ( isRelativePath(libraryFilePath) ) - libraryFilePath = computeAbsolutePath(getCurrentDirectory(), libraryFilePath); + if ( kiwix::isRelativePath(libraryFilePath) ) + libraryFilePath = kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), libraryFilePath); manager.readFile(libraryFilePath, true, true); run(serverPort);