From 0b1623d07db963ecf3789aba7163321812cba88e Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 18 Feb 2021 07:55:39 -0500 Subject: Add QUtil::path_basename --- libqpdf/QUtil.cc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index a3fa94f4..17b163f5 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -663,6 +663,37 @@ QUtil::file_provider(std::string const& filename) }; } +std::string +QUtil::path_basename(std::string const& filename) +{ +#ifdef _WIN32 + char const* pathsep = "/\\"; +#else + char const* pathsep = "/"; +#endif + std::string last = filename; + auto len = last.length(); + while (len > 1) + { + auto pos = last.find_last_of(pathsep); + if (pos == len - 1) + { + last.pop_back(); + --len; + } + else if (pos == std::string::npos) + { + break; + } + else + { + last = last.substr(pos + 1); + break; + } + } + return last; +} + char* QUtil::copy_string(std::string const& str) { -- cgit v1.2.3-70-g09d2