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.
20 #include "serialize.hpp"
24 #include "lisp/lisp.hpp"
25 #include "lisp/list_iterator.hpp"
26 #include "lisp/parser.hpp"
27 #include "lisp/writer.hpp"
28 #include "squirrel_error.hpp"
33 void load_squirrel_table(HSQUIRRELVM vm, int table_idx, const lisp::Lisp* lisp)
40 lisp::ListIterator iter(lisp);
42 const std::string& token = iter.item();
43 sq_pushstring(vm, token.c_str(), token.size());
45 const lisp::Lisp* value = iter.value();
46 switch(value->get_type()) {
49 load_squirrel_table(vm, sq_gettop(vm), iter.lisp());
51 case Lisp::TYPE_INTEGER:
52 sq_pushinteger(vm, value->get_int());
55 sq_pushfloat(vm, value->get_float());
57 case Lisp::TYPE_STRING:
58 sq_pushstring(vm, value->get_string().c_str(), -1);
60 case Lisp::TYPE_BOOLEAN:
61 sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
63 case Lisp::TYPE_SYMBOL:
64 std::cerr << "Unexpected symbol in lisp file...";
72 if(SQ_FAILED(sq_createslot(vm, table_idx)))
73 throw Scripting::SquirrelError(vm, "Couldn't create new index");
77 void save_squirrel_table(HSQUIRRELVM vm, int table_idx, lisp::Writer& writer)
79 // offset because of sq_pushnull
85 while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
86 if(sq_gettype(vm, -2) != OT_STRING) {
87 std::cerr << "Table contains non-string key\n";
91 sq_getstring(vm, -2, &key);
93 switch(sq_gettype(vm, -1)) {
96 sq_getinteger(vm, -1, &val);
97 writer.write_int(key, val);
102 sq_getfloat(vm, -1, &val);
103 writer.write_float(key, val);
108 sq_getbool(vm, -1, &val);
109 writer.write_bool(key, val);
114 sq_getstring(vm, -1, &str);
115 writer.write_string(key, str);
119 writer.start_list(key, true);
120 save_squirrel_table(vm, -1, writer);
121 writer.end_list(key);
126 case OT_NATIVECLOSURE:
129 std::cerr << "Can't serialize key '" << key << "' in table.\n";