]> git.tdb.fi Git - libs/datafile.git/commitdiff
Use negative integers for built-in statements for better extensibility
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 15:52:25 +0000 (18:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 15:52:25 +0000 (18:52 +0300)
source/binaryparser.cpp
source/binaryparser.h
source/binarywriter.cpp
source/binarywriter.h

index 53131289a851b04270e377e8abb5b61b49832b5c..81a86ea9a3b66b7ae9c9a6ba0a3dfff4f134ff17 100644 (file)
@@ -24,8 +24,8 @@ BinaryParser::BinaryParser(Input &i, const string &s):
        ParserMode(i, s),
        first(true)
 {
-       dict[1] = DictEntry("__kwd", "iss");
-       dict[2] = DictEntry("__str", "is");
+       dict[-1] = DictEntry("__kwd", "iss");
+       dict[-2] = DictEntry("__str", "is");
 }
 
 Statement BinaryParser::parse()
@@ -38,7 +38,7 @@ Statement BinaryParser::parse()
                        if(st.args.size()!=3)
                                throw bad_definition("__kwd");
 
-                       const unsigned id = st.args[0].get<unsigned>();
+                       const int 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);
@@ -62,7 +62,7 @@ Statement BinaryParser::parse_statement()
                in.get();
        first = false;
 
-       unsigned id = parse_int();
+       int id = parse_int();
        if(!in)
                return Statement();
 
index f9b8e542865fac10550c8a5c5105d40819efcd69..b10d7b8791916a8e5ce37257c88da1b33603f6bd 100644 (file)
@@ -15,7 +15,7 @@ Parses data in binary format.
 class BinaryParser: public ParserMode
 {
 private:
-       typedef std::map<unsigned, DictEntry> Dictionary;
+       typedef std::map<int, DictEntry> Dictionary;
        typedef std::map<unsigned, std::string> StringMap;
 
        Dictionary dict;
index c1e53fb5f23e7ca3622697c4e5e829383aeafd2f..9c9c893e890354bc1198c0cfdd4603ede61b3270 100644 (file)
@@ -9,11 +9,11 @@ namespace DataFile {
 
 BinaryWriter::BinaryWriter(IO::Base &o):
        WriterMode(o),
-       next_kwd_id(3),
+       next_kwd_id(1),
        next_str_id(1)
 {
-       dict[DictEntry("__kwd", "iss")] = 1;
-       dict[DictEntry("__str", "is")] = 2;
+       dict[DictEntry("__kwd", "iss")] = -1;
+       dict[DictEntry("__str", "is")] = -2;
 }
 
 void BinaryWriter::write(const Statement &st)
@@ -24,7 +24,7 @@ void BinaryWriter::write(const Statement &st)
 
 void BinaryWriter::write_(const Statement &st)
 {
-       unsigned id = get_item(dict, DictEntry(st.keyword, st.get_signature()));
+       int id = get_item(dict, DictEntry(st.keyword, st.get_signature()));
 
        write_int(id);
        for(Statement::Arguments::const_iterator j = st.args.begin(); j!=st.args.end(); ++j)
index 4d18196663d377e9bdf992748a9021e0a845e902..1a11ae5df9d4f17e4080c678b8a7f10d75f16fd2 100644 (file)
@@ -15,7 +15,7 @@ Writes data in binary format.
 class BinaryWriter: public WriterMode
 {
 private:
-       typedef std::map<DictEntry, unsigned> Dictionary;
+       typedef std::map<DictEntry, int> Dictionary;
        typedef std::map<std::string, unsigned> StringMap;
 
        Dictionary dict;