00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00074 if(table_idx < 0)
00075 table_idx -= 1;
00076
00077
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;
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 }
00132
00133