[Super-tux-commit] supertux/lib/utils configfile.cpp,NONE,1.1 configfile.h,NONE,1.1 exceptions.h,NON
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-20 17:51:50
|
Update of /cvsroot/super-tux/supertux/lib/utils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19557/lib/utils Added Files: configfile.cpp configfile.h exceptions.h lispreader.cpp lispreader.h lispwriter.cpp lispwriter.h Log Message: Generated SuperTux libtool library containing more general source, that could prove useful for other applications/games. Caution: It's not yet SuperTux independed, more work on this will follow, that's just the first step. The file structure isn't fixed, better ideas will surely find there way in it! --- NEW FILE: lispwriter.h --- // $Id: lispwriter.h,v 1.1 2004/07/20 17:51:38 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // 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_LISPWRITER_H #define SUPERTUX_LISPWRITER_H #include <iostream> #include <string> #include <vector> class LispWriter { public: LispWriter(std::ostream& out); ~LispWriter(); void write_comment(const std::string& comment); void start_list(const std::string& listname); void write_int(const std::string& name, int value); void write_float(const std::string& name, float value); void write_string(const std::string& name, const std::string& value); void write_bool(const std::string& name, bool value); void write_int_vector(const std::string& name, const std::vector<int>& value); void write_int_vector(const std::string& name, const std::vector<unsigned int>& value); // add more write-functions when needed... void end_list(const std::string& listname); private: void indent(); std::ostream& out; int indent_depth; std::vector<std::string> lists; }; #endif --- NEW FILE: configfile.h --- // $Id: configfile.h,v 1.1 2004/07/20 17:51:37 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Michael George <mi...@ge...> // // 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_CONFIGFILE_H #define SUPERTUX_CONFIGFILE_H #include "utils/lispreader.h" class Config { public: void load (); void save (); virtual void customload(LispReader& reader) {}; virtual void customsave(FILE * config) {}; }; extern Config* config; #endif /* Local Variables: */ /* mode:c++ */ /* End: */ --- NEW FILE: lispreader.h --- /* $Id: lispreader.h,v 1.1 2004/07/20 17:51:38 tobgle Exp $ */ /* * lispreader.h * * Copyright (C) 1998-2000 Mark Probst * Copyright (C) 2002 Ingo Ruhnke <gr...@gm...> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef SUPERTUX_LISPREADER_H #define SUPERTUX_LISPREADER_H #include <cstdio> #include <string> #include <vector> #include <exception> #include <zlib.h> #include "utils/exceptions.h" #define LISP_STREAM_FILE 1 #define LISP_STREAM_STRING 2 #define LISP_STREAM_ANY 3 #define LISP_TYPE_INTERNAL -3 #define LISP_TYPE_PARSE_ERROR -2 #define LISP_TYPE_EOF -1 #define LISP_TYPE_NIL 0 #define LISP_TYPE_SYMBOL 1 #define LISP_TYPE_INTEGER 2 #define LISP_TYPE_STRING 3 #define LISP_TYPE_REAL 4 #define LISP_TYPE_CONS 5 #define LISP_TYPE_PATTERN_CONS 6 #define LISP_TYPE_BOOLEAN 7 #define LISP_TYPE_PATTERN_VAR 8 #define LISP_PATTERN_ANY 1 #define LISP_PATTERN_SYMBOL 2 #define LISP_PATTERN_STRING 3 #define LISP_PATTERN_INTEGER 4 #define LISP_PATTERN_REAL 5 #define LISP_PATTERN_BOOLEAN 6 #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; union { FILE *file; struct { char *buf; int pos; } string; struct { void *data; int (*next_char) (void *data); void (*unget_char) (char c, void *data); } any; } v; } lisp_stream_t; typedef struct _lisp_object_t lisp_object_t; struct _lisp_object_t { int type; union { struct { struct _lisp_object_t *car; struct _lisp_object_t *cdr; } cons; char *string; int integer; float real; struct { int type; int index; struct _lisp_object_t *sub; } pattern; } v; }; lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file); lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf); lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data, int (*next_char) (void *data), void (*unget_char) (char c, void *data)); lisp_object_t* lisp_read (lisp_stream_t *in); lisp_object_t* lisp_read_from_file(const std::string& filename); void lisp_free (lisp_object_t *obj); lisp_object_t* lisp_read_from_string (const char *buf); int lisp_compile_pattern (lisp_object_t **obj, int *num_subs); int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs); int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars); int lisp_type (lisp_object_t *obj); int lisp_integer (lisp_object_t *obj); float lisp_real (lisp_object_t *obj); char* lisp_symbol (lisp_object_t *obj); char* lisp_string (lisp_object_t *obj); int lisp_boolean (lisp_object_t *obj); lisp_object_t* lisp_car (lisp_object_t *obj); lisp_object_t* lisp_cdr (lisp_object_t *obj); lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x); lisp_object_t* lisp_make_integer (int value); lisp_object_t* lisp_make_real (float value); lisp_object_t* lisp_make_symbol (const char *value); lisp_object_t* lisp_make_string (const char *value); lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr); lisp_object_t* lisp_make_boolean (int value); int lisp_list_length (lisp_object_t *obj); lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index); lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index); void lisp_dump (lisp_object_t *obj, FILE *out); #define lisp_nil() ((lisp_object_t*)0) #define lisp_nil_p(obj) (obj == 0) #define lisp_integer_p(obj) (lisp_type((obj)) == LISP_TYPE_INTEGER) #define lisp_real_p(obj) (lisp_type((obj)) == LISP_TYPE_REAL) #define lisp_symbol_p(obj) (lisp_type((obj)) == LISP_TYPE_SYMBOL) #define lisp_string_p(obj) (lisp_type((obj)) == LISP_TYPE_STRING) #define lisp_cons_p(obj) (lisp_type((obj)) == LISP_TYPE_CONS) #define lisp_boolean_p(obj) (lisp_type((obj)) == LISP_TYPE_BOOLEAN) /** */ class LispReader { private: lisp_object_t* owner; lisp_object_t* lst; lisp_object_t* search_for(const char* name); public: /** cur == ((pos 1 2 3) (id 12 3 4)...) */ LispReader(lisp_object_t* l); ~LispReader(); bool read_int_vector(const char* name, std::vector<int>& vec); bool read_int_vector(const char* name, std::vector<unsigned int>& vec); bool read_char_vector(const char* name, std::vector<char>& vec); bool read_string_vector(const char* name, std::vector<std::string>& vec); bool read_string(const char* name, std::string& str, bool translatable = false); bool read_int(const char* name, int& i); bool read_float(const char* name, float& f); bool read_bool(const char* name, bool& b); bool read_lisp(const char* name, lisp_object_t*& b); lisp_object_t* read_lisp(const char* name); static LispReader* load(const std::string& filename, const std::string& toplevellist); lisp_object_t* get_lisp(); }; #endif /*SUPERTUX_LISPREADER_H*/ --- NEW FILE: lispwriter.cpp --- // $Id: lispwriter.cpp,v 1.1 2004/07/20 17:51:38 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // 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. #include <iostream> #include "utils/lispwriter.h" LispWriter::LispWriter(std::ostream& newout) : out(newout), indent_depth(0) { } LispWriter::~LispWriter() { if(lists.size() > 0) { std::cerr << "Warning: Not all sections closed in lispwriter!\n"; } } void LispWriter::write_comment(const std::string& comment) { out << "; " << comment << "\n"; } void LispWriter::start_list(const std::string& listname) { indent(); out << '(' << listname << '\n'; indent_depth += 2; lists.push_back(listname); } void LispWriter::end_list(const std::string& listname) { if(lists.size() == 0) { std::cerr << "Trying to close list '" << listname << "', which is not open.\n"; return; } if(lists.back() != listname) { std::cerr << "Warning: trying to close list '" << listname << "' while list '" << lists.back() << "' is open.\n"; return; } lists.pop_back(); indent_depth -= 2; indent(); out << ")\n"; } void LispWriter::write_int(const std::string& name, int value) { indent(); out << '(' << name << ' ' << value << ")\n"; } void LispWriter::write_float(const std::string& name, float value) { indent(); out << '(' << name << ' ' << value << ")\n"; } void LispWriter::write_string(const std::string& name, const std::string& value) { indent(); out << '(' << name << " \"" << value << "\")\n"; } void LispWriter::write_bool(const std::string& name, bool value) { indent(); out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n"; } void LispWriter::write_int_vector(const std::string& name, const std::vector<int>& value) { indent(); out << '(' << name; for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i) out << " " << *i; out << ")\n"; } void LispWriter::write_int_vector(const std::string& name, const std::vector<unsigned int>& value) { indent(); out << '(' << name; for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i) out << " " << *i; out << ")\n"; } void LispWriter::indent() { for(int i = 0; i<indent_depth; ++i) out << ' '; } --- NEW FILE: configfile.cpp --- // $Id: configfile.cpp,v 1.1 2004/07/20 17:51:37 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Michael George <mi...@ge...> // // 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. #include <cstdlib> #include <string> #include "utils/configfile.h" #include "app/setup.h" #include "app/globals.h" #ifdef WIN32 const char * config_filename = "/st_config.dat"; #else const char * config_filename = "/config"; #endif Config* config = 0; static void defaults () { /* Set defaults: */ debug_mode = false; audio_device = true; use_fullscreen = false; show_fps = false; use_gl = false; use_sound = true; use_music = true; } void Config::load() { FILE * file = NULL; defaults(); /* override defaults from config file */ file = opendata(config_filename, "r"); if (file == NULL) return; /* read config file */ lisp_stream_t stream; lisp_object_t * root_obj = NULL; lisp_stream_init_file (&stream, file); root_obj = lisp_read (&stream); if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) return; if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-config") != 0) return; LispReader reader(lisp_cdr(root_obj)); reader.read_bool("fullscreen", use_fullscreen); reader.read_bool("sound", use_sound); reader.read_bool("music", use_music); reader.read_bool("show_fps", show_fps); std::string video; reader.read_string ("video", video); if (video == "opengl") use_gl = true; else use_gl = false; reader.read_int ("joystick", joystick_num); if (joystick_num >= 0) { reader.read_int ("joystick-x", joystick_keymap.x_axis); reader.read_int ("joystick-y", joystick_keymap.y_axis); reader.read_int ("joystick-a", joystick_keymap.a_button); reader.read_int ("joystick-b", joystick_keymap.b_button); reader.read_int ("joystick-start", joystick_keymap.start_button); reader.read_int ("joystick-deadzone", joystick_keymap.dead_zone); } customload(reader); lisp_free(root_obj); } void Config::save () { /* write settings to config file */ FILE * config = opendata(config_filename, "w"); if(config) { fprintf(config, "(supertux-config\n"); fprintf(config, "\t;; the following options can be set to #t or #f:\n"); fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f"); fprintf(config, "\t(sound %s)\n", use_sound ? "#t" : "#f"); fprintf(config, "\t(music %s)\n", use_music ? "#t" : "#f"); fprintf(config, "\t(show_fps %s)\n", show_fps ? "#t" : "#f"); fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n"); fprintf(config, "\t(video \"%s\")\n", use_gl ? "opengl" : "sdl"); if(use_joystick) { fprintf(config, "\n\t;; joystick number:\n"); fprintf(config, "\t(joystick %d)\n", joystick_num); fprintf(config, "\t(joystick-x %d)\n", joystick_keymap.x_axis); fprintf(config, "\t(joystick-y %d)\n", joystick_keymap.y_axis); fprintf(config, "\t(joystick-a %d)\n", joystick_keymap.a_button); fprintf(config, "\t(joystick-b %d)\n", joystick_keymap.b_button); fprintf(config, "\t(joystick-start %d)\n", joystick_keymap.start_button); fprintf(config, "\t(joystick-deadzone %d)\n", joystick_keymap.dead_zone); } customsave(config); fprintf(config, ")\n"); } } /* EOF */ --- NEW FILE: exceptions.h --- // $Id: exceptions.h,v 1.1 2004/07/20 17:51:38 tobgle 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*/ --- NEW FILE: lispreader.cpp --- /* $Id: lispreader.cpp,v 1.1 2004/07/20 17:51:38 tobgle Exp $ */ /* * lispreader.c * * Copyright (C) 1998-2000 Mark Probst * Copyright (C) 2002 Ingo Ruhnke <gr...@gm...> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the [...1254 lines suppressed...] { return lst; } lisp_object_t* lisp_read_from_file(const std::string& filename) { FILE* in = fopen(filename.c_str(), "r"); if(!in) return 0; lisp_stream_t stream; lisp_stream_init_file(&stream, in); lisp_object_t* obj = lisp_read(&stream); fclose(in); return obj; } // EOF // |