[Super-tux-commit] supertux/src exceptions.h,NONE,1.1 supertux.cpp,1.16,1.17 lispreader.cpp,1.18,1.1
Brought to you by:
wkendrick
From: Ryan F. <sik...@us...> - 2004-05-11 22:16:23
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23965 Modified Files: supertux.cpp lispreader.cpp lispreader.h Added Files: exceptions.h Log Message: - added exceptions --- NEW FILE: exceptions.h --- // $Id: exceptions.h,v 1.1 2004/05/11 22:16:12 sik0fewl Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2003 Tobias Glaesser <tob...@gm...> // // 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 2 // 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, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef SUPERTUX_EXCEPTIONS_H #define SUPERTUX_EXCEPTIONS_H // Exceptions #include <exception> #include <string> class SuperTuxException : public std::exception { public: SuperTuxException(const char* _message, const char* _file = "", const unsigned int _line = 0) : message(_message), file(_file), line(_line) { }; virtual ~SuperTuxException() throw() { }; const char* what() const throw() { return message; }; const char* what_file() const throw() { return file; }; const unsigned int what_line() const throw() { return line; }; private: const char* message; const char* file; const unsigned int line; }; #endif /*SUPERTUX_EXCEPTIONS_H*/ Index: supertux.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/supertux.cpp,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- supertux.cpp 5 May 2004 16:07:20 -0000 1.16 +++ supertux.cpp 11 May 2004 22:16:12 -0000 1.17 @@ -20,6 +20,10 @@ #include <sys/types.h> #include <ctype.h> +#include <iostream> + +#include <exception> +#include "exceptions.h" #include "defines.h" #include "globals.h" @@ -36,40 +40,50 @@ int main(int argc, char * argv[]) { - st_directory_setup(); - parseargs(argc, argv); - - st_audio_setup(); - st_video_setup(); - st_joystick_setup(); - st_general_setup(); - st_menu(); - loadshared(); + try { + st_directory_setup(); + parseargs(argc, argv); - if (launch_leveleditor_mode && level_startup_file) + st_audio_setup(); + st_video_setup(); + st_joystick_setup(); + st_general_setup(); + st_menu(); + loadshared(); + + if (launch_leveleditor_mode && level_startup_file) { leveleditor(level_startup_file); } - else if (level_startup_file) + else if (level_startup_file) { GameSession session(level_startup_file, 1, ST_GL_LOAD_LEVEL_FILE); session.run(); } - else + else { title(); } - - clearscreen(0, 0, 0); - updatescreen(); - unloadshared(); - st_general_free(); - TileManager::destroy_instance(); -#ifdef DEBUG - Surface::debug_check(); -#endif - st_shutdown(); - + clearscreen(0, 0, 0); + updatescreen(); + + unloadshared(); + st_general_free(); + TileManager::destroy_instance(); + #ifdef DEBUG + Surface::debug_check(); + #endif + st_shutdown(); + } + catch (SuperTuxException &e) + { + std::cerr << "Unhandled SuperTux exception:\n " << e.what_file() << ":" << e.what_line() << ": " << e.what() << std::endl; + } + catch (std::exception &e) + { + std:: cerr << "Unhandled exception: " << e.what() << std::endl; + } + return 0; } Index: lispreader.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/lispreader.cpp,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- lispreader.cpp 3 May 2004 23:06:19 -0000 1.18 +++ lispreader.cpp 11 May 2004 22:16:12 -0000 1.19 @@ -23,7 +23,6 @@ #include <iostream> #include <string> -#include <assert.h> #include <ctype.h> #include <stdlib.h> #include <string.h> @@ -64,7 +63,8 @@ static void _token_append (char c) { - assert(token_length < MAX_TOKEN_LENGTH); + if (token_length >= MAX_TOKEN_LENGTH) + throw LispReaderException("_token_append()", __FILE__, __LINE__); token_string[token_length++] = c; token_string[token_length] = '\0'; @@ -93,7 +93,8 @@ case LISP_STREAM_ANY: return stream->v.any.next_char(stream->v.any.data); } - assert(0); + + throw LispReaderException("_next_char()", __FILE__, __LINE__); return EOF; } @@ -115,7 +116,7 @@ break; default : - assert(0); + throw LispReaderException("_unget_char()", __FILE__, __LINE__); } } @@ -267,7 +268,7 @@ } } - assert(0); + throw LispReaderException("_scan()", __FILE__, __LINE__); return TOKEN_ERROR; } @@ -305,7 +306,8 @@ int (*next_char) (void *data), void (*unget_char) (char c, void *data)) { - assert(next_char != 0 && unget_char != 0); + if (next_char == 0 || unget_char == 0) + throw LispReaderException("lisp_stream_init_any()", __FILE__, __LINE__); stream->type = LISP_STREAM_ANY; stream->v.any.data = data; @@ -493,7 +495,7 @@ return lisp_make_boolean(0); } - assert(0); + throw LispReaderException("lisp_read()", __FILE__, __LINE__); return &error_object; } @@ -643,7 +645,8 @@ static int _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars) { - assert(lisp_type(pattern) == LISP_TYPE_PATTERN_VAR); + if (lisp_type(pattern) != LISP_TYPE_PATTERN_VAR) + throw LispReaderException("_match_pattern_var", __FILE__, __LINE__); switch (pattern->v.pattern.type) { @@ -687,7 +690,8 @@ for (sub = pattern->v.pattern.sub; sub != 0; sub = lisp_cdr(sub)) { - assert(lisp_type(sub) == LISP_TYPE_CONS); + if (lisp_type(sub) != LISP_TYPE_CONS) + throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__); if (_match_pattern(lisp_car(sub), obj, vars)) matched = 1; @@ -699,7 +703,7 @@ break; default : - assert(0); + throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__); } if (vars != 0) @@ -749,7 +753,7 @@ break; default : - assert(0); + throw LispReaderException("_match_pattern()", __FILE__, __LINE__); } return 0; @@ -804,7 +808,8 @@ int lisp_integer (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_INTEGER); + if (obj->type != LISP_TYPE_INTEGER) + throw LispReaderException("lisp_integer()", __FILE__, __LINE__); return obj->v.integer; } @@ -812,7 +817,8 @@ char* lisp_symbol (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_SYMBOL); + if (obj->type != LISP_TYPE_SYMBOL) + throw LispReaderException("lisp_symbol()", __FILE__, __LINE__); return obj->v.string; } @@ -820,7 +826,8 @@ char* lisp_string (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_STRING); + if (obj->type != LISP_TYPE_STRING) + throw LispReaderException("lisp_string()", __FILE__, __LINE__); return obj->v.string; } @@ -828,7 +835,8 @@ int lisp_boolean (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_BOOLEAN); + if (obj->type != LISP_TYPE_BOOLEAN) + throw LispReaderException("lisp_boolean()", __FILE__, __LINE__); return obj->v.integer; } @@ -836,7 +844,8 @@ float lisp_real (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_REAL || obj->type == LISP_TYPE_INTEGER); + if (obj->type != LISP_TYPE_REAL && obj->type != LISP_TYPE_INTEGER) + throw LispReaderException("lisp_real()", __FILE__, __LINE__); if (obj->type == LISP_TYPE_INTEGER) return obj->v.integer; @@ -846,7 +855,8 @@ lisp_object_t* lisp_car (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS); + if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS) + throw LispReaderException("lisp_car()", __FILE__, __LINE__); return obj->v.cons.car; } @@ -854,7 +864,8 @@ lisp_object_t* lisp_cdr (lisp_object_t *obj) { - assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS); + if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS) + throw LispReaderException("lisp_cdr()", __FILE__, __LINE__); return obj->v.cons.cdr; } @@ -870,7 +881,7 @@ else if (x[i] == 'd') obj = lisp_cdr(obj); else - assert(0); + throw LispReaderException("lisp_cxr()", __FILE__, __LINE__); return obj; } @@ -882,7 +893,8 @@ while (obj != 0) { - assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS); + if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS) + throw LispReaderException("lisp_list_length()", __FILE__, __LINE__); ++length; obj = obj->v.cons.cdr; @@ -896,8 +908,10 @@ { while (index > 0) { - assert(obj != 0); - assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS); + if (obj == 0) + throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__); + if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS) + throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__); --index; obj = obj->v.cons.cdr; @@ -911,7 +925,8 @@ { obj = lisp_list_nth_cdr(obj, index); - assert(obj != 0); + if (obj == 0) + throw LispReaderException("lisp_list_nth()", __FILE__, __LINE__); return obj->v.cons.car; } @@ -993,7 +1008,7 @@ break; default : - assert(0); + throw LispReaderException("lisp_dump()", __FILE__, __LINE__); } } @@ -1260,7 +1275,8 @@ int buf_pos = 0; int try_number = 1; char* buf = static_cast<char*>(malloc(chunk_size)); - assert(buf); + if (!buf) + throw LispReaderException("lisp_read_from_gzfile()", __FILE__, __LINE__); gzFile in = gzopen(filename, "r"); @@ -1270,14 +1286,16 @@ if (ret == -1) { free (buf); - assert(!"Error while reading from file"); + throw LispReaderException("Error while reading from file", __FILE__, __LINE__); } else if (ret == chunk_size) // buffer got full, eof not yet there so resize { buf_pos = chunk_size * try_number; try_number += 1; buf = static_cast<char*>(realloc(buf, chunk_size * try_number)); - assert(buf); + + if (!buf) + throw LispReaderException("lisp_read_from_gzfile()", __FILE__, __LINE__); } else { Index: lispreader.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/lispreader.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- lispreader.h 25 Mar 2004 11:36:05 -0000 1.8 +++ lispreader.h 11 May 2004 22:16:12 -0000 1.9 @@ -28,6 +28,8 @@ #include <zlib.h> #include <string> #include <vector> +#include <exception> +#include "exceptions.h" #define LISP_STREAM_FILE 1 #define LISP_STREAM_STRING 2 @@ -55,6 +57,14 @@ #define LISP_PATTERN_LIST 7 #define LISP_PATTERN_OR 8 +// Exception +class LispReaderException : public SuperTuxException +{ + public: + LispReaderException(const char* _message = "lispreader error", const char* _file = "", const unsigned int _line = 0) + : SuperTuxException(_message, _file, _line) { }; +}; + typedef struct { int type; |