mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-13 11:19:58 +01:00
Cleanup path_relative_to_path and path_absolute_from_path
This commit is contained in:
@@ -336,45 +336,32 @@ void SystemToolkit::execute(const string& command)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
vector<string> split(const string& str, char delim) {
|
||||||
// 7.3: http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
|
vector<string> strings;
|
||||||
//
|
size_t start;
|
||||||
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
|
size_t end = 0;
|
||||||
{
|
while ((start = str.find_first_not_of(delim, end)) != string::npos) {
|
||||||
// Skip delimiters at beginning.
|
end = str.find(delim, start);
|
||||||
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
|
strings.push_back(str.substr(start, end - start));
|
||||||
// Find first "non-delimiter".
|
|
||||||
string::size_type pos = str.find_first_of(delimiters, lastPos);
|
|
||||||
|
|
||||||
while (string::npos != pos || string::npos != lastPos)
|
|
||||||
{
|
|
||||||
// Found a token, add it to the vector.
|
|
||||||
tokens.push_back(str.substr(lastPos, pos - lastPos));
|
|
||||||
// Skip delimiters. Note the "not_of"
|
|
||||||
lastPos = str.find_first_not_of(delimiters, pos);
|
|
||||||
// Find next "non-delimiter"
|
|
||||||
pos = str.find_first_of(delimiters, lastPos);
|
|
||||||
}
|
}
|
||||||
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative-path.html
|
// loosely inspired from http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative-path.html
|
||||||
//
|
//
|
||||||
string SystemToolkit::path_relative_to_path( const string& absolutePath, const string& relativeTo )
|
string SystemToolkit::path_relative_to_path( const string& absolutePath, const string& relativeTo )
|
||||||
{
|
{
|
||||||
const string separator = string(1, PATH_SEP);
|
|
||||||
string relativePath = "";
|
string relativePath = "";
|
||||||
vector< string > absoluteDirectories;
|
vector<string> absoluteDirectories = split(absolutePath, PATH_SEP);
|
||||||
vector< string > relativeToDirectories;
|
vector<string> relativeToDirectories = split(relativeTo, PATH_SEP);
|
||||||
Tokenize( absolutePath, absoluteDirectories, separator );
|
|
||||||
Tokenize( relativeTo, relativeToDirectories, separator );
|
|
||||||
|
|
||||||
// Get the shortest of the two paths
|
// Get the shortest of the two paths
|
||||||
int length = MINI( absoluteDirectories.size(), relativeToDirectories.size() );
|
size_t length = MINI( absoluteDirectories.size(), relativeToDirectories.size() );
|
||||||
|
|
||||||
// Use to determine where in the loop we exited
|
// Use to determine where in the loop we exited
|
||||||
int lastCommonRoot = -1;
|
size_t lastCommonRoot = SIZE_T_MAX;
|
||||||
int index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
// Find common root
|
// Find common root
|
||||||
for (; index < length; ++index) {
|
for (; index < length; ++index) {
|
||||||
@@ -385,19 +372,19 @@ string SystemToolkit::path_relative_to_path( const string& absolutePath, const s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we didn't find a common prefix then return base absolute path
|
// If we didn't find a common prefix then return base absolute path
|
||||||
if (lastCommonRoot < 0)
|
if (lastCommonRoot == SIZE_T_MAX || absoluteDirectories.size() < 1)
|
||||||
return absolutePath;
|
return absolutePath;
|
||||||
|
|
||||||
// Add the '..'
|
// Add the '..'
|
||||||
for (index = lastCommonRoot + 1; index < relativeToDirectories.size(); ++index) {
|
for (index = lastCommonRoot + 1; index < relativeToDirectories.size(); ++index) {
|
||||||
if (relativeToDirectories[index].size() > 0)
|
if (relativeToDirectories[index].size() > 0)
|
||||||
relativePath += ".." + separator;
|
relativePath += string("..") + PATH_SEP;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the relative folders
|
// Add the relative folders
|
||||||
for (index = lastCommonRoot + 1; index < absoluteDirectories.size() - 1; ++index) {
|
for (index = lastCommonRoot + 1; index < absoluteDirectories.size() - 1; ++index) {
|
||||||
relativePath += absoluteDirectories[index];
|
relativePath += absoluteDirectories[index];
|
||||||
relativePath += separator;
|
relativePath += PATH_SEP;
|
||||||
}
|
}
|
||||||
|
|
||||||
relativePath += absoluteDirectories[absoluteDirectories.size() - 1];
|
relativePath += absoluteDirectories[absoluteDirectories.size() - 1];
|
||||||
@@ -407,28 +394,27 @@ string SystemToolkit::path_relative_to_path( const string& absolutePath, const s
|
|||||||
|
|
||||||
std::string SystemToolkit::path_absolute_from_path(const std::string& relativePath, const std::string& relativeTo)
|
std::string SystemToolkit::path_absolute_from_path(const std::string& relativePath, const std::string& relativeTo)
|
||||||
{
|
{
|
||||||
const string separator = string(1, PATH_SEP);
|
string absolutePath = string(1, PATH_SEP);
|
||||||
string absolutePath = separator;
|
vector<string> relativeDirectories = split(relativePath, PATH_SEP);
|
||||||
vector< string > relativeDirectories;
|
vector<string> relativeToDirectories = split(relativeTo, PATH_SEP);
|
||||||
vector< string > relativeToDirectories;
|
|
||||||
Tokenize( relativePath, relativeDirectories, separator );
|
|
||||||
Tokenize( relativeTo, relativeToDirectories, separator );
|
|
||||||
|
|
||||||
// how many ".."
|
// how many ".."
|
||||||
int count_relative = 0;
|
size_t count_relative = 0;
|
||||||
for (; count_relative < relativeDirectories.size() - 1; ++count_relative) {
|
for (; count_relative < relativeDirectories.size() - 1; ++count_relative) {
|
||||||
if (relativeDirectories[count_relative].compare("..") != 0)
|
if (relativeDirectories[count_relative].compare("..") != 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// take the left part of relativeTo path
|
// take the left part of relativeTo path
|
||||||
for (int i = 0; i < relativeToDirectories.size() -count_relative; ++i) {
|
if (relativeToDirectories.size() > count_relative ) {
|
||||||
|
for (size_t i = 0; i < relativeToDirectories.size() -count_relative; ++i) {
|
||||||
absolutePath += relativeToDirectories[i];
|
absolutePath += relativeToDirectories[i];
|
||||||
absolutePath += separator;
|
absolutePath += PATH_SEP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// add the rest of the relative path
|
// add the rest of the relative path
|
||||||
for (; count_relative < relativeDirectories.size() - 1; ++count_relative) {
|
for (; count_relative < relativeDirectories.size() - 1; ++count_relative) {
|
||||||
absolutePath += relativeDirectories[count_relative];
|
absolutePath += relativeDirectories[count_relative];
|
||||||
absolutePath += separator;
|
absolutePath += PATH_SEP;
|
||||||
}
|
}
|
||||||
absolutePath += relativeDirectories[count_relative];
|
absolutePath += relativeDirectories[count_relative];
|
||||||
|
|
||||||
|
|||||||
@@ -51,8 +51,10 @@ namespace SystemToolkit
|
|||||||
// list all files of a directory mathing the given filter extension (if any)
|
// list all files of a directory mathing the given filter extension (if any)
|
||||||
std::list<std::string> list_directory(const std::string& path, const std::list<std::string> &extensions);
|
std::list<std::string> list_directory(const std::string& path, const std::list<std::string> &extensions);
|
||||||
|
|
||||||
|
// builds a path relative to 'relativeTo' to reach file at 'absolutePath' (e.g. /a/b/c/d rel to /a/b/e -> ../c/d)
|
||||||
std::string path_relative_to_path(const std::string& absolutePath, const std::string& relativeTo);
|
std::string path_relative_to_path(const std::string& absolutePath, const std::string& relativeTo);
|
||||||
|
|
||||||
|
// builds the absolute path (starting with '/') of relativePath starting from relativeTo (e.g. ../c/d rel to /a/b/e -> /a/b/c/d)
|
||||||
std::string path_absolute_from_path(const std::string& relativePath, const std::string& relativeTo);
|
std::string path_absolute_from_path(const std::string& relativePath, const std::string& relativeTo);
|
||||||
|
|
||||||
// true of file exists
|
// true of file exists
|
||||||
|
|||||||
Reference in New Issue
Block a user