LoConfig.C

Go to the documentation of this file.
00001 /**
00002    \file  Robots/LoBot/config/LoConfig.C
00003    \brief Robolocust/lobot configuration database.
00004 */
00005 
00006 // //////////////////////////////////////////////////////////////////// //
00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00008 // by the University of Southern California (USC) and the iLab at USC.  //
00009 // See http://iLab.usc.edu for information about this project.          //
00010 // //////////////////////////////////////////////////////////////////// //
00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00013 // in Visual Environments, and Applications'' by Christof Koch and      //
00014 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00015 // pending; application number 09/912,225 filed July 23, 2001; see      //
00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00017 // //////////////////////////////////////////////////////////////////// //
00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00019 //                                                                      //
00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00021 // redistribute it and/or modify it under the terms of the GNU General  //
00022 // Public License as published by the Free Software Foundation; either  //
00023 // version 2 of the License, or (at your option) any later version.     //
00024 //                                                                      //
00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00028 // PURPOSE.  See the GNU General Public License for more details.       //
00029 //                                                                      //
00030 // You should have received a copy of the GNU General Public License    //
00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00033 // Boston, MA 02111-1307 USA.                                           //
00034 // //////////////////////////////////////////////////////////////////// //
00035 //
00036 // Primary maintainer for this file: mviswana usc edu
00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/config/LoConfig.C $
00038 // $Id: LoConfig.C 13912 2010-09-10 07:24:52Z mviswana $
00039 //
00040 
00041 //------------------------------ HEADERS --------------------------------
00042 
00043 // lobot headers
00044 #include "Robots/LoBot/config/LoConfig.H"
00045 #include "Robots/LoBot/config/LoIniFileLexer.H"
00046 #include "Robots/LoBot/config/LoIniFileParser.H"
00047 #include "Robots/LoBot/misc/LoExcept.H"
00048 
00049 // INVT utilities
00050 #include "Util/log.H"
00051 
00052 // Standard C++ headers
00053 #include <algorithm>
00054 #include <utility>
00055 
00056 // Standard C headers
00057 #include <stdio.h>
00058 
00059 //----------------------------- NAMESPACE -------------------------------
00060 
00061 namespace lobot {
00062 
00063 //-------------------- INITIALIZATION AND CLEAN-UP ----------------------
00064 
00065 ConfigDB::ConfigDB(){}
00066 ConfigDB::~ConfigDB(){}
00067 
00068 void Configuration::load(const std::string& config_file)
00069 {
00070    FILE* file = fopen(config_file.c_str(), "r") ;
00071    if (! file)
00072       throw customization_error(NO_SUCH_CONFIG_FILE) ;
00073 
00074    yyin = file ;
00075    int parse_ok = yyparse() ;
00076    yyin = stdin ;
00077    fclose(file) ;
00078 
00079    switch (parse_ok) {
00080       case 0: // all okay
00081          break ;
00082       case 1: // syntax error
00083          throw customization_error(CONFIG_FILE_SYNTAX_ERROR) ;
00084       case 2: // memory error
00085          throw customization_error(CONFIG_FILE_MEMORY_ERROR) ;
00086    }
00087 }
00088 
00089 //----------------- CONFIGURATION DATABASE OPERATIONS -------------------
00090 
00091 // Adding configuration settings to the database
00092 void ConfigDB::insert(const std::string& section,
00093                       const std::string& key, const std::string& value)
00094 {
00095    KeyValuePairs& key_value_map = m_config_db[section] ;
00096    key_value_map[key] = value ;
00097 }
00098 
00099 void Configuration::set(const std::string& section,
00100                         const std::string& key, const std::string& value)
00101 {
00102    ConfigDB::instance().insert(section, key, value) ;
00103 }
00104 
00105 void Configuration::set_global(const std::string& key, const std::string& val)
00106 {
00107    set(LOCD_TOP_LEVEL, key, val) ;
00108 }
00109 
00110 void Configuration::set_internal(const std::string& key,
00111                                  const std::string& val)
00112 {
00113    set(LOCD_INTERNAL, key, val) ;
00114 }
00115 
00116 // Retrieving configuration settings from the database
00117 std::string ConfigDB::retrieve(const std::string& section,
00118                                const std::string& key,
00119                                const std::string& default_value) const
00120 {
00121    ConfigMap::const_iterator i = m_config_db.find(section) ;
00122    if (i == m_config_db.end())
00123       return default_value ;
00124 
00125    KeyValuePairs::const_iterator j = i->second.find(key) ;
00126    if (j == i->second.end())
00127       return default_value ;
00128    return j->second ;
00129 }
00130 
00131 //--------------------------- DEBUG SUPPORT -----------------------------
00132 
00133 namespace {
00134 
00135 typedef std::map<std::string, std::string> KV ;
00136 typedef std::map<std::string, KV> DB ;
00137 
00138 class dump_kvmap {
00139    const std::string& section ;
00140 public:
00141    dump_kvmap(const std::string& sec) : section(sec) {}
00142    void operator()(const KV::value_type& kv) const {
00143       LERROR("%-15s %-25s %s",
00144              section.c_str(), kv.first.c_str(), kv.second.c_str()) ;
00145    }
00146 } ;
00147 
00148 void dump_section(const DB::value_type& i)
00149 {
00150    std::for_each(i.second.begin(), i.second.end(), dump_kvmap(i.first)) ;
00151 }
00152 
00153 }
00154 
00155 void Configuration::dump()
00156 {
00157    ConfigDB& db = ConfigDB::instance() ;
00158    std::for_each(db.m_config_db.begin(), db.m_config_db.end(), dump_section) ;
00159 }
00160 
00161 //-----------------------------------------------------------------------
00162 
00163 } // end of namespace encapsulating this file's definitions
00164 
00165 /* So things look consistent in everyone's emacs... */
00166 /* Local Variables: */
00167 /* indent-tabs-mode: nil */
00168 /* End: */
Generated on Sun May 8 08:41:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3