diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-03-10 18:35:16 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-03-10 18:35:16 +0300 |
commit | 9c53b966c34d505e27681f2ed51998d3a737a663 (patch) | |
tree | 689af8fb285a3192e20c6906e4b84f54b0db0c32 /src/utils/stringutils.cpp | |
parent | 4df1e68047488ec00a264c076bb3222715f42295 (diff) | |
download | manaplus-9c53b966c34d505e27681f2ed51998d3a737a663.tar.gz manaplus-9c53b966c34d505e27681f2ed51998d3a737a663.tar.bz2 manaplus-9c53b966c34d505e27681f2ed51998d3a737a663.tar.xz manaplus-9c53b966c34d505e27681f2ed51998d3a737a663.zip |
Add pathJoin functions for join parts of path.
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r-- | src/utils/stringutils.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 28b2c8e1e..3663cc007 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -1044,6 +1044,105 @@ void sanitizePath(std::string &path) replaceRecursiveAll(path, sep2Str, sepStr); } +std::string pathJoin(std::string str1, + const std::string &str2) +{ +#ifdef WIN32 + const char sep = '\\'; + std::string sepStr = "\\"; +#else + const char sep = '/'; + std::string sepStr = "/"; +#endif + + if (str1.empty()) + { + if (str2[0] == sep) + return str2; + else + return sepStr.append(str2); + } + const size_t sz1 = str1.size(); + if (str2.empty()) + { + if (str1[sz1 - 1] == sep) + return str1; + else + return str1.append(sepStr); + } + if (str1[sz1 - 1] == sep) + { + if (str2[0] == sep) + return str1.append(str2.substr(1)); + else + return str1.append(str2); + } + else + { + if (str2[0] == sep) + return str1.append(str2); + else + return str1.append(sepStr).append(str2); + } +} + +std::string pathJoin(std::string str1, + const std::string &str2, + const std::string &str3) +{ +#ifdef WIN32 + const char sep = '\\'; + std::string sepStr = "\\"; +#else + const char sep = '/'; + std::string sepStr = "/"; +#endif + + if (str1.empty()) + { + return pathJoin(str2, str3); + } + size_t sz1 = str1.size(); + if (str2.empty()) + { + return pathJoin(str1, str3); + } + if (str3.empty()) + { + return pathJoin(str1, str2); + } + if (str1[sz1 - 1] == sep) + { + if (str2[0] == sep) + str1.append(str2.substr(1)); + else + str1.append(str2); + } + else + { + if (str2[0] == sep) + str1.append(str2); + else + str1.append(sepStr).append(str2); + } + + sz1 = str1.size(); + if (str1[sz1 - 1] == sep) + { + if (str3[0] == sep) + return str1.append(str3.substr(1)); + else + return str1.append(str3); + } + else + { + if (str3[0] == sep) + return str1.append(str3); + else + return str1.append(sepStr).append(str3); + } +} + #ifndef DYECMD void replaceItemLinks(std::string &msg) { |