3 This file is part of libmspdatafile
4 Copyright © 2006-2008, 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/formatter.h>
16 bool signature_match(char s, char a)
20 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
25 bool signature_match(const string &st_sig, const string &act_sig)
29 else if(act_sig.size()==2 && act_sig[1]=='*')
31 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
37 else if(st_sig.size()==act_sig.size())
39 for(unsigned i=0; i<st_sig.size(); ++i)
40 if(!signature_match(st_sig[i], act_sig[i]))
56 allow_pointer_reload(true),
57 check_sub_loads(false)
62 for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
66 void Loader::load(Parser &p)
70 Statement st = p.parse();
77 void Loader::load(const Statement &st)
79 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
84 void Loader::load_statement(const Statement &st)
90 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
94 act->execute(*this, st);
95 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
96 throw Exception("Substatements were not loaded");
103 e.at(st.get_location());
111 void Loader::load_sub_with(Loader &ldr)
114 throw InvalidState("load_sub called without current statement");
120 void Loader::add(const string &kwd, LoaderAction *act)
122 ActionKey key(kwd, act->get_signature());
123 ActionMap::iterator i = actions.find(key);
133 LoaderAction *Loader::find_action(const ActionKey &key) const
135 ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
136 ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
139 throw KeyError("Unknown keyword", key.keyword);
141 for(ActionMap::const_iterator i=begin; i!=end; ++i)
142 if(signature_match(key.signature, i->first.signature))
145 throw KeyError(format("Keyword '%s' does not accept signature '%s'", key.keyword, key.signature));
149 Loader::ActionKey::ActionKey(const string &k, const string &s):
154 bool Loader::ActionKey::operator<(const ActionKey &other) const
156 if(keyword!=other.keyword)
157 return keyword<other.keyword;
158 return signature<other.signature;
161 } // namespace DataFile