- fixes image region tiling bug (by Matze)
[supertux.git] / lib / utils / lispreader.cpp
index 50fe768..6076b16 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+#include <config.h>
+
 #include <iostream>
 #include <vector>
+#include <stdexcept>
 #include <string>
 #include <cctype>
 #include <cstdlib>
 #include <cstring>
+#include <cassert>
 
-#include "../app/globals.h"
-#include "../app/setup.h"
-#include "../utils/lispreader.h"
+#include "app/globals.h"
+#include "app/setup.h"
+#include "lispreader.h"
 
 using namespace SuperTux;
 
@@ -68,7 +72,7 @@ static void
 _token_append (char c)
 {
   if (token_length >= MAX_TOKEN_LENGTH)
-    throw LispReaderException("_token_append()", __FILE__, __LINE__);
+    throw std::runtime_error("token too long.");
 
   token_string[token_length++] = c;
   token_string[token_length] = '\0';
@@ -98,7 +102,7 @@ _next_char (lisp_stream_t *stream)
       return stream->v.any.next_char(stream->v.any.data);
     }
 
-  throw LispReaderException("_next_char()", __FILE__, __LINE__);
+  assert(false);
   return EOF;
 }
 
@@ -120,7 +124,7 @@ _unget_char (char c, lisp_stream_t *stream)
       break;
 
     default :
-      throw LispReaderException("_unget_char()", __FILE__, __LINE__);
+      assert(false);
     }
 }
 
@@ -272,7 +276,7 @@ _scan (lisp_stream_t *stream)
         }
     }
 
-  throw LispReaderException("_scan()", __FILE__, __LINE__);
+  throw std::runtime_error("invalid token in lisp file");
   return TOKEN_ERROR;
 }
 
@@ -311,7 +315,7 @@ SuperTux::lisp_stream_init_any (lisp_stream_t *stream, void *data,
                       void (*unget_char) (char c, void *data))
 {
   if (next_char == 0 || unget_char == 0)
-    throw LispReaderException("lisp_stream_init_any()", __FILE__, __LINE__);
+    throw std::runtime_error("no data");
 
   stream->type = LISP_STREAM_ANY;
   stream->v.any.data = data;
@@ -499,7 +503,7 @@ SuperTux::lisp_read (lisp_stream_t *in)
       return lisp_make_boolean(0);
     }
 
-  throw LispReaderException("lisp_read()", __FILE__, __LINE__);
+  throw std::runtime_error("syntax error in lisp file");
   return &error_object;
 }
 
@@ -663,7 +667,7 @@ static int
 _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars)
 {
   if (lisp_type(pattern) != LISP_TYPE_PATTERN_VAR)
-    throw LispReaderException("_match_pattern_var", __FILE__, __LINE__);
+    throw std::runtime_error("type is not a var");
 
   switch (pattern->v.pattern.type)
     {
@@ -708,7 +712,7 @@ _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **
         for (sub = pattern->v.pattern.sub; sub != 0; sub = lisp_cdr(sub))
           {
             if (lisp_type(sub) != LISP_TYPE_CONS)
-              throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
+              throw std::runtime_error("type isn't a car/cons");
 
             if (_match_pattern(lisp_car(sub), obj, vars))
               matched = 1;
@@ -720,7 +724,7 @@ _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **
       break;
 
     default :
-      throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
+      assert(false);
     }
 
   if (vars != 0)
@@ -770,7 +774,7 @@ _match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars
       break;
 
     default :
-      throw LispReaderException("_match_pattern()", __FILE__, __LINE__);
+      assert(false);
     }
 
   return 0;
@@ -826,7 +830,7 @@ int
 SuperTux::lisp_integer (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_INTEGER)
-    throw LispReaderException("lisp_integer()", __FILE__, __LINE__);
+    throw std::runtime_error("expected integer");
 
   return obj->v.integer;
 }
@@ -835,7 +839,7 @@ char*
 SuperTux::lisp_symbol (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_SYMBOL)
-    throw LispReaderException("lisp_symbol()", __FILE__, __LINE__);
+    throw std::runtime_error("expected symbol");
 
   return obj->v.string;
 }
@@ -844,7 +848,7 @@ char*
 SuperTux::lisp_string (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_STRING)
-    throw LispReaderException("lisp_string()", __FILE__, __LINE__);
+    throw std::runtime_error("expected string");
 
   return obj->v.string;
 }
@@ -853,7 +857,7 @@ int
 SuperTux::lisp_boolean (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_BOOLEAN)
-    throw LispReaderException("lisp_boolean()", __FILE__, __LINE__);
+    throw std::runtime_error("expected boolean");
 
   return obj->v.integer;
 }
@@ -862,7 +866,7 @@ float
 SuperTux::lisp_real (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_REAL && obj->type != LISP_TYPE_INTEGER)
-    throw LispReaderException("lisp_real()", __FILE__, __LINE__);
+    throw std::runtime_error("expected real");
 
   if (obj->type == LISP_TYPE_INTEGER)
     return obj->v.integer;
@@ -873,7 +877,7 @@ lisp_object_t*
 SuperTux::lisp_car (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
-    throw LispReaderException("lisp_car()", __FILE__, __LINE__);
+    throw std::runtime_error("expected car");
 
   return obj->v.cons.car;
 }
@@ -882,7 +886,7 @@ lisp_object_t*
 SuperTux::lisp_cdr (lisp_object_t *obj)
 {
   if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
-    throw LispReaderException("lisp_cdr()", __FILE__, __LINE__);
+    throw std::runtime_error("expected cons");
 
   return obj->v.cons.cdr;
 }
@@ -898,7 +902,7 @@ SuperTux::lisp_cxr (lisp_object_t *obj, const char *x)
     else if (x[i] == 'd')
       obj = lisp_cdr(obj);
     else
-      throw LispReaderException("lisp_cxr()", __FILE__, __LINE__);
+      throw std::runtime_error("couldn't parse cxr");
 
   return obj;
 }
@@ -911,7 +915,7 @@ SuperTux::lisp_list_length (lisp_object_t *obj)
   while (obj != 0)
     {
       if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
-        throw LispReaderException("lisp_list_length()", __FILE__, __LINE__);
+        throw std::runtime_error("expected cons");
 
       ++length;
       obj = obj->v.cons.cdr;
@@ -926,9 +930,9 @@ SuperTux::lisp_list_nth_cdr (lisp_object_t *obj, int index)
   while (index > 0)
     {
       if (obj == 0)
-        throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
+        throw std::runtime_error("list too short");
       if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
-        throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
+        throw std::runtime_error("expected cons");
 
       --index;
       obj = obj->v.cons.cdr;
@@ -943,7 +947,7 @@ SuperTux::lisp_list_nth (lisp_object_t *obj, int index)
   obj = lisp_list_nth_cdr(obj, index);
 
   if (obj == 0)
-    throw LispReaderException("lisp_list_nth()", __FILE__, __LINE__);
+    throw std::runtime_error("list too short");
 
   return obj->v.cons.car;
 }
@@ -1025,7 +1029,7 @@ SuperTux::lisp_dump (lisp_object_t *obj, FILE *out)
       break;
 
     default :
-      throw LispReaderException("lisp_dump()", __FILE__, __LINE__);
+      throw std::runtime_error("unknown list type");
     }
 }
 
@@ -1049,13 +1053,12 @@ LispReader::load(const std::string& filename, const std::string& toplevellist)
 
   if(obj->type == LISP_TYPE_EOF || obj->type == LISP_TYPE_PARSE_ERROR) {
     lisp_free(obj);
-    throw LispReaderException("LispReader::load", __FILE__, __LINE__);
+    throw std::runtime_error("Error while parsing lispfile");
   }
 
   if(toplevellist != lisp_symbol(lisp_car(obj))) {
     lisp_car(obj);
-    throw LispReaderException("LispReader::load wrong toplevel symbol",
-        __FILE__, __LINE__);
+    throw std::runtime_error("Worng toplevel symbol in lisp file");
   }
   
   LispReader* reader = new LispReader(lisp_cdr(obj));  
@@ -1077,7 +1080,6 @@ LispReader::search_for(const char* name)
       if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
         {
           lisp_dump(cur, stdout);
-          //throw ConstruoError (std::string("LispReader: Read error in search_for ") + name);
          printf("LispReader: Read error in search\n");
         }
       else
@@ -1108,6 +1110,20 @@ LispReader::read_int (const char* name, int& i)
 }
 
 bool
+LispReader::read_uint (const char* name, unsigned int& i)
+{
+  lisp_object_t* obj = search_for (name);
+  if(!obj)
+    return false;
+      
+  if (!lisp_integer_p(lisp_car(obj)))
+    return false;
+  
+  i = (unsigned int) lisp_integer(lisp_car(obj));
+  return true;
+}
+
+bool
 LispReader::read_lisp(const char* name, lisp_object_t*& b)
 {
   lisp_object_t* obj = search_for (name);
@@ -1291,5 +1307,3 @@ lisp_object_t* SuperTux::lisp_read_from_file(const std::string& filename)
 
   return obj;
 }
-
-// EOF //