[project @ 2005-01-12 05:36:40 by unknown_lamer]
[clinton/bobotpp.git] / source / BotConfig.C
index 87c3edd..82a8035 100644 (file)
@@ -1,5 +1,5 @@
 // BotConfig.C  -*- C++ -*-
-// Copyright (C) 2004 Clinton Ebadi
+// Copyright (C) 2004,2005 Clinton Ebadi
 
 // 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
@@ -18,6 +18,7 @@
 #include "BotConfig.H"
 #include "Bot.H"
 #include "StringTokenizer.H"
+#include "Utils.H"
 #include <iostream>
 #include <string>
 #include <list>
@@ -27,7 +28,8 @@ BotConfig::BotConfig (std::string cf)
   : config_filename (cf)
 { }
 
-bool BotConfig::read_config ()
+bool
+BotConfig::read_config ()
 {
   std::ifstream config_file (config_filename.c_str ());
   std::string current_line;
@@ -54,22 +56,22 @@ bool BotConfig::read_config ()
          continue;
        }
       
+      // This is broken because overwrites existing values
       StringTokenizer st (current_line);
-      std::string command = st.nextToken('=').trim().toUpper();
-      StringTokenizer params (st.nextToken('=').trim());
+      std::string command = Utils::to_upper (Utils::trim_str(st.next_token('=')));
+      StringTokenizer params (Utils::trim_str (st.next_token('=')));
 
       options_db[command] = t_option_values (t_value_list (), 
-                                           t_watcher_list ());
-      while (params.hasMoreTokens (','))
-       {
-         options_db[command].first.push_back (params.nextToken (',').trim());
-       }
+                                            t_watcher_list ());
+      options_db[command].first.push_back (params.rest ());
+    
     }
   return true;
 }
 
 
-BotConfig::t_option_values BotConfig::get_option_values (std::string key)
+BotConfig::t_option_values
+BotConfig::get_option_values (std::string key)
 {
   t_options_db::const_iterator cf_iter = 
     options_db.find (key);
@@ -80,7 +82,8 @@ BotConfig::t_option_values BotConfig::get_option_values (std::string key)
     return t_option_values (); // Empty vector
 }
 
-std::string BotConfig::set_config_file (std::string fname)
+std::string
+BotConfig::set_config_file (std::string fname)
 {
   std::string old_config_filename = config_filename;
   config_filename = fname;
@@ -122,7 +125,8 @@ namespace
   };
 }
 
-void BotConfig::set_option_value (std::string key, t_value_list values, 
+void
+BotConfig::set_option_value (std::string key, t_value_list values, 
                                  bool append)
 {
   t_options_db::iterator cf_iter = options_db.find (key);
@@ -138,9 +142,25 @@ void BotConfig::set_option_value (std::string key, t_value_list values,
       else
        cf_iter->second.first = values;
     }
+  else
+    options_db[key] = t_option_values (values, t_watcher_list ());
 
   // Call Watchers
   std::for_each (cf_iter->second.second.begin (),
                 cf_iter->second.second.end (),
                 run_fun_ (key, values, append));
 }
+
+bool
+BotConfig::add_watcher (std::string key, t_watcher new_watcher)
+{
+  t_options_db::iterator cf_iter = options_db.find (key); 
+
+  if (cf_iter != options_db.end ())
+    {
+      cf_iter->second.second.push_front (new_watcher);
+      return true;
+    }
+  else
+    return false;
+}