diff --git a/Foundation/include/Poco/Glob.h b/Foundation/include/Poco/Glob.h index 838eddd6f..3a83ece30 100644 --- a/Foundation/include/Poco/Glob.h +++ b/Foundation/include/Poco/Glob.h @@ -139,6 +139,18 @@ public: /// Directories that for whatever reason cannot be traversed are /// ignored. + static void glob(const Path& pathPattern, const Path& basePath, std::set& files, int options = 0); + /// Creates a set of files that match the given pathPattern, starting from basePath. + /// + /// The pattern may contain wildcard expressions even in intermediate + /// directory names (e.g. /usr/include/*/*.h). + /// + /// Note that, for obvious reasons, escaping characters in a pattern + /// with a backslash does not work in Windows-style paths. + /// + /// Directories that for whatever reason cannot be traversed are + /// ignored. + protected: bool match(TextIterator& itp, const TextIterator& endp, TextIterator& its, const TextIterator& ends); bool matchAfterAsterisk(TextIterator itp, const TextIterator& endp, TextIterator its, const TextIterator& ends); diff --git a/Foundation/src/Glob.cpp b/Foundation/src/Glob.cpp index 60ce4bb7e..2a36732c0 100644 --- a/Foundation/src/Glob.cpp +++ b/Foundation/src/Glob.cpp @@ -106,6 +106,18 @@ void Glob::glob(const Path& pathPattern, std::set& files, int optio } +void Glob::glob(const Path& pathPattern, const Path& basePath, std::set& files, int options) +{ + Path pattern(pathPattern); + pattern.makeDirectory(); // to simplify pattern handling later on + Path absBase(basePath); + absBase.makeAbsolute(); + if (pathPattern.isDirectory()) + options |= GLOB_DIRS_ONLY; + collect(pattern, absBase, basePath, pathPattern[basePath.depth()], files, options); +} + + bool Glob::match(TextIterator& itp, const TextIterator& endp, TextIterator& its, const TextIterator& ends) { while (itp != endp) diff --git a/Foundation/testsuite/src/GlobTest.cpp b/Foundation/testsuite/src/GlobTest.cpp index 5244bda9f..e30523670 100644 --- a/Foundation/testsuite/src/GlobTest.cpp +++ b/Foundation/testsuite/src/GlobTest.cpp @@ -470,6 +470,14 @@ void GlobTest::testGlob() assert (files.find("globtest/src/") != files.end()); assert (files.find("globtest/testsuite/") != files.end()); + files.clear(); + Glob::glob("globtest/testsuite/src/*", "globtest/testsuite/", files); + translatePaths(files); + assert (files.size() == 3); + assert (files.find("globtest/testsuite/src/test.h") != files.end()); + assert (files.find("globtest/testsuite/src/test.c") != files.end()); + assert (files.find("globtest/testsuite/src/main.c") != files.end()); + #if !defined(_WIN32_WCE) // won't work if current directory is root dir files.clear();