-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: streamlined absolutePath impl
- Loading branch information
1 parent
c3198d3
commit e59d8ae
Showing
4 changed files
with
35 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
#include <filesystem> | ||
#include "MiscFunctions.hpp" | ||
|
||
std::string absolutePath(const std::string& rawpath, const std::string& currentPath) { | ||
auto value = rawpath; | ||
std::optional<std::string> absolutePath(const std::string& rawpath, const std::string& rawcurrentpath) { | ||
std::filesystem::path path(rawpath); | ||
|
||
if (value[0] == '~') { | ||
// Handling where rawpath starts with '~' | ||
if (!rawpath.empty() && rawpath[0] == '~') { | ||
static const char* const ENVHOME = getenv("HOME"); | ||
value.replace(0, 1, std::string(ENVHOME)); | ||
} else if (value[0] != '/') { | ||
auto currentDir = currentPath.substr(0, currentPath.find_last_of('/')); | ||
|
||
if (value[0] == '.') { | ||
if (value[1] == '.' && value[2] == '/') { | ||
auto parentDir = currentDir.substr(0, currentDir.find_last_of('/')); | ||
value.replace(0, 2 + currentPath.empty(), parentDir); | ||
} else if (value[1] == '/') | ||
value.replace(0, 1 + currentPath.empty(), currentDir); | ||
else | ||
value = currentDir + '/' + value; | ||
} else | ||
value = currentDir + '/' + value; | ||
return std::filesystem::path(ENVHOME) / path.relative_path().string().substr(2); | ||
} | ||
// Handling e.g. ./, ../ | ||
else if (path.is_relative()) { | ||
const std::filesystem::path currentDir = std::filesystem::path(rawcurrentpath).parent_path(); | ||
|
||
return value; | ||
auto finalPath = currentDir / path; | ||
if (exists(finalPath)) | ||
return std::filesystem::canonical(currentDir / path); | ||
return {}; | ||
} else { | ||
return std::filesystem::canonical(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <optional> | ||
|
||
std::string absolutePath(const std::string&, const std::string&); | ||
|
||
std::optional<std::string> absolutePath(const std::string&, const std::string&); |