00001
00002 #include "ace/Configuration.h"
00003 #include "ace/Auto_Ptr.h"
00004 #include "ace/SString.h"
00005 #include "ace/OS_NS_string.h"
00006 #include "ace/OS_NS_strings.h"
00007 #include "ace/Tokenizer_T.h"
00008
00009
00010
00011 #include "ace/Configuration_Import_Export.h"
00012
00013 #if !defined (ACE_LACKS_ACCESS)
00014 # include "ace/OS_NS_unistd.h"
00015 #endif
00016
00017 #if !defined (__ACE_INLINE__)
00018 #include "ace/Configuration.inl"
00019 #endif
00020
00021 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00022
00023 ACE_Section_Key_Internal::ACE_Section_Key_Internal (void)
00024 : ref_count_ (0)
00025 {
00026 }
00027
00028 ACE_Section_Key_Internal::~ACE_Section_Key_Internal (void)
00029 {
00030 }
00031
00032 int
00033 ACE_Section_Key_Internal::add_ref (void)
00034 {
00035 ++ref_count_;
00036 return 0;
00037 }
00038
00039 int
00040 ACE_Section_Key_Internal::dec_ref (void)
00041 {
00042 if (!--ref_count_)
00043 delete this;
00044 return 0;
00045 }
00046
00047 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (void)
00048 : key_ (0)
00049 {
00050 }
00051
00052 ACE_Configuration_Section_Key::~ACE_Configuration_Section_Key (void)
00053 {
00054 if (key_)
00055 key_->dec_ref ();
00056 }
00057
00058 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (ACE_Section_Key_Internal* key)
00059 : key_ (key)
00060 {
00061 if (key_)
00062 key_->add_ref ();
00063 }
00064
00065 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (const ACE_Configuration_Section_Key& rhs)
00066 : key_ (rhs.key_)
00067 {
00068 if (key_)
00069 key_->add_ref ();
00070 }
00071
00072 ACE_Configuration_Section_Key&
00073 ACE_Configuration_Section_Key::operator= (const ACE_Configuration_Section_Key& rhs)
00074 {
00075 if (this != &rhs)
00076 {
00077 if (key_)
00078 key_->dec_ref ();
00079
00080 key_ = rhs.key_;
00081
00082 if (key_)
00083 key_->add_ref ();
00084 }
00085 return *this;
00086 }
00087
00088
00089
00090 ACE_TCHAR ACE_Configuration::NULL_String_ = '\0';
00091
00092 ACE_Configuration::ACE_Configuration (void)
00093 : root_ ()
00094 {
00095 }
00096
00097 ACE_Configuration::~ACE_Configuration (void)
00098 {
00099 }
00100
00101 ACE_Section_Key_Internal*
00102 ACE_Configuration::get_internal_key (const ACE_Configuration_Section_Key& key)
00103 {
00104 return key.key_;
00105 }
00106
00107 int
00108 ACE_Configuration::expand_path (const ACE_Configuration_Section_Key& key,
00109 const ACE_TString& path_in,
00110 ACE_Configuration_Section_Key& key_out,
00111 int create)
00112 {
00113
00114 ACE_Configuration_Section_Key current_section = key;
00115 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> pData (path_in.rep ());
00116 ACE_Tokenizer parser (pData.get ());
00117 parser.delimiter_replace ('\\', '\0');
00118 parser.delimiter_replace ('/', '\0');
00119
00120 for (ACE_TCHAR *temp = parser.next ();
00121 temp != 0;
00122 temp = parser.next ())
00123 {
00124
00125 if (open_section (current_section,
00126 temp,
00127 create,
00128 key_out))
00129 return -1;
00130
00131 current_section = key_out;
00132 }
00133
00134 return 0;
00135
00136 }
00137
00138
00139
00140 int
00141 ACE_Configuration::export_config (const ACE_TCHAR* filename)
00142 {
00143 ACE_Registry_ImpExp exporter (*this);
00144 return exporter.export_config (filename);
00145 }
00146
00147 int
00148 ACE_Configuration::import_config (const ACE_TCHAR* filename)
00149 {
00150 ACE_Registry_ImpExp importer (*this);
00151 return importer.import_config (filename);
00152 }
00153
00154 int
00155 ACE_Configuration::validate_name (const ACE_TCHAR* name, int allow_path)
00156 {
00157
00158 const ACE_TCHAR* reject =
00159 allow_path ? ACE_TEXT ("][") : ACE_TEXT ("\\][");
00160
00161
00162 size_t const pos = ACE_OS::strcspn (name, reject);
00163
00164
00165 if (name[pos] != ACE_TEXT ('\0'))
00166 {
00167 errno = EINVAL;
00168 return -1;
00169 }
00170
00171
00172 if (name[0] == ACE_TEXT ('\\'))
00173 {
00174 errno = EINVAL;
00175 return -1;
00176 }
00177
00178
00179 if (pos == 0 || pos > 255)
00180 {
00181 errno = ENAMETOOLONG;
00182 return -1;
00183 }
00184
00185 return 0;
00186 }
00187
00188 int
00189 ACE_Configuration::validate_value_name (const ACE_TCHAR* name)
00190 {
00191 if (name == 0 || *name == this->NULL_String_)
00192 return 0;
00193
00194 return this->validate_name (name);
00195 }
00196
00197 const ACE_Configuration_Section_Key&
00198 ACE_Configuration::root_section (void) const
00199 {
00200 return root_;
00201 }
00202
00203
00204
00205
00206
00207
00208 bool
00209 ACE_Configuration::operator== (const ACE_Configuration& rhs) const
00210 {
00211 bool rc = true;
00212 int sectionIndex = 0;
00213 ACE_TString sectionName;
00214 ACE_Configuration *nonconst_this = const_cast<ACE_Configuration*> (this);
00215 ACE_Configuration &nonconst_rhs = const_cast<ACE_Configuration&> (rhs);
00216
00217 const ACE_Configuration_Section_Key& rhsRoot = rhs.root_section ();
00218 ACE_Configuration_Section_Key rhsSection;
00219 ACE_Configuration_Section_Key thisSection;
00220
00221
00222 while ((rc) && (nonconst_this->enumerate_sections (this->root_,
00223 sectionIndex,
00224 sectionName) == 0))
00225 {
00226
00227 if (nonconst_rhs.open_section (rhsRoot,
00228 sectionName.c_str (),
00229 0,
00230 rhsSection) != 0)
00231 {
00232
00233
00234 rc = false;
00235 }
00236 else if (nonconst_this->open_section (this->root_,
00237 sectionName.c_str (),
00238 0,
00239 thisSection) != 0)
00240 {
00241
00242 rc = false;
00243 }
00244 else
00245 {
00246
00247 int valueIndex = 0;
00248 ACE_TString valueName;
00249 VALUETYPE valueType;
00250 VALUETYPE rhsType;
00251
00252
00253 while ((rc) && nonconst_this->enumerate_values (thisSection,
00254 valueIndex,
00255 valueName,
00256 valueType) == 0)
00257 {
00258
00259 if (nonconst_rhs.find_value (rhsSection,
00260 valueName.c_str (),
00261 rhsType) != 0)
00262 {
00263
00264
00265 rc = false;
00266 }
00267 else if (valueType != rhsType)
00268 {
00269
00270 rc = false;
00271 }
00272 else
00273 {
00274
00275 if (valueType == STRING)
00276 {
00277 ACE_TString thisString, rhsString;
00278 if (nonconst_this->get_string_value (thisSection,
00279 valueName.c_str (),
00280 thisString) != 0)
00281 {
00282
00283 rc = false;
00284 }
00285 else if (nonconst_rhs.get_string_value (
00286 rhsSection,
00287 valueName.c_str (),
00288 rhsString) != 0)
00289 {
00290
00291 rc = false;
00292 }
00293 rc = (thisString == rhsString);
00294 }
00295 else if (valueType == INTEGER)
00296 {
00297 u_int thisInt = 0;
00298 u_int rhsInt = 0;
00299 if (nonconst_this->get_integer_value (
00300 thisSection,
00301 valueName.c_str (),
00302 thisInt) != 0)
00303 {
00304
00305 rc = false;
00306 }
00307 else if (nonconst_rhs.get_integer_value (
00308 rhsSection,
00309 valueName.c_str (),
00310 rhsInt) != 0)
00311 {
00312
00313 rc = false;
00314 }
00315 rc = (thisInt == rhsInt);
00316 }
00317 else if (valueType == BINARY)
00318 {
00319 void* thisData = 0;
00320 void* rhsData = 0;
00321 size_t thisLength = 0;
00322 size_t rhsLength = 0;
00323 if (nonconst_this->get_binary_value (thisSection,
00324 valueName.c_str (),
00325 thisData,
00326 thisLength) != 0)
00327 {
00328
00329 rc = false;
00330 }
00331 else if (nonconst_rhs.get_binary_value (
00332 rhsSection,
00333 valueName.c_str (),
00334 rhsData,
00335 rhsLength) != 0)
00336 {
00337
00338 rc = false;
00339 }
00340
00341 rc = (thisLength == rhsLength);
00342
00343
00344 if (rc)
00345 {
00346 unsigned char* thisCharData =
00347 (unsigned char*)thisData;
00348 unsigned char* rhsCharData = (unsigned char*)rhsData;
00349
00350 for (size_t count = 0;
00351 (rc) && (count < thisLength);
00352 count++)
00353 {
00354 rc = (* (thisCharData + count) == * (rhsCharData + count));
00355 }
00356
00357 delete [] thisCharData;
00358 delete [] rhsCharData;
00359 }
00360 }
00361
00362
00363
00364
00365
00366 }
00367
00368 ++valueIndex;
00369
00370 }
00371
00372
00373 valueIndex = 0;
00374 while ((rc) && (nonconst_rhs.enumerate_values (rhsSection,
00375 valueIndex,
00376 valueName,
00377 rhsType) == 0))
00378 {
00379
00380 if (nonconst_this->find_value (thisSection,
00381 valueName.c_str (),
00382 valueType) != 0)
00383 {
00384
00385
00386 rc = false;
00387 }
00388 ++valueIndex;
00389 }
00390
00391 }
00392
00393 ++sectionIndex;
00394
00395 }
00396
00397
00398
00399 sectionIndex = 0;
00400 while ((rc)
00401 && (nonconst_rhs.enumerate_sections (rhsRoot,
00402 sectionIndex,
00403 sectionName) == 0))
00404 {
00405
00406 if (nonconst_this->open_section (this->root_,
00407 sectionName.c_str (),
00408 0,
00409 thisSection) != 0)
00410 {
00411
00412 rc = false;
00413 }
00414 else if (nonconst_rhs.open_section (rhsRoot,
00415 sectionName.c_str (),
00416 0,
00417 rhsSection) != 0)
00418 {
00419
00420
00421 rc = false;
00422 }
00423 ++sectionIndex;
00424 }
00425 return rc;
00426 }
00427
00428 bool
00429 ACE_Configuration::operator!= (const ACE_Configuration& rhs) const
00430 {
00431 return !(*this == rhs);
00432 }
00433
00434
00435
00436 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
00437
00438 static const int ACE_DEFAULT_BUFSIZE = 256;
00439
00440 static const ACE_TCHAR *temp_name (const ACE_TCHAR *name)
00441 {
00442 if (name && *name == ACE_Configuration::NULL_String_)
00443 return 0;
00444 return name;
00445 }
00446
00447 ACE_Section_Key_Win32::ACE_Section_Key_Win32 (HKEY hKey)
00448 : hKey_ (hKey)
00449 {
00450 }
00451
00452 ACE_Section_Key_Win32::~ACE_Section_Key_Win32 (void)
00453 {
00454 ::RegCloseKey (hKey_);
00455 }
00456
00457
00458
00459 bool
00460 ACE_Configuration_Win32Registry::operator== (const ACE_Configuration_Win32Registry &rhs) const
00461 {
00462 ACE_UNUSED_ARG (rhs);
00463 return true;
00464 }
00465
00466 bool
00467 ACE_Configuration_Win32Registry::operator!= (const ACE_Configuration_Win32Registry &rhs) const
00468 {
00469 ACE_UNUSED_ARG (rhs);
00470 return true;
00471 }
00472
00473 ACE_Configuration_Win32Registry::ACE_Configuration_Win32Registry (HKEY hKey)
00474 {
00475 ACE_Section_Key_Win32 *temp = 0;
00476
00477 ACE_NEW (temp, ACE_Section_Key_Win32 (hKey));
00478
00479 root_ = ACE_Configuration_Section_Key (temp);
00480 }
00481
00482
00483 ACE_Configuration_Win32Registry::~ACE_Configuration_Win32Registry (void)
00484 {
00485 }
00486
00487 int
00488 ACE_Configuration_Win32Registry::open_section (const ACE_Configuration_Section_Key& base,
00489 const ACE_TCHAR* sub_section,
00490 int create,
00491 ACE_Configuration_Section_Key& result)
00492 {
00493 if (validate_name (sub_section, 1))
00494 return -1;
00495
00496 HKEY base_key;
00497 if (load_key (base, base_key))
00498 return -1;
00499
00500 int errnum;
00501 HKEY result_key;
00502 if ((errnum = ACE_TEXT_RegOpenKeyEx (base_key,
00503 sub_section,
00504 0,
00505 KEY_ALL_ACCESS,
00506 &result_key)) != ERROR_SUCCESS)
00507 {
00508 if (!create)
00509 {
00510 errno = errnum;
00511 return -1;
00512 }
00513
00514 if ((errnum = ACE_TEXT_RegCreateKeyEx (base_key,
00515 sub_section,
00516 0,
00517 0,
00518 REG_OPTION_NON_VOLATILE,
00519 KEY_ALL_ACCESS,
00520 0,
00521 &result_key,
00522 (PDWORD) 0
00523 )) != ERROR_SUCCESS)
00524 {
00525 errno = errnum;
00526 return -1;
00527 }
00528 }
00529
00530 ACE_Section_Key_Win32 *temp;
00531
00532 ACE_NEW_RETURN (temp, ACE_Section_Key_Win32 (result_key), -1);
00533 result = ACE_Configuration_Section_Key (temp);
00534 return 0;
00535 }
00536
00537 int
00538 ACE_Configuration_Win32Registry::remove_section (const ACE_Configuration_Section_Key& key,
00539 const ACE_TCHAR* sub_section,
00540 bool recursive)
00541 {
00542 if (validate_name (sub_section))
00543 return -1;
00544
00545 HKEY base_key;
00546 if (load_key (key, base_key))
00547 return -1;
00548
00549 if (recursive)
00550 {
00551 ACE_Configuration_Section_Key section;
00552 if (open_section (key, sub_section, 0, section))
00553 return -1;
00554
00555 HKEY sub_key;
00556 if (load_key (section, sub_key))
00557 return -1;
00558
00559 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
00560 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
00561
00562
00563
00564
00565 while (ACE_TEXT_RegEnumKeyEx (sub_key,
00566 0,
00567 name_buffer,
00568 &buffer_size,
00569 0,
00570 0,
00571 0,
00572 0) == ERROR_SUCCESS)
00573 {
00574 remove_section (section, name_buffer, true);
00575 buffer_size = ACE_DEFAULT_BUFSIZE;
00576 }
00577 }
00578
00579 int const errnum = ACE_TEXT_RegDeleteKey (base_key, sub_section);
00580 if (errnum != ERROR_SUCCESS)
00581 {
00582 errno = errnum;
00583 return -1;
00584 }
00585
00586 return 0;
00587 }
00588
00589 int
00590 ACE_Configuration_Win32Registry::enumerate_values (const ACE_Configuration_Section_Key& key,
00591 int index,
00592 ACE_TString& name,
00593 VALUETYPE& type)
00594 {
00595 HKEY base_key;
00596 if (load_key (key, base_key))
00597 return -1;
00598
00599 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
00600 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
00601 DWORD value_type;
00602
00603 int rc = ACE_TEXT_RegEnumValue (base_key,
00604 index,
00605 name_buffer,
00606 &buffer_size,
00607 0,
00608 &value_type,
00609 0,
00610 0);
00611 if (rc == ERROR_NO_MORE_ITEMS)
00612 return 1;
00613 else if (rc != ERROR_SUCCESS)
00614 {
00615 errno = rc;
00616 return -1;
00617 }
00618
00619 name = name_buffer;
00620
00621 switch (value_type)
00622 {
00623 case REG_BINARY:
00624 type = BINARY;
00625 break;
00626 case REG_SZ:
00627 type = STRING;
00628 break;
00629 case REG_DWORD:
00630 type = INTEGER;
00631 break;
00632 default:
00633 type = INVALID;
00634 }
00635
00636 return 0;
00637 }
00638
00639 int
00640 ACE_Configuration_Win32Registry::enumerate_sections (const ACE_Configuration_Section_Key& key,
00641 int index,
00642 ACE_TString& name)
00643 {
00644 HKEY base_key;
00645 if (load_key (key, base_key))
00646 return -1;
00647
00648 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
00649 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
00650 int rc = ACE_TEXT_RegEnumKeyEx (base_key,
00651 index,
00652 name_buffer,
00653 &buffer_size,
00654 0,
00655 0,
00656 0,
00657 0);
00658 if (rc == ERROR_NO_MORE_ITEMS)
00659 return 1;
00660 else if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
00661 {
00662 errno = rc;
00663 return -1;
00664 }
00665
00666 name = name_buffer;
00667
00668 return 0;
00669 }
00670
00671 int
00672 ACE_Configuration_Win32Registry::set_string_value (const ACE_Configuration_Section_Key& key,
00673 const ACE_TCHAR* name,
00674 const ACE_TString& value)
00675 {
00676 const ACE_TCHAR *t_name = temp_name (name);
00677 if (validate_value_name (t_name))
00678 return -1;
00679
00680 HKEY base_key;
00681 if (load_key (key, base_key))
00682 return -1;
00683
00684 int errnum;
00685 DWORD len = static_cast<DWORD> (value.length () + 1);
00686 len *= sizeof (ACE_TCHAR);
00687 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
00688 t_name,
00689 0,
00690 REG_SZ,
00691 (BYTE *) value.fast_rep (),
00692 len))
00693 != ERROR_SUCCESS)
00694 {
00695 errno = errnum;
00696 return -1;
00697 }
00698
00699 return 0;
00700 }
00701
00702 int
00703 ACE_Configuration_Win32Registry::set_integer_value (const ACE_Configuration_Section_Key& key,
00704 const ACE_TCHAR* name,
00705 u_int value)
00706 {
00707 const ACE_TCHAR *t_name = temp_name (name);
00708 if (validate_value_name (t_name))
00709 return -1;
00710
00711 HKEY base_key;
00712 if (load_key (key, base_key))
00713 return -1;
00714
00715 int errnum;
00716 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
00717 t_name,
00718 0,
00719 REG_DWORD,
00720 (BYTE *) &value,
00721 sizeof (value))) != ERROR_SUCCESS)
00722 {
00723 errno = errnum;
00724 return -1;
00725 }
00726
00727 return 0;
00728 }
00729
00730 int
00731 ACE_Configuration_Win32Registry::set_binary_value (const ACE_Configuration_Section_Key& key,
00732 const ACE_TCHAR* name,
00733 const void* data,
00734 size_t length)
00735 {
00736 const ACE_TCHAR *t_name = temp_name (name);
00737 if (validate_value_name (t_name))
00738 return -1;
00739
00740 HKEY base_key;
00741 if (load_key (key, base_key))
00742 return -1;
00743
00744 int errnum;
00745 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
00746 t_name,
00747 0,
00748 REG_BINARY,
00749 (BYTE *) data,
00750 static_cast<DWORD> (length)))
00751 != ERROR_SUCCESS)
00752 {
00753 errno = errnum;
00754 return -1;
00755 }
00756
00757 return 0;
00758 }
00759
00760 int
00761 ACE_Configuration_Win32Registry::get_string_value (const ACE_Configuration_Section_Key& key,
00762 const ACE_TCHAR* name,
00763 ACE_TString& value)
00764 {
00765 const ACE_TCHAR *t_name = temp_name (name);
00766 if (validate_value_name (t_name))
00767 return -1;
00768
00769 HKEY base_key;
00770 if (load_key (key, base_key))
00771 return -1;
00772
00773
00774 int errnum;
00775 DWORD buffer_length = 0;
00776 DWORD type;
00777 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
00778 t_name,
00779 0,
00780 &type,
00781 (BYTE *) 0,
00782 &buffer_length)) != ERROR_SUCCESS)
00783 {
00784 errno = errnum;
00785 return -1;
00786 }
00787
00788 if (type != REG_SZ)
00789 {
00790 errno = ERROR_INVALID_DATATYPE;
00791 return -1;
00792 }
00793
00794 ACE_TCHAR *temp = 0;
00795 ACE_NEW_RETURN (temp,
00796 ACE_TCHAR[buffer_length],
00797 -1);
00798
00799 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> buffer (temp);
00800
00801 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
00802 t_name,
00803 0,
00804 &type,
00805 (BYTE *) buffer.get (),
00806 &buffer_length)) != ERROR_SUCCESS)
00807 {
00808 errno = errnum;
00809 return -1;
00810 }
00811
00812 value = buffer.get ();
00813 return 0;
00814 }
00815
00816 int
00817 ACE_Configuration_Win32Registry::get_integer_value (const ACE_Configuration_Section_Key& key,
00818 const ACE_TCHAR* name,
00819 u_int& value)
00820 {
00821 const ACE_TCHAR *t_name = temp_name (name);
00822 if (validate_value_name (t_name))
00823 return -1;
00824
00825 HKEY base_key;
00826 if (load_key (key, base_key))
00827 return -1;
00828
00829 int errnum;
00830 DWORD length = sizeof (value);
00831 DWORD type;
00832 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
00833 t_name,
00834 0,
00835 &type,
00836 (BYTE *) &value,
00837 &length)) != ERROR_SUCCESS)
00838 {
00839 errno = errnum;
00840 return -1;
00841 }
00842
00843 if (type != REG_DWORD)
00844 {
00845 errno = ERROR_INVALID_DATATYPE;
00846 return -1;
00847 }
00848
00849 return 0;
00850 }
00851
00852 int
00853 ACE_Configuration_Win32Registry::get_binary_value (
00854 const ACE_Configuration_Section_Key &key,
00855 const ACE_TCHAR *name,
00856 void *&data,
00857 size_t &length)
00858 {
00859 const ACE_TCHAR *t_name = temp_name (name);
00860 if (validate_value_name (t_name))
00861 return -1;
00862
00863 HKEY base_key;
00864 if (load_key (key, base_key))
00865 return -1;
00866
00867
00868 int errnum;
00869 DWORD buffer_length = 0;
00870 DWORD type;
00871 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
00872 t_name,
00873 0,
00874 &type,
00875 (BYTE *) 0,
00876 &buffer_length)) != ERROR_SUCCESS)
00877 {
00878 errno = errnum;
00879 return -1;
00880 }
00881
00882 if (type != REG_BINARY)
00883 {
00884 errno = ERROR_INVALID_DATATYPE;
00885 return -1;
00886 }
00887
00888 length = buffer_length;
00889
00890 BYTE * the_data = 0;
00891 ACE_NEW_RETURN (the_data, BYTE[length], -1);
00892 ACE_Auto_Basic_Array_Ptr<BYTE> safe_data (the_data);
00893
00894 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
00895 t_name,
00896 0,
00897 &type,
00898 the_data,
00899 &buffer_length)) != ERROR_SUCCESS)
00900 {
00901 data = 0;
00902 errno = errnum;
00903 return -1;
00904 }
00905
00906 data = safe_data.release ();
00907
00908 return 0;
00909 }
00910
00911 int
00912 ACE_Configuration_Win32Registry::find_value (const ACE_Configuration_Section_Key& key,
00913 const ACE_TCHAR* name,
00914 VALUETYPE& type_out)
00915 {
00916 const ACE_TCHAR *t_name = temp_name (name);
00917 if (validate_value_name (t_name))
00918 return -1;
00919
00920 HKEY base_key;
00921 if (load_key (key, base_key))
00922 return -1;
00923
00924 DWORD buffer_length=0;
00925 DWORD type;
00926 int result=ACE_TEXT_RegQueryValueEx (base_key,
00927 t_name,
00928 0,
00929 &type,
00930 0,
00931 &buffer_length);
00932 if (result != ERROR_SUCCESS)
00933 {
00934 errno = result;
00935 return -1;
00936 }
00937
00938 switch (type)
00939 {
00940 case REG_SZ:
00941 type_out = STRING;
00942 break;
00943 case REG_DWORD:
00944 type_out = INTEGER;
00945 break;
00946 case REG_BINARY:
00947 type_out = BINARY;
00948 break;
00949 default:
00950 return -1;
00951 }
00952
00953 return 0;
00954 }
00955
00956 int
00957 ACE_Configuration_Win32Registry::remove_value (const ACE_Configuration_Section_Key& key,
00958 const ACE_TCHAR* name)
00959 {
00960 const ACE_TCHAR *t_name = temp_name (name);
00961 if (validate_value_name (t_name))
00962 return -1;
00963
00964 HKEY base_key;
00965 if (load_key (key, base_key))
00966 return -1;
00967
00968 int errnum;
00969 if ((errnum = ACE_TEXT_RegDeleteValue (base_key, t_name)) != ERROR_SUCCESS)
00970 {
00971 errno = errnum;
00972 return -1;
00973 }
00974
00975 return 0;
00976 }
00977
00978
00979 int
00980 ACE_Configuration_Win32Registry::load_key (const ACE_Configuration_Section_Key& key,
00981 HKEY& hKey)
00982 {
00983 ACE_Section_Key_Win32* pKey = dynamic_cast<ACE_Section_Key_Win32*> (get_internal_key (key));
00984 if (!pKey)
00985 return -1;
00986
00987 hKey = pKey->hKey_;
00988 return 0;
00989 }
00990
00991 HKEY
00992 ACE_Configuration_Win32Registry::resolve_key (HKEY hKey,
00993 const ACE_TCHAR* path,
00994 int create)
00995 {
00996 HKEY result = 0;
00997
00998 int errnum;
00999 #if defined (ACE_HAS_WINCE)
01000 if ((errnum = RegOpenKeyEx (hKey, 0, 0, 0, &result)) != ERROR_SUCCESS)
01001 #else
01002 if ((errnum = RegOpenKey (hKey, 0, &result)) != ERROR_SUCCESS)
01003 #endif // ACE_HAS_WINCE
01004 {
01005 errno = errnum;
01006 return 0;
01007 }
01008
01009
01010 ACE_TCHAR *temp_path = 0;
01011 ACE_NEW_RETURN (temp_path,
01012 ACE_TCHAR[ACE_OS::strlen (path) + 1],
01013 0);
01014 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> pData (temp_path);
01015 ACE_OS::strcpy (pData.get (), path);
01016 ACE_Tokenizer parser (pData.get ());
01017 parser.delimiter_replace ('\\', '\0');
01018 parser.delimiter_replace ('/', '\0');
01019
01020 for (ACE_TCHAR *temp = parser.next ();
01021 temp != 0;
01022 temp = parser.next ())
01023 {
01024
01025 HKEY subkey;
01026
01027 #if defined (ACE_HAS_WINCE)
01028 if ((errnum = ACE_TEXT_RegOpenKeyEx (result,
01029 temp,
01030 0,
01031 0,
01032 &subkey)) != ERROR_SUCCESS)
01033 #else
01034 if ((errnum = ACE_TEXT_RegOpenKey (result,
01035 temp,
01036 &subkey)) != ERROR_SUCCESS)
01037 #endif
01038 {
01039
01040 if (!create || (errnum = ACE_TEXT_RegCreateKeyEx (result,
01041 temp,
01042 0,
01043 0,
01044 0,
01045 KEY_ALL_ACCESS,
01046 0,
01047 &subkey,
01048 (PDWORD) 0
01049 )) !=ERROR_SUCCESS)
01050 {
01051 errno = errnum;
01052
01053 ::RegCloseKey (result);
01054 return 0;
01055 }
01056 }
01057
01058 ::RegCloseKey (result);
01059 result = subkey;
01060 }
01061
01062 return result;
01063 }
01064
01065 #endif
01066
01067
01068
01069 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (void)
01070 : type_ (ACE_Configuration::INVALID),
01071 length_ (0)
01072 {
01073 this->data_.ptr_ = 0;
01074 }
01075
01076 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (ACE_TCHAR* string)
01077 : type_ (ACE_Configuration::STRING),
01078 length_ (0)
01079 {
01080 this->data_.ptr_ = string;
01081 }
01082
01083 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (u_int integer)
01084 : type_ (ACE_Configuration::INTEGER),
01085 length_ (0)
01086 {
01087 this->data_.int_ = integer;
01088 }
01089
01090 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (void* data, size_t length)
01091 : type_ (ACE_Configuration::BINARY),
01092 length_ (length)
01093 {
01094 this->data_.ptr_ = data;
01095 }
01096
01097 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (const ACE_Configuration_Value_IntId& rhs)
01098 : type_ (rhs.type_),
01099 data_ (rhs.data_),
01100 length_ (rhs.length_)
01101 {
01102 }
01103
01104 ACE_Configuration_Value_IntId::~ACE_Configuration_Value_IntId (void)
01105 {
01106 }
01107
01108 ACE_Configuration_Value_IntId& ACE_Configuration_Value_IntId::operator= (const ACE_Configuration_Value_IntId& rhs)
01109 {
01110 if (this != &rhs)
01111 {
01112 type_ = rhs.type_;
01113 data_ = rhs.data_;
01114 length_ = rhs.length_;
01115 }
01116 return *this;
01117 }
01118
01119 void
01120 ACE_Configuration_Value_IntId::free (ACE_Allocator *alloc)
01121 {
01122 if (this->type_ == ACE_Configuration::STRING
01123 || this->type_ == ACE_Configuration::BINARY)
01124 alloc->free (data_.ptr_);
01125
01126 }
01127
01128 ACE_Configuration_ExtId::ACE_Configuration_ExtId (void)
01129 : name_ (0)
01130 {
01131 }
01132
01133 ACE_Configuration_ExtId::ACE_Configuration_ExtId (const ACE_TCHAR* name)
01134 : name_ (name)
01135 {
01136 }
01137
01138 ACE_Configuration_ExtId::ACE_Configuration_ExtId (const ACE_Configuration_ExtId& rhs)
01139 : name_ (rhs.name_)
01140 {
01141 }
01142
01143 ACE_Configuration_ExtId::~ACE_Configuration_ExtId (void)
01144 {
01145 }
01146
01147 ACE_Configuration_ExtId& ACE_Configuration_ExtId::operator= (const ACE_Configuration_ExtId& rhs)
01148 {
01149 if (this != &rhs)
01150 name_ = rhs.name_;
01151
01152 return *this;
01153 }
01154
01155 bool
01156 ACE_Configuration_ExtId::operator== (const ACE_Configuration_ExtId& rhs) const
01157 {
01158 return (ACE_OS::strcasecmp (name_, rhs.name_) == 0);
01159 }
01160
01161 bool
01162 ACE_Configuration_ExtId::operator!= (const ACE_Configuration_ExtId& rhs) const
01163 {
01164 return !this->operator== (rhs);
01165 }
01166
01167 u_long
01168 ACE_Configuration_ExtId::hash (void) const
01169 {
01170 ACE_TString temp (name_, 0, false);
01171 return temp.hash ();
01172 }
01173
01174 void
01175 ACE_Configuration_ExtId::free (ACE_Allocator *alloc)
01176 {
01177 alloc->free ((void *) (name_));
01178 }
01179
01180
01181
01182 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (void)
01183 : value_hash_map_ (0),
01184 section_hash_map_ (0)
01185 {
01186 }
01187
01188 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (VALUE_MAP* value_hash_map, SUBSECTION_MAP* section_hash_map)
01189 : value_hash_map_ (value_hash_map),
01190 section_hash_map_ (section_hash_map)
01191 {
01192 }
01193
01194 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (const ACE_Configuration_Section_IntId& rhs)
01195 : value_hash_map_ (rhs.value_hash_map_),
01196 section_hash_map_ (rhs.section_hash_map_)
01197 {
01198
01199 }
01200
01201 ACE_Configuration_Section_IntId::~ACE_Configuration_Section_IntId ()
01202 {
01203 }
01204
01205 ACE_Configuration_Section_IntId&
01206 ACE_Configuration_Section_IntId::operator= (const ACE_Configuration_Section_IntId& rhs)
01207 {
01208 if (this != &rhs)
01209 {
01210 value_hash_map_ = rhs.value_hash_map_;
01211 section_hash_map_ = rhs.section_hash_map_;
01212 }
01213 return *this;
01214 }
01215
01216 void
01217 ACE_Configuration_Section_IntId::free (ACE_Allocator *alloc)
01218 {
01219 alloc->free ((void *) (value_hash_map_));
01220 alloc->free ((void *) (section_hash_map_));
01221 }
01222
01223 ACE_Configuration_Section_Key_Heap::ACE_Configuration_Section_Key_Heap (const ACE_TCHAR* path)
01224 : path_ (0),
01225 value_iter_ (0),
01226 section_iter_ (0)
01227 {
01228 path_ = ACE_OS::strdup (path);
01229 }
01230
01231 ACE_Configuration_Section_Key_Heap::~ACE_Configuration_Section_Key_Heap ()
01232 {
01233 delete value_iter_;
01234 delete section_iter_;
01235 ACE_OS::free (path_);
01236 }
01237
01238
01239
01240 ACE_Configuration_Heap::ACE_Configuration_Heap (void)
01241 : allocator_ (0),
01242 index_ (0),
01243 default_map_size_ (0)
01244 {
01245 ACE_Configuration_Section_Key_Heap *temp = 0;
01246
01247 ACE_NEW (temp, ACE_Configuration_Section_Key_Heap (ACE_TEXT ("")));
01248 root_ = ACE_Configuration_Section_Key (temp);
01249 }
01250
01251 ACE_Configuration_Heap::~ACE_Configuration_Heap (void)
01252 {
01253 if (allocator_)
01254 allocator_->sync ();
01255
01256 delete allocator_;
01257 }
01258
01259 int
01260 ACE_Configuration_Heap::open (size_t default_map_size)
01261 {
01262 if (this->allocator_ != 0)
01263 {
01264 errno = EBUSY;
01265 return -1;
01266 }
01267
01268 default_map_size_ = default_map_size;
01269
01270
01271
01272 ACE_NEW_RETURN (this->allocator_,
01273 HEAP_ALLOCATOR (),
01274 -1);
01275 return create_index ();
01276 }
01277
01278
01279 int
01280 ACE_Configuration_Heap::open (const ACE_TCHAR* file_name,
01281 void* base_address,
01282 size_t default_map_size)
01283 {
01284 if (this->allocator_ != 0)
01285 {
01286 errno = EBUSY;
01287 return -1;
01288 }
01289
01290 default_map_size_ = default_map_size;
01291
01292
01293 if (ACE_OS::strlen (file_name) >= MAXNAMELEN + MAXPATHLEN)
01294 {
01295 errno = ENAMETOOLONG;
01296 return -1;
01297 }
01298
01299 ACE_MMAP_Memory_Pool::OPTIONS options (base_address);
01300
01301
01302
01303 ACE_NEW_RETURN (this->allocator_,
01304 PERSISTENT_ALLOCATOR (file_name,
01305 file_name,
01306 &options),
01307 -1);
01308
01309 #if !defined (ACE_LACKS_ACCESS)
01310
01311 if (ACE_OS::access (file_name, F_OK) != 0)
01312 ACE_ERROR_RETURN ((LM_ERROR,
01313 ACE_TEXT ("create_index\n")),
01314 -1);
01315 #endif
01316
01317 return create_index ();
01318 }
01319
01320 int
01321 ACE_Configuration_Heap::create_index (void)
01322 {
01323 void *section_index = 0;
01324
01325
01326
01327 if (this->allocator_->find (ACE_CONFIG_SECTION_INDEX, section_index) == 0)
01328 this->index_ = (SECTION_MAP *) section_index;
01329
01330
01331
01332 else
01333 {
01334 size_t index_size = sizeof (SECTION_MAP);
01335 section_index = this->allocator_->malloc (index_size);
01336
01337 if (section_index == 0
01338 || create_index_helper (section_index) == -1
01339 || this->allocator_->bind (ACE_CONFIG_SECTION_INDEX,
01340 section_index) == -1)
01341 {
01342
01343 ACE_ERROR ((LM_ERROR,
01344 ACE_TEXT ("create_index failed\n")));
01345 this->allocator_->remove ();
01346 return -1;
01347 }
01348
01349 return new_section (ACE_TEXT (""), root_);
01350 }
01351 return 0;
01352 }
01353
01354 int
01355 ACE_Configuration_Heap::create_index_helper (void *buffer)
01356 {
01357 ACE_ASSERT (this->allocator_);
01358 this->index_ = new (buffer) SECTION_MAP (this->allocator_);
01359 return 0;
01360 }
01361
01362 int
01363 ACE_Configuration_Heap::load_key (const ACE_Configuration_Section_Key& key,
01364 ACE_TString& name)
01365 {
01366 ACE_ASSERT (this->allocator_);
01367 ACE_Configuration_Section_Key_Heap* pKey =
01368 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
01369
01370 if (!pKey)
01371 {
01372 return -1;
01373 }
01374
01375 ACE_TString temp (pKey->path_, 0, false);
01376 name.assign_nocopy (temp);
01377 return 0;
01378 }
01379
01380
01381 int
01382 ACE_Configuration_Heap::add_section (const ACE_Configuration_Section_Key& base,
01383 const ACE_TCHAR* sub_section,
01384 ACE_Configuration_Section_Key& result)
01385 {
01386 ACE_ASSERT (this->allocator_);
01387 ACE_TString section;
01388 if (load_key (base, section))
01389 return -1;
01390
01391
01392 ACE_Configuration_ExtId ExtId (section.fast_rep ());
01393 ACE_Configuration_Section_IntId IntId;
01394 if (index_->find (ExtId, IntId, allocator_))
01395 return -1;
01396
01397
01398 ACE_Configuration_ExtId SubSectionExtId (sub_section);
01399 int ignored = 0;
01400
01401 if (!IntId.section_hash_map_->find (SubSectionExtId, ignored, allocator_))
01402 {
01403
01404 errno = EEXIST;
01405 return -1;
01406 }
01407
01408
01409
01410 if (section.length ())
01411 section += ACE_TEXT ("\\");
01412
01413 section += sub_section;
01414
01415
01416 ACE_TCHAR* pers_name = (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (sub_section) + 1) * sizeof (ACE_TCHAR));
01417 ACE_OS::strcpy (pers_name, sub_section);
01418 ACE_Configuration_ExtId SSExtId (pers_name);
01419 if (IntId.section_hash_map_->bind (SSExtId, ignored, allocator_))
01420 {
01421 allocator_->free (pers_name);
01422 return -1;
01423 }
01424 return (new_section (section, result));
01425 }
01426
01427 int
01428 ACE_Configuration_Heap::new_section (const ACE_TString& section,
01429 ACE_Configuration_Section_Key& result)
01430 {
01431 ACE_ASSERT (this->allocator_);
01432
01433
01434
01435 size_t section_len = section.length () + 1;
01436 ACE_TCHAR *ptr = (ACE_TCHAR*) this->allocator_->malloc (section_len * sizeof (ACE_TCHAR));
01437
01438 int return_value = -1;
01439
01440 if (ptr == 0)
01441 return -1;
01442 else
01443 {
01444
01445 ACE_OS::strcpy (ptr, section.fast_rep ());
01446
01447 void *value_hash_map = 0;
01448 size_t map_size = sizeof (VALUE_MAP);
01449 value_hash_map = this->allocator_->malloc (map_size);
01450
01451
01452 if (value_hash_map == 0)
01453 return -1;
01454
01455
01456 if (value_open_helper (default_map_size_, value_hash_map ) == -1)
01457 {
01458 this->allocator_->free (value_hash_map );
01459 return -1;
01460 }
01461
01462
01463 void* section_hash_map = 0;
01464 map_size = sizeof (SUBSECTION_MAP);
01465 section_hash_map = this->allocator_->malloc (map_size);
01466
01467
01468 if (section_hash_map == 0)
01469 return -1;
01470
01471
01472 if (section_open_helper (default_map_size_, section_hash_map) == -1)
01473 {
01474 this->allocator_->free (value_hash_map );
01475 this->allocator_->free (section_hash_map);
01476 return -1;
01477 }
01478
01479 ACE_Configuration_ExtId name (ptr);
01480 ACE_Configuration_Section_IntId entry ((VALUE_MAP*) value_hash_map,
01481 (SUBSECTION_MAP*) section_hash_map);
01482
01483
01484
01485 return_value = this->index_->bind (name, entry, this->allocator_);
01486
01487 if (return_value == 1
01488 || return_value == -1 )
01489 {
01490
01491 this->allocator_->free (static_cast<void *> (ptr));
01492 return return_value;
01493 }
01494
01495
01496
01497
01498 this->allocator_->sync (ptr, section_len);
01499 }
01500
01501
01502 ACE_Configuration_Section_Key_Heap *temp;
01503 ACE_NEW_RETURN (temp,
01504 ACE_Configuration_Section_Key_Heap (ptr),
01505 -1);
01506 result = ACE_Configuration_Section_Key (temp);
01507 return return_value;
01508 }
01509
01510 int
01511 ACE_Configuration_Heap::value_open_helper (size_t hash_table_size,
01512 void *buffer)
01513 {
01514 ACE_ASSERT (this->allocator_);
01515 new (buffer) VALUE_MAP (hash_table_size, this->allocator_);
01516 return 0;
01517 }
01518
01519 int
01520 ACE_Configuration_Heap::section_open_helper (size_t hash_table_size,
01521 void *buffer)
01522 {
01523 ACE_ASSERT (this->allocator_);
01524 new (buffer) SUBSECTION_MAP (hash_table_size, this->allocator_);
01525 return 0;
01526 }
01527
01528 int
01529 ACE_Configuration_Heap::open_section (const ACE_Configuration_Section_Key& base,
01530 const ACE_TCHAR* sub_section,
01531 int create,
01532 ACE_Configuration_Section_Key& result)
01533 {
01534 ACE_ASSERT (this->allocator_);
01535 if (validate_name (sub_section, 1))
01536 return -1;
01537
01538 result = base;
01539
01540 for (const ACE_TCHAR* separator;
01541 (separator = ACE_OS::strchr (sub_section, ACE_TEXT ('\\'))) != 0;
01542 )
01543 {
01544 ACE_TString simple_section (sub_section, separator - sub_section);
01545 int ret_val =
01546 open_simple_section (result, simple_section.c_str (), create, result);
01547 if (ret_val)
01548 return ret_val;
01549 sub_section = separator + 1;
01550 }
01551
01552 return open_simple_section (result, sub_section, create, result);
01553 }
01554
01555 int
01556 ACE_Configuration_Heap::open_simple_section (const ACE_Configuration_Section_Key& base,
01557 const ACE_TCHAR* sub_section,
01558 int create,
01559 ACE_Configuration_Section_Key& result)
01560 {
01561 ACE_TString section (0, 0, false);
01562
01563 if (load_key (base, section))
01564 {
01565 return -1;
01566 }
01567
01568
01569 if (section.length ())
01570 {
01571 section += ACE_TEXT ("\\");
01572 }
01573
01574 section += sub_section;
01575
01576
01577 ACE_Configuration_ExtId ExtId (section.fast_rep ());
01578 ACE_Configuration_Section_IntId IntId;
01579
01580 if (index_->find (ExtId, IntId, allocator_))
01581 {
01582 if (!create)
01583 {
01584 errno = ENOENT;
01585 return -1;
01586 }
01587
01588 return add_section (base, sub_section, result);
01589 }
01590
01591 ACE_Configuration_Section_Key_Heap *temp;
01592 ACE_NEW_RETURN (temp,
01593 ACE_Configuration_Section_Key_Heap (section.fast_rep ()),
01594 -1);
01595 result = ACE_Configuration_Section_Key (temp);
01596 return 0;
01597 }
01598
01599 int
01600 ACE_Configuration_Heap::remove_section (const ACE_Configuration_Section_Key& key,
01601 const ACE_TCHAR* sub_section,
01602 bool recursive)
01603 {
01604 ACE_ASSERT (this->allocator_);
01605 if (validate_name (sub_section))
01606 return -1;
01607
01608 ACE_TString section;
01609 if (load_key (key, section))
01610 return -1;
01611
01612
01613 ACE_Configuration_ExtId ParentExtId (section.fast_rep ());
01614 ACE_Configuration_Section_IntId ParentIntId;
01615 if (index_->find (ParentExtId, ParentIntId, allocator_))
01616 return -1;
01617
01618
01619 if (section.length ())
01620 section += ACE_TEXT ("\\");
01621
01622 section += sub_section;
01623 ACE_Configuration_ExtId SectionExtId (section.fast_rep ());
01624 SECTION_HASH::ENTRY* section_entry = 0;
01625 SECTION_HASH* hashmap = index_;
01626 if (hashmap->find (SectionExtId, section_entry))
01627 return -1;
01628
01629 if (recursive)
01630 {
01631 ACE_Configuration_Section_Key section;
01632 if (open_section (key, sub_section, 0, section))
01633 return -1;
01634
01635 int index = 0;
01636 ACE_TString name;
01637 while (!enumerate_sections (section, index, name))
01638 {
01639 if (remove_section (section, name.fast_rep (), true))
01640 return -1;
01641
01642 ++index;
01643 }
01644 }
01645
01646
01647 if (section_entry->int_id_.section_hash_map_->current_size ())
01648 {
01649 errno = ENOTEMPTY;
01650 return -1;
01651 }
01652
01653
01654 ACE_Configuration_ExtId SubSExtId (sub_section);
01655 SUBSECTION_HASH::ENTRY* subsection_entry;
01656 if (((SUBSECTION_HASH*)ParentIntId.section_hash_map_)->
01657 find (SubSExtId, subsection_entry))
01658 return -1;
01659
01660 if (ParentIntId.section_hash_map_->unbind (SubSExtId, allocator_))
01661 return -1;
01662
01663 subsection_entry->ext_id_.free (allocator_);
01664
01665
01666 ACE_Configuration_ExtId ExtIdToFree (section_entry->ext_id_);
01667 ACE_Configuration_Section_IntId IntIdToFree (section_entry->int_id_);
01668
01669
01670 VALUE_HASH* value_hash_map = section_entry->int_id_.value_hash_map_;
01671 VALUE_HASH::ITERATOR value_iter = value_hash_map->begin ();
01672 while (!value_iter.done ())
01673 {
01674 VALUE_HASH::ENTRY* value_entry = 0;
01675 if (!value_iter.next (value_entry))
01676 return 1;
01677
01678 value_entry->ext_id_.free (allocator_);
01679 value_entry->int_id_.free (allocator_);
01680
01681 value_iter.advance ();
01682 }
01683
01684
01685 if (index_->unbind (SectionExtId, allocator_))
01686 return -1;
01687
01688 value_hash_map->close ();
01689 section_entry->int_id_.section_hash_map_->close (allocator_);
01690
01691
01692 ExtIdToFree.free (allocator_);
01693 IntIdToFree.free (allocator_);
01694
01695 return 0;
01696 }
01697
01698 int
01699 ACE_Configuration_Heap::enumerate_values (const ACE_Configuration_Section_Key& key,
01700 int index,
01701 ACE_TString& name,
01702 VALUETYPE& type)
01703 {
01704 ACE_ASSERT (this->allocator_);
01705 ACE_Configuration_Section_Key_Heap* pKey =
01706 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
01707 if (!pKey)
01708 return -1;
01709
01710 name = pKey->path_;
01711
01712
01713 ACE_Configuration_ExtId ExtId (pKey->path_);
01714 ACE_Configuration_Section_IntId IntId;
01715 if (index_->find (ExtId, IntId, allocator_))
01716 return -1;
01717
01718
01719 if (index == 0)
01720 {
01721 ACE_Hash_Map_Manager_Ex<ACE_Configuration_ExtId ,
01722 ACE_Configuration_Value_IntId,
01723 ACE_Hash<ACE_Configuration_ExtId>,
01724 ACE_Equal_To<ACE_Configuration_ExtId>,
01725 ACE_Null_Mutex>* hash_map = IntId.value_hash_map_;
01726 delete pKey->value_iter_;
01727
01728 ACE_NEW_RETURN (pKey->value_iter_,
01729 VALUE_HASH::ITERATOR (hash_map->begin ()),
01730 -1);
01731 }
01732
01733
01734 ACE_Hash_Map_Entry<ACE_Configuration_ExtId, ACE_Configuration_Value_IntId>* entry = 0;
01735
01736 if (!pKey->value_iter_->next (entry))
01737 return 1;
01738
01739
01740 name = entry->ext_id_.name_;
01741 type = entry->int_id_.type_;
01742 pKey->value_iter_->advance ();
01743
01744 return 0;
01745 }
01746
01747 int
01748 ACE_Configuration_Heap::enumerate_sections (const ACE_Configuration_Section_Key& key,
01749 int index,
01750 ACE_TString& name)
01751 {
01752 ACE_ASSERT (this->allocator_);
01753
01754 ACE_Configuration_Section_Key_Heap* pKey =
01755 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
01756 if (!pKey)
01757 return -1;
01758
01759
01760 ACE_Configuration_ExtId ExtId (pKey->path_);
01761 ACE_Configuration_Section_IntId IntId;
01762 if (index_->find (ExtId, IntId, allocator_))
01763 return -1;
01764
01765
01766 if (index == 0)
01767 {
01768 if (pKey->section_iter_)
01769 delete pKey->section_iter_;
01770
01771 ACE_NEW_RETURN (pKey->section_iter_,
01772 SUBSECTION_HASH::ITERATOR (IntId.section_hash_map_->begin ()),
01773 -1);
01774 }
01775
01776
01777 ACE_Hash_Map_Entry<ACE_Configuration_ExtId, int>* entry = 0;
01778 if (!pKey->section_iter_->next (entry))
01779 return 1;
01780
01781
01782 pKey->section_iter_->advance ();
01783 name = entry->ext_id_.name_;
01784
01785 return 0;
01786 }
01787
01788 int
01789 ACE_Configuration_Heap::set_string_value (const ACE_Configuration_Section_Key& key,
01790 const ACE_TCHAR* name,
01791 const ACE_TString& value)
01792 {
01793 ACE_ASSERT (this->allocator_);
01794 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
01795 if (validate_value_name (t_name))
01796 return -1;
01797
01798 ACE_TString section;
01799 if (load_key (key, section))
01800 return -1;
01801
01802 ACE_Configuration_ExtId section_ext (section.fast_rep ());
01803 ACE_Configuration_Section_IntId section_int;
01804 if (index_->find (section_ext, section_int, allocator_))
01805 return -1;
01806
01807
01808 VALUE_HASH::ENTRY* entry = 0;
01809 ACE_Configuration_ExtId item_name (t_name);
01810 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
01811 {
01812
01813
01814 entry->int_id_.free (allocator_);
01815
01816 ACE_TCHAR* pers_value =
01817 (ACE_TCHAR *) allocator_->malloc ((value.length () + 1) * sizeof (ACE_TCHAR));
01818 ACE_OS::strcpy (pers_value, value.fast_rep ());
01819 ACE_Configuration_Value_IntId new_value_int (pers_value);
01820 entry->int_id_ = new_value_int;
01821 }
01822 else
01823 {
01824
01825 ACE_TCHAR* pers_name =
01826 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
01827 ACE_OS::strcpy (pers_name, t_name);
01828 ACE_TCHAR* pers_value =
01829 (ACE_TCHAR *) allocator_->malloc ((value.length () + 1) * sizeof (ACE_TCHAR));
01830 ACE_OS::strcpy (pers_value, value.fast_rep ());
01831 ACE_Configuration_ExtId item_name (pers_name);
01832 ACE_Configuration_Value_IntId item_value (pers_value);
01833 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
01834 {
01835 allocator_->free (pers_value);
01836 allocator_->free (pers_name);
01837 return -1;
01838 }
01839 return 0;
01840 }
01841
01842 return 0;
01843 }
01844
01845 int
01846 ACE_Configuration_Heap::set_integer_value (const ACE_Configuration_Section_Key& key,
01847 const ACE_TCHAR* name,
01848 u_int value)
01849 {
01850 ACE_ASSERT (this->allocator_);
01851 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
01852 if (validate_value_name (t_name))
01853 return -1;
01854
01855
01856 ACE_TString section;
01857 if (load_key (key, section))
01858 return -1;
01859
01860
01861 ACE_Configuration_ExtId section_ext (section.fast_rep ());
01862 ACE_Configuration_Section_IntId section_int;
01863 if (index_->find (section_ext, section_int, allocator_))
01864 return -1;
01865
01866
01867 VALUE_HASH::ENTRY* entry = 0;
01868 ACE_Configuration_ExtId item_name (t_name);
01869 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
01870 {
01871
01872 ACE_Configuration_Value_IntId new_value_int (value);
01873 entry->int_id_ = new_value_int;
01874 }
01875 else
01876 {
01877
01878 ACE_TCHAR* pers_name =
01879 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
01880 ACE_OS::strcpy (pers_name, t_name);
01881 ACE_Configuration_ExtId item_name (pers_name);
01882 ACE_Configuration_Value_IntId item_value (value);
01883 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
01884 {
01885 allocator_->free (pers_name);
01886 return -1;
01887 }
01888 return 0;
01889 }
01890
01891 return 0;
01892 }
01893
01894 int
01895 ACE_Configuration_Heap::set_binary_value (const ACE_Configuration_Section_Key& key,
01896 const ACE_TCHAR* name,
01897 const void* data,
01898 size_t length)
01899 {
01900 ACE_ASSERT (this->allocator_);
01901 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
01902 if (validate_value_name (t_name))
01903 return -1;
01904
01905
01906 ACE_TString section;
01907 if (load_key (key, section))
01908 return -1;
01909
01910
01911 ACE_Configuration_ExtId section_ext (section.fast_rep ());
01912 ACE_Configuration_Section_IntId section_int;
01913 if (index_->find (section_ext, section_int, allocator_))
01914 return -1;
01915
01916
01917 VALUE_HASH::ENTRY* entry = 0;
01918 ACE_Configuration_ExtId item_name (t_name);
01919 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
01920 {
01921
01922
01923 entry->int_id_.free (allocator_);
01924
01925 ACE_TCHAR* pers_value = (ACE_TCHAR *) allocator_->malloc (length);
01926 ACE_OS::memcpy (pers_value, data, length);
01927 ACE_Configuration_Value_IntId new_value_int (pers_value, length);
01928 entry->int_id_ = new_value_int;
01929 }
01930 else
01931 {
01932
01933 ACE_TCHAR* pers_name =
01934 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
01935 ACE_OS::strcpy (pers_name, t_name);
01936 ACE_TCHAR* pers_value = (ACE_TCHAR *) allocator_->malloc (length);
01937 ACE_OS::memcpy (pers_value, data, length);
01938 ACE_Configuration_ExtId item_name (pers_name);
01939 ACE_Configuration_Value_IntId item_value (pers_value, length);
01940 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
01941 {
01942 allocator_->free (pers_value);
01943 allocator_->free (pers_name);
01944 return -1;
01945 }
01946 return 0;
01947 }
01948
01949 return 0;
01950 }
01951
01952 int
01953 ACE_Configuration_Heap::get_string_value (const ACE_Configuration_Section_Key& key,
01954 const ACE_TCHAR* name,
01955 ACE_TString& value)
01956 {
01957 ACE_ASSERT (this->allocator_);
01958 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
01959 if (validate_value_name (t_name))
01960 return -1;
01961
01962
01963 ACE_TString section;
01964 if (load_key (key, section))
01965 return -1;
01966
01967
01968 ACE_Configuration_ExtId ExtId (section.fast_rep ());
01969 ACE_Configuration_Section_IntId IntId;
01970 if (index_->find (ExtId, IntId, allocator_))
01971 return -1;
01972
01973
01974 ACE_Configuration_ExtId VExtId (t_name);
01975 ACE_Configuration_Value_IntId VIntId;
01976 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_))
01977 return -1;
01978
01979
01980 if (VIntId.type_ != ACE_Configuration::STRING)
01981 {
01982 errno = ENOENT;
01983 return -1;
01984 }
01985
01986
01987 value = static_cast<ACE_TCHAR*> (VIntId.data_.ptr_);
01988 return 0;
01989 }
01990
01991 int
01992 ACE_Configuration_Heap::get_integer_value (const ACE_Configuration_Section_Key& key,
01993 const ACE_TCHAR* name,
01994 u_int& value)
01995 {
01996 ACE_ASSERT (this->allocator_);
01997
01998 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
01999 if (validate_value_name (t_name))
02000 return -1;
02001
02002
02003 ACE_TString section (0, 0, false);
02004
02005 if (this->load_key (key, section) != 0)
02006 {
02007 return -1;
02008 }
02009
02010
02011 ACE_Configuration_ExtId ExtId (section.fast_rep ());
02012 ACE_Configuration_Section_IntId IntId;
02013
02014 if (index_->find (ExtId, IntId, allocator_) != 0)
02015 {
02016 return -1;
02017 }
02018
02019
02020
02021 ACE_Configuration_ExtId VExtId (t_name);
02022 ACE_Configuration_Value_IntId VIntId;
02023
02024 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_) != 0)
02025 {
02026 return -1;
02027 }
02028
02029
02030 if (VIntId.type_ != ACE_Configuration::INTEGER)
02031 {
02032 errno = ENOENT;
02033 return -1;
02034 }
02035
02036
02037 value = VIntId.data_.int_;
02038 return 0;
02039 }
02040
02041 int
02042 ACE_Configuration_Heap::get_binary_value (
02043 const ACE_Configuration_Section_Key& key,
02044 const ACE_TCHAR* name,
02045 void*& data,
02046 size_t& length)
02047 {
02048 ACE_ASSERT (this->allocator_);
02049 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
02050 if (validate_value_name (t_name))
02051 return -1;
02052
02053
02054 ACE_TString section;
02055 if (load_key (key, section))
02056 return -1;
02057
02058
02059 ACE_Configuration_ExtId ExtId (section.fast_rep ());
02060 ACE_Configuration_Section_IntId IntId;
02061 if (index_->find (ExtId, IntId, allocator_))
02062 return -1;
02063
02064 ACE_Configuration_ExtId VExtId (t_name);
02065 ACE_Configuration_Value_IntId VIntId;
02066
02067 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_))
02068 return -1;
02069
02070
02071 if (VIntId.type_ != ACE_Configuration::BINARY)
02072 {
02073 errno = ENOENT;
02074 return -1;
02075 }
02076
02077
02078 ACE_NEW_RETURN (data, char[VIntId.length_], -1);
02079 ACE_OS::memcpy (data, VIntId.data_.ptr_, VIntId.length_);
02080 length = VIntId.length_;
02081 return 0;
02082 }
02083
02084 int
02085 ACE_Configuration_Heap::find_value (const ACE_Configuration_Section_Key& key,
02086 const ACE_TCHAR* name,
02087 VALUETYPE& type_out)
02088 {
02089 ACE_ASSERT (this->allocator_);
02090 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
02091 if (validate_value_name (t_name))
02092 return -1;
02093
02094
02095 ACE_TString section;
02096 if (load_key (key, section))
02097 return -1;
02098
02099
02100 ACE_Configuration_ExtId ExtId (section.fast_rep ());
02101 ACE_Configuration_Section_IntId IntId;
02102 if (index_->find (ExtId, IntId, allocator_))
02103 return -1;
02104
02105
02106 ACE_Configuration_ExtId ValueExtId (t_name);
02107 VALUE_HASH::ENTRY* value_entry = 0;
02108 if (((VALUE_HASH *) IntId.value_hash_map_)->find (ValueExtId, value_entry))
02109 return -1;
02110
02111 type_out = value_entry->int_id_.type_;
02112 return 0;
02113 }
02114
02115 int
02116 ACE_Configuration_Heap::remove_value (const ACE_Configuration_Section_Key& key,
02117 const ACE_TCHAR* name)
02118 {
02119 ACE_ASSERT (this->allocator_);
02120 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
02121 if (validate_value_name (t_name))
02122 return -1;
02123
02124
02125 ACE_TString section;
02126 if (load_key (key, section))
02127 return -1;
02128
02129
02130 ACE_Configuration_ExtId ExtId (section.fast_rep ());
02131 ACE_Configuration_Section_IntId IntId;
02132 if (index_->find (ExtId, IntId, allocator_))
02133 return -1;
02134
02135
02136 ACE_Configuration_ExtId ValueExtId (t_name);
02137 VALUE_HASH::ENTRY* value_entry = 0;
02138 if (((VALUE_HASH *) IntId.value_hash_map_)->find (ValueExtId, value_entry))
02139 return -1;
02140
02141
02142 value_entry->ext_id_.free (allocator_);
02143 value_entry->int_id_.free (allocator_);
02144
02145
02146 if (IntId.value_hash_map_->unbind (ValueExtId, allocator_))
02147 return -1;
02148
02149 return 0;
02150 }
02151
02152 ACE_END_VERSIONED_NAMESPACE_DECL