implemented a new object factory mechanism which is now really independent of the...
[supertux.git] / lib / utils / configfile.cpp
index 4634422..ebf9e3b 100644 (file)
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+#include <config.h>
+
 #include <cstdlib>
 #include <string>
+#include <stdexcept>
 
-#include "../utils/configfile.h"
-#include "../app/setup.h"
-#include "../app/globals.h"
-#include "../audio/sound_manager.h"
+#include "configfile.h"
+#include "app/setup.h"
+#include "app/globals.h"
+#include "audio/sound_manager.h"
+#include "lisp/parser.h"
 
 using namespace SuperTux;
 
 #ifdef WIN32
-const char * config_filename = "/st_config.dat";
+const char * config_filename = ("/"+ package_symbol_name + "_config.dat").c_str();
 #else
 const char * config_filename = "/config";
 #endif
@@ -49,95 +53,74 @@ static void defaults ()
   SoundManager::get()->enable_music(true);
 }
 
-FILE * SuperTux::opendata(const char * rel_filename, const char * mode)
+FILE * SuperTux::opendata(const std::string& rel_filename, const char *mode)
 {
-  char * filename = NULL;
+  std::string filename;
   FILE * fi;
 
-  filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
-                                             strlen(rel_filename) + 1));
-
-  strcpy(filename, st_dir);
-  /* Open the high score file: */
-
-  strcat(filename, rel_filename);
+  filename = st_dir + rel_filename;
 
   /* Try opening the file: */
-  fi = fopen(filename, mode);
+  fi = fopen(filename.c_str(), mode);
 
   if (fi == NULL)
     {
-      fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
+      fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename.c_str());
 
       if (strcmp(mode, "r") == 0)
         fprintf(stderr, "for read!!!\n");
       else if (strcmp(mode, "w") == 0)
         fprintf(stderr, "for write!!!\n");
     }
-  free( filename );
 
   return(fi);
 }
 
 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);
-  bool temp;
-  reader.read_bool("sound",     temp);
-  SoundManager::get()->enable_sound(temp);
-  reader.read_bool("music",      temp);
-  SoundManager::get()->enable_music(temp);
-  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);
+  lisp::Parser parser;
+  try {
+    std::auto_ptr<lisp::Lisp> root (parser.parse(st_dir + config_filename));
+
+    const lisp::Lisp* config_lisp = root->get_lisp(
+        package_symbol_name + "-config");
+    if(!config_lisp)
+      throw new std::runtime_error("Config file is not a supertux-config file");
+
+    config_lisp->get("fullscreen", use_fullscreen);
+    bool temp = false;
+    if(config_lisp->get("sound", temp))
+      SoundManager::get()->enable_sound(temp);
+    if(config_lisp->get("music", temp))
+      SoundManager::get()->enable_music(temp);
+    config_lisp->get("show_fps",   show_fps);
+
+    std::string video;
+    if(config_lisp->get("video", video)) {
+      if (video == "opengl")
+        use_gl = true;
+      else
+        use_gl = false;
     }
 
-  customload(reader);
+    joystick_num = 0;
+    config_lisp->get("joystick", joystick_num);
+    
+    if (joystick_num >= 0) {
+      config_lisp->get("joystick-x", joystick_keymap.x_axis);
+      config_lisp->get("joystick-y", joystick_keymap.y_axis);
+      config_lisp->get("joystick-a", joystick_keymap.a_button);
+      config_lisp->get("joystick-b", joystick_keymap.b_button);
+      config_lisp->get("joystick-start", joystick_keymap.start_button);
+      config_lisp->get("joystick-deadzone", joystick_keymap.dead_zone);
+    }
 
-  lisp_free(root_obj);
+    customload(config_lisp);
+  } catch(std::exception& e) {
+    std::cerr << "Couldn't load configfile: " << e.what() << "\n";
+  }
 }
 
 void Config::save ()
@@ -147,7 +130,7 @@ void Config::save ()
 
   if(config)
     {
-      fprintf(config, "(supertux-config\n");
+      fprintf(config, ("("+package_symbol_name+"-config\n").c_str());
       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", SoundManager::get()->sound_enabled()      ? "#t" : "#f");
@@ -173,7 +156,7 @@ void Config::save ()
        customsave(config);
 
       fprintf(config, ")\n");
+      fclose(config);
     }
 }
 
-/* EOF */