From 9c53b966c34d505e27681f2ed51998d3a737a663 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 10 Mar 2017 18:35:16 +0300 Subject: Add pathJoin functions for join parts of path. --- src/utils/stringutils.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'src/utils/stringutils.cpp') 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) { -- cgit v1.2.3-70-g09d2