Procházet zdrojové kódy

New functions used to extract the components of a full filename.

Sebastien Barre před 24 roky
rodič
revize
3546e5611c
2 změnil soubory, kde provedl 101 přidání a 5 odebrání
  1. 82 3
      Source/cmSystemTools.cxx
  2. 19 2
      Source/cmSystemTools.h

+ 82 - 3
Source/cmSystemTools.cxx

@@ -314,7 +314,7 @@ bool cmSystemTools::FileExists(const char* filename)
 
 // Return a capitalized string (i.e the first letter is uppercased, all other
 // are lowercased)
-std::string cmSystemTools::Capitalized(std::string& s)
+std::string cmSystemTools::Capitalized(const std::string& s)
 {
   std::string n;
   n.resize(s.size());
@@ -874,8 +874,9 @@ std::string cmSystemTools::GetProgramPath(const char* in_name)
 }
 
 /**
- * Given the path to a program executable, get the directory part of the path with the
- * file stripped off.  If there is no directory part, the empty string is returned.
+ * Given the path to a program executable, get the directory part of the path
+ * with the file stripped off.  If there is no directory part, the empty
+ * string is returned.
  */
 void cmSystemTools::SplitProgramPath(const char* in_name,
 				     std::string& dir,
@@ -934,3 +935,81 @@ std::string cmSystemTools::CollapseFullPath(const char* in_name)
   return newPath;
 }
 
+/**
+ * Return path of a full filename (no trailing slashes).
+ * Warning: returned path is converted to Unix slashes format.
+ */
+std::string cmSystemTools::GetFilenamePath(const std::string& filename)
+{
+  std::string fn = filename;
+  cmSystemTools::ConvertToUnixSlashes(fn);
+  
+  std::string::size_type slash_pos = fn.rfind("/");
+  if(slash_pos != std::string::npos)
+    {
+    return fn.substr(0, slash_pos);
+    }
+  else
+    {
+    return "";
+    }
+}
+
+
+/**
+ * Return file name of a full filename (i.e. file name without path).
+ */
+std::string cmSystemTools::GetFilenameName(const std::string& filename)
+{
+  std::string fn = filename;
+  cmSystemTools::ConvertToUnixSlashes(fn);
+  
+  std::string::size_type slash_pos = fn.rfind("/");
+  if(slash_pos != std::string::npos)
+    {
+    return fn.substr(slash_pos + 1);
+    }
+  else
+    {
+    return filename;
+    }
+}
+
+
+/**
+ * Return file extension of a full filename (dot included).
+ * Warning: this is the longest extension (for example: .tar.gz)
+ */
+std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
+{
+  std::string name = cmSystemTools::GetFilenameName(filename);
+  std::string::size_type dot_pos = name.find(".");
+  if(dot_pos != std::string::npos)
+    {
+    return name.substr(dot_pos);
+    }
+  else
+    {
+    return "";
+    }
+}
+
+
+/**
+ * Return file name without extension of a full filename (i.e. without path).
+ * Warning: it considers the longest extension (for example: .tar.gz)
+ */
+std::string cmSystemTools::GetFilenameNameWithoutExtension(const std::string& filename)
+{
+  std::string name = cmSystemTools::GetFilenameName(filename);
+  std::string::size_type dot_pos = name.find(".");
+  if(dot_pos != std::string::npos)
+    {
+    return name.substr(0, dot_pos);
+    }
+  else
+    {
+    return name;
+    }
+}
+

+ 19 - 2
Source/cmSystemTools.h

@@ -82,7 +82,7 @@ public:
    * Return a capitalized string (i.e the first letter is uppercased, all other
    * are lowercased).
    */
-  static std::string Capitalized(std::string&);
+  static std::string Capitalized(const std::string&);
   
   /**
    * Replace Windows file system slashes with Unix-style slashes.
@@ -180,15 +180,32 @@ public:
   ///! Find an executable in the system PATH, with optional extra paths.
   static std::string FindProgram(const char* name,
 				 const std::vector<std::string>& path= std::vector<std::string>());
+
   ///! Find a library in the system PATH, with optional extra paths.
   static std::string FindLibrary(const char* name,
 				 const std::vector<std::string>& path);
+
   ///! return true if the file is a directory.
   static bool FileIsDirectory(const char* name);
+
   static std::string GetCurrentWorkingDirectory();
   static std::string GetProgramPath(const char*);
-  static void SplitProgramPath(const char* in_name, std::string& dir, std::string& file);
+  static void SplitProgramPath(const char* in_name, 
+                               std::string& dir, 
+                               std::string& file);
   static std::string CollapseFullPath(const char*);
+
+  ///! return path of a full filename (no trailing slashes).
+  static std::string GetFilenamePath(const std::string&);
+  
+  ///! return file name of a full filename (i.e. file name without path).
+  static std::string GetFilenameName(const std::string&);
+  
+  ///! return file extension of a full filename (dot included).
+  static std::string GetFilenameExtension(const std::string&);
+  
+  ///! return file name without extension of a full filename.
+  static std::string GetFilenameNameWithoutExtension(const std::string&);
   
   static long int ModifiedTime(const char* filename);