]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.cpp
Update formatter.h -> format.h
[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         allow_pointer_reload(true),
50         check_sub_loads(false)
51 { }
52
53 Loader::~Loader()
54 {
55         for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
56                 delete i->second;
57 }
58
59 void Loader::load(Parser &p)
60 {
61         while(p)
62         {
63                 Statement st = p.parse();
64                 if(st.valid)
65                         load_statement(st);
66         }
67         finish();
68 }
69
70 void Loader::load(const Statement &st)
71 {
72         for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
73                 load_statement(*i);
74         finish();
75 }
76
77 void Loader::load_statement(const Statement &st)
78 {
79         cur_st = &st;
80
81         try
82         {
83                 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
84                 if(act)
85                 {
86                         sub_loaded = false;
87                         act->execute(*this, st);
88                         if(check_sub_loads && !st.sub.empty() && !sub_loaded)
89                                 throw Exception("Substatements were not loaded");
90                 }
91         }
92         catch(Exception &e)
93         {
94                 cur_st = 0;
95                 if(!e.where()[0])
96                         e.at(st.get_location());
97
98                 throw;
99         }
100
101         cur_st = 0;
102 }
103
104 void Loader::load_sub_with(Loader &ldr)
105 {
106         if(!cur_st)
107                 throw InvalidState("load_sub called without current statement");
108
109         ldr.load(*cur_st);
110         sub_loaded = true;
111 }
112
113 void Loader::add(const string &kwd, LoaderAction *act)
114 {
115         ActionKey key(kwd, (act ? act->get_signature() : "*"));
116         ActionMap::iterator i = actions.find(key);
117         if(i!=actions.end())
118         {
119                 delete i->second;
120                 i->second = act;
121         }
122         else
123                 actions[key] = act;
124 }
125
126 LoaderAction *Loader::find_action(const ActionKey &key) const
127 {
128         ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
129         ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
130
131         if(begin==end)
132                 throw KeyError("Unknown keyword", key.keyword);
133
134         for(ActionMap::const_iterator i=begin; i!=end; ++i)
135                 if(signature_match(key.signature, i->first.signature))
136                         return i->second;
137
138         throw KeyError(format("Keyword '%s' does not accept signature '%s'", key.keyword, key.signature));
139 }
140
141
142 Loader::ActionKey::ActionKey(const string &k, const string &s):
143         keyword(k),
144         signature(s)
145 { }
146
147 bool Loader::ActionKey::operator<(const ActionKey &other) const
148 {
149         if(keyword!=other.keyword)
150                 return keyword<other.keyword;
151         return signature<other.signature;
152 }
153
154 } // namespace DataFile
155 } // namespace Msp