]> git.tdb.fi Git - libs/datafile.git/blob - source/binaryparser.cpp
Restructure the tool and make it able to handle multiple input files
[libs/datafile.git] / source / binaryparser.cpp
1 #include <limits>
2 #include <sys/param.h>
3 #include <msp/core/maputils.h>
4 #include <msp/strings/format.h>
5 #include "binaryparser.h"
6 #include "binfloat.h"
7 #include "input.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace DataFile {
13
14 class bad_definition: public runtime_error
15 {
16 public:
17         bad_definition(const std::string &w):
18                 runtime_error(w)
19         { }
20
21         virtual ~bad_definition() throw() { }
22 };
23
24
25 BinaryParser::BinaryParser(Input &i, const string &s):
26         ParserMode(i, s),
27         first(true),
28         float_precision(32)
29 {
30         dict[-1] = DictEntry("__kwd", "iss");
31         dict[-2] = DictEntry("__str", "is");
32         dict[-3] = DictEntry("__flt", "i");
33 }
34
35 Statement BinaryParser::parse()
36 {
37         while(1)
38         {
39                 Statement st = parse_statement();
40                 if(st.keyword=="__kwd")
41                 {
42                         int id = st.args[0].get<int>();
43                         if(id<=0)
44                                 throw bad_definition("__kwd");
45
46                         const string &kw = st.args[1].get<const string &>();
47                         const string &args = st.args[2].get<const string &>();
48                         for(string::const_iterator i=args.begin(); i!=args.end(); ++i)
49                                 for(unsigned j=0; valid_signatures[j]!=*i; ++j)
50                                         if(!valid_signatures[j])
51                                                 throw bad_definition("__kwd");
52
53                         dict[id] = DictEntry(kw, args);
54                 }
55                 else if(st.keyword=="__str")
56                 {
57                         int id = st.args[0].get<int>();
58                         if(id<=0)
59                                 throw bad_definition("__str");
60
61                         strings[id] = st.args[1].get<const string &>();
62                 }
63                 else if(st.keyword=="__flt")
64                         float_precision = st.args[0].get<unsigned>();
65                 else
66                         return st;
67         }
68 }
69
70 Statement BinaryParser::parse_statement()
71 {
72         while(first && in.peek()=='\n')
73                 in.get();
74         first = false;
75
76         int id = parse_int();
77         if(!in)
78                 return Statement();
79
80         const DictEntry &de = get_item(dict, id);
81
82         Statement result;
83         result.keyword = de.keyword;
84         result.source = src;
85
86         for(unsigned j = 0; j<de.args.size(); ++j)
87         {
88                 switch(de.args[j])
89                 {
90                 case IntType::signature:
91                         result.args.push_back(parse_int());
92                         break;
93                 case FloatType::signature:
94                         result.args.push_back(parse_float());
95                         break;
96                 case StringType::signature:
97                         result.args.push_back(parse_string());
98                         break;
99                 case BoolType::signature:
100                         result.args.push_back(parse_bool());
101                         break;
102                 case SymbolType::signature:
103                         result.args.push_back(parse_symbol());
104                         break;
105                 }
106         }
107
108         unsigned nsub = parse_int();
109         for(unsigned j = 0; j<nsub; ++j)
110                 result.sub.push_back(parse());
111
112         result.valid = true;
113
114         return result;
115 }
116
117 IntType::Store BinaryParser::parse_int()
118 {
119         IntType::Store result = 0;
120         unsigned bits = 0;
121
122         while(in)
123         {
124                 int c = in.get();
125
126                 result = (result<<7) | (c&0x7F);
127                 bits += 7;
128
129                 if(!(c&0x80))
130                         break;
131         }
132
133         const IntType::Store mask = 1LL<<(bits-1);
134         result = (result^mask)-mask;
135
136         return result;
137 }
138
139 FloatType::Store BinaryParser::parse_float()
140 {
141         UInt64 encoded = 0;
142         for(unsigned i=0; i<float_precision; i+=8)
143         {
144                 int c = in.get();
145                 encoded = (encoded<<8) | (c&0xFF);
146         }
147
148         BinFloat bf = BinFloat::explode(encoded, float_precision);
149
150         if(numeric_limits<FloatType::Store>::is_iec559)
151                 return bf.compose_iec559<FloatType::Store>();
152         else
153         {
154                 /* Put the float together with arithmetic since we don't know its
155                 internal layout */
156                 FloatType::Store f = 0;
157                 if(bf.infinity)
158                 {
159                         if(numeric_limits<FloatType::Store>::has_infinity)
160                                 f = numeric_limits<FloatType::Store>::infinity();
161                         else
162                                 f = numeric_limits<FloatType::Store>::max();
163                 }
164                 else
165                 {
166                         for(unsigned i=0; i<64; ++i)
167                         {
168                                 f /= 2;
169                                 if(bf.mantissa&1)
170                                         f += 1;
171                                 bf.mantissa >>= 1;
172                         }
173                         for(int i=0; i<bf.exponent; ++i)
174                                 f *= 2;
175                         for(int i=0; i>bf.exponent; --i)
176                                 f /= 2;
177                 }
178                 if(bf.sign)
179                         f = -f;
180                 return f;
181         }
182 }
183
184 BoolType::Store BinaryParser::parse_bool()
185 {
186         return in.get();
187 }
188
189 StringType::Store BinaryParser::parse_string()
190 {
191         int len = parse_int();
192         if(len>=0)
193         {
194                 string result;
195                 result.reserve(len);
196                 for(int i = 0; i<len; ++i)
197                         result += in.get();
198                 return result;
199         }
200         else
201                 return get_item(strings, -len);
202 }
203
204 SymbolType::Store BinaryParser::parse_symbol()
205 {
206         return get_item(strings, parse_int());
207 }
208
209 } // namespace DataFile
210 } // namespace Msp