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
=== modified file 'apt-pkg/contrib/fileutl.cc'
--- apt-pkg/contrib/fileutl.cc 2012-03-06 09:53:35 +0000
+++ apt-pkg/contrib/fileutl.cc 2012-09-04 10:51:23 +0000
@@ -41,6 +41,8 @@
41#include <dirent.h>41#include <dirent.h>
42#include <signal.h>42#include <signal.h>
43#include <errno.h>43#include <errno.h>
44#include <glob.h>
45
44#include <set>46#include <set>
45#include <algorithm>47#include <algorithm>
4648
@@ -1552,3 +1554,32 @@
1552 /*}}}*/1554 /*}}}*/
15531555
1554gzFile FileFd::gzFd() { return (gzFile) d->gz; }1556gzFile FileFd::gzFd() { return (gzFile) d->gz; }
1557
1558
1559// Glob - wrapper around "glob()" /*{{{*/
1560// ---------------------------------------------------------------------
1561/* */
1562std::vector<std::string> Glob(std::string const &pattern, int flags)
1563{
1564 std::vector<std::string> result;
1565 glob_t globbuf;
1566 int glob_res, i;
1567
1568 glob_res = glob(pattern.c_str(), flags, NULL, &globbuf);
1569
1570 if (glob_res != 0)
1571 {
1572 if(glob_res != GLOB_NOMATCH) {
1573 _error->Errno("glob", "Problem with glob");
1574 return result;
1575 }
1576 }
1577
1578 // append results
1579 for(i=0;i<globbuf.gl_pathc;i++)
1580 result.push_back(string(globbuf.gl_pathv[i]));
1581
1582 globfree(&globbuf);
1583 return result;
1584}
1585 /*}}}*/
15551586
=== modified file 'apt-pkg/contrib/fileutl.h'
--- apt-pkg/contrib/fileutl.h 2012-02-11 21:36:03 +0000
+++ apt-pkg/contrib/fileutl.h 2012-09-04 10:51:23 +0000
@@ -186,4 +186,7 @@
186std::string flExtension(std::string File);186std::string flExtension(std::string File);
187std::string flCombine(std::string Dir,std::string File);187std::string flCombine(std::string Dir,std::string File);
188188
189// simple c++ glob
190std::vector<std::string> Glob(std::string const &pattern, int flags=0);
191
189#endif192#endif
190193
=== added file 'test/libapt/fileutl_test.cc'
--- test/libapt/fileutl_test.cc 1970-01-01 00:00:00 +0000
+++ test/libapt/fileutl_test.cc 2012-09-04 10:51:23 +0000
@@ -0,0 +1,42 @@
1#include <apt-pkg/error.h>
2#include <apt-pkg/fileutl.h>
3
4#include "assert.h"
5#include <string>
6#include <vector>
7
8#include <stdio.h>
9#include <iostream>
10#include <stdlib.h>
11
12
13int main(int argc,char *argv[])
14{
15 std::vector<std::string> files;
16
17 // normal match
18 files = Glob("*.lst");
19 if (files.size() != 1)
20 {
21 _error->DumpErrors();
22 return 1;
23 }
24
25 // not there
26 files = Glob("xxxyyyzzz");
27 if (files.size() != 0 || _error->PendingError())
28 {
29 _error->DumpErrors();
30 return 1;
31 }
32
33 // many matches (number is a bit random)
34 files = Glob("*.cc");
35 if (files.size() < 10)
36 {
37 _error->DumpErrors();
38 return 1;
39 }
40
41 return 0;
42}
043
=== modified file 'test/libapt/makefile'
--- test/libapt/makefile 2011-12-11 01:55:20 +0000
+++ test/libapt/makefile 2012-09-04 10:51:23 +0000
@@ -80,3 +80,9 @@
80SLIBS = -lapt-pkg80SLIBS = -lapt-pkg
81SOURCE = cdromfindpackages_test.cc81SOURCE = cdromfindpackages_test.cc
82include $(PROGRAM_H)82include $(PROGRAM_H)
83
84# test fileutls
85PROGRAM = FileUtl${BASENAME}
86SLIBS = -lapt-pkg
87SOURCE = fileutl_test.cc
88include $(PROGRAM_H)

Subscribers

People subscribed via source and target branches

to all changes: