00001 #include "ECF_base.h"
00002
00003
00015 bool Registry::registerEntry(std::string key, voidP x, enum ECF::type T, std::string description, uint index)
00016 {
00017 ParamP a = (ParamP) new ECF::Param;
00018 a->value= x;
00019 a->T = T;
00020 a->description = description;
00021 a->bModified=false;
00022
00023 if(index > 0)
00024 key = uint2str(index) + key;
00025
00026 map_iter it = parameters_.find(key);
00027 if(it != parameters_.end())
00028 return false;
00029
00030 parameters_.insert (std::pair < std::string, ParamP >(key, a));
00031 return true;
00032 }
00033
00034
00039 bool Registry::isRegistered(std::string key)
00040 {
00041 return parameters_.find(key) != parameters_.end();
00042 }
00043
00044
00050 bool Registry::modifyEntry(std::string key, voidP x, uint index)
00051 {
00052 if(index > 0)
00053 key = uint2str(index) + key;
00054
00055 map_iter it = parameters_.find(key);
00056 if(it != parameters_.end()) {
00057 it->second->value=x;
00058 it->second->bModified=true;
00059 return true;
00060 }
00061 else
00062 return false;
00063 }
00064
00065
00071 voidP Registry::getEntry(std::string key, uint index)
00072 {
00073 if(index > 0)
00074 key = uint2str(index) + key;
00075
00076 map_iter iter = parameters_.find(key);
00077
00078 if (iter != parameters_.end())
00079 return iter->second->value;
00080
00081 voidP p;
00082 return p;
00083 }
00084
00085
00091 ECF::type Registry::getType(std::string key)
00092 {
00093 map_iter iter = parameters_.find(key);
00094 if (iter != parameters_.end())
00095 return iter->second->T;
00096
00097 return ECF::ERROR;
00098 }
00099
00100
00106 bool Registry::isModified(std::string key, uint index)
00107 {
00108 if(index > 0)
00109 key = uint2str(index) + key;
00110
00111 map_iter iter = parameters_.find(key);
00112
00113 if (iter != parameters_.end())
00114 return iter->second->bModified;
00115
00116 return false;
00117 }
00118
00119
00128 bool Registry::readEntries(const XMLNode& node, std::string prefix, uint index)
00129 {
00130 int num = node.nChildNode(NODE_ENTRY), iter = 0;
00131
00132 for (int i = 0; i < num; ++i) {
00133 XMLNode child = node.getChildNode(NODE_ENTRY, &iter);
00134 if(!child.getAttribute("key")) {
00135 std::cerr << "Error: no attribute 'key' in current entry! (node '" << node.getName() << "')" << std::endl;
00136 return false;
00137 }
00138
00139 std::string key = (prefix.length() > 0 ? prefix + "." : "") + child.getAttribute("key");
00140 if (child.getText() == NULL) {
00141 std::cerr << "Error: no text in Entry node! (key: " << key << ")" << std::endl;
00142 return false;
00143 }
00144 std::string value = child.getText();
00145
00146 if (index > 0)
00147 key = uint2str(index) + key;
00148
00149 if (!isRegistered(key)) {
00150 std::cerr << "Warning: key " << key << " not registered." << std::endl;
00151 continue;
00152 }
00153
00154 ECF::type t = getType(key);
00155 voidP new_value;
00156
00157 bool ok = true;
00158 switch (t) {
00159 case ECF::INT:
00160 {
00161 int *x = new int;
00162 *x = str2int(value);
00163 new_value = (voidP)x;
00164
00165 break;
00166 }
00167 case ECF::UINT:
00168 {
00169 uint *x = new uint;
00170 *x = str2uint(value);
00171 new_value = (voidP)x;
00172
00173 break;
00174 }
00175 case ECF::STRING:
00176 {
00177 std::string *s = new std::string(value);
00178 new_value = (voidP)s;
00179 break;
00180 }
00181 case ECF::FLOAT:
00182 {
00183 float *x = new float;
00184 *x = str2flt(value);
00185 new_value = (voidP)x;
00186
00187 break;
00188 }
00189 case ECF::DOUBLE:
00190 {
00191
00192 double *x = new double;
00193 *x = str2dbl(value);
00194 new_value = (voidP)x;
00195
00196 break;
00197 }
00198 case ECF::CHAR:
00199 {
00200 char *x = new char;
00201 *x = value[0];
00202 new_value = (voidP)x;
00203
00204 break;
00205 }
00206 default:
00207 ok = false;
00208 break;
00209 }
00210
00211 if (ok)
00212 modifyEntry(key, new_value);
00213 }
00214
00215 return true;
00216 }
00217
00218
00223 void Registry::write(XMLNode& xRegistry)
00224 {
00225 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
00226
00227 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
00228
00229 if(!it->second->bModified)
00230 continue;
00231
00232 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
00233 std::string key = it->first;
00234 voidP value = it->second->value;
00235 ECF::type T = it->second->T;
00236 std::stringstream ss;
00237
00238 switch (T) {
00239 case ECF::INT:
00240 ss << *((int *)value.get());
00241 break;
00242 case ECF::UINT:
00243 ss << *((uint *)value.get());
00244 break;
00245 case ECF::STRING:
00246 ss << *((std::string *)value.get());
00247 break;
00248 case ECF::FLOAT:
00249 ss << *((float *)value.get());
00250 break;
00251 case ECF::DOUBLE:
00252 ss << *((double *)value.get());
00253 break;
00254 case ECF::CHAR:
00255 ss << *((char *)value.get());
00256 break;
00257 default:
00258 break;
00259 }
00260 xParam.addAttribute("key", key.c_str());
00261 xParam.addText(ss.str().c_str());
00262 xRegistry.addChild(xParam);
00263 }
00264 }
00265
00266
00271 void Registry::dumpEntries(XMLNode& xRegistry)
00272 {
00273 xRegistry = XMLNode::createXMLTopNode(NODE_REGISTRY);
00274
00275 for (map_iter it = parameters_.begin(); it != parameters_.end(); ++it) {
00276
00277 XMLNode xParam = XMLNode::createXMLTopNode(NODE_ENTRY);
00278 std::string key = it->first;
00279 voidP value = it->second->value;
00280 ECF::type T = it->second->T;
00281 std::string desc = it->second->description;
00282
00283 std::stringstream ss;
00284
00285 switch (T) {
00286 case ECF::INT:
00287 ss << *((int *)value.get());
00288 break;
00289 case ECF::UINT:
00290 ss << *((uint *)value.get());
00291 break;
00292 case ECF::STRING:
00293 ss << *((std::string *)value.get());
00294 break;
00295 case ECF::FLOAT:
00296 ss << *((float *)value.get());
00297 break;
00298 case ECF::DOUBLE:
00299 ss << *((double *)value.get());
00300 break;
00301 case ECF::CHAR:
00302 ss << *((char *)value.get());
00303 break;
00304 default:
00305 break;
00306 }
00307 xParam.addAttribute("key", key.c_str());
00308 xParam.addText(ss.str().c_str());
00309 xParam.addAttribute("desc", desc.c_str());
00310 xRegistry.addChild(xParam);
00311
00312
00313
00314 }
00315 }
00316