X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbinaryparser.cpp;h=550319f46d0a7af21bd939b477adaac1047101c8;hb=8d6360cbcd8fe92d97dcba5e68e3adb6f2c0c3bc;hp=4d510c58c0347a85aa0c13cb9b5603ca6debcf1e;hpb=a582163d380833b1370ba067a1fd0ad5c2984723;p=libs%2Fdatafile.git diff --git a/source/binaryparser.cpp b/source/binaryparser.cpp index 4d510c5..550319f 100644 --- a/source/binaryparser.cpp +++ b/source/binaryparser.cpp @@ -1,12 +1,6 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include -#include +#include +#include #include "binaryparser.h" #include "input.h" @@ -15,6 +9,17 @@ using namespace std; 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) @@ -31,7 +36,7 @@ Statement BinaryParser::parse() 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(); const string &kw = st.args[1].get(); @@ -41,7 +46,7 @@ Statement BinaryParser::parse() 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(); strings[id] = st.args[1].get(); @@ -61,10 +66,7 @@ Statement BinaryParser::parse_statement() 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; @@ -159,20 +161,12 @@ string BinaryParser::parse_string() 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