Преглед изворни кода

* lp:~mvo/apt/add-glob-function:
- add Glob() to fileutl.{cc,h}

Michael Vogt пре 13 година
родитељ
комит
20a2b20138
5 измењених фајлова са 82 додато и 1 уклоњено
  1. 31 0
      apt-pkg/contrib/fileutl.cc
  2. 2 1
      apt-pkg/contrib/fileutl.h
  3. 2 0
      debian/changelog
  4. 42 0
      test/libapt/fileutl_test.cc
  5. 5 0
      test/libapt/makefile

+ 31 - 0
apt-pkg/contrib/fileutl.cc

@@ -41,6 +41,8 @@
 #include <dirent.h>
 #include <dirent.h>
 #include <signal.h>
 #include <signal.h>
 #include <errno.h>
 #include <errno.h>
+#include <glob.h>
+
 #include <set>
 #include <set>
 #include <algorithm>
 #include <algorithm>
 
 
@@ -1778,3 +1780,32 @@ bool FileFd::Sync()
 									/*}}}*/
 									/*}}}*/
 
 
 gzFile FileFd::gzFd() { return (gzFile) d->gz; }
 gzFile FileFd::gzFd() { return (gzFile) d->gz; }
+
+
+// Glob - wrapper around "glob()"                                      /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+std::vector<std::string> Glob(std::string const &pattern, int flags)
+{
+   std::vector<std::string> result;
+   glob_t globbuf;
+   int glob_res, i;
+
+   glob_res = glob(pattern.c_str(),  flags, NULL, &globbuf);
+
+   if (glob_res != 0)
+   {
+      if(glob_res != GLOB_NOMATCH) {
+         _error->Errno("glob", "Problem with glob");
+         return result;
+      }
+   }
+
+   // append results
+   for(i=0;i<globbuf.gl_pathc;i++)
+      result.push_back(string(globbuf.gl_pathv[i]));
+
+   globfree(&globbuf);
+   return result;
+}
+									/*}}}*/

+ 2 - 1
apt-pkg/contrib/fileutl.h

@@ -190,6 +190,7 @@ std::string flNoLink(std::string File);
 std::string flExtension(std::string File);
 std::string flExtension(std::string File);
 std::string flCombine(std::string Dir,std::string File);
 std::string flCombine(std::string Dir,std::string File);
 
 
-
+// simple c++ glob
+std::vector<std::string> Glob(std::string const &pattern, int flags=0);
 
 
 #endif
 #endif

+ 2 - 0
debian/changelog

@@ -9,6 +9,8 @@ apt (0.9.8~exp1) UNRELEASED; urgency=low
     - fix invalid InRelease file download checking and add regression
     - fix invalid InRelease file download checking and add regression
       test to server broken files to the buildin test webserver
       test to server broken files to the buildin test webserver
   * stop exporting the accidently exported parsenetrc() symbol
   * stop exporting the accidently exported parsenetrc() symbol
+  * lp:~mvo/apt/add-glob-function:
+    -  add Glob() to fileutl.{cc,h}
 
 
  -- David Kalnischkies <kalnischkies@gmail.com>  Mon, 09 Jul 2012 17:36:40 +0200
  -- David Kalnischkies <kalnischkies@gmail.com>  Mon, 09 Jul 2012 17:36:40 +0200
 
 

+ 42 - 0
test/libapt/fileutl_test.cc

@@ -0,0 +1,42 @@
+#include <apt-pkg/error.h>
+#include <apt-pkg/fileutl.h>
+
+#include "assert.h"
+#include <string>
+#include <vector>
+
+#include <stdio.h>
+#include <iostream>
+#include <stdlib.h>
+
+
+int main(int argc,char *argv[])
+{
+   std::vector<std::string> files;
+
+   // normal match
+   files = Glob("*.lst");
+   if (files.size() != 1)
+   {
+      _error->DumpErrors();
+      return 1;
+   }
+
+   // not there
+   files = Glob("xxxyyyzzz");
+   if (files.size() != 0 || _error->PendingError())
+   {
+      _error->DumpErrors();
+      return 1;
+   }
+
+   // many matches (number is a bit random)
+   files = Glob("*.cc");
+   if (files.size() < 10)
+   {
+      _error->DumpErrors();
+      return 1;
+   }
+
+   return 0;
+}

+ 5 - 0
test/libapt/makefile

@@ -97,4 +97,9 @@ include $(PROGRAM_H)
 PROGRAM = IndexCopyToSourceList${BASENAME}
 PROGRAM = IndexCopyToSourceList${BASENAME}
 SLIBS = -lapt-pkg
 SLIBS = -lapt-pkg
 SOURCE = indexcopytosourcelist_test.cc
 SOURCE = indexcopytosourcelist_test.cc
+
+# test fileutls
+PROGRAM = FileUtl${BASENAME}
+SLIBS = -lapt-pkg 
+SOURCE = fileutl_test.cc
 include $(PROGRAM_H)
 include $(PROGRAM_H)