APT

Merge lp:~mvo/apt/add-glob-function into lp:~mvo/apt/mvo

Proposed by Michael Vogt
Status: Needs review
Proposed branch: lp:~mvo/apt/add-glob-function
Merge into: lp:~mvo/apt/mvo
Diff against target: 118 lines (+82/-0)
4 files modified
apt-pkg/contrib/fileutl.cc (+31/-0)
apt-pkg/contrib/fileutl.h (+3/-0)
test/libapt/fileutl_test.cc (+42/-0)
test/libapt/makefile (+6/-0)
To merge this branch: bzr merge lp:~mvo/apt/add-glob-function
Reviewer Review Type Date Requested Status
Michael Vogt Pending
Review via email: mp+122654@code.launchpad.net

Description of the change

This adds a c++ friendly Glob() in the hope that it comes useful one day.

To post a comment you must log in.

Unmerged revisions

1845. By Michael Vogt

add "Glob()" to fileutl

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'apt-pkg/contrib/fileutl.cc'
2--- apt-pkg/contrib/fileutl.cc 2012-03-06 09:53:35 +0000
3+++ apt-pkg/contrib/fileutl.cc 2012-09-04 10:51:23 +0000
4@@ -41,6 +41,8 @@
5 #include <dirent.h>
6 #include <signal.h>
7 #include <errno.h>
8+#include <glob.h>
9+
10 #include <set>
11 #include <algorithm>
12
13@@ -1552,3 +1554,32 @@
14 /*}}}*/
15
16 gzFile FileFd::gzFd() { return (gzFile) d->gz; }
17+
18+
19+// Glob - wrapper around "glob()" /*{{{*/
20+// ---------------------------------------------------------------------
21+/* */
22+std::vector<std::string> Glob(std::string const &pattern, int flags)
23+{
24+ std::vector<std::string> result;
25+ glob_t globbuf;
26+ int glob_res, i;
27+
28+ glob_res = glob(pattern.c_str(), flags, NULL, &globbuf);
29+
30+ if (glob_res != 0)
31+ {
32+ if(glob_res != GLOB_NOMATCH) {
33+ _error->Errno("glob", "Problem with glob");
34+ return result;
35+ }
36+ }
37+
38+ // append results
39+ for(i=0;i<globbuf.gl_pathc;i++)
40+ result.push_back(string(globbuf.gl_pathv[i]));
41+
42+ globfree(&globbuf);
43+ return result;
44+}
45+ /*}}}*/
46
47=== modified file 'apt-pkg/contrib/fileutl.h'
48--- apt-pkg/contrib/fileutl.h 2012-02-11 21:36:03 +0000
49+++ apt-pkg/contrib/fileutl.h 2012-09-04 10:51:23 +0000
50@@ -186,4 +186,7 @@
51 std::string flExtension(std::string File);
52 std::string flCombine(std::string Dir,std::string File);
53
54+// simple c++ glob
55+std::vector<std::string> Glob(std::string const &pattern, int flags=0);
56+
57 #endif
58
59=== added file 'test/libapt/fileutl_test.cc'
60--- test/libapt/fileutl_test.cc 1970-01-01 00:00:00 +0000
61+++ test/libapt/fileutl_test.cc 2012-09-04 10:51:23 +0000
62@@ -0,0 +1,42 @@
63+#include <apt-pkg/error.h>
64+#include <apt-pkg/fileutl.h>
65+
66+#include "assert.h"
67+#include <string>
68+#include <vector>
69+
70+#include <stdio.h>
71+#include <iostream>
72+#include <stdlib.h>
73+
74+
75+int main(int argc,char *argv[])
76+{
77+ std::vector<std::string> files;
78+
79+ // normal match
80+ files = Glob("*.lst");
81+ if (files.size() != 1)
82+ {
83+ _error->DumpErrors();
84+ return 1;
85+ }
86+
87+ // not there
88+ files = Glob("xxxyyyzzz");
89+ if (files.size() != 0 || _error->PendingError())
90+ {
91+ _error->DumpErrors();
92+ return 1;
93+ }
94+
95+ // many matches (number is a bit random)
96+ files = Glob("*.cc");
97+ if (files.size() < 10)
98+ {
99+ _error->DumpErrors();
100+ return 1;
101+ }
102+
103+ return 0;
104+}
105
106=== modified file 'test/libapt/makefile'
107--- test/libapt/makefile 2011-12-11 01:55:20 +0000
108+++ test/libapt/makefile 2012-09-04 10:51:23 +0000
109@@ -80,3 +80,9 @@
110 SLIBS = -lapt-pkg
111 SOURCE = cdromfindpackages_test.cc
112 include $(PROGRAM_H)
113+
114+# test fileutls
115+PROGRAM = FileUtl${BASENAME}
116+SLIBS = -lapt-pkg
117+SOURCE = fileutl_test.cc
118+include $(PROGRAM_H)

Subscribers

People subscribed via source and target branches

to all changes: