Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / Configuration.cpp
blob734482f0e55fb48ab2aea7226b78f05840c648a5
1 // $Id: Configuration.cpp 80826 2008-03-04 14:51:23Z wotte $
2 #include "ace/Configuration.h"
3 #include "ace/Auto_Ptr.h"
4 #include "ace/SString.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_strings.h"
8 // Can remove this when import_config and export_config are removed from
9 // ACE_Configuration. They're deprecated at ACE 5.2.
10 #include "ace/Configuration_Import_Export.h"
12 #if !defined (ACE_LACKS_ACCESS)
13 # include "ace/OS_NS_unistd.h"
14 #endif /* ACE_LACKS_ACCESS */
16 #if !defined (__ACE_INLINE__)
17 #include "ace/Configuration.inl"
18 #endif /* __ACE_INLINE__ */
20 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 ACE_Section_Key_Internal::ACE_Section_Key_Internal (void)
23 : ref_count_ (0)
27 ACE_Section_Key_Internal::~ACE_Section_Key_Internal (void)
31 int
32 ACE_Section_Key_Internal::add_ref (void)
34 ++ref_count_;
35 return 0;
38 int
39 ACE_Section_Key_Internal::dec_ref (void)
41 if (!--ref_count_)
42 delete this;
43 return 0;
46 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (void)
47 : key_ (0)
51 ACE_Configuration_Section_Key::~ACE_Configuration_Section_Key (void)
53 if (key_)
54 key_->dec_ref ();
57 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (ACE_Section_Key_Internal* key)
58 : key_ (key)
60 if (key_)
61 key_->add_ref ();
64 ACE_Configuration_Section_Key::ACE_Configuration_Section_Key (const ACE_Configuration_Section_Key& rhs)
65 : key_ (rhs.key_)
67 if (key_)
68 key_->add_ref ();
71 ACE_Configuration_Section_Key&
72 ACE_Configuration_Section_Key::operator= (const ACE_Configuration_Section_Key& rhs)
74 if (this != &rhs)
76 if (key_)
77 key_->dec_ref ();
79 key_ = rhs.key_;
81 if (key_)
82 key_->add_ref ();
84 return *this;
87 //////////////////////////////////////////////////////////////////////////////
89 ACE_TCHAR ACE_Configuration::NULL_String_ = '\0';
91 ACE_Configuration::ACE_Configuration (void)
92 : root_ ()
96 ACE_Configuration::~ACE_Configuration (void)
100 ACE_Section_Key_Internal*
101 ACE_Configuration::get_internal_key (const ACE_Configuration_Section_Key& key)
103 return key.key_;
107 ACE_Configuration::expand_path (const ACE_Configuration_Section_Key& key,
108 const ACE_TString& path_in,
109 ACE_Configuration_Section_Key& key_out,
110 int create)
112 // Make a copy of key
113 ACE_Configuration_Section_Key current_section = key;
114 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> pData (path_in.rep ());
115 ACE_Tokenizer parser (pData.get ());
116 parser.delimiter_replace ('\\', '\0');
117 parser.delimiter_replace ('/', '\0');
119 for (ACE_TCHAR *temp = parser.next ();
120 temp != 0;
121 temp = parser.next ())
123 // Open the section
124 if (open_section (current_section,
125 temp,
126 create,
127 key_out))
128 return -1;
130 current_section = key_out;
133 return 0;
137 // import_config and export_config are here for backward compatibility,
138 // and have been deprecated.
140 ACE_Configuration::export_config (const ACE_TCHAR* filename)
142 ACE_Registry_ImpExp exporter (*this);
143 return exporter.export_config (filename);
147 ACE_Configuration::import_config (const ACE_TCHAR* filename)
149 ACE_Registry_ImpExp importer (*this);
150 return importer.import_config (filename);
154 ACE_Configuration::validate_name (const ACE_TCHAR* name, int allow_path)
156 // Invalid character set
157 const ACE_TCHAR* reject =
158 allow_path ? ACE_TEXT ("][") : ACE_TEXT ("\\][");
160 // Position of the first invalid character or terminating null.
161 size_t const pos = ACE_OS::strcspn (name, reject);
163 // Check if it is an invalid character.
164 if (name[pos] != ACE_TEXT ('\0'))
166 errno = EINVAL;
167 return -1;
170 // The first character can never be a path separator.
171 if (name[0] == ACE_TEXT ('\\'))
173 errno = EINVAL;
174 return -1;
177 // Validate length.
178 if (pos == 0 || pos > 255)
180 errno = ENAMETOOLONG;
181 return -1;
184 return 0;
188 ACE_Configuration::validate_value_name (const ACE_TCHAR* name)
190 if (name == 0 || *name == this->NULL_String_)
191 return 0;
193 return this->validate_name (name);
196 const ACE_Configuration_Section_Key&
197 ACE_Configuration::root_section (void) const
199 return root_;
203 * Determine if the contents of this object is the same as the
204 * contents of the object on the right hand side.
205 * Returns 1 (True) if they are equal and 0 (False) if they are not equal
207 bool
208 ACE_Configuration::operator== (const ACE_Configuration& rhs) const
210 bool rc = true;
211 int sectionIndex = 0;
212 ACE_TString sectionName;
213 ACE_Configuration *nonconst_this = const_cast<ACE_Configuration*> (this);
214 ACE_Configuration &nonconst_rhs = const_cast<ACE_Configuration&> (rhs);
216 const ACE_Configuration_Section_Key& rhsRoot = rhs.root_section ();
217 ACE_Configuration_Section_Key rhsSection;
218 ACE_Configuration_Section_Key thisSection;
220 // loop through each section in this object
221 while ((rc) && (nonconst_this->enumerate_sections (this->root_,
222 sectionIndex,
223 sectionName) == 0))
225 // find that section in the rhs object
226 if (nonconst_rhs.open_section (rhsRoot,
227 sectionName.c_str (),
229 rhsSection) != 0)
231 // If the rhs object does not contain the section then we are
232 // not equal.
233 rc = false;
235 else if (nonconst_this->open_section (this->root_,
236 sectionName.c_str (),
238 thisSection) != 0)
240 // if there is some error opening the section in this object
241 rc = false;
243 else
245 // Well the sections match
246 int valueIndex = 0;
247 ACE_TString valueName;
248 VALUETYPE valueType;
249 VALUETYPE rhsType;
251 // Enumerate each value in this section
252 while ((rc) && nonconst_this->enumerate_values (thisSection,
253 valueIndex,
254 valueName,
255 valueType) == 0)
257 // look for the same value in the rhs section
258 if (nonconst_rhs.find_value (rhsSection,
259 valueName.c_str (),
260 rhsType) != 0)
262 // We're not equal if the same value cannot
263 // be found in the rhs object.
264 rc = false;
266 else if (valueType != rhsType)
268 // we're not equal if the types do not match.
269 rc = false;
271 else
273 // finally compare values.
274 if (valueType == STRING)
276 ACE_TString thisString, rhsString;
277 if (nonconst_this->get_string_value (thisSection,
278 valueName.c_str (),
279 thisString) != 0)
281 // we're not equal if we cannot get this string
282 rc = false;
284 else if (nonconst_rhs.get_string_value (
285 rhsSection,
286 valueName.c_str (),
287 rhsString) != 0)
289 // we're not equal if we cannot get rhs string
290 rc = false;
292 rc = (thisString == rhsString);
294 else if (valueType == INTEGER)
296 u_int thisInt = 0;
297 u_int rhsInt = 0;
298 if (nonconst_this->get_integer_value (
299 thisSection,
300 valueName.c_str (),
301 thisInt) != 0)
303 // we're not equal if we cannot get this int
304 rc = false;
306 else if (nonconst_rhs.get_integer_value (
307 rhsSection,
308 valueName.c_str (),
309 rhsInt) != 0)
311 // we're not equal if we cannot get rhs int
312 rc = false;
314 rc = (thisInt == rhsInt);
316 else if (valueType == BINARY)
318 void* thisData = 0;
319 void* rhsData = 0;
320 size_t thisLength = 0;
321 size_t rhsLength = 0;
322 if (nonconst_this->get_binary_value (thisSection,
323 valueName.c_str (),
324 thisData,
325 thisLength) != 0)
327 // we're not equal if we cannot get this data
328 rc = false;
330 else if (nonconst_rhs.get_binary_value (
331 rhsSection,
332 valueName.c_str (),
333 rhsData,
334 rhsLength) != 0)
336 // we're not equal if we cannot get this data
337 rc = false;
340 rc = (thisLength == rhsLength);
341 // are the length's the same?
343 if (rc)
345 unsigned char* thisCharData =
346 (unsigned char*)thisData;
347 unsigned char* rhsCharData = (unsigned char*)rhsData;
348 // yes, then check each element
349 for (size_t count = 0;
350 (rc) && (count < thisLength);
351 count++)
353 rc = (* (thisCharData + count) == * (rhsCharData + count));
356 delete [] thisCharData;
357 delete [] rhsCharData;
358 }// end if the length's match
360 // We should never have valueTypes of INVALID, therefore
361 // we're not comparing them. How would we since we have
362 // no get operation for invalid types.
363 // So, if we have them, we guess they are equal.
365 }// end else if values match.
367 ++valueIndex;
369 }// end value while loop
371 // look in the rhs for values not in this
372 valueIndex = 0;
373 while ((rc) && (nonconst_rhs.enumerate_values (rhsSection,
374 valueIndex,
375 valueName,
376 rhsType) == 0))
378 // look for the same value in this section
379 if (nonconst_this->find_value (thisSection,
380 valueName.c_str (),
381 valueType) != 0)
383 // We're not equal if the same value cannot
384 // be found in the rhs object.
385 rc = false;
387 ++valueIndex;
388 }// end while for rhs values not in this.
390 }// end else if sections match.
392 ++sectionIndex;
394 }// end section while loop
396 // Finally, make sure that there are no sections in rhs that do not
397 // exist in this
398 sectionIndex = 0;
399 while ((rc)
400 && (nonconst_rhs.enumerate_sections (rhsRoot,
401 sectionIndex,
402 sectionName) == 0))
404 // find the section in this
405 if (nonconst_this->open_section (this->root_,
406 sectionName.c_str (),
408 thisSection) != 0)
410 // if there is some error opening the section in this object
411 rc = false;
413 else if (nonconst_rhs.open_section (rhsRoot,
414 sectionName.c_str (),
416 rhsSection) != 0)
418 // If the rhs object does not contain the section then we
419 // are not equal.
420 rc = false;
422 ++sectionIndex;
424 return rc;
427 bool
428 ACE_Configuration::operator!= (const ACE_Configuration& rhs) const
430 return !(*this == rhs);
433 //////////////////////////////////////////////////////////////////////////////
435 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
437 static const int ACE_DEFAULT_BUFSIZE = 256;
439 static const ACE_TCHAR *temp_name (const ACE_TCHAR *name)
441 if (name && *name == ACE_Configuration::NULL_String_)
442 return 0;
443 return name;
446 ACE_Section_Key_Win32::ACE_Section_Key_Win32 (HKEY hKey)
447 : hKey_ (hKey)
451 ACE_Section_Key_Win32::~ACE_Section_Key_Win32 (void)
453 ::RegCloseKey (hKey_);
456 //////////////////////////////////////////////////////////////////////////////
458 bool
459 ACE_Configuration_Win32Registry::operator== (const ACE_Configuration_Win32Registry &rhs) const
461 ACE_UNUSED_ARG (rhs);
462 return true;
465 bool
466 ACE_Configuration_Win32Registry::operator!= (const ACE_Configuration_Win32Registry &rhs) const
468 ACE_UNUSED_ARG (rhs);
469 return true;
472 ACE_Configuration_Win32Registry::ACE_Configuration_Win32Registry (HKEY hKey)
474 ACE_Section_Key_Win32 *temp = 0;
476 ACE_NEW (temp, ACE_Section_Key_Win32 (hKey));
478 root_ = ACE_Configuration_Section_Key (temp);
482 ACE_Configuration_Win32Registry::~ACE_Configuration_Win32Registry (void)
487 ACE_Configuration_Win32Registry::open_section (const ACE_Configuration_Section_Key& base,
488 const ACE_TCHAR* sub_section,
489 int create,
490 ACE_Configuration_Section_Key& result)
492 if (validate_name (sub_section, 1))
493 return -1;
495 HKEY base_key;
496 if (load_key (base, base_key))
497 return -1;
499 int errnum;
500 HKEY result_key;
501 if ((errnum = ACE_TEXT_RegOpenKeyEx (base_key,
502 sub_section,
504 KEY_ALL_ACCESS,
505 &result_key)) != ERROR_SUCCESS)
507 if (!create)
509 errno = errnum;
510 return -1;
513 if ((errnum = ACE_TEXT_RegCreateKeyEx (base_key,
514 sub_section,
517 REG_OPTION_NON_VOLATILE,
518 KEY_ALL_ACCESS,
520 &result_key,
521 #if defined (__MINGW32__)
522 (PDWORD) 0
523 #else
525 #endif /* __MINGW32__ */
526 )) != ERROR_SUCCESS)
528 errno = errnum;
529 return -1;
533 ACE_Section_Key_Win32 *temp;
535 ACE_NEW_RETURN (temp, ACE_Section_Key_Win32 (result_key), -1);
536 result = ACE_Configuration_Section_Key (temp);
537 return 0;
541 ACE_Configuration_Win32Registry::remove_section (const ACE_Configuration_Section_Key& key,
542 const ACE_TCHAR* sub_section,
543 int recursive)
545 if (validate_name (sub_section))
546 return -1;
548 HKEY base_key;
549 if (load_key (key, base_key))
550 return -1;
552 if (recursive)
554 ACE_Configuration_Section_Key section;
555 if (open_section (key, sub_section, 0, section))
556 return -1;
558 HKEY sub_key;
559 if (load_key (section, sub_key))
560 return -1;
562 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
563 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
564 // Note we don't increment the index because the
565 // enumeration becomes invalid if we change the
566 // subkey, which we do when we delete it. By leaving
567 // it 0, we always delete the top entry
568 while (ACE_TEXT_RegEnumKeyEx (sub_key,
570 name_buffer,
571 &buffer_size,
575 0) == ERROR_SUCCESS)
577 remove_section (section, name_buffer, 1);
578 buffer_size = ACE_DEFAULT_BUFSIZE;
582 int errnum;
583 errnum = ACE_TEXT_RegDeleteKey (base_key, sub_section);
584 if (errnum != ERROR_SUCCESS)
586 errno = errnum;
587 return -1;
590 return 0;
594 ACE_Configuration_Win32Registry::enumerate_values (const ACE_Configuration_Section_Key& key,
595 int index,
596 ACE_TString& name,
597 VALUETYPE& type)
599 HKEY base_key;
600 if (load_key (key, base_key))
601 return -1;
603 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
604 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
605 DWORD value_type;
607 int rc = ACE_TEXT_RegEnumValue (base_key,
608 index,
609 name_buffer,
610 &buffer_size,
612 &value_type,
615 if (rc == ERROR_NO_MORE_ITEMS)
616 return 1;
617 else if (rc != ERROR_SUCCESS)
619 errno = rc;
620 return -1;
623 name = name_buffer;
625 switch (value_type)
627 case REG_BINARY:
628 type = BINARY;
629 break;
630 case REG_SZ:
631 type = STRING;
632 break;
633 case REG_DWORD:
634 type = INTEGER;
635 break;
636 default:
637 type = INVALID;
640 return 0;
644 ACE_Configuration_Win32Registry::enumerate_sections (const ACE_Configuration_Section_Key& key,
645 int index,
646 ACE_TString& name)
648 HKEY base_key;
649 if (load_key (key, base_key))
650 return -1;
652 ACE_TCHAR name_buffer[ACE_DEFAULT_BUFSIZE];
653 DWORD buffer_size = ACE_DEFAULT_BUFSIZE;
654 int rc = ACE_TEXT_RegEnumKeyEx (base_key,
655 index,
656 name_buffer,
657 &buffer_size,
662 if (rc == ERROR_NO_MORE_ITEMS)
663 return 1;
664 else if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
666 errno = rc;
667 return -1;
670 name = name_buffer;
672 return 0;
676 ACE_Configuration_Win32Registry::set_string_value (const ACE_Configuration_Section_Key& key,
677 const ACE_TCHAR* name,
678 const ACE_TString& value)
680 const ACE_TCHAR *t_name = temp_name (name);
681 if (validate_value_name (t_name))
682 return -1;
684 HKEY base_key;
685 if (load_key (key, base_key))
686 return -1;
688 int errnum;
689 DWORD len = static_cast<DWORD> (value.length () + 1);
690 len *= sizeof (ACE_TCHAR);
691 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
692 t_name,
694 REG_SZ,
695 (BYTE *) value.fast_rep (),
696 len))
697 != ERROR_SUCCESS)
699 errno = errnum;
700 return -1;
703 return 0;
707 ACE_Configuration_Win32Registry::set_integer_value (const ACE_Configuration_Section_Key& key,
708 const ACE_TCHAR* name,
709 u_int value)
711 const ACE_TCHAR *t_name = temp_name (name);
712 if (validate_value_name (t_name))
713 return -1;
715 HKEY base_key;
716 if (load_key (key, base_key))
717 return -1;
719 int errnum;
720 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
721 t_name,
723 REG_DWORD,
724 (BYTE *) &value,
725 sizeof (value))) != ERROR_SUCCESS)
727 errno = errnum;
728 return -1;
731 return 0;
735 ACE_Configuration_Win32Registry::set_binary_value (const ACE_Configuration_Section_Key& key,
736 const ACE_TCHAR* name,
737 const void* data,
738 size_t length)
740 const ACE_TCHAR *t_name = temp_name (name);
741 if (validate_value_name (t_name))
742 return -1;
744 HKEY base_key;
745 if (load_key (key, base_key))
746 return -1;
748 int errnum;
749 if ((errnum = ACE_TEXT_RegSetValueEx (base_key,
750 t_name,
752 REG_BINARY,
753 (BYTE *) data,
754 static_cast<DWORD> (length)))
755 != ERROR_SUCCESS)
757 errno = errnum;
758 return -1;
761 return 0;
765 ACE_Configuration_Win32Registry::get_string_value (const ACE_Configuration_Section_Key& key,
766 const ACE_TCHAR* name,
767 ACE_TString& value)
769 const ACE_TCHAR *t_name = temp_name (name);
770 if (validate_value_name (t_name))
771 return -1;
773 HKEY base_key;
774 if (load_key (key, base_key))
775 return -1;
777 // Get the size of the binary data from windows
778 int errnum;
779 DWORD buffer_length = 0;
780 DWORD type;
781 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
782 t_name,
784 &type,
785 (BYTE *) 0,
786 &buffer_length)) != ERROR_SUCCESS)
788 errno = errnum;
789 return -1;
792 if (type != REG_SZ)
794 errno = ERROR_INVALID_DATATYPE;
795 return -1;
798 ACE_TCHAR *temp = 0;
799 ACE_NEW_RETURN (temp,
800 ACE_TCHAR[buffer_length],
801 -1);
803 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> buffer (temp);
805 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
806 t_name,
808 &type,
809 (BYTE *) buffer.get (),
810 &buffer_length)) != ERROR_SUCCESS)
812 errno = errnum;
813 return -1;
816 value = buffer.get ();
817 return 0;
821 ACE_Configuration_Win32Registry::get_integer_value (const ACE_Configuration_Section_Key& key,
822 const ACE_TCHAR* name,
823 u_int& value)
825 const ACE_TCHAR *t_name = temp_name (name);
826 if (validate_value_name (t_name))
827 return -1;
829 HKEY base_key;
830 if (load_key (key, base_key))
831 return -1;
833 int errnum;
834 DWORD length = sizeof (value);
835 DWORD type;
836 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
837 t_name,
839 &type,
840 (BYTE *) &value,
841 &length)) != ERROR_SUCCESS)
843 errno = errnum;
844 return -1;
847 if (type != REG_DWORD)
849 errno = ERROR_INVALID_DATATYPE;
850 return -1;
853 return 0;
857 ACE_Configuration_Win32Registry::get_binary_value (
858 const ACE_Configuration_Section_Key &key,
859 const ACE_TCHAR *name,
860 void *&data,
861 size_t &length)
863 const ACE_TCHAR *t_name = temp_name (name);
864 if (validate_value_name (t_name))
865 return -1;
867 HKEY base_key;
868 if (load_key (key, base_key))
869 return -1;
871 // Get the size of the binary data from windows
872 int errnum;
873 DWORD buffer_length = 0;
874 DWORD type;
875 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
876 t_name,
878 &type,
879 (BYTE *) 0,
880 &buffer_length)) != ERROR_SUCCESS)
882 errno = errnum;
883 return -1;
886 if (type != REG_BINARY)
888 errno = ERROR_INVALID_DATATYPE;
889 return -1;
892 length = buffer_length;
894 BYTE * the_data = 0;
895 ACE_NEW_RETURN (the_data, BYTE[length], -1);
896 ACE_Auto_Basic_Array_Ptr<BYTE> safe_data (the_data);
898 if ((errnum = ACE_TEXT_RegQueryValueEx (base_key,
899 t_name,
901 &type,
902 the_data,
903 &buffer_length)) != ERROR_SUCCESS)
905 data = 0;
906 errno = errnum;
907 return -1;
910 data = safe_data.release ();
912 return 0;
916 ACE_Configuration_Win32Registry::find_value (const ACE_Configuration_Section_Key& key,
917 const ACE_TCHAR* name,
918 VALUETYPE& type_out)
920 const ACE_TCHAR *t_name = temp_name (name);
921 if (validate_value_name (t_name))
922 return -1;
924 HKEY base_key;
925 if (load_key (key, base_key))
926 return -1;
928 DWORD buffer_length=0;
929 DWORD type;
930 int result=ACE_TEXT_RegQueryValueEx (base_key,
931 t_name,
933 &type,
935 &buffer_length);
936 if (result != ERROR_SUCCESS)
938 errno = result;
939 return -1;
942 switch (type)
944 case REG_SZ:
945 type_out = STRING;
946 break;
947 case REG_DWORD:
948 type_out = INTEGER;
949 break;
950 case REG_BINARY:
951 type_out = BINARY;
952 break;
953 default:
954 return -1; // unknown type
957 return 0;
961 ACE_Configuration_Win32Registry::remove_value (const ACE_Configuration_Section_Key& key,
962 const ACE_TCHAR* name)
964 const ACE_TCHAR *t_name = temp_name (name);
965 if (validate_value_name (t_name))
966 return -1;
968 HKEY base_key;
969 if (load_key (key, base_key))
970 return -1;
972 int errnum;
973 if ((errnum = ACE_TEXT_RegDeleteValue (base_key, t_name)) != ERROR_SUCCESS)
975 errno = errnum;
976 return -1;
979 return 0;
984 ACE_Configuration_Win32Registry::load_key (const ACE_Configuration_Section_Key& key,
985 HKEY& hKey)
987 ACE_Section_Key_Win32* pKey = dynamic_cast<ACE_Section_Key_Win32*> (get_internal_key (key));
988 if (!pKey)
989 return -1;
991 hKey = pKey->hKey_;
992 return 0;
995 HKEY
996 ACE_Configuration_Win32Registry::resolve_key (HKEY hKey,
997 const ACE_TCHAR* path,
998 int create)
1000 HKEY result = 0;
1001 // Make a copy of hKey
1002 int errnum;
1003 #if defined (ACE_HAS_WINCE)
1004 if ((errnum = RegOpenKeyEx (hKey, 0, 0, 0, &result)) != ERROR_SUCCESS)
1005 #else
1006 if ((errnum = RegOpenKey (hKey, 0, &result)) != ERROR_SUCCESS)
1007 #endif // ACE_HAS_WINCE
1009 errno = errnum;
1010 return 0;
1013 // recurse through the path
1014 ACE_TCHAR *temp_path = 0;
1015 ACE_NEW_RETURN (temp_path,
1016 ACE_TCHAR[ACE_OS::strlen (path) + 1],
1018 ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> pData (temp_path);
1019 ACE_OS::strcpy (pData.get (), path);
1020 ACE_Tokenizer parser (pData.get ());
1021 parser.delimiter_replace ('\\', '\0');
1022 parser.delimiter_replace ('/', '\0');
1024 for (ACE_TCHAR *temp = parser.next ();
1025 temp != 0;
1026 temp = parser.next ())
1028 // Open the key
1029 HKEY subkey;
1031 #if defined (ACE_HAS_WINCE)
1032 if ((errnum = ACE_TEXT_RegOpenKeyEx (result,
1033 temp,
1036 &subkey)) != ERROR_SUCCESS)
1037 #else
1038 if ((errnum = ACE_TEXT_RegOpenKey (result,
1039 temp,
1040 &subkey)) != ERROR_SUCCESS)
1041 #endif // ACE_HAS_WINCE
1043 // try creating it
1044 if (!create || (errnum = ACE_TEXT_RegCreateKeyEx (result,
1045 temp,
1049 KEY_ALL_ACCESS,
1051 &subkey,
1052 #if defined (__MINGW32__)
1053 (PDWORD) 0
1054 #else
1056 #endif /* __MINGW32__ */
1057 )) !=ERROR_SUCCESS)
1059 errno = errnum;
1060 // error
1061 ::RegCloseKey (result);
1062 return 0;
1065 // release our open key handle
1066 ::RegCloseKey (result);
1067 result = subkey;
1070 return result;
1073 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */
1075 ///////////////////////////////////////////////////////////////
1077 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (void)
1078 : type_ (ACE_Configuration::INVALID),
1079 length_ (0)
1081 this->data_.ptr_ = 0;
1084 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (ACE_TCHAR* string)
1085 : type_ (ACE_Configuration::STRING),
1086 length_ (0)
1088 this->data_.ptr_ = string;
1091 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (u_int integer)
1092 : type_ (ACE_Configuration::INTEGER),
1093 length_ (0)
1095 this->data_.int_ = integer;
1098 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (void* data, size_t length)
1099 : type_ (ACE_Configuration::BINARY),
1100 length_ (length)
1102 this->data_.ptr_ = data;
1105 ACE_Configuration_Value_IntId::ACE_Configuration_Value_IntId (const ACE_Configuration_Value_IntId& rhs)
1106 : type_ (rhs.type_),
1107 data_ (rhs.data_),
1108 length_ (rhs.length_)
1112 ACE_Configuration_Value_IntId::~ACE_Configuration_Value_IntId (void)
1116 ACE_Configuration_Value_IntId& ACE_Configuration_Value_IntId::operator= (const ACE_Configuration_Value_IntId& rhs)
1118 if (this != &rhs)
1120 type_ = rhs.type_;
1121 data_ = rhs.data_;
1122 length_ = rhs.length_;
1124 return *this;
1127 void
1128 ACE_Configuration_Value_IntId::free (ACE_Allocator *alloc)
1130 if (this->type_ == ACE_Configuration::STRING
1131 || this->type_ == ACE_Configuration::BINARY)
1132 alloc->free (data_.ptr_);
1133 // Do nothing in other cases...
1136 ACE_Configuration_ExtId::ACE_Configuration_ExtId (void)
1137 : name_ (0)
1141 ACE_Configuration_ExtId::ACE_Configuration_ExtId (const ACE_TCHAR* name)
1142 : name_ (name)
1146 ACE_Configuration_ExtId::ACE_Configuration_ExtId (const ACE_Configuration_ExtId& rhs)
1147 : name_ (rhs.name_)
1151 ACE_Configuration_ExtId::~ACE_Configuration_ExtId (void)
1155 ACE_Configuration_ExtId& ACE_Configuration_ExtId::operator= (const ACE_Configuration_ExtId& rhs)
1157 if (this != &rhs)
1158 name_ = rhs.name_;
1160 return *this;
1163 bool
1164 ACE_Configuration_ExtId::operator== (const ACE_Configuration_ExtId& rhs) const
1166 return (ACE_OS::strcasecmp (name_, rhs.name_) == 0);
1169 bool
1170 ACE_Configuration_ExtId::operator!= (const ACE_Configuration_ExtId& rhs) const
1172 return !this->operator== (rhs);
1175 u_long
1176 ACE_Configuration_ExtId::hash (void) const
1178 ACE_TString temp (name_, 0, false);
1179 return temp.hash ();
1182 void
1183 ACE_Configuration_ExtId::free (ACE_Allocator *alloc)
1185 alloc->free ((void *) (name_));
1188 ///////////////////////////////////////////////////////////////////////
1190 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (void)
1191 : value_hash_map_ (0),
1192 section_hash_map_ (0)
1196 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (VALUE_MAP* value_hash_map, SUBSECTION_MAP* section_hash_map)
1197 : value_hash_map_ (value_hash_map),
1198 section_hash_map_ (section_hash_map)
1202 ACE_Configuration_Section_IntId::ACE_Configuration_Section_IntId (const ACE_Configuration_Section_IntId& rhs)
1203 : value_hash_map_ (rhs.value_hash_map_),
1204 section_hash_map_ (rhs.section_hash_map_)
1209 ACE_Configuration_Section_IntId::~ACE_Configuration_Section_IntId ()
1213 ACE_Configuration_Section_IntId&
1214 ACE_Configuration_Section_IntId::operator= (const ACE_Configuration_Section_IntId& rhs)
1216 if (this != &rhs)
1218 value_hash_map_ = rhs.value_hash_map_;
1219 section_hash_map_ = rhs.section_hash_map_;
1221 return *this;
1224 void
1225 ACE_Configuration_Section_IntId::free (ACE_Allocator *alloc)
1227 alloc->free ((void *) (value_hash_map_));
1228 alloc->free ((void *) (section_hash_map_));
1231 ACE_Configuration_Section_Key_Heap::ACE_Configuration_Section_Key_Heap (const ACE_TCHAR* path)
1232 : path_ (0),
1233 value_iter_ (0),
1234 section_iter_ (0)
1236 path_ = ACE_OS::strdup (path);
1239 ACE_Configuration_Section_Key_Heap::~ACE_Configuration_Section_Key_Heap ()
1241 delete value_iter_;
1242 delete section_iter_;
1243 ACE_OS::free (path_);
1246 //////////////////////////////////////////////////////////////////////////////
1248 ACE_Configuration_Heap::ACE_Configuration_Heap (void)
1249 : allocator_ (0),
1250 index_ (0),
1251 default_map_size_ (0)
1253 ACE_Configuration_Section_Key_Heap *temp = 0;
1255 ACE_NEW (temp, ACE_Configuration_Section_Key_Heap (ACE_TEXT ("")));
1256 root_ = ACE_Configuration_Section_Key (temp);
1259 ACE_Configuration_Heap::~ACE_Configuration_Heap (void)
1261 if (allocator_)
1262 allocator_->sync ();
1264 delete allocator_;
1268 ACE_Configuration_Heap::open (size_t default_map_size)
1270 default_map_size_ = default_map_size;
1271 // Create the allocator with the appropriate options.
1272 // The name used for the lock is the same as one used
1273 // for the file.
1274 ACE_NEW_RETURN (this->allocator_,
1275 HEAP_ALLOCATOR (),
1276 -1);
1277 return create_index ();
1282 ACE_Configuration_Heap::open (const ACE_TCHAR* file_name,
1283 void* base_address,
1284 size_t default_map_size)
1286 default_map_size_ = default_map_size;
1288 // Make sure that the file name is of the legal length.
1289 if (ACE_OS::strlen (file_name) >= MAXNAMELEN + MAXPATHLEN)
1291 errno = ENAMETOOLONG;
1292 return -1;
1295 ACE_MMAP_Memory_Pool::OPTIONS options (base_address);
1297 // Create the allocator with the appropriate options. The name used
1298 // for the lock is the same as one used for the file.
1299 ACE_NEW_RETURN (this->allocator_,
1300 PERSISTENT_ALLOCATOR (file_name,
1301 file_name,
1302 &options),
1303 -1);
1305 #if !defined (ACE_LACKS_ACCESS)
1306 // Now check if the backing store has been created successfully.
1307 if (ACE_OS::access (file_name, F_OK) != 0)
1308 ACE_ERROR_RETURN ((LM_ERROR,
1309 ACE_TEXT ("create_index\n")),
1310 -1);
1311 #endif /* ACE_LACKS_ACCESS */
1313 return create_index ();
1317 ACE_Configuration_Heap::create_index (void)
1319 void *section_index = 0;
1321 // This is the easy case since if we find hash table in the
1322 // memory-mapped file we know it's already initialized.
1323 if (this->allocator_->find (ACE_CONFIG_SECTION_INDEX, section_index) == 0)
1324 this->index_ = (SECTION_MAP *) section_index;
1326 // Create a new <index_> (because we've just created a new
1327 // memory-mapped file).
1328 else
1330 size_t index_size = sizeof (SECTION_MAP);
1331 section_index = this->allocator_->malloc (index_size);
1333 if (section_index == 0
1334 || create_index_helper (section_index) == -1
1335 || this->allocator_->bind (ACE_CONFIG_SECTION_INDEX,
1336 section_index) == -1)
1338 // Attempt to clean up.
1339 ACE_ERROR ((LM_ERROR,
1340 ACE_TEXT ("create_index failed\n")));
1341 this->allocator_->remove ();
1342 return -1;
1344 // Add the root section
1345 return new_section (ACE_TEXT (""), root_);
1347 return 0;
1351 ACE_Configuration_Heap::create_index_helper (void *buffer)
1353 ACE_ASSERT (this->allocator_);
1354 this->index_ = new (buffer) SECTION_MAP (this->allocator_);
1355 return 0;
1359 ACE_Configuration_Heap::load_key (const ACE_Configuration_Section_Key& key,
1360 ACE_TString& name)
1362 ACE_ASSERT (this->allocator_);
1363 ACE_Configuration_Section_Key_Heap* pKey =
1364 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
1366 if (!pKey)
1368 return -1;
1371 ACE_TString temp (pKey->path_, 0, false);
1372 name.assign_nocopy (temp);
1373 return 0;
1378 ACE_Configuration_Heap::add_section (const ACE_Configuration_Section_Key& base,
1379 const ACE_TCHAR* sub_section,
1380 ACE_Configuration_Section_Key& result)
1382 ACE_ASSERT (this->allocator_);
1383 ACE_TString section;
1384 if (load_key (base, section))
1385 return -1;
1387 // Find the base section
1388 ACE_Configuration_ExtId ExtId (section.fast_rep ());
1389 ACE_Configuration_Section_IntId IntId;
1390 if (index_->find (ExtId, IntId, allocator_))
1391 return -1;
1393 // See if this section already exists
1394 ACE_Configuration_ExtId SubSectionExtId (sub_section);
1395 int ignored = 0;
1397 if (!IntId.section_hash_map_->find (SubSectionExtId, ignored, allocator_))
1399 // already exists!
1400 errno = EEXIST;
1401 return -1;
1404 // Create the new section name
1405 // only prepend a separater if were not at the root
1406 if (section.length ())
1407 section += ACE_TEXT ("\\");
1409 section += sub_section;
1411 // Add it to the base section
1412 ACE_TCHAR* pers_name = (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (sub_section) + 1) * sizeof (ACE_TCHAR));
1413 ACE_OS::strcpy (pers_name, sub_section);
1414 ACE_Configuration_ExtId SSExtId (pers_name);
1415 if (IntId.section_hash_map_->bind (SSExtId, ignored, allocator_))
1417 allocator_->free (pers_name);
1418 return -1;
1420 return (new_section (section, result));
1424 ACE_Configuration_Heap::new_section (const ACE_TString& section,
1425 ACE_Configuration_Section_Key& result)
1427 ACE_ASSERT (this->allocator_);
1428 // Create a new section and add it to the global list
1430 // Allocate memory for items to be stored in the table.
1431 size_t section_len = section.length () + 1;
1432 ACE_TCHAR *ptr = (ACE_TCHAR*) this->allocator_->malloc (section_len * sizeof (ACE_TCHAR));
1434 int return_value = -1;
1436 if (ptr == 0)
1437 return -1;
1438 else
1440 // Populate memory with data.
1441 ACE_OS::strcpy (ptr, section.fast_rep ());
1443 void *value_hash_map = 0;
1444 size_t map_size = sizeof (VALUE_MAP);
1445 value_hash_map = this->allocator_->malloc (map_size);
1447 // If allocation failed ...
1448 if (value_hash_map == 0)
1449 return -1;
1451 // Initialize allocated hash map through placement new.
1452 if (value_open_helper (default_map_size_, value_hash_map ) == -1)
1454 this->allocator_->free (value_hash_map );
1455 return -1;
1458 // create the section map
1459 void* section_hash_map = 0;
1460 map_size = sizeof (SUBSECTION_MAP);
1461 section_hash_map = this->allocator_->malloc (map_size);
1463 // If allocation failed
1464 if (section_hash_map == 0)
1465 return -1;
1467 // initialize allocated hash map through placement new
1468 if (section_open_helper (default_map_size_, section_hash_map) == -1)
1470 this->allocator_->free (value_hash_map );
1471 this->allocator_->free (section_hash_map);
1472 return -1;
1475 ACE_Configuration_ExtId name (ptr);
1476 ACE_Configuration_Section_IntId entry ((VALUE_MAP*) value_hash_map,
1477 (SUBSECTION_MAP*) section_hash_map);
1479 // Do a normal bind. This will fail if there's already an
1480 // entry with the same name.
1481 return_value = this->index_->bind (name, entry, this->allocator_);
1483 if (return_value == 1 /* Entry already existed so bind failed. */
1484 || return_value == -1 /* Unable to bind for other reasons. */)
1486 // Free our dynamically allocated memory.
1487 this->allocator_->free (static_cast<void *> (ptr));
1488 return return_value;
1491 // If bind () succeed, it will automatically sync
1492 // up the map manager entry. However, we must sync up our
1493 // name/value memory.
1494 this->allocator_->sync (ptr, section_len);
1497 // set the result
1498 ACE_Configuration_Section_Key_Heap *temp;
1499 ACE_NEW_RETURN (temp,
1500 ACE_Configuration_Section_Key_Heap (ptr),
1501 -1);
1502 result = ACE_Configuration_Section_Key (temp);
1503 return return_value;
1507 ACE_Configuration_Heap::value_open_helper (size_t hash_table_size,
1508 void *buffer)
1510 ACE_ASSERT (this->allocator_);
1511 new (buffer) VALUE_MAP (hash_table_size, this->allocator_);
1512 return 0;
1516 ACE_Configuration_Heap::section_open_helper (size_t hash_table_size,
1517 void *buffer)
1519 ACE_ASSERT (this->allocator_);
1520 new (buffer) SUBSECTION_MAP (hash_table_size, this->allocator_);
1521 return 0;
1525 ACE_Configuration_Heap::open_section (const ACE_Configuration_Section_Key& base,
1526 const ACE_TCHAR* sub_section,
1527 int create,
1528 ACE_Configuration_Section_Key& result)
1530 ACE_ASSERT (this->allocator_);
1531 if (validate_name (sub_section, 1)) // 1 == allow_path
1532 return -1;
1534 result = base;
1536 for (const ACE_TCHAR* separator;
1537 (separator = ACE_OS::strchr (sub_section, ACE_TEXT ('\\'))) != 0;
1540 ACE_TString simple_section (sub_section, separator - sub_section);
1541 int ret_val =
1542 open_simple_section (result, simple_section.c_str (), create, result);
1543 if (ret_val)
1544 return ret_val;
1545 sub_section = separator + 1;
1548 return open_simple_section (result, sub_section, create, result);
1552 ACE_Configuration_Heap::open_simple_section (const ACE_Configuration_Section_Key& base,
1553 const ACE_TCHAR* sub_section,
1554 int create,
1555 ACE_Configuration_Section_Key& result)
1557 ACE_TString section (0, 0, false);
1559 if (load_key (base, section))
1561 return -1;
1564 // Only add the \\ if were not at the root
1565 if (section.length ())
1567 section += ACE_TEXT ("\\");
1570 section += sub_section;
1572 // resolve the section
1573 ACE_Configuration_ExtId ExtId (section.fast_rep ());
1574 ACE_Configuration_Section_IntId IntId;
1576 if (index_->find (ExtId, IntId, allocator_))
1578 if (!create)
1580 errno = ENOENT;
1581 return -1;
1584 return add_section (base, sub_section, result);
1587 ACE_Configuration_Section_Key_Heap *temp;
1588 ACE_NEW_RETURN (temp,
1589 ACE_Configuration_Section_Key_Heap (section.fast_rep ()),
1590 -1);
1591 result = ACE_Configuration_Section_Key (temp);
1592 return 0;
1596 ACE_Configuration_Heap::remove_section (const ACE_Configuration_Section_Key& key,
1597 const ACE_TCHAR* sub_section,
1598 int recursive)
1600 ACE_ASSERT (this->allocator_);
1601 if (validate_name (sub_section))
1602 return -1;
1604 ACE_TString section;
1605 if (load_key (key, section))
1606 return -1;
1608 // Find this key
1609 ACE_Configuration_ExtId ParentExtId (section.fast_rep ());
1610 ACE_Configuration_Section_IntId ParentIntId;
1611 if (index_->find (ParentExtId, ParentIntId, allocator_))
1612 return -1;// no parent key
1614 // Find this subkey
1615 if (section.length ())
1616 section += ACE_TEXT ("\\");
1618 section += sub_section;
1619 ACE_Configuration_ExtId SectionExtId (section.fast_rep ());
1620 SECTION_HASH::ENTRY* section_entry;
1621 SECTION_HASH* hashmap = index_;
1622 if (hashmap->find (SectionExtId, section_entry))
1623 return -1;
1625 if (recursive)
1627 ACE_Configuration_Section_Key section;
1628 if (open_section (key, sub_section, 0, section))
1629 return -1;
1631 int index = 0;
1632 ACE_TString name;
1633 while (!enumerate_sections (section, index, name))
1635 if (remove_section (section, name.fast_rep (), 1))
1636 return -1;
1638 ++index;
1642 // Now make sure we dont have any subkeys
1643 if (section_entry->int_id_.section_hash_map_->current_size ())
1645 errno = ENOTEMPTY;
1646 return -1;
1649 // Now remove subkey from parent key
1650 ACE_Configuration_ExtId SubSExtId (sub_section);
1651 SUBSECTION_HASH::ENTRY* subsection_entry;
1652 if (((SUBSECTION_HASH*)ParentIntId.section_hash_map_)->
1653 find (SubSExtId, subsection_entry))
1654 return -1;
1656 if (ParentIntId.section_hash_map_->unbind (SubSExtId, allocator_))
1657 return -1;
1659 subsection_entry->ext_id_.free (allocator_);
1661 // Remember the pointers so we can free them after we unbind
1662 ACE_Configuration_ExtId ExtIdToFree (section_entry->ext_id_);
1663 ACE_Configuration_Section_IntId IntIdToFree (section_entry->int_id_);
1665 // iterate over all values and free memory
1666 VALUE_HASH* value_hash_map = section_entry->int_id_.value_hash_map_;
1667 VALUE_HASH::ITERATOR value_iter = value_hash_map->begin ();
1668 while (!value_iter.done ())
1670 VALUE_HASH::ENTRY* value_entry = 0;
1671 if (!value_iter.next (value_entry))
1672 return 1;
1674 value_entry->ext_id_.free (allocator_);
1675 value_entry->int_id_.free (allocator_);
1677 value_iter.advance ();
1680 // remove it
1681 if (index_->unbind (SectionExtId, allocator_))
1682 return -1;
1684 value_hash_map->close ();
1685 section_entry->int_id_.section_hash_map_->close (allocator_);
1687 // Free the memory
1688 ExtIdToFree.free (allocator_);
1689 IntIdToFree.free (allocator_);
1691 return 0;
1695 ACE_Configuration_Heap::enumerate_values (const ACE_Configuration_Section_Key& key,
1696 int index,
1697 ACE_TString& name,
1698 VALUETYPE& type)
1700 ACE_ASSERT (this->allocator_);
1701 ACE_Configuration_Section_Key_Heap* pKey =
1702 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
1703 if (!pKey)
1704 return -1;
1706 name = pKey->path_;
1708 // resolve the section
1709 ACE_Configuration_ExtId ExtId (pKey->path_);
1710 ACE_Configuration_Section_IntId IntId;
1711 if (index_->find (ExtId, IntId, allocator_))
1712 return -1;
1714 // Handle iterator resets
1715 if (index == 0)
1717 ACE_Hash_Map_Manager_Ex<ACE_Configuration_ExtId ,
1718 ACE_Configuration_Value_IntId,
1719 ACE_Hash<ACE_Configuration_ExtId>,
1720 ACE_Equal_To<ACE_Configuration_ExtId>,
1721 ACE_Null_Mutex>* hash_map = IntId.value_hash_map_;
1722 delete pKey->value_iter_;
1724 ACE_NEW_RETURN (pKey->value_iter_,
1725 VALUE_HASH::ITERATOR (hash_map->begin ()),
1726 -1);
1729 // Get the next entry
1730 ACE_Hash_Map_Entry<ACE_Configuration_ExtId, ACE_Configuration_Value_IntId>* entry = 0;
1732 if (!pKey->value_iter_->next (entry))
1733 return 1;
1735 // Return the value of the iterator and advance it
1736 name = entry->ext_id_.name_;
1737 type = entry->int_id_.type_;
1738 pKey->value_iter_->advance ();
1740 return 0;
1744 ACE_Configuration_Heap::enumerate_sections (const ACE_Configuration_Section_Key& key,
1745 int index,
1746 ACE_TString& name)
1748 ACE_ASSERT (this->allocator_);
1749 // cast to a heap section key
1750 ACE_Configuration_Section_Key_Heap* pKey =
1751 dynamic_cast<ACE_Configuration_Section_Key_Heap*> (get_internal_key (key));
1752 if (!pKey)
1753 return -1; // not a heap key!
1755 // resolve the section
1756 ACE_Configuration_ExtId ExtId (pKey->path_);
1757 ACE_Configuration_Section_IntId IntId;
1758 if (index_->find (ExtId, IntId, allocator_))
1759 return -1; // unknown section
1761 // Handle iterator resets
1762 if (index == 0)
1764 if (pKey->section_iter_)
1765 delete pKey->section_iter_;
1767 ACE_NEW_RETURN (pKey->section_iter_,
1768 SUBSECTION_HASH::ITERATOR (IntId.section_hash_map_->begin ()),
1769 -1);
1772 // Get the next entry
1773 ACE_Hash_Map_Entry<ACE_Configuration_ExtId, int>* entry = 0;
1774 if (!pKey->section_iter_->next (entry))
1775 return 1;
1777 // Return the value of the iterator and advance it
1778 pKey->section_iter_->advance ();
1779 name = entry->ext_id_.name_;
1781 return 0;
1785 ACE_Configuration_Heap::set_string_value (const ACE_Configuration_Section_Key& key,
1786 const ACE_TCHAR* name,
1787 const ACE_TString& value)
1789 ACE_ASSERT (this->allocator_);
1790 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
1791 if (validate_value_name (t_name))
1792 return -1;
1794 ACE_TString section;
1795 if (load_key (key, section))
1796 return -1;
1798 ACE_Configuration_ExtId section_ext (section.fast_rep ());
1799 ACE_Configuration_Section_IntId section_int;
1800 if (index_->find (section_ext, section_int, allocator_))
1801 return -1;
1803 // Get the entry for this item (if it exists)
1804 VALUE_HASH::ENTRY* entry;
1805 ACE_Configuration_ExtId item_name (t_name);
1806 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
1808 // found item, replace it
1809 // Free the old value
1810 entry->int_id_.free (allocator_);
1811 // Allocate the new value in this heap
1812 ACE_TCHAR* pers_value =
1813 (ACE_TCHAR *) allocator_->malloc ((value.length () + 1) * sizeof (ACE_TCHAR));
1814 ACE_OS::strcpy (pers_value, value.fast_rep ());
1815 ACE_Configuration_Value_IntId new_value_int (pers_value);
1816 entry->int_id_ = new_value_int;
1818 else
1820 // it doesn't exist, bind it
1821 ACE_TCHAR* pers_name =
1822 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
1823 ACE_OS::strcpy (pers_name, t_name);
1824 ACE_TCHAR* pers_value =
1825 (ACE_TCHAR *) allocator_->malloc ((value.length () + 1) * sizeof (ACE_TCHAR));
1826 ACE_OS::strcpy (pers_value, value.fast_rep ());
1827 ACE_Configuration_ExtId item_name (pers_name);
1828 ACE_Configuration_Value_IntId item_value (pers_value);
1829 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
1831 allocator_->free (pers_value);
1832 allocator_->free (pers_name);
1833 return -1;
1835 return 0;
1838 return 0;
1842 ACE_Configuration_Heap::set_integer_value (const ACE_Configuration_Section_Key& key,
1843 const ACE_TCHAR* name,
1844 u_int value)
1846 ACE_ASSERT (this->allocator_);
1847 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
1848 if (validate_value_name (t_name))
1849 return -1;
1851 // Get the section name from the key
1852 ACE_TString section;
1853 if (load_key (key, section))
1854 return -1;
1856 // Find this section
1857 ACE_Configuration_ExtId section_ext (section.fast_rep ());
1858 ACE_Configuration_Section_IntId section_int;
1859 if (index_->find (section_ext, section_int, allocator_))
1860 return -1; // section does not exist
1862 // Get the entry for this item (if it exists)
1863 VALUE_HASH::ENTRY* entry;
1864 ACE_Configuration_ExtId item_name (t_name);
1865 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
1867 // found item, replace it
1868 ACE_Configuration_Value_IntId new_value_int (value);
1869 entry->int_id_ = new_value_int;
1871 else
1873 // it doesn't exist, bind it
1874 ACE_TCHAR* pers_name =
1875 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
1876 ACE_OS::strcpy (pers_name, t_name);
1877 ACE_Configuration_ExtId item_name (pers_name);
1878 ACE_Configuration_Value_IntId item_value (value);
1879 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
1881 allocator_->free (pers_name);
1882 return -1;
1884 return 0;
1887 return 0;
1891 ACE_Configuration_Heap::set_binary_value (const ACE_Configuration_Section_Key& key,
1892 const ACE_TCHAR* name,
1893 const void* data,
1894 size_t length)
1896 ACE_ASSERT (this->allocator_);
1897 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
1898 if (validate_value_name (t_name))
1899 return -1;
1901 // Get the section name from the key
1902 ACE_TString section;
1903 if (load_key (key, section))
1904 return -1;
1906 // Find this section
1907 ACE_Configuration_ExtId section_ext (section.fast_rep ());
1908 ACE_Configuration_Section_IntId section_int;
1909 if (index_->find (section_ext, section_int, allocator_))
1910 return -1; // section does not exist
1912 // Get the entry for this item (if it exists)
1913 VALUE_HASH::ENTRY* entry;
1914 ACE_Configuration_ExtId item_name (t_name);
1915 if (section_int.value_hash_map_->VALUE_HASH::find (item_name, entry) == 0)
1917 // found item, replace it
1918 // Free the old value
1919 entry->int_id_.free (allocator_);
1920 // Allocate the new value in this heap
1921 ACE_TCHAR* pers_value = (ACE_TCHAR *) allocator_->malloc (length);
1922 ACE_OS::memcpy (pers_value, data, length);
1923 ACE_Configuration_Value_IntId new_value_int (pers_value, length);
1924 entry->int_id_ = new_value_int;
1926 else
1928 // it doesn't exist, bind it
1929 ACE_TCHAR* pers_name =
1930 (ACE_TCHAR *) allocator_->malloc ((ACE_OS::strlen (t_name) + 1) * sizeof (ACE_TCHAR));
1931 ACE_OS::strcpy (pers_name, t_name);
1932 ACE_TCHAR* pers_value = (ACE_TCHAR *) allocator_->malloc (length);
1933 ACE_OS::memcpy (pers_value, data, length);
1934 ACE_Configuration_ExtId item_name (pers_name);
1935 ACE_Configuration_Value_IntId item_value (pers_value, length);
1936 if (section_int.value_hash_map_->bind (item_name, item_value, allocator_))
1938 allocator_->free (pers_value);
1939 allocator_->free (pers_name);
1940 return -1;
1942 return 0;
1945 return 0;
1949 ACE_Configuration_Heap::get_string_value (const ACE_Configuration_Section_Key& key,
1950 const ACE_TCHAR* name,
1951 ACE_TString& value)
1953 ACE_ASSERT (this->allocator_);
1954 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
1955 if (validate_value_name (t_name))
1956 return -1;
1958 // Get the section name from the key
1959 ACE_TString section;
1960 if (load_key (key, section))
1961 return -1;
1963 // Find this section
1964 ACE_Configuration_ExtId ExtId (section.fast_rep ());
1965 ACE_Configuration_Section_IntId IntId;
1966 if (index_->find (ExtId, IntId, allocator_))
1967 return -1; // section does not exist
1969 // See if it exists first
1970 ACE_Configuration_ExtId VExtId (t_name);
1971 ACE_Configuration_Value_IntId VIntId;
1972 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_))
1973 return -1; // unknown value
1975 // Check type
1976 if (VIntId.type_ != ACE_Configuration::STRING)
1978 errno = ENOENT;
1979 return -1;
1982 // everythings ok, return the data
1983 value = static_cast<ACE_TCHAR*> (VIntId.data_.ptr_);
1984 return 0;
1988 ACE_Configuration_Heap::get_integer_value (const ACE_Configuration_Section_Key& key,
1989 const ACE_TCHAR* name,
1990 u_int& value)
1992 ACE_ASSERT (this->allocator_);
1994 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
1995 if (validate_value_name (t_name))
1996 return -1;
1998 // Get the section name from the key
1999 ACE_TString section (0, 0, false);
2001 if (this->load_key (key, section) != 0)
2003 return -1;
2006 // Find this section
2007 ACE_Configuration_ExtId ExtId (section.fast_rep ());
2008 ACE_Configuration_Section_IntId IntId;
2010 if (index_->find (ExtId, IntId, allocator_) != 0)
2012 return -1; // section does not exist
2016 // See if it exists first
2017 ACE_Configuration_ExtId VExtId (t_name);
2018 ACE_Configuration_Value_IntId VIntId;
2020 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_) != 0)
2022 return -1; // unknown value
2025 // Check type
2026 if (VIntId.type_ != ACE_Configuration::INTEGER)
2028 errno = ENOENT;
2029 return -1;
2032 // Everythings ok, return the data
2033 value = VIntId.data_.int_;
2034 return 0;
2038 ACE_Configuration_Heap::get_binary_value (
2039 const ACE_Configuration_Section_Key& key,
2040 const ACE_TCHAR* name,
2041 void*& data,
2042 size_t& length)
2044 ACE_ASSERT (this->allocator_);
2045 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
2046 if (validate_value_name (t_name))
2047 return -1;
2049 // Get the section name from the key
2050 ACE_TString section;
2051 if (load_key (key, section))
2052 return -1;
2054 // Find this section
2055 ACE_Configuration_ExtId ExtId (section.fast_rep ());
2056 ACE_Configuration_Section_IntId IntId;
2057 if (index_->find (ExtId, IntId, allocator_))
2058 return -1; // section does not exist
2060 ACE_Configuration_ExtId VExtId (t_name);
2061 ACE_Configuration_Value_IntId VIntId;
2062 // See if it exists first
2063 if (IntId.value_hash_map_->find (VExtId, VIntId, allocator_))
2064 return -1; // unknown value
2066 // Check type
2067 if (VIntId.type_ != ACE_Configuration::BINARY)
2069 errno = ENOENT;
2070 return -1;
2073 // Make a copy
2074 ACE_NEW_RETURN (data, char[VIntId.length_], -1);
2075 ACE_OS::memcpy (data, VIntId.data_.ptr_, VIntId.length_);
2076 length = VIntId.length_;
2077 return 0;
2081 ACE_Configuration_Heap::find_value (const ACE_Configuration_Section_Key& key,
2082 const ACE_TCHAR* name,
2083 VALUETYPE& type_out)
2085 ACE_ASSERT (this->allocator_);
2086 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
2087 if (validate_value_name (t_name))
2088 return -1;
2090 // Get the section name from the key
2091 ACE_TString section;
2092 if (load_key (key, section))
2093 return -1;
2095 // Find this section
2096 ACE_Configuration_ExtId ExtId (section.fast_rep ());
2097 ACE_Configuration_Section_IntId IntId;
2098 if (index_->find (ExtId, IntId, allocator_))
2099 return -1; // section does not exist
2101 // Find it
2102 ACE_Configuration_ExtId ValueExtId (t_name);
2103 VALUE_HASH::ENTRY* value_entry;
2104 if (((VALUE_HASH *) IntId.value_hash_map_)->find (ValueExtId, value_entry))
2105 return -1; // value does not exist
2107 type_out = value_entry->int_id_.type_;
2108 return 0;
2112 ACE_Configuration_Heap::remove_value (const ACE_Configuration_Section_Key& key,
2113 const ACE_TCHAR* name)
2115 ACE_ASSERT (this->allocator_);
2116 const ACE_TCHAR *t_name = name ? name : &this->NULL_String_;
2117 if (validate_value_name (t_name))
2118 return -1;
2120 // Get the section name from the key
2121 ACE_TString section;
2122 if (load_key (key, section))
2123 return -1;
2125 // Find this section
2126 ACE_Configuration_ExtId ExtId (section.fast_rep ());
2127 ACE_Configuration_Section_IntId IntId;
2128 if (index_->find (ExtId, IntId, allocator_))
2129 return -1; // section does not exist
2131 // Find it
2132 ACE_Configuration_ExtId ValueExtId (t_name);
2133 VALUE_HASH::ENTRY* value_entry;
2134 if (((VALUE_HASH *) IntId.value_hash_map_)->find (ValueExtId, value_entry))
2135 return -1;
2137 // free it
2138 value_entry->ext_id_.free (allocator_);
2139 value_entry->int_id_.free (allocator_);
2141 // Unbind it
2142 if (IntId.value_hash_map_->unbind (ValueExtId, allocator_))
2143 return -1;
2145 return 0;
2148 ACE_END_VERSIONED_NAMESPACE_DECL