/tests/io.h

https://gitlab.com/nexxuz/cryptonote · C Header · 107 lines · 92 code · 12 blank · 3 comment · 31 complexity · 896ef8e3e4f67ff7681d9ca80101e406 MD5 · raw file

  1. // Copyright (c) 2011-2014 The Cryptonote developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <cstddef>
  5. #include <ios>
  6. #include <iostream>
  7. #include <type_traits>
  8. #include <vector>
  9. inline bool hexdecode(const char *from, std::size_t length, void *to) {
  10. std::size_t i;
  11. for (i = 0; i < length; i++) {
  12. int v = 0;
  13. if (from[2 * i] >= '0' && from[2 * i] <= '9') {
  14. v = from[2 * i] - '0';
  15. } else if (from[2 * i] >= 'a' && from[2 * i] <= 'f') {
  16. v = from[2 * i] - 'a' + 10;
  17. } else {
  18. return false;
  19. }
  20. v <<= 4;
  21. if (from[2 * i + 1] >= '0' && from[2 * i + 1] <= '9') {
  22. v |= from[2 * i + 1] - '0';
  23. } else if (from[2 * i + 1] >= 'a' && from[2 * i + 1] <= 'f') {
  24. v |= from[2 * i + 1] - 'a' + 10;
  25. } else {
  26. return false;
  27. }
  28. *(reinterpret_cast<unsigned char *>(to) + i) = v;
  29. }
  30. return true;
  31. }
  32. inline void get(std::istream &input, bool &res) {
  33. std::string sres;
  34. input >> sres;
  35. if (sres == "false") {
  36. res = false;
  37. } else if (sres == "true") {
  38. res = true;
  39. } else {
  40. input.setstate(std::ios_base::failbit);
  41. }
  42. }
  43. template<typename T>
  44. typename std::enable_if<std::is_integral<T>::value, void>::type
  45. get(std::istream &input, T &res) {
  46. input >> res;
  47. }
  48. inline void getvar(std::istream &input, std::size_t length, void *res) {
  49. std::string sres;
  50. input >> sres;
  51. if (sres.length() != 2 * length || !hexdecode(sres.data(), length, res)) {
  52. input.setstate(std::ios_base::failbit);
  53. }
  54. }
  55. template<typename T>
  56. typename std::enable_if<std::is_standard_layout<T>::value && !std::is_scalar<T>::value, void>::type
  57. get(std::istream &input, T &res) {
  58. getvar(input, sizeof(T), &res);
  59. }
  60. inline void get(std::istream &input, std::vector<char> &res) {
  61. std::string sres;
  62. input >> sres;
  63. if (sres == "x") {
  64. res.clear();
  65. } else if (sres.length() % 2 != 0) {
  66. input.setstate(std::ios_base::failbit);
  67. } else {
  68. std::size_t length = sres.length() / 2;
  69. res.resize(length);
  70. if (!hexdecode(sres.data(), length, res.data())) {
  71. input.setstate(std::ios_base::failbit);
  72. }
  73. }
  74. }
  75. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  76. template<typename T, typename... TT>
  77. typename std::enable_if<(sizeof...(TT) > 0), void>::type
  78. get(std::istream &input, T &res, TT &... resres) {
  79. get(input, res);
  80. get(input, resres...);
  81. }
  82. #else
  83. #include <boost/preprocessor/cat.hpp>
  84. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  85. #include <boost/preprocessor/repetition/enum_params.hpp>
  86. #include <boost/preprocessor/repetition/repeat.hpp>
  87. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  88. #define NESTED_GET(z, n, data) get(input, BOOST_PP_CAT(res, n));
  89. #define GET(z, n, data) \
  90. template<BOOST_PP_ENUM_PARAMS(n, typename T)> \
  91. void get(std::istream &input, BOOST_PP_ENUM_BINARY_PARAMS(n, T, &res)) { \
  92. BOOST_PP_REPEAT(n, NESTED_GET, ~) \
  93. }
  94. BOOST_PP_REPEAT_FROM_TO(2, 5, GET, ~)
  95. #endif