+ 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

This commit is contained in:
kelson42 2011-10-28 15:40:17 +00:00
parent 7e00a3339d
commit 9182f4e529
6 changed files with 126 additions and 75 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright 2011 Emmanuel Engelhart <kelson@kiwix.org>
*
* 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;
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2011 Emmanuel Engelhart <kelson@kiwix.org>
*
* 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

View File

@ -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());

View File

@ -23,15 +23,16 @@
#include <string>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <pugixml.hpp>
#include "../base64.h"
#include "../regexTools.h"
#include "../pathTools.h"
#include <kiwix/library.h>
#include <kiwix/reader.h>
@ -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);
};
}

View File

@ -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
}
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
NS_CStringGetData(tmpStr, &cStr);
return cStr;
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;
}
const char *nsStringToUTF8(const nsAString &str) {
const char *cStr;
nsCString tmpStr;
CopyUTF16toUTF8(str, tmpStr);
NS_CStringGetData(tmpStr, &cStr);
return cStr;
}

View File

@ -20,9 +20,14 @@
#ifndef KIWIX_PATHTOOLS_H
#define KIWIX_PATHTOOLS_H
#include "nsStringAPI.h"
#include <string.h>
#include <string>
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