From 8768197c241b89f9ff1ba53911d0b809b3618ce7 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Sat, 2 Aug 2014 18:12:28 -0700 Subject: [PATCH] feat(util/lean_path): add dirname and path_append aux functions Signed-off-by: Leonardo de Moura --- src/util/lean_path.cpp | 27 +++++++++++++++++++++++++++ src/util/lean_path.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/src/util/lean_path.cpp b/src/util/lean_path.cpp index 23a585d11..fccc9ca11 100644 --- a/src/util/lean_path.cpp +++ b/src/util/lean_path.cpp @@ -267,4 +267,31 @@ void display_path(std::ostream & out, std::string const & fname) { out << fname; #endif } + +std::string dirname(char const * fname) { + if (fname == nullptr) + return "."; + unsigned i = 0; + optional last_sep; + char const * it = fname; + while (*it) { + if (*it == g_sep) + last_sep = i; + ++i; + ++it; + } + if (!last_sep) + return "."; + std::string r; + for (unsigned i = 0; i < *last_sep; i++) + r.push_back(fname[i]); + return r; +} + +std::string path_append(char const * p1, char const * p2) { + std::string r(p1); + r += g_sep; + r += p2; + return r; +} } diff --git a/src/util/lean_path.h b/src/util/lean_path.h index f41d257aa..ce47963aa 100644 --- a/src/util/lean_path.h +++ b/src/util/lean_path.h @@ -38,4 +38,7 @@ std::string name_to_file(name const & fname); In some platforms it will fix the notation used to display the path. */ void display_path(std::ostream & out, std::string const & fname); + +std::string dirname(char const * fname); +std::string path_append(char const * path1, char const * path2); }