2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "scripting/serialize.hpp"
21 #include "lisp/writer.hpp"
22 #include "lisp/list_iterator.hpp"
23 #include "scripting/squirrel_error.hpp"
27 void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const Reader& lisp)
34 lisp::ListIterator iter(&lisp);
35 while(iter.next() && iter.lisp() != NULL) {
36 const std::string& token = iter.item();
37 sq_pushstring(vm, token.c_str(), token.size());
39 const lisp::Lisp* value = iter.value();
40 switch(value->get_type()) {
43 load_squirrel_table(vm, sq_gettop(vm), *iter.lisp());
45 case Lisp::TYPE_INTEGER:
46 sq_pushinteger(vm, value->get_int());
49 sq_pushfloat(vm, value->get_float());
51 case Lisp::TYPE_STRING:
52 sq_pushstring(vm, value->get_string().c_str(), -1);
54 case Lisp::TYPE_BOOLEAN:
55 sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
57 case Lisp::TYPE_SYMBOL:
58 std::cerr << "Unexpected symbol in lisp file...";
66 if(SQ_FAILED(sq_createslot(vm, table_idx)))
67 throw scripting::SquirrelError(vm, "Couldn't create new index");
71 void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer)
73 // offset because of sq_pushnull
79 while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
80 if(sq_gettype(vm, -2) != OT_STRING) {
81 std::cerr << "Table contains non-string key\n";
85 sq_getstring(vm, -2, &key);
87 switch(sq_gettype(vm, -1)) {
90 sq_getinteger(vm, -1, &val);
91 writer.write(key, static_cast<int> (val));
96 sq_getfloat(vm, -1, &val);
97 writer.write(key, static_cast<float> (val));
102 sq_getbool(vm, -1, &val);
103 writer.write(key, val == SQTrue);
108 sq_getstring(vm, -1, &str);
109 writer.write(key, reinterpret_cast<const char*> (str));
113 writer.start_list(key, true);
114 save_squirrel_table(vm, -1, writer);
115 writer.end_list(key);
120 case OT_NATIVECLOSURE:
123 std::cerr << "Can't serialize key '" << key << "' in table.\n";
131 } // namespace scripting