]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/binaryparser.cpp
More senseful validity checks for keyword and string definitions
[libs/datafile.git] / source / binaryparser.cpp
index 6aed5b1590bb93b383fb45c535a6cd75af94a030..fd918758294fd6c1d66ac497af5d5674d629c691 100644 (file)
@@ -1,13 +1,9 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <limits>
 #include <sys/param.h>
-#include <msp/strings/formatter.h>
+#include <msp/core/maputils.h>
+#include <msp/strings/format.h>
 #include "binaryparser.h"
+#include "binfloat.h"
 #include "input.h"
 
 using namespace std;
@@ -15,37 +11,57 @@ 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)
+       first(true),
+       float_precision(32)
 {
-       dict[1]=DictEntry("__st", "iss");
-       dict[2]=DictEntry("__enum", "is");
+       dict[-1] = DictEntry("__kwd", "iss");
+       dict[-2] = DictEntry("__str", "is");
+       dict[-3] = DictEntry("__flt", "i");
 }
 
 Statement BinaryParser::parse()
 {
        while(1)
        {
-               Statement st=parse_statement();
-               if(st.keyword=="__st")
+               Statement st = parse_statement();
+               if(st.keyword=="__kwd")
                {
-                       if(st.args.size()!=3)
-                               throw TypeError(src+": Keyword definition must have three arguments");
-
-                       const unsigned id=st.args[0].get<unsigned>();
-                       const string &kw=st.args[1].get<const string &>();
-                       const string &args=st.args[2].get<const string &>();
-                       dict[id]=DictEntry(kw, args);
+                       int id = st.args[0].get<int>();
+                       if(id<=0)
+                               throw bad_definition("__kwd");
+
+                       const string &kw = st.args[1].get<const string &>();
+                       const string &args = st.args[2].get<const string &>();
+                       for(string::const_iterator i=args.begin(); i!=args.end(); ++i)
+                               for(unsigned j=0; valid_signatures[j]!=*i; ++j)
+                                       if(!valid_signatures[j])
+                                               throw bad_definition("__kwd");
+
+                       dict[id] = DictEntry(kw, args);
                }
-               else if(st.keyword=="__enum")
+               else if(st.keyword=="__str")
                {
-                       if(st.args.size()!=2)
-                               throw TypeError(src+": Enum definition must have three arguments");
+                       int id = st.args[0].get<int>();
+                       if(id<=0)
+                               throw bad_definition("__str");
 
-                       const unsigned id=st.args[0].get<unsigned>();
-                       enums[id]=st.args[1].get<const std::string &>();
+                       strings[id] = st.args[1].get<const string &>();
                }
+               else if(st.keyword=="__flt")
+                       float_precision = st.args[0].get<unsigned>();
                else
                        return st;
        }
@@ -55,115 +71,139 @@ Statement BinaryParser::parse_statement()
 {
        while(first && in.peek()=='\n')
                in.get();
-       first=false;
+       first = false;
 
-       unsigned id=parse_int();
+       int id = parse_int();
        if(!in)
                return Statement();
 
-       Dictionary::const_iterator i=dict.find(id);
-       if(i==dict.end())
-               throw ParseError(format("%s: Unknown statement ID %d", src, id), src, 0);
-       const DictEntry &de=i->second;
+       const DictEntry &de = get_item(dict, id);
 
        Statement result;
-       result.keyword=de.keyword;
-       result.source=src;
+       result.keyword = de.keyword;
+       result.source = src;
 
-       for(unsigned j=0; j<de.args.size(); ++j)
+       for(unsigned j = 0; j<de.args.size(); ++j)
        {
                switch(de.args[j])
                {
-               case 'i':
+               case IntType::signature:
                        result.args.push_back(parse_int());
                        break;
-               case 'f':
+               case FloatType::signature:
                        result.args.push_back(parse_float());
                        break;
-               case 's':
+               case StringType::signature:
                        result.args.push_back(parse_string());
                        break;
-               case 'b':
+               case BoolType::signature:
                        result.args.push_back(parse_bool());
                        break;
-               case 'e':
-                       result.args.push_back(parse_enum());
+               case SymbolType::signature:
+                       result.args.push_back(parse_symbol());
                        break;
                }
        }
 
-       unsigned nsub=parse_int();
-       for(unsigned j=0; j<nsub; ++j)
+       unsigned nsub = parse_int();
+       for(unsigned j = 0; j<nsub; ++j)
                result.sub.push_back(parse());
 
-       result.valid=true;
+       result.valid = true;
 
        return result;
 }
 
-long long BinaryParser::parse_int()
+IntType::Store BinaryParser::parse_int()
 {
-       long long result=0;
-       unsigned bits=0;
+       IntType::Store result = 0;
+       unsigned bits = 0;
 
        while(in)
        {
-               int c=in.get();
+               int c = in.get();
 
-               result=result<<7 | c&0x7F;
-               bits+=7;
+               result = (result<<7) | (c&0x7F);
+               bits += 7;
 
                if(!(c&0x80))
                        break;
        }
 
-       const long long mask=1<<(bits-1);
-       result=(result^mask)-mask;
+       const IntType::Store mask = 1LL<<(bits-1);
+       result = (result^mask)-mask;
 
        return result;
 }
 
-float BinaryParser::parse_float()
+FloatType::Store BinaryParser::parse_float()
 {
-       union
+       UInt64 encoded = 0;
+       for(unsigned i=0; i<float_precision; i+=8)
+       {
+               int c = in.get();
+               encoded = (encoded<<8) | (c&0xFF);
+       }
+
+       BinFloat bf = BinFloat::explode(encoded, float_precision);
+
+       if(numeric_limits<FloatType::Store>::is_iec559)
+               return bf.compose_iec559<FloatType::Store>();
+       else
        {
-               float f;
-               char d[sizeof(float)];
-       };
-
-#if BYTE_ORDER == LITTLE_ENDIAN
-       for(unsigned i=sizeof(float)-1; i--;)
-               d[i]=in.get();
-#else
-       for(unsigned i=0; i<sizeof(float); ++i)
-               d[i]=in.get();
-#endif
-
-       return f;
+               /* Put the float together with arithmetic since we don't know its
+               internal layout */
+               FloatType::Store f = 0;
+               if(bf.infinity)
+               {
+                       if(numeric_limits<FloatType::Store>::has_infinity)
+                               f = numeric_limits<FloatType::Store>::infinity();
+                       else
+                               f = numeric_limits<FloatType::Store>::max();
+               }
+               else
+               {
+                       for(unsigned i=0; i<64; ++i)
+                       {
+                               f /= 2;
+                               if(bf.mantissa&1)
+                                       f += 1;
+                               bf.mantissa >>= 1;
+                       }
+                       for(int i=0; i<bf.exponent; ++i)
+                               f *= 2;
+                       for(int i=0; i>bf.exponent; --i)
+                               f /= 2;
+               }
+               if(bf.sign)
+                       f = -f;
+               return f;
+       }
 }
 
-bool BinaryParser::parse_bool()
+BoolType::Store BinaryParser::parse_bool()
 {
        return in.get();
 }
 
-string BinaryParser::parse_string()
+StringType::Store BinaryParser::parse_string()
 {
-       unsigned len=parse_int();
-       string result;
-       result.reserve(len);
-       for(unsigned i=0; i<len; ++i)
-               result+=in.get();
-       return result;
+       int len = parse_int();
+       if(len>=0)
+       {
+               string result;
+               result.reserve(len);
+               for(int i = 0; i<len; ++i)
+                       result += in.get();
+               return result;
+       }
+       else
+               return get_item(strings, -len);
 }
 
-string BinaryParser::parse_enum()
+SymbolType::Store BinaryParser::parse_symbol()
 {
-       unsigned id=parse_int();
-       EnumMap::iterator i=enums.find(id);
-       if(i==enums.end())
-               throw KeyError("Unknown enum");
-       return i->second;
+       return get_item(strings, parse_int());
 }
 
 } // namespace DataFile