-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblStreamReadWrite.hpp
195 lines (139 loc) · 5.42 KB
/
blStreamReadWrite.hpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef BL_STREAMREADWRITE_HPP
#define BL_STREAMREADWRITE_HPP
//-------------------------------------------------------------------
// FILE: blReadWrite.hpp
//
//
//
// PURPOSE: Generic functions that simplify reading from a
// stream into a variable or a contiguous buffer
// and writing to a stream from a variable or a
// contiguous buffer
//
// -- All functions/algorithms are defined within
// the "blAlgorithmsLIB" namespace
//
//
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.com
//
//
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blAlgorithmsLIB namespace
//-------------------------------------------------------------------
namespace blAlgorithmsLIB
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions can be used to:
// -- Read from a stream into a buffer/variable
// of any type
// -- Write from a buffer/variable of
// any type into a stream
//
// It assumes the following:
//
// -- stream defines the functions: "read" and "write" that look
// like the iostream functions
//
// -- buffer defines data() function that returns pointer
// to its first element
//
// -- buffer defines size() function that returns
// the size/length of the buffer
//-------------------------------------------------------------------
template<typename blInputStreamType,
typename blValueType>
inline blInputStreamType& readValue(blInputStreamType& is,blValueType& value)
{
is.read(reinterpret_cast<char*>(&value),sizeof(value));
return is;
}
template<typename blInputStreamType,
typename blBufferType>
inline blInputStreamType& readBuffer(blInputStreamType& is,blBufferType& buffer)
{
is.read(reinterpret_cast<char*>(buffer.data()),sizeof(buffer.data()[0])*buffer.size());
return is;
}
template<typename blInputStreamType,
typename blBufferType>
inline blInputStreamType& readBuffer(blInputStreamType& is,blBufferType& buffer,const int& bufferLength)
{
is.read(reinterpret_cast<char*>(buffer.data()),sizeof(buffer.data()[0])*bufferLength);
return is;
}
template<typename blOutputStreamType,
typename blValueType>
inline blOutputStreamType& writeValue(blOutputStreamType& os,const blValueType& value)
{
os.write(reinterpret_cast<const char*>(&value),sizeof(value));
return os;
}
template<typename blOutputStreamType,
typename blBufferType>
inline blOutputStreamType& writeBuffer(blOutputStreamType& os,const blBufferType& buffer)
{
os.write(reinterpret_cast<const char*>(buffer.data()),sizeof(buffer.data()[0])*buffer.size());
return os;
}
template<typename blOutputStreamType,
typename blBufferType>
inline blOutputStreamType& writeBuffer(blOutputStreamType& os,const blBufferType& buffer,const int& bufferLength)
{
os.write(reinterpret_cast<const char*>(buffer.data()),sizeof(buffer.data()[0])*bufferLength);
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function does buffered reading/writing from an input
// stream to an output stream.
// The function keeps reading/writing until no bytes are read
//
// -- The function assumes the following:
//
// -- The input stream defines the functions "read" and "gcount"
// -- The output stream defines the "write" function
// -- The buffer defines the "operator[]" and "size" functions
//-------------------------------------------------------------------
template<typename blInputStreamType,
typename blOutputStreamType,
typename blBufferType>
inline int readFromStreamWriteToStream(blInputStreamType& inputStream,
blOutputStreamType& outputStream,
blBufferType& buffer)
{
// Variable used to know how
// many bytes were read from
// the input stream during
// the last read
int numberOfBytesRead = 1;
// Total number of bytes read
int totalNumberOfBytesRead = 0;
// Keep reading/writing until
// no more bytes are read
if(buffer.size() > 0)
{
while(numberOfBytesRead > 0)
{
inputStream.read(reinterpret_cast<char*>(&buffer[0]),sizeof(buffer[0]) * buffer.size());
numberOfBytesRead = inputStream.gcount();
totalNumberOfBytesRead += numberOfBytesRead;
outputStream.write(reinterpret_cast<char*>(&buffer[0]),numberOfBytesRead);
}
}
// We're done
return totalNumberOfBytesRead;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of namespace
}
//-------------------------------------------------------------------
#endif // BL_STREAMREADWRITE_HPP