Electroneum
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#include <cstddef>
33#include <ios>
34#include <iostream>
35#include <type_traits>
36#include <vector>
37
38inline bool hexdecode(const char *from, std::size_t length, void *to) {
39 std::size_t i;
40 for (i = 0; i < length; i++) {
41 int v = 0;
42 if (from[2 * i] >= '0' && from[2 * i] <= '9') {
43 v = from[2 * i] - '0';
44 } else if (from[2 * i] >= 'a' && from[2 * i] <= 'f') {
45 v = from[2 * i] - 'a' + 10;
46 } else {
47 return false;
48 }
49 v <<= 4;
50 if (from[2 * i + 1] >= '0' && from[2 * i + 1] <= '9') {
51 v |= from[2 * i + 1] - '0';
52 } else if (from[2 * i + 1] >= 'a' && from[2 * i + 1] <= 'f') {
53 v |= from[2 * i + 1] - 'a' + 10;
54 } else {
55 return false;
56 }
57 *(reinterpret_cast<unsigned char *>(to) + i) = v;
58 }
59 return true;
60}
61
62inline void get(std::istream &input, bool &res) {
63 std::string sres;
64 input >> sres;
65 if (sres == "false") {
66 res = false;
67 } else if (sres == "true") {
68 res = true;
69 } else {
70 input.setstate(std::ios_base::failbit);
71 }
72}
73
74template<typename T>
75typename std::enable_if<std::is_integral<T>::value, void>::type
76get(std::istream &input, T &res) {
77 input >> res;
78}
79
80inline void getvar(std::istream &input, std::size_t length, void *res) {
81 std::string sres;
82 input >> sres;
83 if (sres.length() != 2 * length || !hexdecode(sres.data(), length, res)) {
84 input.setstate(std::ios_base::failbit);
85 }
86}
87
88template<typename T>
89typename std::enable_if<std::is_standard_layout<T>::value && !std::is_scalar<T>::value, void>::type
90get(std::istream &input, T &res) {
91 getvar(input, sizeof(T), &res);
92}
93
94inline void get(std::istream &input, std::vector<char> &res) {
95 std::string sres;
96 input >> sres;
97 if (sres == "x") {
98 res.clear();
99 } else if (sres.length() % 2 != 0) {
100 input.setstate(std::ios_base::failbit);
101 } else {
102 std::size_t length = sres.length() / 2;
103 res.resize(length);
104 if (!hexdecode(sres.data(), length, res.data())) {
105 input.setstate(std::ios_base::failbit);
106 }
107 }
108}
109
110#if !defined(_MSC_VER) || _MSC_VER >= 1800
111
112template<typename T, typename... TT>
113typename std::enable_if<(sizeof...(TT) > 0), void>::type
114get(std::istream &input, T &res, TT &... resres) {
115 get(input, res);
116 get(input, resres...);
117}
118
119#else
120#include <boost/preprocessor/cat.hpp>
121#include <boost/preprocessor/repetition/enum_binary_params.hpp>
122#include <boost/preprocessor/repetition/enum_params.hpp>
123#include <boost/preprocessor/repetition/repeat.hpp>
124#include <boost/preprocessor/repetition/repeat_from_to.hpp>
125
126#define NESTED_GET(z, n, data) get(input, BOOST_PP_CAT(res, n));
127#define GET(z, n, data) \
128template<BOOST_PP_ENUM_PARAMS(n, typename T)> \
129void get(std::istream &input, BOOST_PP_ENUM_BINARY_PARAMS(n, T, &res)) { \
130 BOOST_PP_REPEAT(n, NESTED_GET, ~) \
131}
132BOOST_PP_REPEAT_FROM_TO(2, 5, GET, ~)
133
134#endif
const char * res
void getvar(std::istream &input, std::size_t length, void *res)
Definition io.h:80
bool hexdecode(const char *from, std::size_t length, void *to)
Definition io.h:38
void get(std::istream &input, bool &res)
Definition io.h:62
#define T(x)