From 9182f4e529ccb1edfadc7e32484f80da0467ed51 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Fri, 28 Oct 2011 15:40:17 +0000 Subject: [PATCH] + reorganisation of the code: creation of componentTools.[h|cpp] with two methods for charset manipulation (only used in contentManager XPCOM) and move of 3 path manipulations functions from manager.cpp to pathTools.cpp --- src/common/componentTools.cpp | 42 ++++++++++++++++++++++++ src/common/componentTools.h | 28 ++++++++++++++++ src/common/kiwix/manager.cpp | 52 ----------------------------- src/common/kiwix/manager.h | 7 ++-- src/common/pathTools.cpp | 61 ++++++++++++++++++++++++++--------- src/common/pathTools.h | 11 +++++-- 6 files changed, 126 insertions(+), 75 deletions(-) create mode 100644 src/common/componentTools.cpp create mode 100644 src/common/componentTools.h diff --git a/src/common/componentTools.cpp b/src/common/componentTools.cpp new file mode 100644 index 000000000..764d6a185 --- /dev/null +++ b/src/common/componentTools.cpp @@ -0,0 +1,42 @@ +/* + * Copyright 2011 Emmanuel Engelhart + * + * 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. + */ + +#include "componentTools.h" + +const char *nsStringToCString(const nsAString &str) { + const char *cStr; + nsCString tmpStr; + +#ifdef _WIN32 + LossyCopyUTF16toASCII(str, tmpStr); +#else + CopyUTF16toUTF8(str, tmpStr); +#endif + + NS_CStringGetData(tmpStr, &cStr); + return cStr; +} + +const char *nsStringToUTF8(const nsAString &str) { + const char *cStr; + nsCString tmpStr; + CopyUTF16toUTF8(str, tmpStr); + NS_CStringGetData(tmpStr, &cStr); + return cStr; +} diff --git a/src/common/componentTools.h b/src/common/componentTools.h new file mode 100644 index 000000000..74c83bcc9 --- /dev/null +++ b/src/common/componentTools.h @@ -0,0 +1,28 @@ +/* + * Copyright 2011 Emmanuel Engelhart + * + * 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_COMPONENTTOOLS_H +#define KIWIX_COMPONENTTOOLS_H + +#include "nsStringAPI.h" + +const char *nsStringToCString(const nsAString &str); +const char *nsStringToUTF8(const nsAString &str); + +#endif diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index 57f94f23e..8919ab056 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -82,58 +82,6 @@ namespace kiwix { return true; } - bool Manager::isRelativePath(const string &path) { -#ifdef _WIN32 - return path.empty() || path.substr(1, 2) == ":\\" ? false : true; -#else - return path.empty() || path.substr(0, 1) == "/" ? false : true; -#endif - } - - string Manager::computeAbsolutePath(const string libraryPath, const string relativePath) { -#ifdef _WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - string absolutePath = removeLastPathElement(libraryPath, true, false); - char *cRelativePath = strdup(relativePath.c_str()); - char *token = strtok(cRelativePath, "/"); - - while (token != NULL) { - if (string(token) == "..") { - absolutePath = removeLastPathElement(absolutePath, true, false); - token = strtok(NULL, "/"); - } else if (token != "." && token != "") { - absolutePath += string(token); - token = strtok(NULL, "/"); - if (token != NULL) - absolutePath += separator; - } else { - token = strtok(NULL, "/"); - } - } - - return absolutePath; - } - - string Manager::removeLastPathElement(const string path, const bool removePreSeparator, const bool removePostSeparator) { -#ifdef _WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - - string newPath = path; - size_t offset = newPath.find_last_of(separator); - if (removePreSeparator && offset != newPath.find_first_of(separator) && offset == newPath.length()-1) { - newPath = newPath.substr(0, offset); - offset = newPath.find_last_of(separator); - } - newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1); - return newPath; - } - bool Manager::readXml(const string xml, const bool readOnly, const string libraryPath) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_buffer_inplace((void*)xml.data(), xml.size()); diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index a38b7b0aa..17cbded75 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -23,15 +23,16 @@ #include #include #include +#include #include #include -#include #include #include #include "../base64.h" #include "../regexTools.h" +#include "../pathTools.h" #include #include @@ -77,10 +78,6 @@ namespace kiwix { bool readBookFromPath(const string path, Book &book); bool parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath); - bool isRelativePath(const string &path); - string computeAbsolutePath(const string libraryPath, const string relativePath); - string removeLastPathElement(const string path, const bool removePreSeparator = false, - const bool removePostSeparator = false); }; } diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index 169405ca9..5c6918bcf 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -19,24 +19,55 @@ #include "pathTools.h" -const char *nsStringToCString(const nsAString &str) { - const char *cStr; - nsCString tmpStr; - +bool isRelativePath(const string &path) { #ifdef _WIN32 - LossyCopyUTF16toASCII(str, tmpStr); + return path.empty() || path.substr(1, 2) == ":\\" ? false : true; #else - CopyUTF16toUTF8(str, tmpStr); + return path.empty() || path.substr(0, 1) == "/" ? false : true; #endif - - NS_CStringGetData(tmpStr, &cStr); - return cStr; } -const char *nsStringToUTF8(const nsAString &str) { - const char *cStr; - nsCString tmpStr; - CopyUTF16toUTF8(str, tmpStr); - NS_CStringGetData(tmpStr, &cStr); - return cStr; +string computeAbsolutePath(const string libraryPath, const string relativePath) { +#ifdef _WIN32 + string separator = "\\"; +#else + string separator = "/"; +#endif + string absolutePath = removeLastPathElement(libraryPath, true, false); + char *cRelativePath = strdup(relativePath.c_str()); + char *token = strtok(cRelativePath, "/"); + + while (token != NULL) { + if (string(token) == "..") { + absolutePath = removeLastPathElement(absolutePath, true, false); + token = strtok(NULL, "/"); + } else if (token != "." && token != "") { + absolutePath += string(token); + token = strtok(NULL, "/"); + if (token != NULL) + absolutePath += separator; + } else { + token = strtok(NULL, "/"); + } + } + + return absolutePath; } + +string removeLastPathElement(const string path, const bool removePreSeparator, const bool removePostSeparator) { +#ifdef _WIN32 + string separator = "\\"; +#else + string separator = "/"; +#endif + + string newPath = path; + size_t offset = newPath.find_last_of(separator); + if (removePreSeparator && offset != newPath.find_first_of(separator) && offset == newPath.length()-1) { + newPath = newPath.substr(0, offset); + offset = newPath.find_last_of(separator); + } + newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1); + return newPath; +} + diff --git a/src/common/pathTools.h b/src/common/pathTools.h index b91292574..2345a708d 100644 --- a/src/common/pathTools.h +++ b/src/common/pathTools.h @@ -20,9 +20,14 @@ #ifndef KIWIX_PATHTOOLS_H #define KIWIX_PATHTOOLS_H -#include "nsStringAPI.h" +#include +#include -const char *nsStringToCString(const nsAString &str); -const char *nsStringToUTF8(const nsAString &str); +using namespace std; + +bool isRelativePath(const string &path); +string computeAbsolutePath(const string libraryPath, const string relativePath); +string removeLastPathElement(const string path, const bool removePreSeparator = false, + const bool removePostSeparator = false); #endif