[llvm-commits] [llvm] r49352 - in /llvm/trunk: include/llvm/System/Path.h lib/System/Path.cpp lib/System/Unix/Path.inc lib/System/Win32/Path.inc

Ted Kremenek kremenek at apple.com
Mon Apr 7 14:53:57 PDT 2008


Author: kremenek
Date: Mon Apr  7 16:53:57 2008
New Revision: 49352

URL: http://llvm.org/viewvc/llvm-project?rev=49352&view=rev
Log:
Added method Path::getDirname().

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

Modified: llvm/trunk/include/llvm/System/Path.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/Path.h?rev=49352&r1=49351&r2=49352&view=diff

==============================================================================
--- llvm/trunk/include/llvm/System/Path.h (original)
+++ llvm/trunk/include/llvm/System/Path.h Mon Apr  7 16:53:57 2008
@@ -269,6 +269,10 @@
       /// @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 @@
       /// 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.
+            
 
     /// @}
   };

Modified: llvm/trunk/lib/System/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Path.cpp?rev=49352&r1=49351&r2=49352&view=diff

==============================================================================
--- llvm/trunk/lib/System/Path.cpp (original)
+++ llvm/trunk/lib/System/Path.cpp Mon Apr  7 16:53:57 2008
@@ -196,6 +196,45 @@
         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"

Modified: llvm/trunk/lib/System/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/Path.inc?rev=49352&r1=49351&r2=49352&view=diff

==============================================================================
--- llvm/trunk/lib/System/Unix/Path.inc (original)
+++ llvm/trunk/lib/System/Unix/Path.inc Mon Apr  7 16:53:57 2008
@@ -277,16 +277,18 @@
 }
 
 
+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

Modified: llvm/trunk/lib/System/Win32/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Win32/Path.inc?rev=49352&r1=49351&r2=49352&view=diff

==============================================================================
--- llvm/trunk/lib/System/Win32/Path.inc (original)
+++ llvm/trunk/lib/System/Win32/Path.inc Mon Apr  7 16:53:57 2008
@@ -229,6 +229,8 @@
   return len > 0 && path[len-1] == '/';
 }
 
+std::string Path::getDirname() const { return getDirnameCharSep('\\'); }
+
 std::string
 Path::getBasename() const {
   // Find the last slash





More information about the llvm-commits mailing list