6b59e728 |
1 | // BotConfig.H -*- 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 |
4da877a5 |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | // 02110-1301, USA. |
6b59e728 |
18 | |
19 | /* |
20 | Config Database: |
21 | map<key,pair<list<string>,list<watcher>> |
22 | void watcher (key, options, appended?) |
23 | |
a6339323 |
24 | NOTE: The key is always UPPERCASE internally. Keys are converted |
4da877a5 |
25 | automagically to uppercase. |
a6339323 |
26 | |
6b59e728 |
27 | */ |
28 | |
29 | #include <map> |
30 | #include <list> |
31 | #include <string> |
32 | |
33 | class BotConfig |
34 | { |
35 | public: |
36 | typedef std::string t_value; |
37 | typedef std::list<t_value> t_value_list; |
38 | |
4da877a5 |
39 | typedef void (*t_watcher) |
40 | (std::string key, t_value_list vals, bool appended); |
6b59e728 |
41 | |
42 | typedef std::list<t_watcher> t_watcher_list; |
43 | typedef std::pair<t_value_list, t_watcher_list> t_option_values; |
44 | |
45 | typedef std::map<std::string, t_option_values> t_options_db; |
46 | |
47 | |
48 | private: |
49 | t_options_db options_db; |
50 | std::string config_filename; |
51 | |
52 | public: |
4da877a5 |
53 | BotConfig (std::string); // sets config_filename but DOES NOT read |
54 | // config! |
6b59e728 |
55 | |
4da877a5 |
56 | bool read_config (); // true if read successfully. This also clears |
57 | // the option_db. |
6b59e728 |
58 | bool write_config (); // true if written succesfully |
59 | |
60 | // Getters |
61 | t_option_values get_option_values (std::string key); |
62 | |
63 | |
64 | |
65 | // Setters |
66 | void set_option_value (std::string key, t_value_list values, |
67 | bool append); |
f0dad759 |
68 | // Convinience proc |
69 | void set_option_value (std::string key, t_value value, bool append); |
6b59e728 |
70 | |
71 | std::string set_config_file (std::string new_filename); // returns |
72 | // old |
73 | // filename |
74 | |
75 | bool add_watcher (std::string key, t_watcher new_watcher); // t if key |
4da877a5 |
76 | // exists, |
77 | // f |
78 | // otherwise |
6b59e728 |
79 | bool clear_watchers (std::string key); // t if key exists, f |
80 | // otherwise |
81 | |
82 | // Static Procedures (these are for convinience) |
83 | t_value_list get_option_value_list (t_option_values v) |
84 | { return v.first; } |
85 | |
86 | t_watcher_list get_option_watcher_list (t_option_values v) |
87 | { return v.second; } |
88 | }; |