1 // BotConfig.C -*- C++ -*-
2 // Copyright (C) 2004 Clinton Ebadi
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 #include "BotConfig.H"
20 #include "StringTokenizer.H"
26 BotConfig::BotConfig (std::string cf)
27 : config_filename (cf)
30 bool BotConfig::read_config ()
32 std::ifstream config_file (config_filename.c_str ());
33 std::string current_line;
34 unsigned int line_number = 1;
40 // FIXME: log lines (needs local bot object, probably a
43 // Bot::logLine ("Error: Config File Not Found `" +
44 // config_filename + "'");
48 while (!config_file.eof ())
50 std::getline (config_file, current_line);
51 if (current_line.length () == 0 || current_line[0] == '#')
57 StringTokenizer st (current_line);
58 std::string command = st.nextToken('=').trim().toUpper();
59 StringTokenizer params (st.nextToken('=').trim());
61 options_db[command] = t_option_values (t_value_list (),
63 while (params.hasMoreTokens (','))
65 options_db[command].first.push_back (params.nextToken (',').trim());
72 BotConfig::t_option_values BotConfig::get_option_values (std::string key)
74 t_options_db::const_iterator cf_iter =
75 options_db.find (key);
77 if (cf_iter != options_db.end ())
78 return cf_iter->second;
80 return t_option_values (); // Empty vector
83 std::string BotConfig::set_config_file (std::string fname)
85 std::string old_config_filename = config_filename;
86 config_filename = fname;
89 return old_config_filename;
96 BotConfig::t_value_list* lst;
98 push_back_ (BotConfig::t_value_list* l)
102 void operator()(BotConfig::t_value val)
104 lst->push_back (val);
109 void BotConfig::set_option_value (std::string key, t_value_list values,
112 t_options_db::iterator cf_iter = options_db.find (key);
113 if (cf_iter != options_db.end ())
117 // FIXME: Potentially slow (make this push_front in 3.0)
118 std::for_each (values.begin (), values.end (),
119 push_back_ (&cf_iter->second.first));
122 cf_iter->second.first = values;