Added method Path::getDirname().
authorTed Kremenek <kremenek@apple.com>
Mon, 7 Apr 2008 21:53:57 +0000 (21:53 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 7 Apr 2008 21:53:57 +0000 (21:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49352 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Path.h
lib/System/Path.cpp
lib/System/Unix/Path.inc
lib/System/Win32/Path.inc

index 1178d689819315e79a2e382830dc398def5cba8c..5846fa92537f4bebddef2848cec3b79b215709c7 100644 (file)
@@ -269,6 +269,10 @@ namespace sys {
       /// @returns std::string containing the basename of the path
       /// @brief Get the base name of the path
       std::string getBasename() const;
+    
+      /// This function strips off the suffix of the path beginning with the
+      /// path separator ('/' on Unix, '\' on Windows) and returns the result.
+      std::string getDirname() const;
 
       /// This function strips off the path and basename(up to and
       /// including the last dot) of the file or directory name and
@@ -567,11 +571,19 @@ namespace sys {
       /// MemoryBuffer::getFile instead.
       static void UnMapFilePages(const char *Base, uint64_t FileSize);
     
+
+    /// @}
+    /// @name Internal methods.
+    /// @{
+    protected:
+      std::string getDirnameCharSep(char Sep) const; 
+    
     /// @}
     /// @name Data
     /// @{
     protected:
       mutable std::string path;   ///< Storage for the path name.
+            
 
     /// @}
   };
index 8a1de75e478c90979d2a6abd6b3fd6d3c42053f5..43c36d5e09b9c97f1100c65027afd2d77eed6b54 100644 (file)
@@ -196,6 +196,45 @@ static void getPathList(const char*path, std::vector<Path>& 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"
index c3d06a3fa1d573fe63f47ea3fe4e46ab02b956d1..fe2e3c6777819c327a3f3f5553f940f3b7937da3 100644 (file)
@@ -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
index da29cd3596cefdc31ed090ffa49671054f96a6e1..35bae337da7f5e0f1f5bed3595c3654ffdc9ddb3 100644 (file)
@@ -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