/Src/Dependencies/Boost/libs/property_tree/test/test_registry_parser.cpp

http://hadesmem.googlecode.com/ · C++ · 110 lines · 71 code · 19 blank · 20 comment · 4 complexity · eadee5cc3a435a3d44006e416f6ac474 MD5 · raw file

  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #include "test_utils.hpp"
  11. // Only test registry parser if we have windows platform
  12. #ifdef BOOST_WINDOWS
  13. #include <boost/property_tree/registry_parser.hpp>
  14. #include <boost/property_tree/info_parser.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // Test data
  17. const char *data_1 =
  18. "root\n"
  19. "{\n"
  20. " subkey1 \"default value 1\"\n"
  21. " subkey2 \"default value 2\"\n"
  22. " \\\\values\n"
  23. " {\n"
  24. " REG_NONE \"\"\n"
  25. " REG_BINARY \"de ad be ef\"\n"
  26. " REG_DWORD 1234567890\n"
  27. " REG_QWORD 12345678901234567890\n"
  28. " REG_SZ \"some text\"\n"
  29. " REG_EXPAND_SZ \"some other text\"\n"
  30. " }\n"
  31. " \\\\types\n"
  32. " {\n"
  33. " REG_NONE 0\n"
  34. " REG_BINARY 3\n"
  35. " REG_DWORD 4\n"
  36. " REG_QWORD 11\n"
  37. " REG_SZ 1\n"
  38. " REG_EXPAND_SZ 2\n"
  39. " }\n"
  40. "}\n";
  41. template<class Ptree>
  42. void test_registry_parser()
  43. {
  44. using namespace boost::property_tree;
  45. typedef typename Ptree::key_type::value_type Ch;
  46. typedef std::basic_string<Ch> Str;
  47. // Delete test registry key
  48. RegDeleteKeyA(HKEY_CURRENT_USER, "boost ptree test");
  49. // Get test ptree
  50. Ptree pt;
  51. std::basic_stringstream<Ch> stream(detail::widen<Ch>(data_1));
  52. read_info(stream, pt);
  53. try
  54. {
  55. // Write to registry, read back and compare contents
  56. Ptree pt2;
  57. write_registry(HKEY_CURRENT_USER, detail::widen<Ch>("boost ptree test"), pt);
  58. read_registry(HKEY_CURRENT_USER, detail::widen<Ch>("boost ptree test"), pt2);
  59. BOOST_CHECK(pt == pt2);
  60. // Test binary translation
  61. Str s = pt2.template get<Str>(detail::widen<Ch>("root.\\values.REG_BINARY"));
  62. std::vector<BYTE> bin = registry_parser::translate(REG_BINARY, s);
  63. BOOST_REQUIRE(bin.size() == 4);
  64. BOOST_CHECK(*reinterpret_cast<DWORD *>(&bin.front()) == 0xEFBEADDE);
  65. Str s2 = registry_parser::translate<Ch>(REG_BINARY, bin);
  66. BOOST_CHECK(s == s2);
  67. }
  68. catch (std::exception &e)
  69. {
  70. BOOST_ERROR(e.what());
  71. }
  72. // Delete test registry key
  73. RegDeleteKeyA(HKEY_CURRENT_USER, "boost ptree test");
  74. }
  75. int test_main(int argc, char *argv[])
  76. {
  77. using namespace boost::property_tree;
  78. test_registry_parser<ptree>();
  79. //test_registry_parser<iptree>();
  80. #ifndef BOOST_NO_CWCHAR
  81. //test_registry_parser<wptree>();
  82. //test_registry_parser<wiptree>();
  83. #endif
  84. return 0;
  85. }
  86. #else
  87. int test_main(int argc, char *argv[])
  88. {
  89. return 0;
  90. }
  91. #endif