-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <set>
+#include <msp/core/algorithm.h>
#include "collection.h"
+using namespace std;
+
namespace Msp {
namespace DataFile {
+Collection::Collection():
+ fallback(0)
+{ }
+
Collection::~Collection()
{
- for(ItemMap::iterator i = items.begin(); i!=items.end(); ++i)
- delete i->second;
- for(ItemKeywordSeq::iterator i = keywords.begin(); i!=keywords.end(); ++i)
- delete *i;
- for(ItemCreatorSeq::iterator i = creators.begin(); i!=creators.end(); ++i)
- delete *i;
+ for(CollectionItemTypeBase *t: types)
+ delete t;
+}
+
+void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var)
+{
+ insert_unique(items, name, var);
+ try
+ {
+ if(type)
+ type->notify_item(name, var);
+ }
+ catch(...)
+ {
+ remove_existing(items, name);
+ throw;
+ }
+}
+
+const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
+{
+ const Variant *var = find_var(name, type);
+ if(var)
+ return *var;
+
+ throw key_error(name);
+}
+
+const Variant *Collection::find_var(const string &name, const CollectionItemTypeBase *type)
+{
+ ItemMap::iterator i = items.find(name);
+ if(i!=items.end())
+ return &i->second;
+
+ if(type)
+ {
+ bool loaded = false;
+ if(type->can_create())
+ {
+ type->create_item(*this, name);
+ loaded = items.count(name);
+ }
+ for(auto j=sources.begin(); (!loaded && j!=sources.end()); ++j)
+ {
+ (*j)->load(*this, *type, name);
+ loaded = items.count(name);
+ }
+ if(!loaded && fallback)
+ if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
+ if(fallback->get_status(name, *fb_type))
+ return fallback->find_var(name, fb_type);
+ }
+
+ i = items.find(name);
+ return (i!=items.end() ? &i->second : 0);
+}
+
+void Collection::gather_items(vector<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
+{
+ for(const auto &kvp: items)
+ if(type.check_item_type(kvp.second))
+ {
+ if(vars)
+ vars->push_back(&kvp.second);
+ if(names)
+ names->push_back(kvp.first);
+ }
+
+ if(include_sources && names)
+ gather_names_from_sources(*names, type);
+}
+
+unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
+{
+ ItemMap::const_iterator i = items.find(name);
+ if(i==items.end())
+ {
+ auto j = find_if(sources, [&name, &type](const CollectionSource *s){ return s->is_loadable(type, name); });
+ if(j!=sources.end())
+ return 2;
+ if(fallback)
+ if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
+ return fallback->get_status(name, *fb_type);
+ return 0;
+ }
+
+ return type.check_item_type(i->second);
+}
+
+CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
+{
+ for(CollectionItemTypeBase *t: types)
+ if(t->is_same_type(type))
+ return t;
+ return 0;
+}
+
+CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
+{
+ for(CollectionItemTypeBase *t: types)
+ if(t->check_item_type(var))
+ return t;
+ return 0;
+}
+
+void Collection::add_source(const CollectionSource &s)
+{
+ sources.push_back(&s);
+}
+
+IO::Seekable *Collection::open_raw(const string &name) const
+{
+ for(const CollectionSource *s: sources)
+ if(IO::Seekable *io = s->open(name))
+ return io;
+
+ return 0;
+}
+
+void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
+{
+ set<string> new_names;
+ for(const CollectionSource *s: sources)
+ for(const string &n: s->get_names(type))
+ if(!items.count(n))
+ new_names.insert(n);
+ names.insert(names.end(), new_names.begin(), new_names.end());
+}
+
+void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
+{
+ for(const CollectionSource *s: sources)
+ for(const string &n: s->get_names(type))
+ if(!items.count(n))
+ {
+ bool loaded = false;
+ if(type.can_create())
+ {
+ type.create_item(*this, n);
+ loaded = items.count(n);
+ }
+ if(!loaded)
+ s->load(*this, type, n);
+ }
}
-bool Collection::contains(const std::string &n) const
+void Collection::set_fallback(Collection *f)
{
- return items.count(n);
+ fallback = f;
}
Collection::Loader::Loader(Collection &c):
coll(c)
{
- for(ItemKeywordSeq::const_iterator i = coll.keywords.begin(); i!=coll.keywords.end(); ++i)
- (*i)->add_to_loader(*this);
+ for(const CollectionItemTypeBase *t: coll.types)
+ t->add_to_loader(*this);
+}
+
+
+CollectionItemTypeBase::~CollectionItemTypeBase()
+{
+ for(ExtractorBase *e: extractors)
+ delete e;
+}
+
+void CollectionItemTypeBase::set_keyword(const string &k)
+{
+ kwd = k;
+ if(suffixes.empty())
+ add_suffix("."+kwd);
+}
+
+void CollectionItemTypeBase::add_suffix(const string &s)
+{
+ suffixes.push_back(s);
+}
+
+bool CollectionItemTypeBase::match_name(const string &name) const
+{
+ for(const string &s: suffixes)
+ if(name.size()>s.size() && !name.compare(name.size()-s.size(), string::npos, s))
+ return true;
+ return false;
}
} // namespace DataFile