diff options
author | Max Horn | 2007-10-04 08:04:18 +0000 |
---|---|---|
committer | Max Horn | 2007-10-04 08:04:18 +0000 |
commit | 51f082dcde8c6f1b5036f81ae7b504bcc291bced (patch) | |
tree | cf55068fb49a8c9cbe2bd26d9af2ed71be7c571f | |
parent | 9e8167b10ce21d8286bcdd39a2de30f0da6d3aee (diff) | |
download | scummvm-rg350-51f082dcde8c6f1b5036f81ae7b504bcc291bced.tar.gz scummvm-rg350-51f082dcde8c6f1b5036f81ae7b504bcc291bced.tar.bz2 scummvm-rg350-51f082dcde8c6f1b5036f81ae7b504bcc291bced.zip |
Patch #1805208: move matchString to Common::Util
svn-id: r29154
-rw-r--r-- | common/fs.cpp | 47 | ||||
-rw-r--r-- | common/util.cpp | 33 | ||||
-rw-r--r-- | common/util.h | 22 |
3 files changed, 57 insertions, 45 deletions
diff --git a/common/fs.cpp b/common/fs.cpp index 7d7aa363fc..100f6c86bd 100644 --- a/common/fs.cpp +++ b/common/fs.cpp @@ -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++; diff --git a/common/util.cpp b/common/util.cpp index 8bf704456a..eaef9ce0b5 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -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 // diff --git a/common/util.h b/common/util.h index f8f166e29e..2d55bf1fda 100644 --- a/common/util.h +++ b/common/util.h @@ -53,6 +53,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. * @param data the data to be dumped |