#include <sys/param.h>
+#include <msp/core/maputils.h>
#include <msp/strings/format.h>
#include "binaryparser.h"
#include "input.h"
namespace Msp {
namespace DataFile {
+class bad_definition: public runtime_error
+{
+public:
+ bad_definition(const std::string &w):
+ runtime_error(w)
+ { }
+
+ virtual ~bad_definition() throw() { }
+};
+
+
BinaryParser::BinaryParser(Input &i, const string &s):
ParserMode(i, s),
first(true)
if(st.keyword=="__kwd")
{
if(st.args.size()!=3)
- throw_at(TypeError("Keyword definition must have three arguments"), src);
+ throw bad_definition("__kwd");
const unsigned id = st.args[0].get<unsigned>();
const string &kw = st.args[1].get<const string &>();
else if(st.keyword=="__str")
{
if(st.args.size()!=2)
- throw_at(TypeError("String definition must have two arguments"), src);
+ throw bad_definition("__str");
const unsigned id = st.args[0].get<unsigned>();
strings[id] = st.args[1].get<const string &>();
if(!in)
return Statement();
- Dictionary::const_iterator i = dict.find(id);
- if(i==dict.end())
- throw_at(KeyError("Unknown statement ID", lexical_cast(id)), src);
- const DictEntry &de = i->second;
+ const DictEntry &de = get_item(dict, id);
Statement result;
result.keyword = de.keyword;
return result;
}
else
- return lookup_string(-len);
+ return get_item(strings, -len);
}
string BinaryParser::parse_enum()
{
- return lookup_string(parse_int());
-}
-
-const string &BinaryParser::lookup_string(unsigned id) const
-{
- StringMap::const_iterator i = strings.find(id);
- if(i==strings.end())
- throw_at(KeyError("Unknown string", lexical_cast(id)), src);
- return i->second;
+ return get_item(strings, parse_int());
}
} // namespace DataFile