Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Environ.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Environ.h"
00010 
00011 #include "Log.h"
00012 #include "Path.h"
00013 #include "BaseMsg.h"
00014 #include "NameException.h"
00015 #include "StringTool.h"
00016 
00017 #include <stdio.h>
00018 
00019 //-----------------------------------------------------------------
00020 /**
00021  * Free all remain messages for watchers.
00022  */
00023 Environ::~Environ()
00024 {
00025     t_watchers::iterator end = m_watchers.end();
00026     for (t_watchers::iterator i = m_watchers.begin(); i != end; ++i) {
00027         delete i->second;
00028     }
00029 }
00030 //-----------------------------------------------------------------
00031 /**
00032  * Save params.
00033  * @param file where to store params, this file will be overwritten
00034  */
00035     void
00036 Environ::store(const Path &file)
00037 {
00038     FILE *config = fopen(file.getNative().c_str(), "w");
00039     if (config) {
00040         fputs("-- this file is automatically generated\n", config);
00041 
00042         t_values::iterator end = m_values.end();
00043         for (t_values::iterator i = m_values.begin(); i != end; ++i) {
00044             fprintf(config, "setParam(\"%s\", \"%s\")\n",
00045                     i->first.c_str(), i->second.c_str());
00046         }
00047 
00048         fclose(config);
00049     }
00050     else {
00051         LOG_WARNING(ExInfo("cannot save config")
00052                 .addInfo("file", file.getNative()));
00053     }
00054 }
00055 //-----------------------------------------------------------------
00056 /**
00057  * Set param.
00058  * Notice watchers.
00059  * When watcher is not available, it will be removed.
00060  *
00061  * @param name param name
00062  * @param value param value
00063  */
00064     void
00065 Environ::setParam(const std::string &name, const std::string &value)
00066 {
00067     if (m_values[name] != value) {
00068         m_values[name] = value;
00069         LOG_DEBUG(ExInfo("setParam")
00070                 .addInfo("param", name)
00071                 .addInfo("value", value));
00072 
00073         t_watchers::iterator it = m_watchers.find(name);
00074         if (m_watchers.end() != it) {
00075             t_watchers::size_type count = m_watchers.count(name);
00076             for (t_watchers::size_type i = 0; i < count; ++i) {
00077                 t_watchers::iterator cur_it = it++;
00078                 try {
00079                     cur_it->second->sendClone();
00080                 }
00081                 catch (NameException &e) {
00082                     LOG_WARNING(e.info());
00083                     delete cur_it->second;
00084                     m_watchers.erase(cur_it);
00085                 }
00086             }
00087         }
00088     }
00089 }
00090 //-----------------------------------------------------------------
00091 /**
00092  * Store this integer value like string param.
00093  * @param name param name
00094  * @param value param value
00095  */
00096     void
00097 Environ::setParam(const std::string &name, long value)
00098 {
00099     setParam(name, StringTool::toString(value));
00100 }
00101 //-----------------------------------------------------------------
00102 /**
00103  * Return value.
00104  * Implicit value is "".
00105  *
00106  * @param name param name
00107  * @param implicit default value = ""
00108  * @return value or implicit value
00109  */
00110     std::string
00111 Environ::getParam(const std::string &name,
00112                 const std::string &implicit) const
00113 {
00114     std::string result = implicit;
00115 
00116     t_values::const_iterator it = m_values.find(name);
00117     if (m_values.end() != it) {
00118         result = it->second;
00119     }
00120     return result;
00121 }
00122 //-----------------------------------------------------------------
00123 /**
00124  * Returns number value.
00125  * Implicit value is zero.
00126  *
00127  * @param name param name
00128  * @param implicit default value = 0
00129  * @return number or implicit
00130  */
00131     int
00132 Environ::getAsInt(const std::string &name,
00133                 int implicit) const
00134 {
00135     std::string value = getParam(name);
00136     bool ok;
00137     int result = StringTool::readInt(value.c_str(), &ok);
00138     if (!ok) {
00139         if (value != "") {
00140             LOG_WARNING(ExInfo("cannot recognize numeric value")
00141                     .addInfo("property", name)
00142                     .addInfo("value", value)
00143                     .addInfo("default", implicit));
00144         }
00145         result = implicit;
00146     }
00147     return result;
00148 }
00149 //-----------------------------------------------------------------
00150 /**
00151  * Returns boolean value.
00152  * Recognizes 1/0, true/false, on/off, yes/no.
00153  *
00154  * @param name param name
00155  * @param implicit default value = false
00156  * @return stored value or implicit value when value is not recognized
00157  */
00158     bool
00159 Environ::getAsBool(const std::string &name,
00160                 bool implicit) const
00161 {
00162     bool result = false;
00163     std::string value = getParam(name);
00164     if (value == "1" || value == "true" || value == "on" || value == "yes") {
00165         result = true;
00166     }
00167     else if (value == "0" || value == "false" || value == "off" ||
00168             value == "no")
00169     {
00170         result = false;
00171     }
00172     else {
00173         if (value != "") {
00174             //TODO: don't print this every time
00175             LOG_WARNING(ExInfo("cannot recognize boolean value")
00176                     .addInfo("property", name)
00177                     .addInfo("value", value)
00178                     .addInfo("default", implicit)
00179                     .addInfo("hint", "use 1/0, true/false, on/off, yes/no"));
00180         }
00181         result = implicit;
00182     }
00183     return result;
00184 }
00185 //-----------------------------------------------------------------
00186 /**
00187  * Multiple watcher can watch param change.
00188  * @param name param name
00189  * @param msg message to raise
00190  */
00191     void
00192 Environ::addWatcher(const std::string &name, BaseMsg *msg)
00193 {
00194     m_watchers.insert(std::pair<std::string,BaseMsg*>(name, msg));
00195     LOG_DEBUG(ExInfo("add watcher")
00196             .addInfo("param", name)
00197             .addInfo("msg", msg->toString()));
00198 }
00199 //-----------------------------------------------------------------
00200 /**
00201  * Removes all registered watchers for given listener.
00202  */
00203 void
00204 Environ::removeWatchers(const std::string &listenerName)
00205 {
00206     t_watchers::iterator end = m_watchers.end();
00207     for (t_watchers::iterator i = m_watchers.begin(); i != end; /*empty*/) {
00208         t_watchers::iterator cur = i++;
00209         if (cur->second->getListenerName() == listenerName) {
00210             delete cur->second;
00211             m_watchers.erase(cur);
00212         }
00213     }
00214 }
00215 //-----------------------------------------------------------------
00216 std::string
00217 Environ::toString() const
00218 {
00219     ExInfo info("environ");
00220     t_values::const_iterator end = m_values.end();
00221     for (t_values::const_iterator i = m_values.begin(); i != end; ++i) {
00222         info.addInfo(i->first, i->second);
00223     }
00224     return info.info();
00225 }
00226 //-----------------------------------------------------------------
00227 std::string
00228 Environ::getHelpInfo() const
00229 {
00230     std::string help;
00231     t_values::const_iterator end = m_values.end();
00232     for (t_values::const_iterator i = m_values.begin(); i != end; ++i) {
00233         help += i->first + "='" + i->second + "'\n";
00234     }
00235     return help;
00236 }
00237 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2