]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.cpp
Remove pointer reload prevention feature; the purpose it was added for is now gone
[libs/datafile.git] / source / loader.cpp
1 #include <msp/strings/format.h>
2 #include "loader.h"
3 #include "type.h"
4
5 using namespace std;
6
7 namespace {
8
9 bool signature_match(char s, char a)
10 {
11         if(s==a)
12                 return true;
13         if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
14                 return true;
15         return false;
16 }
17
18 bool signature_match(const string &st_sig, const string &act_sig)
19 {
20         if(act_sig=="*")
21                 return true;
22         else if(act_sig.size()==2 && act_sig[1]=='*')
23         {
24                 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
25                         if(*i!=act_sig[0])
26                                 return false;
27
28                 return true;
29         }
30         else if(st_sig.size()==act_sig.size())
31         {
32                 for(unsigned i=0; i<st_sig.size(); ++i)
33                         if(!signature_match(st_sig[i], act_sig[i]))
34                                 return false;
35
36                 return true;
37         }
38         else
39                 return false;
40 }
41
42 }
43
44 namespace Msp {
45 namespace DataFile {
46
47 Loader::Loader():
48         cur_st(0),
49         check_sub_loads(false)
50 { }
51
52 Loader::~Loader()
53 {
54         for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
55                 delete i->second;
56 }
57
58 void Loader::load(Parser &p)
59 {
60         while(p)
61         {
62                 Statement st = p.parse();
63                 if(st.valid)
64                         load_statement(st);
65         }
66         finish();
67 }
68
69 void Loader::load(const Statement &st)
70 {
71         for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
72                 load_statement(*i);
73         finish();
74 }
75
76 void Loader::load_statement(const Statement &st)
77 {
78         cur_st = &st;
79
80         try
81         {
82                 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
83                 if(act)
84                 {
85                         sub_loaded = false;
86                         act->execute(*this, st);
87                         if(check_sub_loads && !st.sub.empty() && !sub_loaded)
88                                 throw Exception("Substatements were not loaded");
89                 }
90         }
91         catch(Exception &e)
92         {
93                 cur_st = 0;
94                 if(!e.where()[0])
95                         e.at(st.get_location());
96
97                 throw;
98         }
99
100         cur_st = 0;
101 }
102
103 void Loader::load_sub_with(Loader &ldr)
104 {
105         if(!cur_st)
106                 throw InvalidState("load_sub called without current statement");
107
108         ldr.load(*cur_st);
109         sub_loaded = true;
110 }
111
112 void Loader::add(const string &kwd, LoaderAction *act)
113 {
114         ActionKey key(kwd, (act ? act->get_signature() : "*"));
115         ActionMap::iterator i = actions.find(key);
116         if(i!=actions.end())
117         {
118                 delete i->second;
119                 i->second = act;
120         }
121         else
122                 actions[key] = act;
123 }
124
125 LoaderAction *Loader::find_action(const ActionKey &key) const
126 {
127         ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
128         ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
129
130         if(begin==end)
131                 throw KeyError("Unknown keyword", key.keyword);
132
133         for(ActionMap::const_iterator i=begin; i!=end; ++i)
134                 if(signature_match(key.signature, i->first.signature))
135                         return i->second;
136
137         throw KeyError(format("Keyword '%s' does not accept signature '%s'", key.keyword, key.signature));
138 }
139
140
141 Loader::ActionKey::ActionKey(const string &k, const string &s):
142         keyword(k),
143         signature(s)
144 { }
145
146 bool Loader::ActionKey::operator<(const ActionKey &other) const
147 {
148         if(keyword!=other.keyword)
149                 return keyword<other.keyword;
150         return signature<other.signature;
151 }
152
153 } // namespace DataFile
154 } // namespace Msp