LoConfig.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
00050 #include "Util/log.H"
00051
00052
00053 #include <algorithm>
00054 #include <utility>
00055
00056
00057 #include <stdio.h>
00058
00059
00060
00061 namespace lobot {
00062
00063
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:
00081 break ;
00082 case 1:
00083 throw customization_error(CONFIG_FILE_SYNTAX_ERROR) ;
00084 case 2:
00085 throw customization_error(CONFIG_FILE_MEMORY_ERROR) ;
00086 }
00087 }
00088
00089
00090
00091
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
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
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 }
00164
00165
00166
00167
00168