]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.cpp
Some more code reformatting
[libs/datafile.git] / source / loader.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008  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 Loader::~Loader()
16 {
17         for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
18                 delete i->second;
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         finish();
30 }
31
32 void Loader::load(const Statement &st)
33 {
34         for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
35                 load_statement(*i);
36         finish();
37 }
38
39 void Loader::load_statement(const Statement &st)
40 {
41         cur_st = &st;
42         ActionMap::iterator j = actions.find(st.keyword);
43         if(j==actions.end())
44                 throw_at(KeyError("Unknown keyword", st.keyword), st.get_location());
45         if(j->second)
46         {
47                 try
48                 {
49                         j->second->execute(*this, st);
50                 }
51                 catch(Exception &e)
52                 {
53                         if(!e.where()[0])
54                                 e.at(st.get_location());
55                         throw;
56                 }
57         }
58         cur_st = 0;
59 }
60
61 void Loader::load_sub_with(Loader &ldr)
62 {
63         if(!cur_st)
64                 throw InvalidState("load_sub called without current statement");
65
66         ldr.load(*cur_st);
67 }
68
69 void Loader::add(const string &k, LoaderAction *a)
70 {
71         ActionMap::iterator i = actions.find(k);
72         if(i!=actions.end())
73         {
74                 delete i->second;
75                 i->second = a;
76         }
77         else
78                 actions[k] = a;
79 }
80
81 } // namespace DataFile
82 } // namespace Msp