From acc78f585baef40674df0f30d17c82973258a238 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sun, 4 Aug 2013 14:26:33 +0800 Subject: [PATCH] + replace a few variables with a CONSTANT + new method computeRelativePath() --- src/common/pathTools.cpp | 74 +++++++++++++++++++++++----------------- src/common/pathTools.h | 6 +++- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index b736ee557..55f6b6a05 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -31,6 +31,12 @@ #include #endif +#ifdef _WIN32 +#define SEPARATOR "\\" +#else +#define SEPARATOR "/" +#endif + #ifndef PATH_MAX #define PATH_MAX 1024 #endif @@ -43,15 +49,35 @@ bool isRelativePath(const string &path) { #endif } +string computeRelativePath(const string path, const string absolutePath) { + std::vector pathParts = kiwix::split(path, SEPARATOR); + std::vector 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 */ string computeAbsolutePath(const string path, const string relativePath) { -#ifdef _WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - string absolutePath; + if (path.empty()) { char *path=NULL; size_t size = 0; @@ -62,9 +88,9 @@ string computeAbsolutePath(const string path, const string relativePath) { path = getcwd(path,size); #endif - absolutePath = string(path) + separator; + absolutePath = string(path) + SEPARATOR; } else { - absolutePath = path[path.length() - 1] == separator[0] ? path : path + separator; + absolutePath = path.substr(path.length() - 1, 1) == SEPARATOR ? path : path + SEPARATOR; } #if _WIN32 @@ -82,7 +108,7 @@ string computeAbsolutePath(const string path, const string relativePath) { absolutePath += string(token); token = strtok(NULL, "/"); if (token != NULL) - absolutePath += separator; + absolutePath += SEPARATOR; } else { 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) { -#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) { + 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); + offset = newPath.find_last_of(SEPARATOR); } newPath = removePostSeparator ? newPath.substr(0, offset) : newPath.substr(0, offset+1); return newPath; } string appendToDirectory(const string &directoryPath, const string &filename) { -#ifdef _WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - - string newPath = directoryPath + separator + filename; + string newPath = directoryPath + SEPARATOR + filename; return newPath; } string getLastPathElement(const string &path) { -#ifdef _WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - - return path.substr(path.find_last_of(separator) + 1); + return path.substr(path.find_last_of(SEPARATOR) + 1); } unsigned int getFileSize(const string &path) { diff --git a/src/common/pathTools.h b/src/common/pathTools.h index 3b5bc1942..783179b0e 100644 --- a/src/common/pathTools.h +++ b/src/common/pathTools.h @@ -21,6 +21,7 @@ #define KIWIX_PATHTOOLS_H #include +#include #include #include #include @@ -36,10 +37,13 @@ #include #endif +#include + using namespace std; 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, const bool removePostSeparator = false); string appendToDirectory(const string &directoryPath, const string &filename);