]> git.tdb.fi Git - libs/datafile.git/commitdiff
Add an API to open files from a collection's sources
authorMikko Rasa <tdb@tdb.fi>
Wed, 16 Jan 2013 14:01:28 +0000 (16:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 16 Jan 2013 14:01:28 +0000 (16:01 +0200)
source/collection.cpp
source/collection.h
source/collectionsource.h
source/directorysource.cpp
source/directorysource.h
source/packsource.cpp
source/packsource.h

index 070991948d33d7315074ec6bb480cc06344d45cc..984fb74da8e7a6007645e6f85aa002b8ae6f894a 100644 (file)
@@ -91,6 +91,15 @@ void Collection::add_source(CollectionSource &s)
        sources.push_back(&s);
 }
 
+IO::Seekable *Collection::open_from_sources(const string &name)
+{
+       for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+               if(IO::Seekable *io = (*i)->open(name))
+                       return io;
+
+       throw IO::file_not_found(name);
+}
+
 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
 {
        for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
index ee7eb469cf53d5ee1ab8d9cd06a0b88688511b06..0d2dc6be2fd7a6c4043ff4db18bfdc32a04bb270 100644 (file)
@@ -264,6 +264,8 @@ private:
 protected:
        void add_source(CollectionSource &);
 
+       IO::Seekable *open_from_sources(const std::string &);
+
 private:
        void gather_names_from_sources(std::list<std::string> &, const CollectionItemTypeBase &) const;
 
index 61bb11555decbbf85d36500f18ed829073b64f31..398f7942c85c6260d514796beac77146b5cc7d32 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list>
 #include <string>
+#include <msp/io/seekable.h>
 
 namespace Msp {
 namespace DataFile {
@@ -31,6 +32,10 @@ public:
        virtual NameList get_names(const CollectionItemTypeBase &type) const = 0;
 
        virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const = 0;
+
+       /** Opens a file from the source.  The caller is responsible for deleting
+       the returned object when done with it. */
+       virtual IO::Seekable *open(const std::string &) const = 0;
 };
 
 } // namespace DataFile
index 5d70bb5deb3f9202a5f2aa0686b2ca7e2c2f25a5..4bf9f5cd3d12b0808f288b9e3cd647a5ee0db682 100644 (file)
@@ -40,6 +40,15 @@ void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type,
        }
 }
 
+IO::Seekable *DirectorySource::open(const string &name) const
+{
+       FS::Path file;
+       if(lookup_file(name, file))
+               return new IO::BufferedFile(file.str());
+
+       return 0;
+}
+
 bool DirectorySource::lookup_file(const string &name, FS::Path &result) const
 {
        ObjectMap::const_iterator i = objects.find(name);
index dfce833562abb0cd3fd34e57d366740d1615da16..b1324a711d2ac6c7f8e489215589c39fc87b62f8 100644 (file)
@@ -23,6 +23,7 @@ public:
        virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
        virtual NameList get_names(const CollectionItemTypeBase &) const;
        virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const;
+       virtual IO::Seekable *open(const std::string &) const;
 
        bool lookup_file(const std::string &, FS::Path &) const;
 };
index d22c7dc4235990591fffbf7c9d2dafba0eca3aaa..c8968665d41d23c936ef8efbc87fa1b5dca5bd7a 100644 (file)
@@ -38,6 +38,7 @@ void PackSource::add_pack_file(const string &fn, const string &filter)
 
        FileMap pack_files;
        pack->collect_files(pack_files, filter);
+       files.insert(pack_files.begin(), pack_files.end());
        for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
                i->second->collect_objects(objects);
 }
@@ -96,6 +97,15 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons
                type.load_item(coll, parser, name);
 }
 
+IO::Seekable *PackSource::open(const string &fn) const
+{
+       FileMap::const_iterator i = files.find(fn);
+       if(i!=files.end())
+               return i->second->open().release();
+
+       return 0;
+}
+
 
 PackSource::Pack::Pack(const string &fn):
        filename(fn),
@@ -128,7 +138,7 @@ PackSource::File::File(const Pack &p, const string &fn):
        loaded(false)
 { }
 
-RefPtr<IO::Base> PackSource::File::open() const
+RefPtr<IO::Seekable> PackSource::File::open() const
 {
        IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
        IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
index f552d2ab479ed952c47a04619b76d75bdde29e83..10e1433279aaa36dee9b1dda39938af5ab6a42b2 100644 (file)
@@ -77,7 +77,7 @@ private:
        public:
                File(const Pack &, const std::string &);
 
-               RefPtr<IO::Base> open() const;
+               RefPtr<IO::Seekable> open() const;
                const std::string &get_filename() const { return filename; }
                std::string get_full_name() const;
                bool is_collection() const { return collection; }
@@ -104,6 +104,7 @@ private:
        };
 
        std::list<Pack> packs;
+       FileMap files;
        ObjectMap objects;
 
 public:
@@ -117,6 +118,7 @@ public:
        virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const;
        virtual NameList get_names(const CollectionItemTypeBase &) const;
        virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const;
+       virtual IO::Seekable *open(const std::string &) const;
 };
 
 } // namespace DataFile