1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIDUTIL_STREAM_IO_UTIL_H_
#define HIDUTIL_STREAM_IO_UTIL_H_
#include "HidLog.h"
#include <istream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <cassert>
namespace HidUtil {
template<typename CharT>
class charvectorbuf : public std::streambuf { // class name is consistent with std lib
static_assert(std::is_const<CharT>::value == false, "cannot use const type");
public:
// r/w buffer constructors
charvectorbuf(std::vector<CharT> &vec) {
init(vec.data(), vec.size());
}
charvectorbuf(CharT *begin, CharT *end) {
assert(end >= begin);
init(begin, end - begin);
}
charvectorbuf(CharT *begin, size_t size) {
init(begin, size);
}
// r/o buffer constructor
charvectorbuf(const std::vector<CharT> &vec) {
init(vec.data(), vec.size());
}
charvectorbuf(const CharT *begin, const CharT *end) {
assert(end >= begin);
init(begin, end - begin);
}
charvectorbuf(const CharT *begin, size_t size) {
init(begin, size);
}
protected:
virtual std::streampos seekpos(
std::streampos sp, std::ios_base::openmode which =
std::ios_base::in | std::ios_base::out) override {
return seekoff(std::streamoff(sp), std::ios_base::beg, which);
}
// this is needed to use ftell() on stream
virtual std::streampos seekoff(
std::streamoff off, std::ios_base::seekdir way,
std::ios_base::openmode which =
std::ios_base::in | std::ios_base::out) override {
// pptr() == nullptr: read-only
assert(pptr() == nullptr || egptr() - eback() == epptr() - pbase());
bool in = which & std::ios_base::in;
bool out = which & std::ios_base::out;
pos_type end = egptr() - eback();
if (!in && !out) {
return pos_type(-1);
}
if (in && out && way == std::ios_base::cur) {
return pos_type(-1);
}
off_type noff;
switch (way) {
case std::ios_base::beg:
noff = 0;
break;
case std::ios_base::cur:
if (in) {
noff = gptr() - eback();
} else {
noff = pptr() - pbase();
}
break;
case std::ios_base::end:
noff = end;
break;
default:
return pos_type(-1);
}
noff += off;
if (noff < 0 || noff > end) {
return pos_type(-1);
}
if (noff != 0 && ((in && gptr() == nullptr) || (out && pptr() == nullptr))) {
return pos_type(-1);
}
if (in) {
setg(eback(), eback() + noff, egptr());
}
if (out) {
setp(pbase(), epptr());
pbump(noff);
}
return pos_type(noff);
}
private:
// read only buffer init
void init(const CharT *base, size_t size) {
setg((char*)base, (char*)base, (char*)(base + size));
}
// read write buffer init
void init(CharT *base, size_t size) {
setg((char*)base, (char*)base, (char*)(base + size));
setp((char*)base, (char*)(base + size));
}
};
// dump binary values
template <class ForwardIterator>
void hexdumpToStream(std::ostream &os, const ForwardIterator &first, const ForwardIterator &last) {
static_assert(
std::is_convertible<
typename std::iterator_traits<ForwardIterator>::iterator_category,
std::forward_iterator_tag>::value
&& std::is_convertible<
typename std::iterator_traits<ForwardIterator>::value_type,
unsigned char>::value
&& sizeof(typename std::iterator_traits<ForwardIterator>::value_type)
== sizeof(unsigned char),
"Only accepts forward iterator of a type of size 1 "
"that can be convert to unsigned char.\n");
size_t c = 0;
std::ostringstream ss;
for (ForwardIterator i = first; i != last; ++i, ++c) {
unsigned char v = *i;
// formatting
switch (c & 0xf) {
case 0:
// address
os << ss.str() << LOG_ENDL;
ss.str("");
ss << std::hex;
ss << std::setfill('0') << std::setw(4) << c << ": ";
break;
case 8:
// space
ss << " ";
break;
}
ss << std::setfill('0') << std::setw(2)
<< static_cast<unsigned>(static_cast<unsigned char>(v)) << " ";
}
os << ss.str() << LOG_ENDL;
}
} //namespace HidUtil
#endif // HIDUTIL_STREAM_IO_UTIL_H_
|