]> git.tdb.fi Git - libs/datafile.git/commitdiff
Fix binary format
authorMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2007 20:22:41 +0000 (20:22 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 27 Sep 2007 20:22:41 +0000 (20:22 +0000)
Use c_escape and c_unescape functions from mspstrings

source/binaryparser.cpp
source/binarywriter.cpp
source/textparser.cpp
source/textparser.h
source/textwriter.cpp

index 6aed5b1590bb93b383fb45c535a6cd75af94a030..c3d53d7241d4aaa069aeee617f98b16f6a504cb8 100644 (file)
@@ -87,7 +87,7 @@ Statement BinaryParser::parse_statement()
                        result.args.push_back(parse_bool());
                        break;
                case 'e':
-                       result.args.push_back(parse_enum());
+                       result.args.push_back(Value(ENUM, parse_enum()));
                        break;
                }
        }
@@ -132,7 +132,7 @@ float BinaryParser::parse_float()
        };
 
 #if BYTE_ORDER == LITTLE_ENDIAN
-       for(unsigned i=sizeof(float)-1; i--;)
+       for(unsigned i=sizeof(float); i--;)
                d[i]=in.get();
 #else
        for(unsigned i=0; i<sizeof(float); ++i)
index 95526bbae84666b6b02997ec68c724a1a357e6d8..75cdded3c4de0bb9ce8f6a776be3a13c36aa48c5 100644 (file)
@@ -19,7 +19,7 @@ BinaryWriter::BinaryWriter(ostream &o):
        next_enum_id(1)
 {
        dict[DictEntry("__st", "iss")]=1;
-       dict[DictEntry("__enum", "is")]=1;
+       dict[DictEntry("__enum", "is")]=2;
 }
 
 void BinaryWriter::write(const Statement &st)
@@ -78,7 +78,7 @@ void BinaryWriter::collect_keywords(const Statement &st)
                kst.args.push_back(de.args);
                write_(kst);
 
-               dict.insert(Dictionary::value_type(de, next_st_id++)).first;
+               dict[de]=next_st_id++;
        }
 
        for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
@@ -99,9 +99,14 @@ void BinaryWriter::collect_keywords(const Statement &st)
 
 void BinaryWriter::write_int(long long n)
 {
-       unsigned i=1;
-       for(; n>>(i*7); ++i);
-       for(; i--;)
+       unsigned i=sizeof(long long)-1;
+
+       if(n>=0)
+               for(; (i>0 && (n>>(i*7-1))==0); --i);
+       else
+               for(; (i>0 && (n>>(i*7-1))==-1); --i);
+
+       for(++i; i--;)
                out.put(n>>(i*7) & 0x7F | (i?0x80:0));
 }
 
index 298b7cac363ebd2b75a712b2ea41d18dd17b3801..78d2f1df6334b32dbf67a7871975deda4dafb0a4 100644 (file)
@@ -6,6 +6,7 @@ Distributed under the LGPL
 */
 
 #include <msp/strings/formatter.h>
+#include <msp/strings/utils.h>
 #include "input.h"
 #include "textparser.h"
 #include "token.h"
@@ -266,7 +267,16 @@ Token TextParser::parse_token()
                        if(c=='\\')
                                escape=!escape;
                        else if(c=='"' && !escape)
-                               return Token(Token::STRING, unescape_string(buf));
+                       {
+                               try
+                               {
+                                       return Token(Token::STRING, c_unescape(buf.substr(1, buf.size()-2)));
+                               }
+                               catch(const Exception &e)
+                               {
+                                       throw ParseError(format("%s: %s", get_location(), e.what()), src, in.get_line_number());
+                               }
+                       }
                        else
                                escape=false;
                        break;
@@ -297,57 +307,6 @@ bool TextParser::isodigit(int c)
        return (c>='0' && c<='7');
 }
 
-string TextParser::unescape_string(const string &str)
-{
-       string   result;
-       bool     escape=false;
-       unsigned hexcape=0;
-       for(string::const_iterator i=str.begin()+1; i!=str.end()-1; ++i)
-       {
-               if(escape)
-               {
-                       if(*i=='n')
-                               result+='\n';
-                       else if(*i=='t')
-                               result+='\t';
-                       else if(*i=='\\')
-                               result+='\\';
-                       else if(*i=='"')
-                               result+='"';
-                       else if(*i=='x')
-                               hexcape=0x100;
-                       else
-                               throw ParseError(format("%s: Invalid escape sequence '\\%c'", get_location(), *i), src, in.get_line_number());
-                       escape=false;
-               }
-               else if(hexcape)
-               {
-                       unsigned digit=0;
-                       if(*i>='0' && *i<='9')
-                               digit=*i-'0';
-                       else if(*i>='a' && *i<='f')
-                               digit=*i-'a'+10;
-                       else if(*i>='A' && *i<='F')
-                               digit=*i-'A'+10;
-                       else
-                               throw ParseError(get_location()+": Invalid hex digit", src, in.get_line_number());
-
-                       hexcape=(hexcape<<4)|digit;
-                       if(hexcape&0x10000)
-                       {
-                               result+=hexcape&0xFF;
-                               hexcape=0;
-                       }
-               }
-               else if(*i=='\\')
-                       escape=true;
-               else
-                       result+=*i;
-       }
-
-       return result;
-}
-
 string TextParser::get_location()
 {
        ostringstream ss;
index 9dfcf6d2b13208df717b6cf0f9c84a573674adb8..a260cd8f2e5d5a58df3f0156c552fa49c5ecaa18 100644 (file)
@@ -25,7 +25,6 @@ protected:
        Token parse_token();
        bool  is_delimiter(int);
        bool  isodigit(int);
-       std::string unescape_string(const std::string &);
        std::string get_location();
        void  parse_error(int, int);
 };
index 68111c0d3038c9b6b4b0f1f0a70df6b436d0c167..def744c33b06ff945c4cfccbe28025bb43402872 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/strings/utils.h>
 #include "statement.h"
 #include "textwriter.h"
 
@@ -31,7 +32,7 @@ void TextWriter::write_(const Statement &st, unsigned level)
        {
                out<<' ';
                if(i->get_type()==STRING)
-                       out<<'\"'<<i->get_raw()<<'\"';
+                       out<<'\"'<<c_escape(i->get_raw(), false)<<'\"';
                else if(i->get_type()==BOOLEAN)
                        out<<(i->get<bool>() ? "true" : "false");
                else