config_watcher.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : config_watcher                                                    *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2008-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "config_watcher.h"
00016 
00017 #include <basis/function.h>
00018 #include <basis/set.cpp>
00019 #include <data_struct/string_table.h>
00020 #include <data_struct/table_configurator.h>
00021 
00022 config_watcher::config_watcher(configurator &to_watch)
00023 : _watching(to_watch),
00024   _current_config(new table_configurator),
00025   _previous_config(new table_configurator)
00026 {
00027   rescan();  // fill out our lists.
00028 }
00029 
00030 config_watcher::~config_watcher()
00031 {
00032   WHACK(_current_config);
00033   WHACK(_previous_config);
00034 }
00035 
00036 bool config_watcher::rescan()
00037 {
00038   // copy the current configuration into our previous config tracker.
00039   *_previous_config = *_current_config;
00040   // clean out any current items held.
00041   _current_config->reset();
00042 
00043   // iterate across the sections in the watched config.
00044   string_set sects;
00045   _watching.section_set(sects);
00046   for (int sectindy = 0; sectindy < sects.length(); sectindy++) {
00047     // every entry in the current section gets added to our current config.
00048     istring curr_section = sects[sectindy];
00049     string_table entries;
00050   _watching.get_section(curr_section, entries);
00051     _current_config->put_section(curr_section, entries);
00052   }
00053 
00054   return true;
00055 }
00056 
00057 string_set config_watcher::new_sections() const
00058 {
00059   string_set before;
00060   _previous_config->section_set(before);
00061   string_set after;
00062   _current_config->section_set(before);
00063   return after - before;
00064 }
00065 
00066 string_set config_watcher::deleted_sections() const
00067 {
00068   string_set before;
00069   _previous_config->section_set(before);
00070   string_set after;
00071   _current_config->section_set(before);
00072   return before - after;
00073 }
00074 
00075 string_set config_watcher::changed_sections() const
00076 {
00077   string_set before;
00078   _previous_config->section_set(before);
00079   string_set after;
00080   _current_config->section_set(before);
00081   string_set possible_changes = before.intersection(after);
00082   string_set definite_changes;
00083   for (int i = 0; i < possible_changes.elements(); i++) {
00084     const istring &sect_name = possible_changes[i];
00085     string_table previous_section;
00086     _previous_config->get_section(sect_name, previous_section);
00087     string_table current_section;
00088     _current_config->get_section(sect_name, current_section);
00089     if (current_section != previous_section)
00090       definite_changes += sect_name;
00091   }
00092   return definite_changes;
00093 }
00094 
00095 string_set config_watcher::deleted_items(const istring &section_name)
00096 {
00097   string_table previous_section;
00098   _previous_config->get_section(section_name, previous_section);
00099   string_set previous_names;
00100   previous_section.names(previous_names);
00101   string_table current_section;
00102   _current_config->get_section(section_name, current_section);
00103   string_set current_names;
00104   current_section.names(current_names);
00105   return previous_names - current_names;
00106 }
00107 
00108 string_set config_watcher::new_items(const istring &section_name)
00109 {
00110   string_table previous_section;
00111   _previous_config->get_section(section_name, previous_section);
00112   string_set previous_names;
00113   previous_section.names(previous_names);
00114   string_table current_section;
00115   _current_config->get_section(section_name, current_section);
00116   string_set current_names;
00117   current_section.names(current_names);
00118   return current_names - previous_names;
00119 }
00120 
00121 string_set config_watcher::changed_items(const istring &section_name)
00122 {
00123   string_table previous_section;
00124   _previous_config->get_section(section_name, previous_section);
00125   string_set previous_names;
00126   previous_section.names(previous_names);
00127   string_table current_section;
00128   _current_config->get_section(section_name, current_section);
00129   string_set current_names;
00130   current_section.names(current_names);
00131 
00132   string_set possible_changes = current_names.intersection(previous_names);
00133   string_set definite_changes;
00134   for (int i = 0; i < possible_changes.elements(); i++) {
00135     const istring &curr_item = possible_changes[i];
00136     istring prev_value;
00137     _previous_config->get(section_name, curr_item, prev_value);
00138     istring curr_value;
00139     _current_config->get(section_name, curr_item, curr_value);
00140     if (prev_value != curr_value)
00141       definite_changes += curr_item;
00142   }
00143   return definite_changes;
00144 }
00145 

Generated on Tue Aug 19 04:29:35 2008 for HOOPLE Libraries by  doxygen 1.5.1