From: Ingo Ruhnke Date: Sat, 20 Mar 2004 18:09:49 +0000 (+0000) Subject: - added zlib support to lispreader X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=3181262386659262786395df341544a24c667caf;p=supertux.git - added zlib support to lispreader SVN-Revision: 280 --- diff --git a/configure.ac b/configure.ac index fac9c086b..0aeb89d1a 100644 --- a/configure.ac +++ b/configure.ac @@ -104,6 +104,8 @@ if test "x${enable_opengl}" != "xno"; then enable_opengl="no"]) fi +AC_CHECK_LIB(z, gzopen,, AC_MSG_ERROR([*** zlib is missing])) + # FIXME: Evil CXXFLAGS="$CXXFLAGS -DDATA_PREFIX='\"$datadir/supertux/\"' -DLINUX" diff --git a/src/lispreader.cpp b/src/lispreader.cpp index 8a64fc25d..add98b2c6 100644 --- a/src/lispreader.cpp +++ b/src/lispreader.cpp @@ -1196,3 +1196,65 @@ LispWriter::create_lisp () return lisp_obj; } +void mygzungetc(char c, void* file) +{ + gzungetc(c, file); +} + +lisp_stream_t* lisp_stream_init_gzfile (lisp_stream_t *stream, gzFile file) +{ + return lisp_stream_init_any (stream, file, gzgetc, mygzungetc); +} + +bool has_suffix(const char* data, const char* suffix) +{ + int suffix_len = strlen(suffix); + int data_len = strlen(data); + + const char* data_suffix = (data + data_len - suffix_len); + + if (data_suffix >= data) + { + return (strcmp(data_suffix, suffix) == 0); + } + else + { + return false; + } +} + +lisp_object_t* lisp_read_from_file(const char* filename) +{ + lisp_stream_t stream; + + if (has_suffix(filename, ".gz")) + { + lisp_object_t* obj = 0; + gzFile in = gzopen(filename, "r"); + + if (in) + { + lisp_stream_init_gzfile(&stream, in); + obj = lisp_read(&stream); + gzclose(in); + } + + return obj; + } + else + { + lisp_object_t* obj = 0; + FILE* in = fopen(filename, "r"); + + if (in) + { + lisp_stream_init_file(&stream, in); + obj = lisp_read(&stream); + fclose(in); + } + + return obj; + } +} + +// EOF // diff --git a/src/lispreader.h b/src/lispreader.h index 28d1a2083..5dde3b1aa 100644 --- a/src/lispreader.h +++ b/src/lispreader.h @@ -25,6 +25,7 @@ #define __LISPREADER_H__ #include +#include #include #include @@ -106,6 +107,7 @@ struct _lisp_object_t } v; }; +lisp_stream_t* lisp_stream_init_gzfile (lisp_stream_t *stream, gzFile file); 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, @@ -113,6 +115,7 @@ lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, 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 char* filename); void lisp_free (lisp_object_t *obj); lisp_object_t* lisp_read_from_string (const char *buf); diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 97bc486b6..9cd8ba569 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -32,12 +32,10 @@ TileManager* TileManager::instance_ = 0; TileManager::TileManager() { - lisp_stream_t stream; - FILE* in = fopen(DATA_PREFIX "images/worldmap/antarctica.scm", "r"); - assert(in); - lisp_stream_init_file (&stream, in); - lisp_object_t* root_obj = lisp_read (&stream); + lisp_object_t* root_obj = lisp_read_from_file(DATA_PREFIX "images/worldmap/antarctica.scm"); + assert(root_obj); + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0) { lisp_object_t* cur = lisp_cdr(root_obj); @@ -218,11 +216,8 @@ WorldMap::~WorldMap() void WorldMap::load_map() { - lisp_stream_t stream; - FILE* in = fopen(DATA_PREFIX "levels/default/worldmap.scm", "r"); - assert(in); - lisp_stream_init_file (&stream, in); - lisp_object_t* root_obj = lisp_read (&stream); + lisp_object_t* root_obj = lisp_read_from_file(DATA_PREFIX "levels/default/worldmap.scm"); + assert(root_obj); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0) { diff --git a/src/worldmap.h b/src/worldmap.h index b347bc928..4767c2e4d 100644 --- a/src/worldmap.h +++ b/src/worldmap.h @@ -44,13 +44,6 @@ struct Point int y; }; -struct Level -{ - int x; - int y; - std::string name; -}; - struct Tile { texture_type sprite; @@ -128,6 +121,13 @@ private: int width; int height; + struct Level + { + int x; + int y; + std::string name; + }; + typedef std::vector Levels; Levels levels;