Patch #1805208: move matchString to Common::Util

svn-id: r29154
This commit is contained in:
Max Horn 2007-10-04 08:04:18 +00:00
parent 9e8167b10c
commit 51f082dcde
3 changed files with 57 additions and 45 deletions

View file

@ -26,43 +26,6 @@
#include "backends/fs/abstract-fs.h"
#include "backends/fs/abstract-fs-factory.h"
/**
* Simple DOS-style pattern matching function (understands * and ? like used in DOS).
* Taken from exult/files/listfiles.cc
*/
static bool matchString(const char *str, const char *pat) {
const char *p = 0;
const char *q = 0;
for (;;) {
switch (*pat) {
case '*':
p = ++pat;
q = str;
break;
default:
if (*pat != *str) {
if (p) {
pat = p;
str = ++q;
if (!*str)
return !*pat;
break;
}
else
return false;
}
// fallthrough
case '?':
if (!*str)
return !*pat;
pat++;
str++;
}
}
}
FilesystemNode::FilesystemNode() {
_realNode = 0;
_refCount = 0;
@ -233,25 +196,19 @@ bool FilesystemNode::lookupFile(FSList &results, FilesystemNode &dir, Common::St
return ((matches > 0) ? true : false);
}
// HACK HACK HACK
extern const char *lastPathComponent(const Common::String &str);
int FilesystemNode::lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &filename, bool hidden, bool exhaustive) const
{
FSList entries;
FSList children;
int matches = 0;
dir.getChildren(entries, FilesystemNode::kListAll, hidden);
//Breadth search (entries in the same level)
for (FSList::iterator entry = entries.begin(); entry != entries.end(); ++entry) {
if (entry->isDirectory()) {
children.push_back(*entry);
} else {
//TODO: here we assume all backends implement the lastPathComponent method. It is currently static,
// so it might be a good idea to include it inside the backend class. This would enforce its
// implementation by all ports.
if (matchString(lastPathComponent(entry->getPath()), filename.c_str())) {
if (Common::matchString(entry->getName().c_str(), filename.c_str())) {
results.push_back(*entry);
matches++;

View file

@ -61,6 +61,39 @@ extern bool isSmartphone(void);
namespace Common {
bool matchString(const char *str, const char *pat) {
const char *p = 0;
const char *q = 0;
for (;;) {
switch (*pat) {
case '*':
p = ++pat;
q = str;
break;
default:
if (*pat != *str) {
if (p) {
pat = p;
str = ++q;
if (!*str)
return !*pat;
break;
}
else
return false;
}
// fallthrough
case '?':
if (!*str)
return !*pat;
pat++;
str++;
}
}
}
//
// Print hexdump of the data passed in
//

View file

@ -52,6 +52,28 @@ template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
namespace Common {
/**
* Simple DOS-style pattern matching function (understands * and ? like used in DOS).
* Taken from exult/files/listfiles.cc
*
* Token meaning:
* "*": any character, any amount of times.
* "?": any character, only once.
*
* Example strings/patterns:
* String: monkey.s?? Pattern: monkey.s01 => true
* String: monkey.s?? Pattern: monkey.s101 => false
* String: monkey.s?1 Pattern: monkey.s99 => false
* String: monkey.s* Pattern: monkey.s101 => true
* String: monkey.s*1 Pattern: monkey.s99 => false
*
* @param str Text to be matched against the given pattern.
* @param pat Glob pattern.
*
* @return true if str matches the pattern, false otherwise.
*/
bool matchString(const char *str, const char *pat);
/**
* Print a hexdump of the data passed in. The number of bytes per line is
* customizable.