X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floader.cpp;h=6a4a8d845dee3642a9091d31fc847f067937b4e9;hb=feb225725f96c1717f391855b7955937215b013e;hp=a05e5f0dfe187b0060cd307f21146d7f199dbb55;hpb=5520420ecb8da0f6528b2681569f7504f75ddf38;p=libs%2Fdatafile.git diff --git a/source/loader.cpp b/source/loader.cpp index a05e5f0..6a4a8d8 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -48,6 +48,7 @@ namespace Msp { namespace DataFile { Loader::Loader(): + actions(0), cur_st(0), direct(false), check_sub_loads(false) @@ -55,12 +56,15 @@ Loader::Loader(): Loader::~Loader() { - for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i) + for(ActionMap::iterator i=local_actions.begin(); i!=local_actions.end(); ++i) delete i->second; } void Loader::load(Parser &p) { + if(!actions) + throw logic_error("no actions"); + while(p) { if(p.peek(0)) @@ -78,6 +82,9 @@ void Loader::load(Parser &p) void Loader::load(const Statement &st) { + if(!actions) + throw logic_error("no actions"); + for(list::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i) load_statement(*i); finish(); @@ -116,7 +123,7 @@ void Loader::load_statement(const Statement &st) if(!aux_loaders.empty() && !has_action(key)) { - for(list::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i) + for(vector::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i) if((*i)->has_action(key)) return (*i)->load_statement(st); } @@ -156,17 +163,30 @@ void Loader::load_sub_with(Loader &ldr) throw logic_error("no current statement"); } +void Loader::set_actions(ActionMap &a) +{ + if(actions) + throw logic_error("actions already set"); + + actions = &a; + if(a.empty()) + init_actions(); +} + void Loader::add(const string &kwd, LoaderAction *act) { + if(!actions) + actions = &local_actions; + StatementKey key(kwd, (act ? act->get_signature() : "*")); - ActionMap::iterator i = actions.find(key); - if(i!=actions.end()) + ActionMap::iterator i = actions->find(key); + if(i!=actions->end()) { delete i->second; i->second = act; } else - actions[key] = act; + (*actions)[key] = act; } void Loader::add_auxiliary_loader(Loader &ldr) @@ -176,8 +196,11 @@ void Loader::add_auxiliary_loader(Loader &ldr) bool Loader::has_action(const StatementKey &key) const { - ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string())); - for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i) + if(!actions) + return false; + + ActionMap::const_iterator i = actions->lower_bound(StatementKey(key.keyword, string())); + for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i) if(signature_match(key.signature, i->first.signature)) return true; return false; @@ -185,8 +208,11 @@ bool Loader::has_action(const StatementKey &key) const LoaderAction *Loader::find_action(const StatementKey &key) const { - ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string())); - ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~")); + if(!actions) + return 0; + + ActionMap::const_iterator begin = actions->lower_bound(StatementKey(key.keyword, string())); + ActionMap::const_iterator end = actions->upper_bound(StatementKey(key.keyword, "~")); if(begin==end) throw unknown_keyword(key.keyword); @@ -203,7 +229,7 @@ LoaderAction *Loader::find_action(const StatementKey &key) const } } - if(!act) + if(!match) throw invalid_signature(key.keyword, key.signature); return act;