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