瀏覽代碼

Add a simple globbing of files and directories

Andy Cedilnik 23 年之前
父節點
當前提交
f895a94995
共有 2 個文件被更改,包括 65 次插入0 次删除
  1. 53 0
      Source/cmSystemTools.cxx
  2. 12 0
      Source/cmSystemTools.h

+ 53 - 0
Source/cmSystemTools.cxx

@@ -2259,6 +2259,59 @@ bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath)
 #endif
 }
 
+bool cmSystemTools::SimpleGlob(const std::string& glob, 
+                               std::vector<std::string>& files, 
+                               int type /* = 0 */)
+{
+  if ( glob[glob.size()-1] != '*' )
+    {
+    return false;
+    }
+  std::string path = cmSystemTools::GetFilenamePath(glob);
+  std::string ppath = cmSystemTools::GetFilenameName(glob);
+  ppath = ppath.substr(0, ppath.size()-1);
+  if ( path.size() == 0 )
+    {
+    path = "/";
+    }
+
+  bool res;
+  cmDirectory d;
+  if (d.Load(path.c_str()))
+    {
+    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
+      {
+      if((std::string(d.GetFile(i)) != ".")
+         && (std::string(d.GetFile(i)) != ".."))
+        {
+        std::string fname = path;
+        if ( path[path.size()-1] != '/' )
+          {
+          fname +="/";
+          }
+        fname += d.GetFile(i);
+        std::string sfname = d.GetFile(i);
+        if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
+          {
+          continue;
+          }
+        if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
+          {
+          continue;
+          }
+        if ( sfname.size() >= ppath.size() && 
+             sfname.substr(0, ppath.size()) == 
+             ppath )
+          {
+          files.push_back(fname);
+          res = true;
+          }
+        }
+      }
+    }
+  return res;
+}
+
 cmSystemTools::e_FileFormat cmSystemTools::GetFileFormat(const char* cext)
 {
   if ( ! cext || *cext == 0 )

+ 12 - 0
Source/cmSystemTools.h

@@ -235,6 +235,18 @@ public:
   static void Glob(const char *directory, const char *regexp,
                    std::vector<std::string>& files);
   static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
+
+  /**
+   * Try to find a list of files that match the "simple" globbing
+   * expression. At this point in time the globbing expressions have
+   * to be in form: /directory/partial_file_name*. The * character has
+   * to be at the end of the string and it does not support ?
+   * []... The optional argument type specifies what kind of files you
+   * want to find. 0 means all files, -1 means directories, 1 means
+   * files only. This method returns true if search was succesfull.
+   */
+  static bool SimpleGlob(const std::string& glob, std::vector<std::string>& files, 
+                         int type = 0);
   
   static std::string GetCurrentWorkingDirectory();
   static std::string GetProgramPath(const char*);