]> git.saurik.com Git - apt.git/commitdiff
* lp:~mvo/apt/add-glob-function:
authorMichael Vogt <egon@debian-devbox>
Wed, 17 Oct 2012 08:27:50 +0000 (10:27 +0200)
committerMichael Vogt <mvo@debian.org>
Thu, 15 Aug 2013 07:43:43 +0000 (09:43 +0200)
  -  add Glob() to fileutl.{cc,h}
Conflicts:
apt-pkg/contrib/fileutl.h
debian/changelog

apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
debian/changelog
test/libapt/fileutl_test.cc [new file with mode: 0644]
test/libapt/makefile

index f24df65fcfa98e4be9ea9ec3fd76fc80af95437c..dca468c6310d2db22aa5ce1276d207d88ec3184d 100644 (file)
@@ -41,6 +41,8 @@
 #include <dirent.h>
 #include <signal.h>
 #include <errno.h>
+#include <glob.h>
+
 #include <set>
 #include <algorithm>
 
@@ -1766,3 +1768,32 @@ bool FileFd::FileFdError(const char *Description,...) {
                                                                        /*}}}*/
 
 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;
+}
+                                                                       /*}}}*/
index 3ec01dd9a7113f40bb4311b44fb56c58e66847c8..decd64d9d37b06d1d1f8ea27a0d7b5d11a8516d9 100644 (file)
@@ -191,4 +191,7 @@ std::string flNoLink(std::string File);
 std::string flExtension(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
index 1c7afd863bef875146d59977026b9e31a1d53529..b597d1b201b2a99f37d768f18a48ee8ab265a14e 100644 (file)
@@ -9,6 +9,8 @@ apt (0.9.11) UNRELEASED; urgency=low
   * lp:~mvo/apt/config-clear:
     - support Configuration.Clear() for a clear of the entire 
       configuration
+  * lp:~mvo/apt/add-glob-function:
+    -  add Glob() to fileutl.{cc,h}
 
  -- Michael Vogt <michael.vogt@ubuntu.com>  Thu, 15 Aug 2013 09:27:35 +0200
 
diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc
new file mode 100644 (file)
index 0000000..b6b8ac5
--- /dev/null
@@ -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;
+}
index 1b67cba9d82b0e6c2fc8e9c50eff3195ca80cf56..73403b24c0abc03f89fdf7593ae117c142bdf582 100644 (file)
@@ -98,6 +98,11 @@ include $(PROGRAM_H)
 PROGRAM = IndexCopyToSourceList${BASENAME}
 SLIBS = -lapt-pkg
 SOURCE = indexcopytosourcelist_test.cc
+
+# test fileutls
+PROGRAM = FileUtl${BASENAME}
+SLIBS = -lapt-pkg 
+SOURCE = fileutl_test.cc
 include $(PROGRAM_H)
 
 # test tagfile