[project @ 2005-01-06 18:19:16 by unknown_lamer]
[clinton/bobotpp.git] / source / BotConfig.C
1 // BotConfig.C -*- C++ -*-
2 // Copyright (C) 2004 Clinton Ebadi
3
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
7 // any later version.
8
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.
13
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.
17
18 #include "BotConfig.H"
19 #include "Bot.H"
20 #include "StringTokenizer.H"
21 #include <iostream>
22 #include <string>
23 #include <list>
24 #include <algorithm>
25
26 BotConfig::BotConfig (std::string cf)
27 : config_filename (cf)
28 { }
29
30 bool BotConfig::read_config ()
31 {
32 std::ifstream config_file (config_filename.c_str ());
33 std::string current_line;
34 unsigned int line_number = 1;
35
36 options_db.clear ();
37
38 if (!config_file)
39 {
40 // FIXME: log lines (needs local bot object, probably a
41 // redesign. UGH).
42
43 // Bot::logLine ("Error: Config File Not Found `" +
44 // config_filename + "'");
45 return false;
46 }
47
48 while (!config_file.eof ())
49 {
50 std::getline (config_file, current_line);
51 if (current_line.length () == 0 || current_line[0] == '#')
52 {
53 line_number++;
54 continue;
55 }
56
57 StringTokenizer st (current_line);
58 std::string command = st.nextToken('=').trim().toUpper();
59 StringTokenizer params (st.nextToken('=').trim());
60
61 options_db[command] = t_option_values (t_value_list (),
62 t_watcher_list ());
63 while (params.hasMoreTokens (','))
64 {
65 options_db[command].first.push_back (params.nextToken (',').trim());
66 }
67 }
68 return true;
69 }
70
71
72 BotConfig::t_option_values BotConfig::get_option_values (std::string key)
73 {
74 t_options_db::const_iterator cf_iter =
75 options_db.find (key);
76
77 if (cf_iter != options_db.end ())
78 return cf_iter->second;
79 else
80 return t_option_values (); // Empty vector
81 }
82
83 std::string BotConfig::set_config_file (std::string fname)
84 {
85 std::string old_config_filename = config_filename;
86 config_filename = fname;
87 options_db.clear ();
88 read_config ();
89 return old_config_filename;
90 }
91
92 namespace
93 {
94 struct push_back_
95 {
96 BotConfig::t_value_list* lst;
97
98 push_back_ (BotConfig::t_value_list* l)
99 : lst (l)
100 {}
101
102 void operator()(BotConfig::t_value val)
103 {
104 lst->push_back (val);
105 }
106 };
107 }
108
109 void BotConfig::set_option_value (std::string key, t_value_list values,
110 bool append)
111 {
112 t_options_db::iterator cf_iter = options_db.find (key);
113 if (cf_iter != options_db.end ())
114 {
115 if (append)
116 {
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));
120 }
121 else
122 cf_iter->second.first = values;
123 }
124 }
125
126