00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #ifndef WINSTL_INCL_H_WINSTL
00066 # include "winstl.h"
00067 #endif
00068 #ifndef WINSTL_INCL_H_WINSTL_REG_DEFS
00069 # include "winstl_reg_defs.h"
00070 #endif
00071 #ifndef WINSTL_INCL_H_WINSTL_REG_TRAITS
00072 # include "winstl_reg_traits.h"
00073 #endif
00074 #ifndef WINSTL_INCL_H_WINSTL_AUTO_DESTRUCTOR
00075 # include "winstl_processheap_allocator.h"
00076 #endif
00077 #ifndef STLSOFT_INCL_H_STLSOFT_AUTO_DESTRUCTOR
00078 # include "stlsoft_auto_buffer.h"
00079 #endif
00080 #ifndef STLSOFT_INCL_H_STLSOFT_ITERATOR
00081 # include "stlsoft_iterator.h"
00082 #endif
00083 #include <algorithm>
00084
00085
00086
00087
00088
00089 #ifndef _WINSTL_NO_NAMESPACE
00090 # if defined(_STLSOFT_NO_NAMESPACE) || \
00091 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00092
00093 namespace winstl
00094 {
00095 # else
00096
00097
00098 namespace stlsoft
00099 {
00100
00101 namespace winstl_project
00102 {
00103
00104 # endif
00105 #endif
00106
00107
00108
00111
00112
00113
00114
00115
00116 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00117
00118 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k V, ss_typename_param_k A>
00119 class basic_reg_value_sequence_const_iterator;
00120
00121 #endif
00122
00123
00124
00125
00126
00128 template<ss_typename_param_k A>
00129 class reg_blob
00130 : protected A
00131 {
00132 typedef auto_buffer<ws_byte_t, processheap_allocator<ws_byte_t>, CCH_REG_API_AUTO_BUFFER> buffer_type;
00133 public:
00135 typedef A allocator_type;
00137 typedef reg_blob<A> class_type;
00139 typedef ws_byte_t value_type;
00141 typedef value_type const &const_reference;
00143 typedef value_type const *const_pointer;
00145 typedef value_type const *const_iterator;
00147 typedef ws_size_t size_type;
00149 typedef ws_ptrdiff_t difference_type;
00150 #if defined(__STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
00151
00152 typedef const_reverse_iterator_base < const_iterator
00153 , value_type const
00154 , const_reference
00155 , const_pointer
00156 , difference_type
00157 > const_reverse_iterator;
00158 #endif
00159
00160
00161 public:
00162 reg_blob();
00163 reg_blob(value_type const *data, size_type n);
00164 reg_blob(class_type const &rhs);
00165 ~reg_blob() winstl_throw_0();
00166
00167
00168 public:
00169 size_type size() const;
00170 const_pointer data() const;
00171
00172
00173 public:
00174 const_iterator begin() const;
00175 const_iterator end() const;
00176
00177
00178 private:
00179 buffer_type m_buffer;
00180
00181
00182 private:
00183 reg_blob &operator =(class_type const &rhs);
00184 };
00185
00194 template< ss_typename_param_k C
00195 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00196 , ss_typename_param_k T = reg_traits<C>
00197 , ss_typename_param_k A = processheap_allocator<C>
00198 #else
00199 , ss_typename_param_k T
00200 , ss_typename_param_k A
00201 #endif
00202 >
00203 class basic_reg_value
00204 {
00205 public:
00207 typedef C char_type;
00209 typedef T traits_type;
00211 typedef A allocator_type;
00213 typedef basic_reg_value<C, T, A> class_type;
00215 typedef ss_typename_type_k traits_type::size_type size_type;
00217 typedef ss_typename_type_k traits_type::string_type string_type;
00219 #if defined(__STLSOFT_CF_THROW_BAD_ALLOC) && \
00220 _MSC_VER == 1100
00221
00222 typedef HKEY hkey_type;
00223 #else
00224 typedef ss_typename_type_k traits_type::hkey_type hkey_type;
00225 #endif
00226
00227 typedef reg_blob<A> blob_type;
00228
00229
00230 public:
00232 basic_reg_value();
00234 basic_reg_value(class_type const &rhs);
00235 protected:
00236 basic_reg_value(hkey_type hkeyParent, string_type const &value_name)
00237 : m_hkey(traits_type::key_dup(hkeyParent))
00238 , m_name(value_name)
00239 , m_type(REG_NONE)
00240 , m_bTypeRetrieved(ws_false_v)
00241 {}
00242 public:
00244 ~basic_reg_value() winstl_throw_0();
00245
00247 class_type &operator =(class_type const &rhs);
00248
00249
00250 public:
00267 ws_dword_t type() const;
00268
00270 string_type name() const;
00271
00275 string_type value_sz() const;
00279 string_type value_expand_sz() const;
00281 ws_dword_t value_dword() const;
00283 ws_dword_t value_dword_littleendian() const;
00285 ws_dword_t value_dword_bigendian() const;
00287 blob_type value_binary() const;
00288
00289
00290 private:
00291 ws_dword_t _type() const;
00292
00293
00294 private:
00295 friend class basic_reg_value_sequence_const_iterator<C, T, class_type, A>;
00296
00297 hkey_type m_hkey;
00298 string_type m_name;
00299 ss_mutable_k ws_dword_t m_type;
00300 ss_mutable_k ws_bool_t m_bTypeRetrieved;
00301 };
00302
00303
00305 typedef basic_reg_value<ws_char_a_t, reg_traits<ws_char_a_t>, processheap_allocator<ws_char_a_t> > reg_value_a;
00307 typedef basic_reg_value<ws_char_w_t, reg_traits<ws_char_w_t>, processheap_allocator<ws_char_w_t> > reg_value_w;
00309 typedef basic_reg_value<TCHAR, reg_traits<TCHAR>, processheap_allocator<TCHAR> > reg_value;
00310
00312
00313
00314 #ifdef STLSOFT_UNITTEST
00315
00316 namespace unittest
00317 {
00318 ss_bool_t test_winstl_reg_value(unittest_reporter *r)
00319 {
00320 using stlsoft::unittest::unittest_initialiser;
00321
00322 ss_bool_t bSuccess = true;
00323
00324 unittest_initialiser init(r, "WinSTL", "reg_value", __FILE__);
00325
00326 #if 0
00327 if(<<TODO>>)
00328 {
00329 r->report("<<TODO>> failed ", __LINE__);
00330 bSuccess = false;
00331 }
00332 #endif
00333
00334 return bSuccess;
00335 }
00336
00337 unittest_registrar unittest_winstl_reg_value(test_winstl_reg_value);
00338
00339 }
00340
00341 #endif
00342
00343
00344
00345
00346
00347 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00348
00349 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00350 inline basic_reg_value<C, T, A>::basic_reg_value()
00351 : m_hkey(NULL)
00352 , m_type(REG_NONE)
00353 , m_bTypeRetrieved(ws_false_v)
00354 {}
00355
00356 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00357 inline basic_reg_value<C, T, A>::basic_reg_value(class_type const &rhs)
00358 : m_hkey(traits_type::key_dup(rhs.m_hkey))
00359 , m_name(rhs.m_name)
00360 , m_type(rhs.m_type)
00361 , m_bTypeRetrieved(rhs.m_bTypeRetrieved)
00362 {}
00363
00364 #if 0
00365 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00366 inline basic_reg_value<C, T, A>::basic_reg_value(basic_reg_value<C, T, A>::hkey_type hkeyParent, basic_reg_value<C, T, A>::string_type const &value_name)
00367 : m_hkey(traits_type::key_dup(hkeyParent))
00368 , m_name(value_name)
00369 , m_type(REG_NONE)
00370 , m_bTypeRetrieved(ws_false_v)
00371 {}
00372 #endif
00373
00374 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00375 inline basic_reg_value<C, T, A>::~basic_reg_value() winstl_throw_0()
00376 {
00377 if(m_hkey != NULL)
00378 {
00379 ::RegCloseKey(m_hkey);
00380 }
00381 }
00382
00383 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00384 inline ss_typename_type_k basic_reg_value<C, T, A>::class_type &basic_reg_value<C, T, A>::operator =(class_type const &rhs)
00385 {
00386 if(m_hkey != NULL)
00387 {
00388 ::RegCloseKey(m_hkey);
00389 }
00390
00391 m_hkey = traits_type::key_dup(rhs.m_hkey);
00392 m_name = rhs.m_name;
00393 m_type = rhs.m_type;
00394 m_bTypeRetrieved = rhs.m_bTypeRetrieved;
00395
00396 return *this;
00397 }
00398
00399
00400 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00401 inline ws_dword_t basic_reg_value<C, T, A>::_type() const
00402 {
00403 if(!m_bTypeRetrieved)
00404 {
00405 size_type data_size;
00406
00407 #ifndef __STLSOFT_CF_MUTABLE_KEYWORD_SUPPORT
00408
00409
00410
00411 ws_dword_t &m_type = const_cast<ws_dword_t &>(this->m_type);
00412 ws_bool_t &m_bTypeRetrieved = const_cast<ws_bool_t &>(this->m_bTypeRetrieved);
00413 #endif
00414
00415 if(0 == traits_type::reg_query_value(m_hkey, m_name.c_str(), m_type, NULL, data_size))
00416 {
00417 m_bTypeRetrieved = ws_true_v;
00418 }
00419 }
00420
00421 return m_type;
00422 }
00423
00424
00425 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00426 inline ws_dword_t basic_reg_value<C, T, A>::type() const
00427 {
00428 return _type();
00429 }
00430
00431 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00432 inline ss_typename_type_k basic_reg_value<C, T, A>::string_type basic_reg_value<C, T, A>::name() const
00433 {
00434 return m_name;
00435 }
00436
00437 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00438 inline ss_typename_type_k basic_reg_value<C, T, A>::string_type basic_reg_value<C, T, A>::value_sz() const
00439 {
00440
00441 string_type ret;
00442 size_type data_size;
00443 ws_long_t res = traits_type::reg_query_info(m_hkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &data_size, NULL, NULL);
00444
00445 if( res == ERROR_SUCCESS &&
00446 data_size > 0)
00447 {
00448 stlsoft_ns_qual(auto_buffer)<char_type, allocator_type, CCH_REG_API_AUTO_BUFFER> buffer(1 + data_size);
00449 ws_dword_t dw;
00450
00451 data_size = buffer.size();
00452 res = traits_type::reg_query_value(m_hkey, m_name.c_str(), dw, static_cast<char_type*>(buffer), data_size);
00453
00454 if(res == ERROR_SUCCESS)
00455 {
00456 buffer[data_size / sizeof(char_type)] = 0;
00457
00458 ret = buffer.data();
00459 }
00460 }
00461
00462 return ret;
00463 }
00464
00465 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00466 inline ss_typename_type_k basic_reg_value<C, T, A>::string_type basic_reg_value<C, T, A>::value_expand_sz() const
00467 {
00468
00469 string_type ret = value_sz();
00470
00471 if( ret.length() > 0 &&
00472 m_type == REG_EXPAND_SZ)
00473 {
00474 size_type size = traits_type::expand_env_strings(ret.c_str(), NULL, 0);
00475 stlsoft_ns_qual(auto_buffer)<char_type, allocator_type, CCH_REG_API_AUTO_BUFFER> buffer(1 + size);
00476
00477 if(traits_type::expand_env_strings(ret.c_str(), buffer, size) != 0)
00478 {
00479 ret = static_cast<char_type const *>(buffer);
00480 }
00481 }
00482
00483 return ret;
00484 }
00485
00486 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00487 inline ws_dword_t basic_reg_value<C, T, A>::value_dword() const
00488 {
00489 ws_dword_t dwValue;
00490 size_type cbData = sizeof(dwValue);
00491 ws_dword_t value_type;
00492 ws_long_t res = traits_type::reg_query_value(m_hkey, m_name.c_str(), value_type, &dwValue, cbData);
00493
00494 if(res != ERROR_SUCCESS)
00495 {
00496 dwValue = 0;
00497 }
00498
00499 return dwValue;
00500 }
00501
00502 #if 0
00503 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00504 inline ws_dword_t basic_reg_value<C, T, A>::value_dword_littleendian() const
00505 {}
00506
00507 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00508 inline ws_dword_t basic_reg_value<C, T, A>::value_dword_bigendian() const
00509 {}
00510 #endif
00511
00512 template <ss_typename_param_k C, ss_typename_param_k T, ss_typename_param_k A>
00513 inline ss_typename_type_k basic_reg_value<C, T, A>::blob_type basic_reg_value<C, T, A>::value_binary() const
00514 {
00515 size_type data_size;
00516 ws_dword_t dw;
00517 ws_long_t res = traits_type::reg_query_value(m_hkey, m_name.c_str(), dw, NULL, data_size);
00518
00519 if(res == ERROR_SUCCESS)
00520 {
00521 stlsoft_message_assert("queried registry value is not binary", dw == REG_BINARY);
00522
00523 if(data_size > 0)
00524 {
00525
00526 stlsoft_ns_qual(auto_buffer)<ws_byte_t, processheap_allocator<ws_byte_t>, CCH_REG_API_AUTO_BUFFER> buffer(data_size);
00527
00528 data_size = buffer.size();
00529 res = traits_type::reg_query_value(m_hkey, m_name.c_str(), dw, buffer.data(), data_size);
00530
00531 if(res == ERROR_SUCCESS)
00532 {
00533 return blob_type(buffer.data(), buffer.size());
00534 }
00535 }
00536 }
00537
00538 return blob_type();
00539 }
00540
00541
00542
00543 template<ss_typename_param_k A>
00544 inline reg_blob<A>::reg_blob()
00545 : m_buffer(0)
00546 {}
00547
00548 template<ss_typename_param_k A>
00549 inline reg_blob<A>::reg_blob(ss_typename_type_k reg_blob<A>::value_type const *data, ss_typename_type_k reg_blob<A>::size_type n)
00550 : m_buffer(n)
00551 {
00552 winstl_ns_qual_std(copy)(data, data + m_buffer.size(), m_buffer.begin());
00553 }
00554
00555 template<ss_typename_param_k A>
00556 inline reg_blob<A>::reg_blob(ss_typename_type_k reg_blob<A>::class_type const &rhs)
00557 : m_buffer(rhs.size())
00558 {
00559 winstl_ns_qual_std(copy)(rhs.data(), rhs.data() + m_buffer.size(), m_buffer.begin());
00560 }
00561
00562 template<ss_typename_param_k A>
00563 inline reg_blob<A>::~reg_blob() winstl_throw_0()
00564 {}
00565
00566 template<ss_typename_param_k A>
00567 inline ss_typename_type_k reg_blob<A>::size_type reg_blob<A>::size() const
00568 {
00569 return m_buffer.size();
00570 }
00571
00572 template<ss_typename_param_k A>
00573 inline ss_typename_type_k reg_blob<A>::const_pointer reg_blob<A>::data() const
00574 {
00575 return m_buffer.data();
00576 }
00577
00578 template<ss_typename_param_k A>
00579 inline ss_typename_type_k reg_blob<A>::const_iterator reg_blob<A>::begin() const
00580 {
00581 return m_buffer.begin();
00582 }
00583
00584 template<ss_typename_param_k A>
00585 inline ss_typename_type_k reg_blob<A>::const_iterator reg_blob<A>::end() const
00586 {
00587 return m_buffer.end();
00588 }
00589
00590 #endif
00591
00592
00593
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 #endif
00607
00608
00609
00610
00611
00612