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
This commit is contained in:
Max Horn 2009-02-15 18:45:53 +00:00
parent 544bda60fa
commit 17014c4f47
4 changed files with 19 additions and 62 deletions

View File

@ -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++;
}

View File

@ -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"

View File

@ -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

View File

@ -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<String> {