]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.cpp
0a9f68f249a8d56fe76ab5ba36f5c5bfe76a57e8
[libs/datafile.git] / source / loader.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "loader.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace DataFile {
14
15 void Loader::load(const Statement &st)
16 {
17         for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
18                 load_statement(*i);
19 }
20
21 void Loader::load(Parser &p)
22 {
23         while(p)
24         {
25                 Statement st=p.parse();
26                 if(st.valid)
27                         load_statement(st);
28         }
29 }
30
31 Loader::~Loader()
32 {
33         for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
34                 delete i->second;
35 }
36
37 void Loader::add(const string &k, LoaderAction *a)
38 {
39         ActionMap::iterator i=actions.find(k);
40         if(i!=actions.end())
41         {
42                 delete i->second;
43                 i->second=a;
44         }
45         else
46                 actions[k]=a;
47 }
48
49 void Loader::load_statement(const Statement &st)
50 {
51         cur_st=&st;
52         ActionMap::iterator j=actions.find(st.keyword);
53         if(j==actions.end())
54                 throw KeyError(st.get_location()+": Unknown keyword '"+st.keyword+"'");
55         if(j->second)
56                 j->second->execute(*this, st);
57         cur_st=0;
58 }
59
60 } // namespace DataFile
61 } // namespace Msp