src/scripting/serialize.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "scripting/serialize.hpp"
00018 
00019 #include <iostream>
00020 
00021 #include "lisp/writer.hpp"
00022 #include "lisp/list_iterator.hpp"
00023 #include "scripting/squirrel_error.hpp"
00024 
00025 namespace scripting {
00026 
00027 void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const Reader& lisp)
00028 {
00029   using namespace lisp;
00030 
00031   if(table_idx < 0)
00032     table_idx -= 2;
00033 
00034   lisp::ListIterator iter(&lisp);
00035   while(iter.next() && iter.lisp() != NULL) {
00036     const std::string& token = iter.item();
00037     sq_pushstring(vm, token.c_str(), token.size());
00038 
00039     const lisp::Lisp* value = iter.value();
00040     switch(value->get_type()) {
00041       case Lisp::TYPE_CONS:
00042         sq_newtable(vm);
00043         load_squirrel_table(vm, sq_gettop(vm), *iter.lisp());
00044         break;
00045       case Lisp::TYPE_INTEGER:
00046         sq_pushinteger(vm, value->get_int());
00047         break;
00048       case Lisp::TYPE_REAL:
00049         sq_pushfloat(vm, value->get_float());
00050         break;
00051       case Lisp::TYPE_STRING:
00052         sq_pushstring(vm, value->get_string().c_str(), -1);
00053         break;
00054       case Lisp::TYPE_BOOLEAN:
00055         sq_pushbool(vm, value->get_bool() ? SQTrue : SQFalse);
00056         break;
00057       case Lisp::TYPE_SYMBOL:
00058         std::cerr << "Unexpected symbol in lisp file...";
00059         sq_pushnull(vm);
00060         break;
00061       default:
00062         assert(false);
00063         break;
00064     }
00065 
00066     if(SQ_FAILED(sq_createslot(vm, table_idx)))
00067       throw scripting::SquirrelError(vm, "Couldn't create new index");
00068   }
00069 }
00070 
00071 void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer)
00072 {
00073   // offset because of sq_pushnull
00074   if(table_idx < 0)
00075     table_idx -= 1;
00076 
00077   //iterator table
00078   sq_pushnull(vm);
00079   while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
00080     if(sq_gettype(vm, -2) != OT_STRING) {
00081       std::cerr << "Table contains non-string key\n";
00082       continue;
00083     }
00084     const SQChar* key;
00085     sq_getstring(vm, -2, &key);
00086 
00087     switch(sq_gettype(vm, -1)) {
00088       case OT_INTEGER: {
00089         SQInteger val;
00090         sq_getinteger(vm, -1, &val);
00091         writer.write(key, static_cast<int> (val));
00092         break;
00093       }
00094       case OT_FLOAT: {
00095         SQFloat val;
00096         sq_getfloat(vm, -1, &val);
00097         writer.write(key, static_cast<float> (val));
00098         break;
00099       }
00100       case OT_BOOL: {
00101         SQBool val;
00102         sq_getbool(vm, -1, &val);
00103         writer.write(key, val == SQTrue);
00104         break;
00105       }
00106       case OT_STRING: {
00107         const SQChar* str;
00108         sq_getstring(vm, -1, &str);
00109         writer.write(key, reinterpret_cast<const char*> (str));
00110         break;
00111       }
00112       case OT_TABLE: {
00113         writer.start_list(key, true);
00114         save_squirrel_table(vm, -1, writer);
00115         writer.end_list(key);
00116         break;
00117       }
00118       case OT_CLOSURE:
00119         break; // ignore
00120       case OT_NATIVECLOSURE:
00121         break;
00122       default:
00123         std::cerr << "Can't serialize key '" << key << "' in table.\n";
00124         break;
00125     }
00126     sq_pop(vm, 2);
00127   }
00128   sq_pop(vm, 1);
00129 }
00130 
00131 } // namespace scripting
00132 
00133 /* EOF */

Generated on Mon Jun 9 03:38:21 2014 for SuperTux by  doxygen 1.5.1