From 17014c4f472a2325fa75b4a14cf1acace3464020 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 15 Feb 2009 18:45:53 +0000 Subject: Merged internal 'matchPath' method of class Archive into global matchString function (via an optional 'path mode' in the latter). Also changed Archive::listMatchingMembers to use path mode when matching, just like FSDirectory::listMatchingMembers svn-id: r38277 --- common/archive.cpp | 55 ++---------------------------------------------------- common/archive.h | 1 - common/str.cpp | 17 ++++++++++++----- common/str.h | 8 +++++--- 4 files changed, 19 insertions(+), 62 deletions(-) (limited to 'common') diff --git a/common/archive.cpp b/common/archive.cpp index 9299d8e495..1fce9c144a 100644 --- a/common/archive.cpp +++ b/common/archive.cpp @@ -56,7 +56,7 @@ int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) ArchiveMemberList::iterator it = allNames.begin(); for ( ; it != allNames.end(); ++it) { - if ((*it)->getName().matchString(lowercasePattern)) { + if ((*it)->getName().matchString(lowercasePattern, true)) { list.push_back(*it); matches++; } @@ -210,57 +210,6 @@ void FSDirectory::ensureCached() const { _cached = true; } -bool matchPath(const char *str, const char *pat) { - assert(str); - assert(pat); - - const char *p = 0; - const char *q = 0; - - for (;;) { - if (*str == '/') { - p = 0; - q = 0; - } - - switch (*pat) { - case '*': - // Record pattern / string possition for backtracking - p = ++pat; - q = str; - // If pattern ended with * -> match - if (!*pat) - return true; - break; - - default: - if (*pat != *str) { - if (p) { - // No match, oops -> try to backtrack - pat = p; - str = ++q; - if (!*str) - return !*pat; - break; - } - else - return false; - } - if (!*str) - return !*pat; - pat++; - str++; - break; - - case '?': - if (!*str || *str == '/') - return !*pat; - pat++; - str++; - } - } -} - int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &pattern) { if (!_node.isDirectory()) return 0; @@ -274,7 +223,7 @@ int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &patt int matches = 0; NodeCache::iterator it = _fileCache.begin(); for ( ; it != _fileCache.end(); ++it) { - if (matchPath(it->_key.c_str(), lowercasePattern.c_str())) { + if (it->_key.matchString(lowercasePattern, true)) { list.push_back(ArchiveMemberPtr(new FSNode(it->_value))); matches++; } diff --git a/common/archive.h b/common/archive.h index 36bc5bc7c9..a7ebb8aa75 100644 --- a/common/archive.h +++ b/common/archive.h @@ -26,7 +26,6 @@ #ifndef COMMON_ARCHIVE_H #define COMMON_ARCHIVE_H -//#include "common/fs.h" #include "common/str.h" #include "common/hash-str.h" #include "common/list.h" diff --git a/common/str.cpp b/common/str.cpp index d3522a665a..10d258e058 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -330,12 +330,12 @@ bool String::contains(char x) const { return strchr(c_str(), x) != NULL; } -bool String::matchString(const char *pat) const { - return Common::matchString(c_str(), pat); +bool String::matchString(const char *pat, bool pathMode) const { + return Common::matchString(c_str(), pat, pathMode); } -bool String::matchString(const String &pat) const { - return Common::matchString(c_str(), pat.c_str()); +bool String::matchString(const String &pat, bool pathMode) const { + return Common::matchString(c_str(), pat.c_str(), pathMode); } void String::deleteLastChar() { @@ -615,7 +615,7 @@ Common::String normalizePath(const Common::String &path, const char sep) { return result; } -bool matchString(const char *str, const char *pat) { +bool matchString(const char *str, const char *pat, bool pathMode) { assert(str); assert(pat); @@ -623,6 +623,13 @@ bool matchString(const char *str, const char *pat) { const char *q = 0; for (;;) { + if (pathMode && *str == '/') { + p = 0; + q = 0; + if (*pat == '?') + return false; + } + switch (*pat) { case '*': // Record pattern / string possition for backtracking diff --git a/common/str.h b/common/str.h index 43b75edc5b..b9b269468f 100644 --- a/common/str.h +++ b/common/str.h @@ -166,11 +166,12 @@ public: * * @param str Text to be matched against the given pattern. * @param pat Glob pattern. + * @param pathMode Whether to use path mode, i.e., whether slashes must be matched explicitly. * * @return true if str matches the pattern, false otherwise. */ - bool matchString(const char *pat) const; - bool matchString(const String &pat) const; + bool matchString(const char *pat, bool pathMode = false) const; + bool matchString(const String &pat, bool pathMode = false) const; inline const char *c_str() const { return _str; } @@ -306,10 +307,11 @@ Common::String normalizePath(const Common::String &path, const char sep); * * @param str Text to be matched against the given pattern. * @param pat Glob pattern. + * @param pathMode Whether to use path mode, i.e., whether slashes must be matched explicitly. * * @return true if str matches the pattern, false otherwise. */ -bool matchString(const char *str, const char *pat); +bool matchString(const char *str, const char *pat, bool pathMode = false); class StringList : public Array { -- cgit v1.2.3