presage  0.8.7
configuration.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************
00003  *  Presage, an extensible predictive text entry system
00004  *  ---------------------------------------------------
00005  *
00006  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License along
00019     with this program; if not, write to the Free Software Foundation, Inc.,
00020     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021                                                                              *
00022                                                                 **********(*)*/
00023 
00024 
00025 #include "configuration.h"
00026 
00027 #include <iostream>
00028 
00029 Configuration::Configuration()
00030 {
00031     configuration = new std::map<std::string, Variable*>();
00032 }
00033 
00034 Configuration::~Configuration()
00035 {
00036     for (std::map<std::string, Variable*>::iterator it = configuration->begin();
00037          it != configuration->end();
00038          it++) {
00039         //std::cerr << "[Configuration] Deleting variable: " << it->first << '\t' << it->second << std::endl;
00040         delete it->second;
00041         //std::cerr << "[Configuration] Deleted  variable: " << it->first << std::endl;
00042     }
00043     delete configuration;
00044 }
00045     
00046 Variable* Configuration::find(const std::string& variable) const
00047 {
00048     std::map<std::string, Variable*>::const_iterator it = configuration->find (variable);
00049     if (it == configuration->end()) {
00050         // variable not found, create exception message
00051         std::string message = "[Configuration] Cannot find variable " + variable;
00052         
00053         // if we get here, variable was not found in the configuration,
00054         // hence we have a right to complain
00055         throw ConfigurationException(PRESAGE_CONFIG_VARIABLE_ERROR, message);
00056     }
00057 
00058     return it->second;
00059 }
00060 
00061 Variable* Configuration::operator[](const std::string& variable) const
00062 {
00063     return find(variable);
00064 }
00065 
00066 void Configuration::insert(const std::string& variable, 
00067                            const std::string& value)
00068 {
00069     std::map<std::string, Variable*>::const_iterator it = configuration->find (variable);
00070     if (it != configuration->end ()) {
00071         it->second->set_value (value);
00072         //std::cerr << "[Configuration] Modifying existing variable: " << variable << std::endl;
00073 
00074     } else {
00075         Variable* var = new Variable (variable);
00076         var->set_value (value);
00077         configuration->insert (std::pair<std::string, Variable*> (variable, var));
00078       
00079         //std::cerr << "[Configuration] Adding new variable: " << variable << '\t' << var << std::endl;
00080     }
00081 
00082     //std::cerr << "[Configuration] Inserted variable: " << variable << std::endl;
00083 }
00084 
00085 void Configuration::remove(const std::string& variable)
00086 {
00087     std::map<std::string, Variable*>::iterator it = configuration->find (variable);
00088     if (it != configuration->end()) {
00089         delete it->second;
00090         configuration->erase (it);
00091     }
00092 }
00093 
00094 void Configuration::print() const
00095 {
00096     // iterate map
00097     for (std::map<std::string, Variable*>::const_iterator map_it = configuration->begin ();
00098          map_it != configuration->end ();
00099          map_it++) {
00100 
00101         // variable
00102         std::cout << map_it->first;
00103 
00104         // value
00105         std::cout << " = " << map_it->second->get_value () << std::endl;
00106     }
00107 }
00108 
00109 std::map<std::string, Variable*>::const_iterator Configuration::begin () const
00110 {
00111     return configuration->begin();
00112 }
00113 
00114 std::map<std::string, Variable*>::const_iterator Configuration::end () const
00115 {
00116     return configuration->end();
00117 }