Fix coverity #29352
[supertux.git] / src / scripting / serialize.cpp
index 7d7992d..ccbfd20 100644 (file)
@@ -1,25 +1,38 @@
-#include "serialize.hpp"
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "scripting/serialize.hpp"
+
+#include <iostream>
 
-#include <memory>
-#include <assert.h>
-#include "lisp/lisp.hpp"
-#include "lisp/list_iterator.hpp"
-#include "lisp/parser.hpp"
 #include "lisp/writer.hpp"
-#include "squirrel_error.hpp"
+#include "lisp/list_iterator.hpp"
+#include "scripting/squirrel_error.hpp"
 
-namespace Scripting
-{
+namespace scripting {
 
-void load_squirrel_table(HSQUIRRELVM vm, int table_idx, const lisp::Lisp* lisp)
+void load_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, const Reader& lisp)
 {
   using namespace lisp;
 
   if(table_idx < 0)
-    table_idx -= 2; 
-  lisp::ListIterator iter(lisp);
-  while(iter.next()) {
+    table_idx -= 2;
+
+  lisp::ListIterator iter(&lisp);
+  while(iter.next() && iter.lisp() != NULL) {
     const std::string& token = iter.item();
     sq_pushstring(vm, token.c_str(), token.size());
 
@@ -27,7 +40,7 @@ void load_squirrel_table(HSQUIRRELVM vm, int table_idx, const lisp::Lisp* lisp)
     switch(value->get_type()) {
       case Lisp::TYPE_CONS:
         sq_newtable(vm);
-        load_squirrel_table(vm, sq_gettop(vm), iter.lisp());
+        load_squirrel_table(vm, sq_gettop(vm), *iter.lisp());
         break;
       case Lisp::TYPE_INTEGER:
         sq_pushinteger(vm, value->get_int());
@@ -51,16 +64,16 @@ void load_squirrel_table(HSQUIRRELVM vm, int table_idx, const lisp::Lisp* lisp)
     }
 
     if(SQ_FAILED(sq_createslot(vm, table_idx)))
-      throw Scripting::SquirrelError(vm, "Couldn't create new index");
+      throw scripting::SquirrelError(vm, "Couldn't create new index");
   }
 }
 
-void save_squirrel_table(HSQUIRRELVM vm, int table_idx, lisp::Writer& writer)
+void save_squirrel_table(HSQUIRRELVM vm, SQInteger table_idx, Writer& writer)
 {
   // offset because of sq_pushnull
   if(table_idx < 0)
     table_idx -= 1;
-  
+
   //iterator table
   sq_pushnull(vm);
   while(SQ_SUCCEEDED(sq_next(vm, table_idx))) {
@@ -68,32 +81,32 @@ void save_squirrel_table(HSQUIRRELVM vm, int table_idx, lisp::Writer& writer)
       std::cerr << "Table contains non-string key\n";
       continue;
     }
-    const char* key;
+    const SQChar* key;
     sq_getstring(vm, -2, &key);
 
     switch(sq_gettype(vm, -1)) {
       case OT_INTEGER: {
-        int val;
+        SQInteger val;
         sq_getinteger(vm, -1, &val);
-        writer.write_int(key, val);
+        writer.write(key, static_cast<int> (val));
         break;
       }
       case OT_FLOAT: {
-        float val;
+        SQFloat val;
         sq_getfloat(vm, -1, &val);
-        writer.write_float(key, val);
+        writer.write(key, static_cast<float> (val));
         break;
       }
       case OT_BOOL: {
         SQBool val;
-        sq_getbool(vm, -1, &val);
-        writer.write_bool(key, val);
+        if(SQ_SUCCEEDED(sq_getbool(vm, -1, &val)))
+          writer.write(key, val == SQTrue);
         break;
       }
       case OT_STRING: {
-        const char* str;
+        const SQChar* str;
         sq_getstring(vm, -1, &str);
-        writer.write_string(key, str);
+        writer.write(key, reinterpret_cast<const char*> (str));
         break;
       }
       case OT_TABLE: {
@@ -115,5 +128,6 @@ void save_squirrel_table(HSQUIRRELVM vm, int table_idx, lisp::Writer& writer)
   sq_pop(vm, 1);
 }
 
-}
+} // namespace scripting
 
+/* EOF */