// join two path segments cleanly together // but only if right side is not absolute yet std::string join_paths(std::string l, std::string r) { #ifdef _WIN32 // convert Windows backslashes to URL forward slashes replace(l.begin(), l.end(), '\\', '/'); replace(r.begin(), r.end(), '\\', '/'); #endif if (l.empty()) return r; if (r.empty()) return l; if (is_absolute_path(r)) return r; if (l[l.length()-1] != '/') l += '/'; while ((r.length() > 3) && ((r.substr(0, 3) == "../") || (r.substr(0, 3)) == "..\\")) { size_t L = l.length(), pos = find_last_folder_separator(l, L - 2); bool is_slash = pos + 2 == L && (l[pos+1] == '/' || l[pos+1] == '\\'); bool is_self = pos + 3 == L && (l[pos+1] == '.'); if (!is_self && !is_slash) r = r.substr(3); l = l.substr(0, pos == std::string::npos ? pos : pos + 1); } return l + r; }
// return only the filename part of path std::string base_name(const std::string& path) { size_t pos = find_last_folder_separator(path); if (pos == std::string::npos) return path; else return path.substr(pos+1); }