4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "serialize.hpp"
25 #include "lisp/lisp.hpp"
26 #include "lisp/list_iterator.hpp"
27 #include "lisp/parser.hpp"
28 #include "lisp/writer.hpp"
29 #include "squirrel_error.hpp"
34 void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const lisp::Lisp* lisp)
41 lisp::ListIterator iter(lisp);
42 while(iter.next() && iter.lisp() != NULL) {
43 const std::string& token = iter.item();
44 sq_pushstring(vm, token.c_str(), token.size());
46 const lisp::Lisp* value = iter.value();
47 switch(value->get_type()) {
50 load_squirrel_table(vm, sq_gettop(vm), iter.lisp());
52 case Lisp::TYPE_INTEGER:
53 sq_pushinteger(vm, value->get_int());
56 sq_pushfloat(vm, value->get_float());
58 case Lisp::TYPE_STRING:
59 sq_pushstring(vm, value->get_string().c_str(), -1);
61 case Lisp::TYPE_BOOLEAN:
62 sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
64 case Lisp::TYPE_SYMBOL:
65 std::cerr << "Unexpected symbol in lisp file...";
73 if(SQ_FAILED(sq_createslot(vm, table_idx)))
74 throw Scripting::SquirrelError(vm, "Couldn't create new index");
78 void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, lisp::Writer& writer)
80 // offset because of sq_pushnull
86 while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
87 if(sq_gettype(vm, -2) != OT_STRING) {
88 std::cerr << "Table contains non-string key\n";
92 sq_getstring(vm, -2, &key);
94 switch(sq_gettype(vm, -1)) {
97 sq_getinteger(vm, -1, &val);
98 writer.write(key, static_cast<int> (val));
103 sq_getfloat(vm, -1, &val);
104 writer.write(key, static_cast<float> (val));
109 sq_getbool(vm, -1, &val);
110 writer.write(key, val == SQTrue);
115 sq_getstring(vm, -1, &str);
116 writer.write(key, reinterpret_cast<const char*> (str));
120 writer.start_list(key, true);
121 save_squirrel_table(vm, -1, writer);
122 writer.end_list(key);
127 case OT_NATIVECLOSURE:
130 std::cerr << "Can't serialize key '" << key << "' in table.\n";