]> git.tdb.fi Git - libs/datafile.git/commitdiff
Add a method to filter the files loaded from a pack
authorMikko Rasa <tdb@tdb.fi>
Fri, 4 Jan 2013 17:29:58 +0000 (19:29 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 5 Jan 2013 11:01:43 +0000 (13:01 +0200)
source/packsource.cpp
source/packsource.h

index c06b270e30d8ecb4ef4fed14840989ae85e5e47f..d054e1d4ee9e50ef99c8d9c136aee978237113bf 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/strings/format.h>
+#include <msp/strings/regex.h>
 #include "collection.h"
 #include "packsource.h"
 
@@ -9,11 +10,26 @@ namespace DataFile {
 
 void PackSource::add_pack_file(const string &fn)
 {
-       packs.push_back(Pack(fn));
-       Pack &pack = packs.back();
-       DataFile::load(pack, fn);
+       add_pack_file(fn, string());
+}
+
+void PackSource::add_pack_file(const string &fn, const string &filter)
+{
+       Pack *pack = 0;
+       for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
+               if(i->get_filename()==fn)
+                       pack = &*i;
+       if(!pack)
+       {
+               packs.push_back(Pack(fn));
+               pack = &packs.back();
+               DataFile::load(*pack, fn);
+       }
 
-       pack.collect_objects(objects);
+       FileMap pack_files;
+       pack->collect_files(pack_files, filter);
+       for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
+               i->second->collect_objects(objects);
 }
 
 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
@@ -76,10 +92,20 @@ PackSource::Pack::Pack(const string &fn):
        base_offset(0)
 { }
 
-void PackSource::Pack::collect_objects(ObjectMap &objs) const
+void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
 {
-       for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
-               i->collect_objects(objs);
+       if(filter.empty())
+       {
+               for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
+                       fm[i->get_filename()] = &*i;
+       }
+       else
+       {
+               Regex re(filter);
+               for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
+                       if(re.match(i->get_filename()))
+                               fm[i->get_filename()] = &*i;
+       }
 }
 
 
index e2c84a0ac62135b27f3a09cbf3d6bb711880d93b..f552d2ab479ed952c47a04619b76d75bdde29e83 100644 (file)
@@ -25,6 +25,7 @@ private:
        class File;
        struct Object;
 
+       typedef std::map<std::string, const File *> FileMap;
        typedef std::map<std::string, const Object *> ObjectMap;
 
        class Pack
@@ -49,7 +50,7 @@ private:
                const std::string &get_filename() const { return filename; }
                unsigned get_base_offset() const { return base_offset; }
 
-               void collect_objects(ObjectMap &) const;
+               void collect_files(FileMap &, const std::string &) const;
        };
 
        class File
@@ -109,6 +110,9 @@ public:
        /// Adds a pack file to load objects from.  The index is read immediately.
        void add_pack_file(const std::string &);
 
+       /** Adds a pack file with a regex to filter logical files.  The index is
+       read on the first call; subsequent calls will use cached data. */
+       void add_pack_file(const std::string &, const std::string &);
 
        virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
        virtual NameList get_names(const CollectionItemTypeBase &) const;