6b59e728 |
1 | // BotConfig.C -*- C++ -*- |
a6339323 |
2 | // Copyright (C) 2004,2005 Clinton Ebadi |
6b59e728 |
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" |
a6339323 |
21 | #include "Utils.H" |
6b59e728 |
22 | #include <iostream> |
23 | #include <string> |
24 | #include <list> |
25 | #include <algorithm> |
26 | |
27 | BotConfig::BotConfig (std::string cf) |
28 | : config_filename (cf) |
29 | { } |
30 | |
a6339323 |
31 | bool |
32 | BotConfig::read_config () |
6b59e728 |
33 | { |
34 | std::ifstream config_file (config_filename.c_str ()); |
35 | std::string current_line; |
36 | unsigned int line_number = 1; |
37 | |
38 | options_db.clear (); |
39 | |
40 | if (!config_file) |
41 | { |
42 | // FIXME: log lines (needs local bot object, probably a |
43 | // redesign. UGH). |
44 | |
45 | // Bot::logLine ("Error: Config File Not Found `" + |
46 | // config_filename + "'"); |
47 | return false; |
48 | } |
49 | |
50 | while (!config_file.eof ()) |
51 | { |
52 | std::getline (config_file, current_line); |
53 | if (current_line.length () == 0 || current_line[0] == '#') |
54 | { |
55 | line_number++; |
56 | continue; |
57 | } |
58 | |
a6339323 |
59 | // This is broken because overwrites existing values |
6b59e728 |
60 | StringTokenizer st (current_line); |
a6339323 |
61 | std::string command = Utils::to_upper (Utils::trim_str(st.next_token('='))); |
62 | StringTokenizer params (Utils::trim_str (st.next_token('='))); |
6b59e728 |
63 | |
64 | options_db[command] = t_option_values (t_value_list (), |
a6339323 |
65 | t_watcher_list ()); |
66 | options_db[command].first.push_back (params.rest ()); |
67 | |
6b59e728 |
68 | } |
69 | return true; |
70 | } |
71 | |
72 | |
a6339323 |
73 | BotConfig::t_option_values |
74 | BotConfig::get_option_values (std::string key) |
6b59e728 |
75 | { |
76 | t_options_db::const_iterator cf_iter = |
77 | options_db.find (key); |
78 | |
79 | if (cf_iter != options_db.end ()) |
80 | return cf_iter->second; |
81 | else |
82 | return t_option_values (); // Empty vector |
83 | } |
84 | |
a6339323 |
85 | std::string |
86 | BotConfig::set_config_file (std::string fname) |
6b59e728 |
87 | { |
88 | std::string old_config_filename = config_filename; |
89 | config_filename = fname; |
90 | options_db.clear (); |
91 | read_config (); |
92 | return old_config_filename; |
93 | } |
94 | |
95 | namespace |
96 | { |
97 | struct push_back_ |
98 | { |
99 | BotConfig::t_value_list* lst; |
100 | |
101 | push_back_ (BotConfig::t_value_list* l) |
102 | : lst (l) |
103 | {} |
104 | |
f59bce33 |
105 | void operator() (BotConfig::t_value val) |
6b59e728 |
106 | { |
107 | lst->push_back (val); |
108 | } |
109 | }; |
f59bce33 |
110 | |
111 | struct run_fun_ |
112 | { |
113 | std::string key; |
114 | BotConfig::t_value_list& vals; |
115 | bool appended; |
116 | |
117 | run_fun_ (std::string k, BotConfig::t_value_list& vl, bool a) |
118 | : key (k), vals (vl), appended (a) |
119 | {} |
120 | |
121 | void operator() (BotConfig::t_watcher w) |
122 | { |
123 | w (key, vals, appended); |
124 | } |
125 | }; |
6b59e728 |
126 | } |
127 | |
a6339323 |
128 | void |
129 | BotConfig::set_option_value (std::string key, t_value_list values, |
6b59e728 |
130 | bool append) |
131 | { |
132 | t_options_db::iterator cf_iter = options_db.find (key); |
f59bce33 |
133 | |
6b59e728 |
134 | if (cf_iter != options_db.end ()) |
135 | { |
136 | if (append) |
137 | { |
138 | // FIXME: Potentially slow (make this push_front in 3.0) |
139 | std::for_each (values.begin (), values.end (), |
140 | push_back_ (&cf_iter->second.first)); |
141 | } |
f59bce33 |
142 | else |
143 | cf_iter->second.first = values; |
6b59e728 |
144 | } |
a6339323 |
145 | else |
146 | options_db[key] = t_option_values (values, t_watcher_list ()); |
6b59e728 |
147 | |
f59bce33 |
148 | // Call Watchers |
149 | std::for_each (cf_iter->second.second.begin (), |
150 | cf_iter->second.second.end (), |
151 | run_fun_ (key, values, append)); |
152 | } |
a6339323 |
153 | |
154 | bool |
155 | BotConfig::add_watcher (std::string key, t_watcher new_watcher) |
156 | { |
157 | t_options_db::iterator cf_iter = options_db.find (key); |
158 | |
159 | if (cf_iter != options_db.end ()) |
160 | { |
161 | cf_iter->second.second.push_front (new_watcher); |
162 | return true; |
163 | } |
164 | else |
165 | return false; |
166 | } |