3 This file is part of libmspdatafile
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/strings/formatter.h>
10 #include "binaryparser.h"
18 BinaryParser::BinaryParser(Input &i, const string &s):
22 dict[1]=DictEntry("__kwd", "iss");
23 dict[2]=DictEntry("__str", "is");
26 Statement BinaryParser::parse()
30 Statement st=parse_statement();
31 if(st.keyword=="__kwd")
34 throw TypeError(src+": Keyword definition must have three arguments");
36 const unsigned id=st.args[0].get<unsigned>();
37 const string &kw=st.args[1].get<const string &>();
38 const string &args=st.args[2].get<const string &>();
39 dict[id]=DictEntry(kw, args);
41 else if(st.keyword=="__str")
44 throw TypeError(src+": String definition must have two arguments");
46 const unsigned id=st.args[0].get<unsigned>();
47 strings[id]=st.args[1].get<const string &>();
54 Statement BinaryParser::parse_statement()
56 while(first && in.peek()=='\n')
60 unsigned id=parse_int();
64 Dictionary::const_iterator i=dict.find(id);
66 throw ParseError(format("%s: Unknown statement ID %d", src, id), src, 0);
67 const DictEntry &de=i->second;
70 result.keyword=de.keyword;
73 for(unsigned j=0; j<de.args.size(); ++j)
78 result.args.push_back(parse_int());
81 result.args.push_back(parse_float());
84 result.args.push_back(parse_string());
87 result.args.push_back(parse_bool());
90 result.args.push_back(Value(ENUM, parse_enum()));
95 unsigned nsub=parse_int();
96 for(unsigned j=0; j<nsub; ++j)
97 result.sub.push_back(parse());
104 long long BinaryParser::parse_int()
113 result=(result<<7) | (c&0x7F);
120 const long long mask=1LL<<(bits-1);
121 result=(result^mask)-mask;
126 float BinaryParser::parse_float()
131 char d[sizeof(float)];
134 #if BYTE_ORDER == LITTLE_ENDIAN
135 for(unsigned i=sizeof(float); i--;)
138 for(unsigned i=0; i<sizeof(float); ++i)
145 bool BinaryParser::parse_bool()
150 string BinaryParser::parse_string()
157 for(int i=0; i<len; ++i)
162 return lookup_string(-len);
165 string BinaryParser::parse_enum()
167 return lookup_string(parse_int());
170 const string &BinaryParser::lookup_string(unsigned id) const
172 StringMap::const_iterator i=strings.find(id);
174 throw KeyError("Unknown string", lexical_cast(id));
178 } // namespace DataFile