Ticket #8904: archivemember.patch

File archivemember.patch, 5.7 KB (added by peres, 11 years ago)

ArchiveMember + new implementation of matchPattern and getAllNames. Fixed patch.

  • archive.cpp

     
    3030namespace Common {
    3131
    3232
    33 int Archive::matchPattern(StringList &list, const String &pattern) {
     33int Archive::matchPattern(ArchiveMemberList &list, const String &pattern) {
    3434        // Get all "names" (TODO: "files" ?)
    35         StringList allNames;
     35        ArchiveMemberList allNames;
    3636        getAllNames(allNames);
    3737
    3838        int matches = 0;
     
    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::getAllNames(StringList &list) {
     191int FSDirectory::getAllNames(ArchiveMemberList &list) {
    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        
    175201        // Add all filenames from our cache
    176202        NodeCache::iterator it = _fileCache.begin();
    177203        for ( ; it != _fileCache.end(); it++) {
    178                 list.push_back((*it)._key);
     204                list.push_back(FSDirectoryMemberPtr(new FSDirectoryMember((*it)._value)));
    179205        }
    180        
     206
    181207        return _fileCache.size();
    182208}
    183209
    184210
    185211
     212
     213
    186214SearchSet::ArchiveList::iterator SearchSet::find(const String &name) const {
    187215        ArchiveList::iterator it = _list.begin();
    188216        for ( ; it != _list.end(); it++) {
     
    265293        return false;
    266294}
    267295
    268 int SearchSet::matchPattern(StringList &list, const String &pattern) {
     296int SearchSet::matchPattern(ArchiveMemberList &list, const String &pattern) {
    269297        // Shall we short circuit out if pattern is empty?
    270298
    271299        int matches = 0;
     
    278306        return matches;
    279307}
    280308
     309
    281310SeekableReadStream *SearchSet::openFile(const String &name) {
    282311        if (name.empty()) {
    283312                return 0;
  • archive.h

     
    3737namespace Common {
    3838
    3939/**
     40 * ArchiveMember is the base class for the objects returned when Archive
     41 * is searched using matchPattern() or getAllNames(), thus - possibly -
     42 * yielding multiple matches.
     43 */
     44class ArchiveMember {
     45public:
     46        virtual ~ArchiveMember() { }
     47
     48        /**
     49         * Return a name (or description) of the instance.
     50         */
     51        virtual String getName() const = 0;
     52
     53        /**
     54         * Open the underlying file/handle/entity for reading.
     55         */
     56        virtual SeekableReadStream *open() = 0;
     57};
     58
     59typedef List<SharedPtr<ArchiveMember> > ArchiveMemberList;
     60
     61
     62/**
    4063 * FilePtr is a convenient way to keep track of a SeekableReadStream without
    4164 * having to worry about releasing its memory.
    4265 */
     
    5780        virtual bool hasFile(const String &name) = 0;
    5881
    5982        /**
    60          * Add all the names present in the Archive which match pattern to
    61          * list. Returned names can be used as parameters to openFile.
     83         * Add all the objects present in the Archive which match pattern to
     84         * list.
    6285         * Must not remove elements from the list.
    6386         *
    6487         * @return the number of names added to list
    6588         */
    66         virtual int matchPattern(StringList &list, const String &pattern);
     89        virtual int matchPattern(ArchiveMemberList &list, const String &pattern);
    6790
    6891        /**
    69          * Add all the names present in the Archive to list. Returned
    70          * names can be used as parameters to openFile.
     92         * Add all the names present in the Archive to list.
    7193         * Must not remove elements from the list.
    7294         *
    7395         * @return the number of names added to list
    7496         */
    75         virtual int getAllNames(StringList &list) = 0;
     97        virtual int getAllNames(ArchiveMemberList &list) = 0;
    7698
    7799        /**
    78100         * Create a stream bound to a file in the archive.
     
    138160        FSDirectory *getSubDirectory(const String &name);
    139161
    140162        virtual bool hasFile(const String &name);
    141         virtual int getAllNames(StringList &list);
     163        virtual int getAllNames(ArchiveMemberList &list);
    142164        virtual SeekableReadStream *openFile(const String &name);
    143165};
    144166
     
    191213        void setPriority(const String& name, uint priority);
    192214
    193215        virtual bool hasFile(const String &name);
    194         virtual int matchPattern(StringList &list, const String &pattern);
    195         virtual int getAllNames(StringList &list) {
     216        virtual int matchPattern(ArchiveMemberList &list, const String &pattern);
     217        virtual int getAllNames(ArchiveMemberList &list) {
    196218                return matchPattern(list, "*");
    197219        }
    198220
     
    239261
    240262
    241263        virtual bool hasFile(const String &name);
    242         virtual int getAllNames(StringList &list) {
     264        virtual int getAllNames(ArchiveMemberList &list) {
    243265                return matchPattern(list, "*");
    244266        }
    245267