- replaced YES/NO with true/false
[supertux.git] / src / lispreader.cpp
index 8a64fc2..db0b5c3 100644 (file)
@@ -1060,6 +1060,22 @@ LispReader::read_float (const char* name, float* f)
 }
 
 bool
+LispReader::read_string_vector (const char* name, std::vector<std::string>* vec)
+{
+  lisp_object_t* obj = search_for (name);
+  if (obj)
+    {
+      while(!lisp_nil_p(obj))
+        {
+          vec->push_back(lisp_string(lisp_car(obj)));
+          obj = lisp_cdr(obj);
+        }
+      return true;
+    }
+  return false;    
+}
+
+bool
 LispReader::read_int_vector (const char* name, std::vector<int>* vec)
 {
   lisp_object_t* obj = search_for (name);
@@ -1196,3 +1212,113 @@ LispWriter::create_lisp ()
   return lisp_obj;
 }
 
+#if 0
+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);
+}
+#endif
+
+lisp_object_t* lisp_read_from_gzfile(const char* filename)
+{
+  bool done = false;
+  lisp_object_t* root_obj = 0;
+  int chunk_size = 128 * 1024;
+  int buf_pos = 0;
+  int try_number = 1;
+  char* buf = static_cast<char*>(malloc(chunk_size));
+  assert(buf);
+
+  gzFile in = gzopen(filename, "r");
+
+  while (!done)
+    {
+      int ret = gzread(in, buf + buf_pos, chunk_size);
+      if (ret == -1)
+        {
+          free (buf);
+          assert(!"Error while reading from file");
+        }
+      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);
+        }
+      else 
+        {
+          // everything fine, encountered EOF 
+          done = true;
+        }
+    }
+      
+  lisp_stream_t stream;
+  lisp_stream_init_string (&stream, buf);
+  root_obj = lisp_read (&stream);
+      
+  free(buf);
+  gzclose(in);
+
+  return root_obj;
+}
+
+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 std::string& filename)
+{
+  lisp_stream_t stream;
+
+  if (has_suffix(filename.c_str(), ".gz"))
+    {
+      return lisp_read_from_gzfile(filename.c_str());
+#if 0
+      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;
+#endif
+    }
+  else
+    {
+      lisp_object_t* obj = 0;
+      FILE* in = fopen(filename.c_str(), "r");
+
+      if (in)
+        {
+          lisp_stream_init_file(&stream, in);
+          obj = lisp_read(&stream);
+          fclose(in);
+        }
+
+      return obj;
+    }
+}
+
+// EOF //