From cf55c8e221c1d31a361f99ee49078d261cdf431c Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 7 Apr 2008 21:53:57 +0000 Subject: Added method Path::getDirname(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49352 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/System/Path.cpp | 39 +++++++++++++++++++++++++++++++++++++++ lib/System/Unix/Path.inc | 6 ++++-- lib/System/Win32/Path.inc | 2 ++ 3 files changed, 45 insertions(+), 2 deletions(-) (limited to 'lib/System') diff --git a/lib/System/Path.cpp b/lib/System/Path.cpp index 8a1de75..43c36d5 100644 --- a/lib/System/Path.cpp +++ b/lib/System/Path.cpp @@ -196,6 +196,45 @@ static void getPathList(const char*path, std::vector& Paths) { Paths.push_back(tmpPath); } +std::string Path::getDirnameCharSep(char Sep) const { + + if (path.empty()) + return "."; + + // If the path is all slashes, return a single slash. + // Otherwise, remove all trailing slashes. + + signed pos = path.size() - 1; + + while (pos >= 0 && path[pos] == Sep) + --pos; + + if (pos < 0) + return path[0] == Sep ? std::string(1, Sep) : std::string("."); + + // Any slashes left? + signed i = 0; + + while (i < pos && path[i] != Sep) + ++i; + + if (i == pos) // No slashes? Return "." + return "."; + + // There is at least one slash left. Remove all trailing non-slashes. + while (pos >= 0 && path[pos] != Sep) + --pos; + + // Remove any trailing slashes. + while (pos >= 0 && path[pos] == Sep) + --pos; + + if (pos < 0) + return path[0] == Sep ? std::string(1, Sep) : std::string("."); + + return path.substr(0, pos+1); +} + // Include the truly platform-specific parts of this class. #if defined(LLVM_ON_UNIX) #include "Unix/Path.inc" diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc index c3d06a3..fe2e3c6 100644 --- a/lib/System/Unix/Path.inc +++ b/lib/System/Unix/Path.inc @@ -277,16 +277,18 @@ Path Path::GetMainExecutable(const char *argv0, void *MainAddr) { } +std::string Path::getDirname() const { return getDirnameCharSep('/'); } + std::string Path::getBasename() const { // Find the last slash - size_t slash = path.rfind('/'); + std::string::size_type slash = path.rfind('/'); if (slash == std::string::npos) slash = 0; else slash++; - size_t dot = path.rfind('.'); + std::string::size_type dot = path.rfind('.'); if (dot == std::string::npos || dot < slash) return path.substr(slash); else diff --git a/lib/System/Win32/Path.inc b/lib/System/Win32/Path.inc index da29cd3..35bae33 100644 --- a/lib/System/Win32/Path.inc +++ b/lib/System/Win32/Path.inc @@ -229,6 +229,8 @@ Path::isRootDirectory() const { return len > 0 && path[len-1] == '/'; } +std::string Path::getDirname() const { return getDirnameCharSep('\\'); } + std::string Path::getBasename() const { // Find the last slash -- cgit v1.1