+ replace a few variables with a CONSTANT

+ new method computeRelativePath()
This commit is contained in:
kelson42 2013-08-04 14:26:33 +08:00
parent ac50d0eade
commit acc78f585b
2 changed files with 47 additions and 33 deletions

View File

@ -31,6 +31,12 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef _WIN32
#define SEPARATOR "\\"
#else
#define SEPARATOR "/"
#endif
#ifndef PATH_MAX #ifndef PATH_MAX
#define PATH_MAX 1024 #define PATH_MAX 1024
#endif #endif
@ -43,15 +49,35 @@ bool isRelativePath(const string &path) {
#endif #endif
} }
string computeRelativePath(const string path, const string absolutePath) {
std::vector<std::string> pathParts = kiwix::split(path, SEPARATOR);
std::vector<std::string> absolutePathParts = kiwix::split(absolutePath, SEPARATOR);
unsigned int commonCount = 0;
while (commonCount < pathParts.size() &&
commonCount < absolutePathParts.size() &&
pathParts[commonCount] == absolutePathParts[commonCount]) {
if (!pathParts[commonCount].empty()) {
commonCount++;
}
}
string relativePath;
for (unsigned int i = commonCount ; i < pathParts.size() ; i++) {
relativePath += "../";
}
for (unsigned int i = commonCount ; i < absolutePathParts.size() ; i++) {
relativePath += absolutePathParts[i];
relativePath += i + 1 < absolutePathParts.size() ? "/" : "";
}
return relativePath;
}
/* Warning: the relative path must be with slashes */ /* Warning: the relative path must be with slashes */
string computeAbsolutePath(const string path, const string relativePath) { string computeAbsolutePath(const string path, const string relativePath) {
#ifdef _WIN32
string separator = "\\";
#else
string separator = "/";
#endif
string absolutePath; string absolutePath;
if (path.empty()) { if (path.empty()) {
char *path=NULL; char *path=NULL;
size_t size = 0; size_t size = 0;
@ -62,9 +88,9 @@ string computeAbsolutePath(const string path, const string relativePath) {
path = getcwd(path,size); path = getcwd(path,size);
#endif #endif
absolutePath = string(path) + separator; absolutePath = string(path) + SEPARATOR;
} else { } else {
absolutePath = path[path.length() - 1] == separator[0] ? path : path + separator; absolutePath = path.substr(path.length() - 1, 1) == SEPARATOR ? path : path + SEPARATOR;
} }
#if _WIN32 #if _WIN32
@ -82,7 +108,7 @@ string computeAbsolutePath(const string path, const string relativePath) {
absolutePath += string(token); absolutePath += string(token);
token = strtok(NULL, "/"); token = strtok(NULL, "/");
if (token != NULL) if (token != NULL)
absolutePath += separator; absolutePath += SEPARATOR;
} else { } else {
token = strtok(NULL, "/"); token = strtok(NULL, "/");
} }
@ -92,41 +118,25 @@ string computeAbsolutePath(const string path, const string relativePath) {
} }
string removeLastPathElement(const string path, const bool removePreSeparator, const bool removePostSeparator) { string removeLastPathElement(const string path, const bool removePreSeparator, const bool removePostSeparator) {
#ifdef _WIN32
string separator = "\\";
#else
string separator = "/";
#endif
string newPath = path; string newPath = path;
size_t offset = newPath.find_last_of(separator); size_t offset = newPath.find_last_of(SEPARATOR);
if (removePreSeparator && offset != newPath.find_first_of(separator) && offset == newPath.length()-1) { if (removePreSeparator &&
offset != newPath.find_first_of(SEPARATOR) &&
offset == newPath.length()-1) {
newPath = newPath.substr(0, offset); newPath = newPath.substr(0, offset);
offset = newPath.find_last_of(separator); offset = newPath.find_last_of(SEPARATOR);
} }
newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1); newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1);
return newPath; return newPath;
} }
string appendToDirectory(const string &directoryPath, const string &filename) { string appendToDirectory(const string &directoryPath, const string &filename) {
#ifdef _WIN32 string newPath = directoryPath + SEPARATOR + filename;
string separator = "\\";
#else
string separator = "/";
#endif
string newPath = directoryPath + separator + filename;
return newPath; return newPath;
} }
string getLastPathElement(const string &path) { string getLastPathElement(const string &path) {
#ifdef _WIN32 return path.substr(path.find_last_of(SEPARATOR) + 1);
string separator = "\\";
#else
string separator = "/";
#endif
return path.substr(path.find_last_of(separator) + 1);
} }
unsigned int getFileSize(const string &path) { unsigned int getFileSize(const string &path) {

View File

@ -21,6 +21,7 @@
#define KIWIX_PATHTOOLS_H #define KIWIX_PATHTOOLS_H
#include <string> #include <string>
#include <vector>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -36,10 +37,13 @@
#include <direct.h> #include <direct.h>
#endif #endif
#include <stringTools.h>
using namespace std; using namespace std;
bool isRelativePath(const string &path); bool isRelativePath(const string &path);
string computeAbsolutePath(const string libraryPath, const string relativePath); string computeAbsolutePath(const string path, const string relativePath);
string computeRelativePath(const string path, const string absolutePath);
string removeLastPathElement(const string path, const bool removePreSeparator = false, string removeLastPathElement(const string path, const bool removePreSeparator = false,
const bool removePostSeparator = false); const bool removePostSeparator = false);
string appendToDirectory(const string &directoryPath, const string &filename); string appendToDirectory(const string &directoryPath, const string &filename);