]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.cpp
Add an API to open files from a collection's sources
[libs/datafile.git] / source / collection.cpp
index fbe894a8bb4b073c0baa227655bd2d3ecf3781e3..984fb74da8e7a6007645e6f85aa002b8ae6f894a 100644 (file)
@@ -5,6 +5,10 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
+Collection::Collection():
+       fallback(0)
+{ }
+
 Collection::~Collection()
 {
        for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
@@ -30,6 +34,16 @@ const Variant &Collection::get_var(const string &name, const CollectionItemTypeB
                        (*j)->load(*this, *type, name);
                        loaded = items.count(name);
                }
+               if(!loaded && fallback)
+               {
+                       for(TypeList::const_iterator j=fallback->types.begin(); j!=fallback->types.end(); ++j)
+                               if((*j)->is_same_type(*type))
+                               {
+                                       if(fallback->get_status(name, **j))
+                                               return fallback->get_var(name, *j);
+                                       break;
+                               }
+               }
        }
 
        return get_item(items, name);
@@ -77,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)
@@ -99,6 +122,11 @@ void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
        }
 }
 
+void Collection::set_fallback(Collection *f)
+{
+       fallback = f;
+}
+
 
 Collection::Loader::Loader(Collection &c):
        coll(c)