00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Utils/SystemProperties.h"
00010
00011 #include "ace/Configuration_Import_Export.h"
00012
00013 #include <string.h>
00014
00015 #include <sstream>
00016 #include <algorithm>
00017
00018
00019 SystemProperties * s_pInstance = NULL;
00020
00021
00022 SystemProperties *SystemProperties::Instance(const char *configFilename,
00023 bool registryFormat)
00024 {
00025 if (s_pInstance == NULL)
00026 {
00027 s_pInstance = new SystemProperties();
00028
00029 if (registryFormat)
00030 {
00031 s_pInstance->ImportPropertiesRegistry(configFilename);
00032 }
00033 else
00034 {
00035 s_pInstance->ImportPropertiesIni(configFilename);
00036 }
00037 }
00038
00039 return s_pInstance;
00040 }
00041
00042
00043
00044 SystemProperties::SystemProperties()
00045 {
00046 this->open();
00047 }
00048
00049
00050
00051 int SystemProperties::GetStringEntry(const char *sectionName, const char *name,
00052 std::string &value)
00053 {
00054 SectionKey_t current;
00055 int retVal = GetSectionKey(sectionName, current);
00056
00057 if (retVal)
00058 ACE_ERROR_RETURN((LM_ERROR,
00059 "(%N:%l) Failed to find system property section: %s\n",
00060 sectionName), retVal);
00061
00062 return GetStringEntry(current, name, value);
00063 }
00064
00065
00066 int SystemProperties::GetIntegerEntry(const char *sectionName,
00067 const char *name, unsigned int &value)
00068 {
00069 SectionKey_t current;
00070 int retVal = GetSectionKey(sectionName, current);
00071
00072 if (retVal)
00073 ACE_ERROR_RETURN((LM_ERROR,
00074 "(%N:%l) Failed to find system property section: %s\n",
00075 sectionName), retVal);
00076
00077 return GetIntegerEntry(current, name, value);
00078 }
00079
00080
00081 int SystemProperties::GetDoubleEntry(const char *sectionName,
00082 const char *name, double &value)
00083 {
00084 SectionKey_t current;
00085 int retVal = GetSectionKey(sectionName, current);
00086
00087 if (retVal)
00088 ACE_ERROR_RETURN((LM_ERROR,
00089 "(%N:%l) Failed to find system property section: %s\n",
00090 sectionName), retVal);
00091
00092 return GetDoubleEntry(current, name, value);
00093 }
00094
00095
00096 int SystemProperties::GetBinaryEntry(const char *sectionName, const char *name,
00097 void * &value, size_t &length)
00098 {
00099 SectionKey_t current;
00100 int retVal = GetSectionKey(sectionName, current);
00101
00102 if (retVal)
00103 ACE_ERROR_RETURN((LM_ERROR,
00104 "(%N:%l) Failed to find system property section: %s\n",
00105 sectionName), retVal);
00106
00107 return GetBinaryEntry(current, name, value, length);
00108
00109 }
00110
00111
00112
00113 int SystemProperties::GetBooleanEntry(const char *sectionName,
00114 const char *name, bool &value)
00115 {
00116 SectionKey_t current;
00117 int retVal = GetSectionKey(sectionName, current);
00118
00119 if (retVal)
00120 ACE_ERROR_RETURN((LM_ERROR,
00121 "(%N:%l) Failed to find system property section: %s\n",
00122 sectionName), retVal);
00123
00124 return GetBooleanEntry(current, name, value);
00125 }
00126
00127
00128
00129
00130 int SystemProperties::GetStringEntry(const SectionKey_t &parent,
00131 const char *name, std::string &value)
00132 {
00133 ACE_TString tstring;
00134
00135 int retVal = this->get_string_value(parent, name, tstring);
00136
00137 if (retVal)
00138 ACE_ERROR_RETURN((LM_ERROR,
00139 "(%N:%l) Failed to get system property: %s\n", name), retVal);
00140
00141 value = tstring.c_str();
00142 return 0;
00143 }
00144
00145
00146 int SystemProperties::GetIntegerEntry(const SectionKey_t &parent,
00147 const char *name, unsigned int &value)
00148 {
00149 int retVal = this->get_integer_value(parent, name, value);
00150
00151
00152
00153
00154
00155 if (retVal)
00156 {
00157 ACE_TString tstring;
00158 retVal = this->get_string_value(parent, name, tstring);
00159
00160 if (retVal)
00161 ACE_ERROR_RETURN((LM_ERROR,
00162 "(%N:%l) Failed to get system property: %s\n", name), retVal);
00163
00164 const char * buf=tstring.c_str();
00165 char * endptr;
00166 value = strtol(buf, &endptr, 0);
00167 if ((buf + tstring.length()) != endptr)
00168 {
00169 ACE_ERROR_RETURN((LM_ERROR,
00170 "(%N:%l) System property '%s' did not convert to an integer correctly.\n",
00171 name), retVal);
00172 }
00173 }
00174
00175 if (retVal)
00176 ACE_ERROR_RETURN((LM_ERROR,
00177 "(%N:%l) Failed to get system property: %s\n", name), retVal);
00178
00179 return 0;
00180 }
00181
00182
00183 int SystemProperties::GetDoubleEntry(const SectionKey_t &parent,
00184 const char *name, double &value)
00185 {
00186 ACE_TString tstring;
00187 int retVal = this->get_string_value(parent, name, tstring);
00188
00189 if (retVal)
00190 ACE_ERROR_RETURN((LM_ERROR,
00191 "(%N:%l) Failed to get system property: %s\n", name), retVal);
00192
00193 const char * buf=tstring.c_str();
00194 char * endptr;
00195 value = strtod(buf, &endptr);
00196 if ((buf + tstring.length()) != endptr)
00197 {
00198 ACE_ERROR_RETURN((LM_ERROR,
00199 "(%N:%l) System property '%s' did not convert to a double correctly.\n",
00200 name), retVal);
00201 }
00202
00203 return 0;
00204 }
00205
00206
00207 int SystemProperties::GetBinaryEntry(const SectionKey_t &parent,
00208 const char *name, void * &value,
00209 size_t &length)
00210 {
00211
00212
00213 int retVal = this->get_binary_value(parent, name, value, length);
00214
00215
00216
00217
00218
00219 if (retVal == -1)
00220 {
00221 ACE_TString tstring;
00222 retVal = this->get_string_value(parent, name, tstring);
00223
00224 if (retVal == 0)
00225 {
00226 unsigned int slen = tstring.length();
00227
00228
00229
00230
00231 if (slen == 0)
00232 {
00233 value = NULL;
00234 length = 0;
00235 }
00236
00237
00238
00239
00240
00241 else
00242 {
00243
00244
00245 std::vector<u_char> data;
00246
00247
00248
00249 const char *idx = tstring.c_str();
00250
00251
00252
00253 char *end;
00254
00255
00256
00257 idx += strspn(idx, " \t\r\n");
00258
00259
00260
00261
00262 while(*idx != '\0')
00263 {
00264
00265
00266
00267 data.push_back( char(strtoul(idx, &end, 16)) );
00268
00269
00270
00271
00272 if (end != (idx+2))
00273 {
00274 ACE_ERROR_RETURN((LM_ERROR,
00275 "(%N:%l) System property '%s' is not a valid"
00276 " string of comma-separated hex bytes.\n",
00277 name), -1);
00278 }
00279
00280
00281
00282 idx = end + strspn(end, " \t\r\n");
00283
00284
00285
00286 if (*idx == '\0')
00287 break;
00288
00289
00290
00291
00292 if (*idx != ',')
00293 {
00294 ACE_ERROR_RETURN((LM_ERROR,
00295 "(%N:%l) System property '%s' is not a valid"
00296 " string of comma-separated hex bytes.\n",
00297 name), -1);
00298 }
00299
00300
00301
00302 ++idx;
00303
00304
00305
00306 idx += strspn(idx, " \t\r\n");
00307 }
00308
00309
00310
00311 length = data.size();
00312
00313
00314
00315 char *v = new char[length];
00316 value = v;
00317
00318
00319
00320 std::copy(data.begin(), data.end(), v);
00321 }
00322 }
00323 }
00324
00325 if (retVal)
00326 ACE_ERROR_RETURN((LM_ERROR,
00327 "(%N:%l) Failed to get system property: %s\n", name), retVal);
00328
00329 return 0;
00330 }
00331
00332
00333
00334 int SystemProperties::GetBooleanEntry(const SectionKey_t &parent,
00335 const char *name, bool &value)
00336 {
00337 Valuetype_t vt;
00338
00339
00340
00341 int retVal = this->find_value(parent, name, vt);
00342 if (retVal == -1)
00343 {
00344 ACE_ERROR_RETURN((LM_ERROR,
00345 "(%N:%l) Failed to find system property: %s\n", name), -1);
00346 }
00347
00348
00349
00350 switch (vt)
00351 {
00352 case SystemProperties::INTEGER:
00353 {
00354
00355
00356 unsigned int numeric;
00357 int retVal = this->get_integer_value(parent, name, numeric);
00358 if (retVal == 0)
00359 {
00360 value = (numeric != 0);
00361 return 0;
00362 }
00363 }
00364 break;
00365
00366 case SystemProperties::STRING:
00367 {
00368
00369
00370 ACE_TString tstring;
00371 retVal = this->get_string_value(parent, name, tstring);
00372 if (retVal == 0)
00373 {
00374 const char *val = tstring.c_str();
00375
00376 char *end;
00377
00378
00379
00380
00381 value = (strtol(val, &end, 0) != 0);
00382 if (*end == '\0')
00383 return 0;
00384
00385
00386
00387 value = (ACE_OS::strncasecmp(tstring.c_str(), "FALSE", 5) != 0)
00388 && (ACE_OS::strncasecmp(tstring.c_str(), "NO", 5) != 0);
00389 return 0;
00390 }
00391 }
00392
00393 case SystemProperties::BINARY:
00394 {
00395 void *data = NULL;
00396 size_t len = 0;
00397
00398
00399
00400 retVal = this->get_binary_value(parent, name, data, len);
00401 if (retVal == 0)
00402 {
00403
00404
00405 char *it = static_cast<char *>(data) + len;
00406 value = false;
00407
00408
00409
00410
00411 while (value == false && it != data)
00412 {
00413 value = (*(--it) != 0);
00414 }
00415
00416
00417
00418 delete [] static_cast<char *>(data);
00419 return 0;
00420 }
00421 }
00422 break;
00423
00424 case SystemProperties::INVALID:
00425 default:
00426 break;
00427 }
00428
00429
00430
00431 ACE_ERROR_RETURN((LM_ERROR,
00432 "(%N:%l) Unable to interpret field as a boolean: %s\n", name),
00433 -1);
00434 }
00435
00436
00437
00438
00439
00440 int SystemProperties::SetStringEntry(const char *sectionName, const char *name,
00441 const char *value)
00442 {
00443 SectionKey_t current;
00444 int retVal = GetSectionKey(sectionName, current, true);
00445
00446 if (retVal)
00447 ACE_ERROR_RETURN((LM_ERROR,
00448 "(%N:%l) Failed to find system property section: %s\n",
00449 sectionName), retVal);
00450
00451 retVal = this->set_string_value(current, name, value);
00452
00453 if (retVal)
00454 ACE_ERROR_RETURN((LM_ERROR,
00455 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00456
00457 return 0;
00458 }
00459
00460
00461
00462 int SystemProperties::SetIntegerEntry(const char *sectionName,
00463 const char *name, unsigned int value)
00464 {
00465 SectionKey_t current;
00466 int retVal = GetSectionKey(sectionName, current, true);
00467
00468 if (retVal)
00469 ACE_ERROR_RETURN((LM_ERROR,
00470 "(%N:%l) Failed to find system property section: %s\n",
00471 sectionName), retVal);
00472
00473 retVal = this->set_integer_value(current, name, value);
00474
00475 if (retVal)
00476 ACE_ERROR_RETURN((LM_ERROR,
00477 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00478
00479 return 0;
00480 }
00481
00482
00483
00484 int SystemProperties::SetDoubleEntry(const char *sectionName, const char *name,
00485 double value)
00486 {
00487 SectionKey_t current;
00488 int retVal = GetSectionKey(sectionName, current, true);
00489
00490 if (retVal)
00491 ACE_ERROR_RETURN((LM_ERROR,
00492 "(%N:%l) Failed to find system property section: %s\n",
00493 sectionName), retVal);
00494
00495 std::ostringstream oss;
00496 oss << value;
00497
00498 retVal = this->set_string_value(current, name, oss.str().c_str());
00499
00500 if (retVal)
00501 ACE_ERROR_RETURN((LM_ERROR,
00502 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00503
00504 return 0;
00505 }
00506
00507
00508
00509 int SystemProperties::SetBinaryEntry(const char *sectionName,
00510 const char *name, const void * value,
00511 size_t length)
00512 {
00513 SectionKey_t current;
00514 int retVal = GetSectionKey(sectionName, current, true);
00515
00516 if (retVal)
00517 ACE_ERROR_RETURN((LM_ERROR,
00518 "(%N:%l) Failed to find system property section: %s\n",
00519 sectionName), retVal);
00520
00521 retVal = this->set_binary_value(current, name, value, length);
00522
00523 if (retVal)
00524 ACE_ERROR_RETURN((LM_ERROR,
00525 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00526
00527 return 0;
00528 }
00529
00530
00531
00532 int SystemProperties::SetBooleanEntry(const char *sectionName,
00533 const char *name, bool value)
00534 {
00535 SectionKey_t current;
00536 int retVal = GetSectionKey(sectionName, current, true);
00537
00538 if (retVal)
00539 ACE_ERROR_RETURN((LM_ERROR,
00540 "(%N:%l) Failed to find system property section: %s\n",
00541 sectionName), retVal);
00542
00543 retVal = this->set_integer_value(current, name, value);
00544
00545 if (retVal)
00546 ACE_ERROR_RETURN((LM_ERROR,
00547 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00548
00549 return 0;
00550 }
00551
00552
00553
00554 int SystemProperties::SetStringEntry(const SectionKey_t &parent,
00555 const char *name, const char *value)
00556 {
00557 int retVal = this->set_string_value(parent, name, value);
00558
00559 if (retVal)
00560 ACE_ERROR_RETURN((LM_ERROR,
00561 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00562
00563 return 0;
00564 }
00565
00566
00567
00568 int SystemProperties::SetIntegerEntry(const SectionKey_t &parent,
00569 const char *name, unsigned int value)
00570 {
00571 int retVal= this->set_integer_value(parent, name, value);
00572
00573 if (retVal)
00574 ACE_ERROR_RETURN((LM_ERROR,
00575 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00576
00577 return 0;
00578 }
00579
00580
00581
00582 int SystemProperties::SetDoubleEntry(const SectionKey_t &parent,
00583 const char *name, double value)
00584 {
00585 std::ostringstream oss;
00586 oss << value;
00587
00588 int retVal = this->set_string_value(parent, name, oss.str().c_str());
00589
00590 if (retVal)
00591 ACE_ERROR_RETURN((LM_ERROR,
00592 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00593
00594 return 0;
00595 }
00596
00597
00598
00599 int SystemProperties::SetBinaryEntry(const SectionKey_t &parent,
00600 const char *name, const void * value,
00601 size_t length)
00602 {
00603 int retVal = this->set_binary_value(parent, name, value, length);
00604
00605 if (retVal)
00606 ACE_ERROR_RETURN((LM_ERROR,
00607 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00608
00609 return 0;
00610 }
00611
00612
00613
00614 int SystemProperties::SetBooleanEntry(const SectionKey_t &parent,
00615 const char *name, bool value)
00616 {
00617 int retVal= this->set_integer_value(parent, name, value);
00618
00619 if (retVal)
00620 ACE_ERROR_RETURN((LM_ERROR,
00621 "(%N:%l) Failed to set system property: %s\n", name), retVal);
00622
00623 return 0;
00624 }
00625
00626
00627
00628 int SystemProperties::EnumerateSections(int index, std::string &name)
00629 {
00630 return EnumerateSections(this->root_section(), index, name);
00631 }
00632
00633
00634
00635 int SystemProperties::EnumerateSections(const SectionKey_t &parent, int index,
00636 std::string &name)
00637 {
00638 ACE_TString tStringName;
00639 int retVal = this->enumerate_sections(parent, index, tStringName);
00640
00641 if (retVal != -1)
00642 {
00643 name = tStringName.c_str();
00644 }
00645
00646 return retVal;
00647 }
00648
00649
00650
00651 int SystemProperties::EnumerateEntries(int index, std::string &name,
00652 Valuetype_t &valuetype)
00653 {
00654 return EnumerateEntries(this->root_section(), index, name, valuetype);
00655 }
00656
00657
00658
00659 int SystemProperties::EnumerateEntries(const SectionKey_t &parent, int index,
00660 std::string &name,
00661 Valuetype_t &valuetype)
00662 {
00663 ACE_TString tStringName;
00664 int retVal = this->enumerate_values(parent, index, tStringName, valuetype);
00665
00666 if (retVal != -1)
00667 {
00668 name = tStringName.c_str();
00669 }
00670
00671 return retVal;
00672 }
00673
00674
00675
00676 int SystemProperties::GetSectionKey(const char *sectionName, SectionKey_t &key,
00677 bool createNew)
00678 {
00679 return this->expand_path(this->root_section(),
00680 sectionName, key, createNew);
00681 }
00682
00683
00684
00685 int SystemProperties::GetSectionKey(const SectionKey_t &parent,
00686 const char *sectionName,
00687 SectionKey_t &key, bool createNew)
00688 {
00689 return this->expand_path(parent, sectionName, key, createNew);
00690 }
00691
00692
00693
00694 int SystemProperties::ImportPropertiesRegistry(const char * filename)
00695 {
00696 ACE_Registry_ImpExp impexp(*this);
00697
00698 return impexp.import_config(filename);
00699 }
00700
00701
00702
00703 int SystemProperties::ExportPropertiesRegistry(const char * filename)
00704 {
00705 ACE_Registry_ImpExp impexp(*this);
00706
00707 return impexp.export_config(filename);
00708 }
00709
00710
00711
00712 int SystemProperties::ImportPropertiesIni(const char * filename)
00713 {
00714 ACE_Ini_ImpExp impexp(*this);
00715
00716 return impexp.import_config(filename);
00717 }
00718
00719
00720
00721 int SystemProperties::ExportPropertiesIni(const char * filename)
00722 {
00723 ACE_Ini_ImpExp impexp(*this);
00724
00725 return impexp.export_config(filename);
00726 }
00727