mirror of https://github.com/kiwix/libkiwix.git
+ 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:
parent
7e00a3339d
commit
9182f4e529
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
|
@ -82,58 +82,6 @@ namespace kiwix {
|
||||||
return true;
|
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) {
|
bool Manager::readXml(const string xml, const bool readOnly, const string libraryPath) {
|
||||||
pugi::xml_document doc;
|
pugi::xml_document doc;
|
||||||
pugi::xml_parse_result result = doc.load_buffer_inplace((void*)xml.data(), xml.size());
|
pugi::xml_parse_result result = doc.load_buffer_inplace((void*)xml.data(), xml.size());
|
||||||
|
|
|
@ -23,15 +23,16 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <pugixml.hpp>
|
#include <pugixml.hpp>
|
||||||
|
|
||||||
#include "../base64.h"
|
#include "../base64.h"
|
||||||
#include "../regexTools.h"
|
#include "../regexTools.h"
|
||||||
|
#include "../pathTools.h"
|
||||||
#include <kiwix/library.h>
|
#include <kiwix/library.h>
|
||||||
#include <kiwix/reader.h>
|
#include <kiwix/reader.h>
|
||||||
|
|
||||||
|
@ -77,10 +78,6 @@ namespace kiwix {
|
||||||
|
|
||||||
bool readBookFromPath(const string path, Book &book);
|
bool readBookFromPath(const string path, Book &book);
|
||||||
bool parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath);
|
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,24 +19,55 @@
|
||||||
|
|
||||||
#include "pathTools.h"
|
#include "pathTools.h"
|
||||||
|
|
||||||
const char *nsStringToCString(const nsAString &str) {
|
bool isRelativePath(const string &path) {
|
||||||
const char *cStr;
|
|
||||||
nsCString tmpStr;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
LossyCopyUTF16toASCII(str, tmpStr);
|
return path.empty() || path.substr(1, 2) == ":\\" ? false : true;
|
||||||
#else
|
#else
|
||||||
CopyUTF16toUTF8(str, tmpStr);
|
return path.empty() || path.substr(0, 1) == "/" ? false : true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NS_CStringGetData(tmpStr, &cStr);
|
|
||||||
return cStr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nsStringToUTF8(const nsAString &str) {
|
string computeAbsolutePath(const string libraryPath, const string relativePath) {
|
||||||
const char *cStr;
|
#ifdef _WIN32
|
||||||
nsCString tmpStr;
|
string separator = "\\";
|
||||||
CopyUTF16toUTF8(str, tmpStr);
|
#else
|
||||||
NS_CStringGetData(tmpStr, &cStr);
|
string separator = "/";
|
||||||
return cStr;
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,14 @@
|
||||||
#ifndef KIWIX_PATHTOOLS_H
|
#ifndef KIWIX_PATHTOOLS_H
|
||||||
#define KIWIX_PATHTOOLS_H
|
#define KIWIX_PATHTOOLS_H
|
||||||
|
|
||||||
#include "nsStringAPI.h"
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
const char *nsStringToCString(const nsAString &str);
|
using namespace std;
|
||||||
const char *nsStringToUTF8(const nsAString &str);
|
|
||||||
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue