1 /*****************************************************************************\
3 * Name : config_watcher *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2008-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "config_watcher.h"
17 #include <basis/functions.h>
18 #include <configuration/table_configurator.h>
19 #include <structures/set.h>
20 #include <structures/string_table.h>
22 using namespace basis;
23 using namespace structures;
25 namespace configuration {
27 config_watcher::config_watcher(configurator &to_watch)
28 : _watching(to_watch),
29 _current_config(new table_configurator),
30 _previous_config(new table_configurator)
32 rescan(); // fill out our lists.
35 config_watcher::~config_watcher()
37 WHACK(_current_config);
38 WHACK(_previous_config);
41 bool config_watcher::rescan()
43 // copy the current configuration into our previous config tracker.
44 *_previous_config = *_current_config;
45 // clean out any current items held.
46 _current_config->reset();
48 // iterate across the sections in the watched config.
50 _watching.section_set(sects);
51 for (int sectindy = 0; sectindy < sects.length(); sectindy++) {
52 // every entry in the current section gets added to our current config.
53 astring curr_section = sects[sectindy];
55 _watching.get_section(curr_section, entries);
56 _current_config->put_section(curr_section, entries);
62 string_set config_watcher::new_sections() const
65 _previous_config->section_set(before);
67 _current_config->section_set(before);
68 return after - before;
71 string_set config_watcher::deleted_sections() const
74 _previous_config->section_set(before);
76 _current_config->section_set(before);
77 return before - after;
80 string_set config_watcher::changed_sections() const
83 _previous_config->section_set(before);
85 _current_config->section_set(before);
86 string_set possible_changes = before.intersection(after);
87 string_set definite_changes;
88 for (int i = 0; i < possible_changes.elements(); i++) {
89 const astring §_name = possible_changes[i];
90 string_table previous_section;
91 _previous_config->get_section(sect_name, previous_section);
92 string_table current_section;
93 _current_config->get_section(sect_name, current_section);
94 if (current_section != previous_section)
95 definite_changes += sect_name;
97 return definite_changes;
100 string_set config_watcher::deleted_items(const astring §ion_name)
102 string_table previous_section;
103 _previous_config->get_section(section_name, previous_section);
104 string_set previous_names;
105 previous_section.names(previous_names);
106 string_table current_section;
107 _current_config->get_section(section_name, current_section);
108 string_set current_names;
109 current_section.names(current_names);
110 return previous_names - current_names;
113 string_set config_watcher::new_items(const astring §ion_name)
115 string_table previous_section;
116 _previous_config->get_section(section_name, previous_section);
117 string_set previous_names;
118 previous_section.names(previous_names);
119 string_table current_section;
120 _current_config->get_section(section_name, current_section);
121 string_set current_names;
122 current_section.names(current_names);
123 return current_names - previous_names;
126 string_set config_watcher::changed_items(const astring §ion_name)
128 string_table previous_section;
129 _previous_config->get_section(section_name, previous_section);
130 string_set previous_names;
131 previous_section.names(previous_names);
132 string_table current_section;
133 _current_config->get_section(section_name, current_section);
134 string_set current_names;
135 current_section.names(current_names);
137 string_set possible_changes = current_names.intersection(previous_names);
138 string_set definite_changes;
139 for (int i = 0; i < possible_changes.elements(); i++) {
140 const astring &curr_item = possible_changes[i];
142 _previous_config->get(section_name, curr_item, prev_value);
144 _current_config->get(section_name, curr_item, curr_value);
145 if (prev_value != curr_value)
146 definite_changes += curr_item;
148 return definite_changes;