Ticket #8904: archivemember-2.patch

File archivemember-2.patch, 6.0 KB (added by peres, 16 years ago)
  • archive.cpp

     
    3030namespace Common {
    3131
    3232
    33 int Archive::matchPattern(StringList &list, const String &pattern) {
     33int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    3434        // Get all "names" (TODO: "files" ?)
    35         StringList allNames;
    36         getAllNames(allNames);
     35        ArchiveMemberList allNames;
     36        listMembers(allNames);
    3737
    3838        int matches = 0;
    3939
     
    4141        String lowercasePattern = pattern;
    4242        lowercasePattern.toLowercase();
    4343
    44         StringList::iterator it = allNames.begin();
     44        ArchiveMemberList::iterator it = allNames.begin();
    4545        for ( ; it != allNames.end(); it++) {
    46                 if (it->matchString(lowercasePattern)) {
     46                if ((*it)->getName().matchString(lowercasePattern)) {
    4747                        list.push_back(*it);
    4848                        matches++;
    4949                }
     
    5252        return matches;
    5353}
    5454
     55/**
     56 *  FSDirectoryMemeber is the implementation of ArchiveMember used by
     57 *  by FSDirectory. It is right now a light wrapper or FSNode.
     58 */
     59class FSDirectoryMember : public ArchiveMember {
     60        FilesystemNode  _node;
    5561
     62public:
     63        FSDirectoryMember(FilesystemNode &node) : _node(node) {
     64        }
     65
     66        /*
     67                NOTE/FIXME: since I assume that the only use case for getName()
     68                is for error messages, I am returning the full path of the node
     69                here. This seems better than we did before, when matchPattern
     70                and getAllNames used to work with StringList, and we used to
     71                put the relative path of the file to the list instead.
     72        */
     73        String getName() const {
     74                return _node.getPath();
     75        }
     76
     77        SeekableReadStream *open() {
     78                return _node.openForReading();
     79        }
     80};
     81
     82typedef SharedPtr<FSDirectoryMember> FSDirectoryMemberPtr;
     83
     84
    5685FSDirectory::FSDirectory(const FilesystemNode &node, int depth)
    5786  : _node(node), _cached(false), _depth(depth) {
    5887}
     
    159188
    160189}
    161190
    162 int FSDirectory::matchPattern(StringList &list, const String &pattern) {
     191int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    163192        if (!_node.isDirectory())
    164193                return 0;
    165194
     
    169198                _cached = true;
    170199        }
    171200
    172         // Small optimization: Ensure the StringList has to grow at most once
    173         list.reserve(list.size() + _fileCache.size());
    174        
    175         // Add all filenames from our cache
     201        String lowercasePattern(pattern);
     202        lowercasePattern.toLowercase();
     203
     204        int matches = 0;
    176205        NodeCache::iterator it = _fileCache.begin();
    177206        for ( ; it != _fileCache.end(); it++) {
    178                 if (it->_key.matchString(pattern))
    179                         list.push_back(it->_key);
     207                if ((*it)._key.matchString(lowercasePattern)) {
     208                        list.push_back(FSDirectoryMemberPtr(new FSDirectoryMember((*it)._value)));
     209                        matches++;
     210                }
    180211        }
    181        
    182         return _fileCache.size();
     212        return matches;
    183213}
    184214
    185 int FSDirectory::getAllNames(StringList &list) {
    186         if (!_node.isDirectory())
    187                 return 0;
     215int FSDirectory::listMembers(ArchiveMemberList &list) {
     216        return listMatchingMembers(list, "*");
     217}
    188218
    189         // Cache dir data
    190         if (!_cached) {
    191                 cacheDirectoryRecursive(_node, _depth, "");
    192                 _cached = true;
    193         }
    194219
    195         // Small optimization: Ensure the StringList has to grow at most once
    196         list.reserve(list.size() + _fileCache.size());
    197        
    198         // Add all filenames from our cache
    199         NodeCache::iterator it = _fileCache.begin();
    200         for ( ; it != _fileCache.end(); it++) {
    201                 list.push_back((*it)._key);
    202         }
    203        
    204         return _fileCache.size();
    205 }
    206220
    207221
    208222
     
    288302        return false;
    289303}
    290304
    291 int SearchSet::matchPattern(StringList &list, const String &pattern) {
     305int SearchSet::listMatchingMembers(ArchiveMemberList &list, const String &pattern) {
    292306        int matches = 0;
    293307
    294308        ArchiveList::iterator it = _list.begin();
    295309        for ( ; it != _list.end(); it++) {
    296                 matches += (*it)._arc->matchPattern(list, pattern);
     310                matches += (*it)._arc->listMatchingMembers(list, pattern);
    297311        }
    298312
    299313        return matches;
    300314}
    301315
    302 int SearchSet::getAllNames(StringList &list) {
     316int SearchSet::listMembers(ArchiveMemberList &list) {
    303317        int matches = 0;
    304318
    305319        ArchiveList::iterator it = _list.begin();
    306320        for ( ; it != _list.end(); it++) {
    307                 matches += (*it)._arc->getAllNames(list);
     321                matches += (*it)._arc->listMembers(list);
    308322        }
    309323
    310324        return matches;
  • 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
    3948/**
    4049 * FilePtr is a convenient way to keep track of a SeekableReadStream without
    4150 * having to worry about releasing its memory.
     
    6372         *
    6473         * @return the number of names added to list
    6574         */
    66         virtual int matchPattern(StringList &list, const String &pattern);
     75        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
    6776
    6877        /**
    6978         * Add all the names present in the Archive to list. Returned
     
    7281         *
    7382         * @return the number of names added to list
    7483         */
    75         virtual int getAllNames(StringList &list) = 0;
     84        virtual int listMembers(ArchiveMemberList &list) = 0;
    7685
    7786        /**
    7887         * Create a stream bound to a file in the archive.
     
    138147        FSDirectory *getSubDirectory(const String &name);
    139148
    140149        virtual bool hasFile(const String &name);
    141         virtual int matchPattern(StringList &list, const String &pattern);
    142         virtual int getAllNames(StringList &list);
     150        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
     151        virtual int listMembers(ArchiveMemberList &list);
    143152        virtual SeekableReadStream *openFile(const String &name);
    144153};
    145154
     
    192201        void setPriority(const String& name, uint priority);
    193202
    194203        virtual bool hasFile(const String &name);
    195         virtual int matchPattern(StringList &list, const String &pattern);
    196         virtual int getAllNames(StringList &list);
     204        virtual int listMatchingMembers(ArchiveMemberList &list, const String &pattern);
     205        virtual int listMembers(ArchiveMemberList &list);
    197206
    198207        /**
    199208         * Implements openFile from Archive base class. The current policy is