uuid_io.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Boost uuid_io.hpp header file ----------------------------------------------//
  2. // Copyright 2009 Andy Tompkins.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // https://www.boost.org/LICENSE_1_0.txt)
  6. // Revision History
  7. // 20 Mar 2009 - Initial Revision
  8. // 28 Nov 2009 - disabled deprecated warnings for MSVC
  9. #ifndef BOOST_UUID_IO_HPP
  10. #define BOOST_UUID_IO_HPP
  11. #include <boost/uuid/uuid.hpp>
  12. #include <ios>
  13. #include <ostream>
  14. #include <istream>
  15. #include <boost/io/ios_state.hpp>
  16. #include <locale>
  17. #include <algorithm>
  18. #if defined(_MSC_VER)
  19. #pragma warning(push) // Save warning settings.
  20. #pragma warning(disable : 4996) // Disable deprecated std::ctype<char>::widen, std::copy
  21. #endif
  22. namespace boost {
  23. namespace uuids {
  24. template <typename ch, typename char_traits>
  25. std::basic_ostream<ch, char_traits>& operator<<(std::basic_ostream<ch, char_traits> &os, uuid const& u)
  26. {
  27. io::ios_flags_saver flags_saver(os);
  28. io::basic_ios_fill_saver<ch, char_traits> fill_saver(os);
  29. const typename std::basic_ostream<ch, char_traits>::sentry ok(os);
  30. if (ok) {
  31. const std::streamsize width = os.width(0);
  32. const std::streamsize uuid_width = 36;
  33. const std::ios_base::fmtflags flags = os.flags();
  34. const typename std::basic_ios<ch, char_traits>::char_type fill = os.fill();
  35. if (flags & (std::ios_base::right | std::ios_base::internal)) {
  36. for (std::streamsize i=uuid_width; i<width; i++) {
  37. os << fill;
  38. }
  39. }
  40. os << std::hex << std::right;
  41. os.fill(os.widen('0'));
  42. std::size_t i=0;
  43. for (uuid::const_iterator i_data = u.begin(); i_data!=u.end(); ++i_data, ++i) {
  44. os.width(2);
  45. os << static_cast<unsigned int>(*i_data);
  46. if (i == 3 || i == 5 || i == 7 || i == 9) {
  47. os << os.widen('-');
  48. }
  49. }
  50. if (flags & std::ios_base::left) {
  51. for (std::streamsize s=uuid_width; s<width; s++) {
  52. os << fill;
  53. }
  54. }
  55. os.width(0); //used the width so reset it
  56. }
  57. return os;
  58. }
  59. template <typename ch, typename char_traits>
  60. std::basic_istream<ch, char_traits>& operator>>(std::basic_istream<ch, char_traits> &is, uuid &u)
  61. {
  62. const typename std::basic_istream<ch, char_traits>::sentry ok(is);
  63. if (ok) {
  64. unsigned char data[16];
  65. typedef std::ctype<ch> ctype_t;
  66. ctype_t const& ctype = std::use_facet<ctype_t>(is.getloc());
  67. ch xdigits[16];
  68. {
  69. char szdigits[] = "0123456789ABCDEF";
  70. ctype.widen(szdigits, szdigits+16, xdigits);
  71. }
  72. ch*const xdigits_end = xdigits+16;
  73. ch c;
  74. for (std::size_t i=0; i<u.size() && is; ++i) {
  75. is >> c;
  76. c = ctype.toupper(c);
  77. ch* f = std::find(xdigits, xdigits_end, c);
  78. if (f == xdigits_end) {
  79. is.setstate(std::ios_base::failbit);
  80. break;
  81. }
  82. unsigned char byte = static_cast<unsigned char>(std::distance(&xdigits[0], f));
  83. is >> c;
  84. c = ctype.toupper(c);
  85. f = std::find(xdigits, xdigits_end, c);
  86. if (f == xdigits_end) {
  87. is.setstate(std::ios_base::failbit);
  88. break;
  89. }
  90. byte <<= 4;
  91. byte |= static_cast<unsigned char>(std::distance(&xdigits[0], f));
  92. data[i] = byte;
  93. if (is) {
  94. if (i == 3 || i == 5 || i == 7 || i == 9) {
  95. is >> c;
  96. if (c != is.widen('-')) is.setstate(std::ios_base::failbit);
  97. }
  98. }
  99. }
  100. if (is) {
  101. std::copy(data, data+16, u.begin());
  102. }
  103. }
  104. return is;
  105. }
  106. namespace detail {
  107. inline char to_char(size_t i) {
  108. if (i <= 9) {
  109. return static_cast<char>('0' + i);
  110. } else {
  111. return static_cast<char>('a' + (i-10));
  112. }
  113. }
  114. inline wchar_t to_wchar(size_t i) {
  115. if (i <= 9) {
  116. return static_cast<wchar_t>(L'0' + i);
  117. } else {
  118. return static_cast<wchar_t>(L'a' + (i-10));
  119. }
  120. }
  121. } // namespace detail
  122. inline std::string to_string(uuid const& u)
  123. {
  124. std::string result;
  125. result.reserve(36);
  126. std::size_t i=0;
  127. for (uuid::const_iterator it_data = u.begin(); it_data!=u.end(); ++it_data, ++i) {
  128. const size_t hi = ((*it_data) >> 4) & 0x0F;
  129. result += detail::to_char(hi);
  130. const size_t lo = (*it_data) & 0x0F;
  131. result += detail::to_char(lo);
  132. if (i == 3 || i == 5 || i == 7 || i == 9) {
  133. result += '-';
  134. }
  135. }
  136. return result;
  137. }
  138. #ifndef BOOST_NO_STD_WSTRING
  139. inline std::wstring to_wstring(uuid const& u)
  140. {
  141. std::wstring result;
  142. result.reserve(36);
  143. std::size_t i=0;
  144. for (uuid::const_iterator it_data = u.begin(); it_data!=u.end(); ++it_data, ++i) {
  145. const size_t hi = ((*it_data) >> 4) & 0x0F;
  146. result += detail::to_wchar(hi);
  147. const size_t lo = (*it_data) & 0x0F;
  148. result += detail::to_wchar(lo);
  149. if (i == 3 || i == 5 || i == 7 || i == 9) {
  150. result += L'-';
  151. }
  152. }
  153. return result;
  154. }
  155. #endif
  156. }} //namespace boost::uuids
  157. #if defined(_MSC_VER)
  158. #pragma warning(pop) // Restore warnings to previous state.
  159. #endif
  160. #endif // BOOST_UUID_IO_HPP