diff options
author | Michael Vogt <michael.vogt@ubuntu.com> | 2012-09-04 12:21:29 +0200 |
---|---|---|
committer | Michael Vogt <michael.vogt@ubuntu.com> | 2012-09-04 12:21:29 +0200 |
commit | 8d01b9d692f49309235e710b79937baf605fda3d (patch) | |
tree | f49d527913ed7a504bd0ffcf2910554eea5ae043 | |
parent | 7735ad0500b6fefef03b2a3dc2a6843e82353e94 (diff) |
add "Glob()" to fileutl
-rw-r--r-- | apt-pkg/contrib/fileutl.cc | 31 | ||||
-rw-r--r-- | apt-pkg/contrib/fileutl.h | 3 | ||||
-rw-r--r-- | test/libapt/fileutl_test.cc | 42 | ||||
-rw-r--r-- | test/libapt/makefile | 6 |
4 files changed, 82 insertions, 0 deletions
diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 1808489d7..d6930ddd5 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -41,6 +41,8 @@ #include <dirent.h> #include <signal.h> #include <errno.h> +#include <glob.h> + #include <set> #include <algorithm> @@ -1552,3 +1554,32 @@ bool FileFd::Sync() /*}}}*/ 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; +} + /*}}}*/ diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 1ca41cb7d..ed4902332 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -186,4 +186,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 diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc new file mode 100644 index 000000000..b6b8ac579 --- /dev/null +++ b/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; +} diff --git a/test/libapt/makefile b/test/libapt/makefile index 1952051e2..1e3652e00 100644 --- a/test/libapt/makefile +++ b/test/libapt/makefile @@ -80,3 +80,9 @@ PROGRAM = CdromFindPackages${BASENAME} SLIBS = -lapt-pkg SOURCE = cdromfindpackages_test.cc include $(PROGRAM_H) + +# test fileutls +PROGRAM = FileUtl${BASENAME} +SLIBS = -lapt-pkg +SOURCE = fileutl_test.cc +include $(PROGRAM_H) |