From 6232d13f5321b87ddf12a1aa36b4545da45f173d Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Wed, 17 Nov 2021 22:34:57 +0100 Subject: Travel3: Simple image and video display site Reads the images and videos from filesystem and builds a site in memroy. --- src/pathutil.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/pathutil.cc (limited to 'src/pathutil.cc') diff --git a/src/pathutil.cc b/src/pathutil.cc new file mode 100644 index 0000000..bb2b8e8 --- /dev/null +++ b/src/pathutil.cc @@ -0,0 +1,44 @@ +#include "common.hh" + +#include "pathutil.hh" +#include "strutil.hh" + +#include + +namespace path { + +std::string cleanup(std::string_view path) { + auto trimmed_path = str::trim(path); + bool trailing = !trimmed_path.empty() && trimmed_path.back() == '/'; + auto parts = str::split(trimmed_path, '/'); + auto it = parts.begin(); + while (it != parts.end()) { + if (it->empty() || *it == ".") { + if (it + 1 == parts.end()) + trailing = true; + it = parts.erase(it); + } else if (*it == "..") { + if (it + 1 == parts.end()) + trailing = true; + if (it > parts.begin()) { + it = parts.erase(it - 1, it); + } else { + it = parts.erase(it); + } + } else { + ++it; + } + } + if (parts.empty()) + return "/"; + std::string ret; + for (auto const& part : parts) { + ret.push_back('/'); + ret.append(part); + } + if (trailing) + ret.push_back('/'); + return ret; +} + +} // namespace path -- cgit v1.2.3-70-g09d2