Ticket #8904: archivemember-3.patch

File archivemember-3.patch, 10.3 KB (added by lordhoto, 16 years ago)

Patch against r34726 (Updated, ZipArchive + KYRA adopted)

  • common/unzip.cpp

     
    13921392        return (_zipFile && unzLocateFile(_zipFile, name.c_str(), 2) == UNZ_OK);
    13931393}
    13941394
    1395 int ZipArchive::getAllNames(Common::StringList &list) {
    1396         if (!_zipFile)
    1397                 return 0;
    1398 
    1399         if (unzGoToFirstFile(_zipFile) != UNZ_OK)
    1400                 return 0;
    1401 
    1402         char fileNameBuffer[UNZ_MAXFILENAMEINZIP + 1];
    1403         int fileCount = 0;
    1404 
    1405         do {
    1406                 unzGetCurrentFileInfo(_zipFile, 0, fileNameBuffer, UNZ_MAXFILENAMEINZIP + 1, 0, 0, 0, 0);
    1407                 list.push_back(Common::String(fileNameBuffer));
    1408                 fileCount++;
    1409         } while (unzGoToNextFile(_zipFile) == UNZ_OK);
    1410 
    1411         return fileCount;
    1412 }
    1413 
    1414 /*
    14151395int ZipArchive::listMembers(Common::ArchiveMemberList &list) {
    14161396        if (!_zipFile)
    14171397                return 0;
     
    14241404                unzGetCurrentFileInfo(_zipFile, NULL,
    14251405                                                                szCurrentFileName, sizeof(szCurrentFileName)-1,
    14261406                                                                NULL, 0, NULL, 0);
    1427                
    1428                 szCurrentFileName
     1407                list.push_back(ArchiveMemberList::value_type(new GenericArchiveMember(szCurrentFileName, this)));
    14291408                matches++;
    1430                 err = unzGoToNextFile(file);
     1409                err = unzGoToNextFile(_zipFile);
    14311410        }
    1432         return 0;
     1411
     1412        return matches;
    14331413}
    1434 */
    14351414
    14361415Common::SeekableReadStream *ZipArchive::openFile(const Common::String &name) {
    14371416        if (!_zipFile)
  • common/archive.cpp

     
    3030
    3131namespace Common {
    3232
     33GenericArchiveMember::GenericArchiveMember(String name, Archive *parent)
     34        : _parent(parent), _name(name) {
     35}
    3336
    34 int Archive::matchPattern(StringList &list, const String &pattern) {
     37String GenericArchiveMember::getName() const {
     38        return _name;
     39}
     40
     41SeekableReadStream *GenericArchiveMember::open() {
     42        return _parent->openFile(_name);
     43}
     44
     45
     46int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    3547        // Get all "names" (TODO: "files" ?)
    36         StringList allNames;
    37         getAllNames(allNames);
     48        ArchiveMemberList allNames;
     49        listMembers(allNames);
    3850
    3951        int matches = 0;
    4052
     
    4254        String lowercasePattern = pattern;
    4355        lowercasePattern.toLowercase();
    4456
    45         StringList::iterator it = allNames.begin();
     57        ArchiveMemberList::iterator it = allNames.begin();
    4658        for ( ; it != allNames.end(); it++) {
    47                 if (it->matchString(lowercasePattern)) {
     59                if ((*it)->getName().matchString(lowercasePattern)) {
    4860                        list.push_back(*it);
    4961                        matches++;
    5062                }
     
    5365        return matches;
    5466}
    5567
     68/**
     69 *  FSDirectoryMemeber is the implementation of ArchiveMember used by
     70 *  by FSDirectory. It is right now a light wrapper or FSNode.
     71 */
     72class FSDirectoryMember : public ArchiveMember {
     73        FSNode  _node;
     74 
     75public:
     76        FSDirectoryMember(FSNode &node) : _node(node) {
     77        }
    5678
     79        /*
     80                NOTE/FIXME: since I assume that the only use case for getName()
     81                is for error messages, I am returning the full path of the node
     82                here. This seems better than we did before, when matchPattern
     83                and getAllNames used to work with StringList, and we used to
     84                put the relative path of the file to the list instead.
     85        */
     86        String getName() const {
     87                return _node.getPath();
     88        }
     89
     90        SeekableReadStream *open() {
     91                return _node.openForReading();
     92        }
     93};
     94
     95typedef SharedPtr<FSDirectoryMember> FSDirectoryMemberPtr;
     96
    5797FSDirectory::FSDirectory(const FSNode &node, int depth)
    5898  : _node(node), _cached(false), _depth(depth) {
    5999}
     
    160200
    161201}
    162202
    163 int FSDirectory::matchPattern(StringList &list, const String &pattern) {
     203int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    164204        if (!_node.isDirectory())
    165205                return 0;
    166206
     
    170210                _cached = true;
    171211        }
    172212
    173         // Small optimization: Ensure the StringList has to grow at most once
    174         list.reserve(list.size() + _fileCache.size());
    175        
    176         // Add all filenames from our cache
     213        String lowercasePattern(pattern);
     214        lowercasePattern.toLowercase();
     215
     216        int matches = 0;
    177217        NodeCache::iterator it = _fileCache.begin();
    178218        for ( ; it != _fileCache.end(); it++) {
    179                 if (it->_key.matchString(pattern))
    180                         list.push_back(it->_key);
     219                if ((*it)._key.matchString(lowercasePattern)) {
     220                        list.push_back(FSDirectoryMemberPtr(new FSDirectoryMember((*it)._value)));
     221                        matches++;
     222                }
    181223        }
    182        
    183         return _fileCache.size();
     224        return matches;
    184225}
    185226
    186 int FSDirectory::getAllNames(StringList &list) {
    187         if (!_node.isDirectory())
    188                 return 0;
     227int FSDirectory::listMembers(ArchiveMemberList &list) {
     228        return listMatchingMembers(list, "*");
     229}
    189230
    190         // Cache dir data
    191         if (!_cached) {
    192                 cacheDirectoryRecursive(_node, _depth, "");
    193                 _cached = true;
    194         }
    195231
    196         // Small optimization: Ensure the StringList has to grow at most once
    197         list.reserve(list.size() + _fileCache.size());
    198        
    199         // Add all filenames from our cache
    200         NodeCache::iterator it = _fileCache.begin();
    201         for ( ; it != _fileCache.end(); it++) {
    202                 list.push_back((*it)._key);
    203         }
    204        
    205         return _fileCache.size();
    206 }
    207232
    208233
    209234
     
    289314        return false;
    290315}
    291316
    292 int SearchSet::matchPattern(StringList &list, const String &pattern) {
     317int SearchSet::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    293318        int matches = 0;
    294319
    295320        ArchiveList::iterator it = _list.begin();
    296321        for ( ; it != _list.end(); it++) {
    297                 matches += (*it)._arc->matchPattern(list, pattern);
     322                matches += (*it)._arc->listMatchingMembers(list, pattern);
    298323        }
    299324
    300325        return matches;
    301326}
    302327
    303 int SearchSet::getAllNames(StringList &list) {
     328int SearchSet::listMembers(ArchiveMemberList &list) {
    304329        int matches = 0;
    305330
    306331        ArchiveList::iterator it = _list.begin();
    307332        for ( ; it != _list.end(); it++) {
    308                 matches += (*it)._arc->getAllNames(list);
     333                matches += (*it)._arc->listMembers(list);
    309334        }
    310335
    311336        return matches;
  • common/unzip.h

     
    4444        bool isOpen() const;
    4545
    4646        virtual bool hasFile(const String &name);
    47         virtual int getAllNames(StringList &list);
     47        virtual int listMembers(Common::ArchiveMemberList &list);
    4848        virtual Common::SeekableReadStream *openFile(const Common::String &name);
    4949};
    5050
  • common/archive.h

     
    3636
    3737namespace Common {
    3838
     39class ArchiveMember {
     40public:
     41        virtual ~ArchiveMember() { }
     42        virtual String getName() const = 0;
     43        virtual SeekableReadStream *open() = 0;
     44};
     45
     46typedef List<SharedPtr<ArchiveMember> > ArchiveMemberList;
     47
     48class Archive;
     49
    3950/**
     51 * Simple ArchiveMemeber implementation which allows
     52 * creation of ArchiveMember compatible objects via
     53 * a simple Archive and name pair.
     54 */
     55class GenericArchiveMember : public ArchiveMember {
     56        Archive *_parent;
     57        String _name;
     58public:
     59        GenericArchiveMember(String name, Archive *parent);
     60        String getName() const;
     61        SeekableReadStream *open();
     62};
     63
     64/**
    4065 * FilePtr is a convenient way to keep track of a SeekableReadStream without
    4166 * having to worry about releasing its memory.
    4267 */
     
    6388         *
    6489         * @return the number of names added to list
    6590         */
    66         virtual int matchPattern(StringList &list, const String &pattern);
     91        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
    6792
    6893        /**
    6994         * Add all the names present in the Archive to list. Returned
     
    7297         *
    7398         * @return the number of names added to list
    7499         */
    75         virtual int getAllNames(StringList &list) = 0;
     100        virtual int listMembers(ArchiveMemberList &list) = 0;
    76101
    77102        /**
    78103         * Create a stream bound to a file in the archive.
     
    138163        FSDirectory *getSubDirectory(const String &name);
    139164
    140165        virtual bool hasFile(const String &name);
    141         virtual int matchPattern(StringList &list, const String &pattern);
    142         virtual int getAllNames(StringList &list);
     166        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
     167        virtual int listMembers(ArchiveMemberList &list);
    143168        virtual SeekableReadStream *openFile(const String &name);
    144169};
    145170
     
    195220        void setPriority(const String& name, int priority);
    196221
    197222        virtual bool hasFile(const String &name);
    198         virtual int matchPattern(StringList &list, const String &pattern);
    199         virtual int getAllNames(StringList &list);
     223        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
     224        virtual int listMembers(ArchiveMemberList &list);
    200225
    201226        /**
    202227         * Implements openFile from Archive base class. The current policy is
  • engines/kyra/resource_intern.cpp

     
    5151        return (_files.find(name) != _files.end());
    5252}
    5353
    54 int PlainArchive::getAllNames(Common::StringList &list) {
     54int PlainArchive::listMembers(Common::ArchiveMemberList &list) {
    5555        int count = 0;
    5656
    5757        for (FileMap::const_iterator i = _files.begin(); i != _files.end(); ++i) {
    58                 list.push_back(i->_key);
     58                list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(i->_key, this)));
    5959                ++count;
    6060        }
    6161
     
    9898        return (_files.find(name) != _files.end());
    9999}
    100100
    101 int CachedArchive::getAllNames(Common::StringList &list) {
     101int CachedArchive::listMembers(Common::ArchiveMemberList &list) {
    102102        int count = 0;
    103103
    104104        for (FileMap::const_iterator i = _files.begin(); i != _files.end(); ++i) {
    105                 list.push_back(i->_key);
     105                list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(i->_key, this)));
    106106                ++count;
    107107        }
    108108
  • engines/kyra/resource_intern.h

     
    5050        PlainArchive(Resource *owner, const Common::String &filename, const FileInputList &files);
    5151
    5252        bool hasFile(const Common::String &name);
    53         int getAllNames(Common::StringList &list);
     53        int listMembers(Common::ArchiveMemberList &list);
    5454        Common::SeekableReadStream *openFile(const Common::String &name);
    5555private:
    5656        struct Entry {
     
    8080        ~CachedArchive();
    8181
    8282        bool hasFile(const Common::String &name);
    83         int getAllNames(Common::StringList &list);
     83        int listMembers(Common::ArchiveMemberList &list);
    8484        Common::SeekableReadStream *openFile(const Common::String &name);
    8585private:
    8686        struct Entry {