// BotConfig.C -*- C++ -*- // Copyright (C) 2004 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 // the Free Software Foundation; either version 2 of the License, or // 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 "BotConfig.H" #include "Bot.H" #include "StringTokenizer.H" #include #include #include #include BotConfig::BotConfig (std::string cf) : config_filename (cf) { } bool BotConfig::read_config () { std::ifstream config_file (config_filename.c_str ()); std::string current_line; unsigned int line_number = 1; options_db.clear (); if (!config_file) { // FIXME: log lines (needs local bot object, probably a // redesign. UGH). // Bot::logLine ("Error: Config File Not Found `" + // config_filename + "'"); return false; } while (!config_file.eof ()) { std::getline (config_file, current_line); if (current_line.length () == 0 || current_line[0] == '#') { line_number++; continue; } StringTokenizer st (current_line); std::string command = st.nextToken('=').trim().toUpper(); StringTokenizer params (st.nextToken('=').trim()); options_db[command] = t_option_values (t_value_list (), t_watcher_list ()); while (params.hasMoreTokens (',')) { options_db[command].first.push_back (params.nextToken (',').trim()); } } return true; } BotConfig::t_option_values BotConfig::get_option_values (std::string key) { t_options_db::const_iterator cf_iter = options_db.find (key); if (cf_iter != options_db.end ()) return cf_iter->second; else return t_option_values (); // Empty vector } std::string BotConfig::set_config_file (std::string fname) { std::string old_config_filename = config_filename; config_filename = fname; options_db.clear (); read_config (); return old_config_filename; } namespace { struct push_back_ { BotConfig::t_value_list* lst; push_back_ (BotConfig::t_value_list* l) : lst (l) {} void operator()(BotConfig::t_value val) { lst->push_back (val); } }; } void BotConfig::set_option_value (std::string key, t_value_list values, bool append) { t_options_db::iterator cf_iter = options_db.find (key); if (cf_iter != options_db.end ()) { if (append) { // FIXME: Potentially slow (make this push_front in 3.0) std::for_each (values.begin (), values.end (), push_back_ (&cf_iter->second.first)); } else cf_iter->second.first = values; } }