Ticket #8728: matchString.patch

File matchString.patch, 4.1 KB (added by SF/david_corrales, 17 years ago)

matchString patch

  • home/david/Projects/scummvm/common/fs.cpp

     
    2626#include "backends/fs/abstract-fs.h"
    2727#include "backends/fs/abstract-fs-factory.h"
    2828
    29 /**
    30  * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
    31  * Taken from exult/files/listfiles.cc
    32  */
    33 static bool matchString(const char *str, const char *pat) {
    34         const char *p = 0;
    35         const char *q = 0;
    36 
    37         for (;;) {
    38                 switch (*pat) {
    39                 case '*':
    40                         p = ++pat;
    41                         q = str;
    42                         break;
    43 
    44                 default:
    45                         if (*pat != *str) {
    46                                 if (p) {
    47                                         pat = p;
    48                                         str = ++q;
    49                                         if (!*str)
    50                                                 return !*pat;
    51                                         break;
    52                                 }
    53                                 else
    54                                         return false;
    55                         }
    56                         // fallthrough
    57                 case '?':
    58                         if (!*str)
    59                                 return !*pat;
    60                         pat++;
    61                         str++;
    62                 }
    63         }
    64 }
    65 
    6629FilesystemNode::FilesystemNode() {
    6730        _realNode = 0;
    6831        _refCount = 0;
     
    233196        return ((matches > 0) ? true : false);
    234197}
    235198
    236 // HACK HACK HACK
    237 extern const char *lastPathComponent(const Common::String &str);
    238 
    239199int FilesystemNode::lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &filename, bool hidden, bool exhaustive) const
    240200{
    241201        FSList entries;
     
    242202        FSList children;
    243203        int matches = 0;
    244204        dir.getChildren(entries, FilesystemNode::kListAll, hidden);
    245 
     205       
    246206        //Breadth search (entries in the same level)
    247207        for (FSList::iterator entry = entries.begin(); entry != entries.end(); ++entry) {
    248208                if (entry->isDirectory()) {
     
    248208                if (entry->isDirectory()) {
    249209                        children.push_back(*entry);
    250210                } else {
    251                         //TODO: here we assume all backends implement the lastPathComponent method. It is currently static,
    252                         //              so it might be a good idea to include it inside the backend class. This would enforce its
    253                         //              implementation by all ports.
    254                         if (matchString(lastPathComponent(entry->getPath()), filename.c_str())) {
     211                        if (Common::matchString(entry->getName().c_str(), filename.c_str())) {
    255212                                results.push_back(*entry);
    256213                                matches++;
    257214
  • home/david/Projects/scummvm/common/util.cpp

     
    6161
    6262namespace Common {
    6363
     64bool matchString(const char *str, const char *pat) {
     65        const char *p = 0;
     66        const char *q = 0;
     67
     68        for (;;) {
     69                switch (*pat) {
     70                case '*':
     71                        p = ++pat;
     72                        q = str;
     73                        break;
     74
     75                default:
     76                        if (*pat != *str) {
     77                                if (p) {
     78                                        pat = p;
     79                                        str = ++q;
     80                                        if (!*str)
     81                                                return !*pat;
     82                                        break;
     83                                }
     84                                else
     85                                        return false;
     86                        }
     87                        // fallthrough
     88                case '?':
     89                        if (!*str)
     90                                return !*pat;
     91                        pat++;
     92                        str++;
     93                }
     94        }
     95}
     96
    6497//
    6598// Print hexdump of the data passed in
    6699//
  • home/david/Projects/scummvm/common/util.h

     
    5353namespace Common {
    5454
    5555/**
     56 * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
     57 * Taken from exult/files/listfiles.cc
     58 *
     59 * Token meaning:
     60 *              "*": any character, any amount of times.
     61 *              "?": any character, only once.
     62 *
     63 * Example strings/patterns:
     64 *              String: monkey.s??       Pattern: monkey.s01    => true
     65 *              String: monkey.s??       Pattern: monkey.s101   => false
     66 *              String: monkey.s?1       Pattern: monkey.s99    => false
     67 *              String: monkey.s*        Pattern: monkey.s101   => true
     68 *              String: monkey.s*1       Pattern: monkey.s99    => false
     69 *
     70 * @param str Text to be matched against the given pattern.
     71 * @param pat Glob pattern.
     72 *
     73 * @return true if str matches the pattern, false otherwise.
     74 */
     75bool matchString(const char *str, const char *pat);
     76
     77/**
    5678 * Print a hexdump of the data passed in. The number of bytes per line is
    5779 * customizable.
    5880 * @param data  the data to be dumped