1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
9 #include "allocators.h"
10 #include "serialize.h"
16 /** Double ended buffer combining vector and stream-like interfaces.
18 * >> and << read and write unformatted data using the above serialization templates.
19 * Fills with data in linear time; some stringstream implementations take N^2 time.
24 typedef CSerializeData vector_type;
26 unsigned int nReadPos;
33 typedef vector_type::allocator_type allocator_type;
34 typedef vector_type::size_type size_type;
35 typedef vector_type::difference_type difference_type;
36 typedef vector_type::reference reference;
37 typedef vector_type::const_reference const_reference;
38 typedef vector_type::value_type value_type;
39 typedef vector_type::iterator iterator;
40 typedef vector_type::const_iterator const_iterator;
41 typedef vector_type::reverse_iterator reverse_iterator;
43 explicit CDataStream(int nTypeIn, int nVersionIn);
45 CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn);
46 CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn);
47 CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn);
48 CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn);
49 CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn);
51 void Init(int nTypeIn, int nVersionIn);
53 CDataStream& operator+=(const CDataStream& b);
55 friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
62 std::string str() const;
67 const_iterator begin() const { return vch.begin() + nReadPos; }
68 iterator begin() { return vch.begin() + nReadPos; }
69 const_iterator end() const { return vch.end(); }
70 iterator end() { return vch.end(); }
71 size_type size() const { return vch.size() - nReadPos; }
72 bool empty() const { return vch.size() == nReadPos; }
73 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
74 void reserve(size_type n) { vch.reserve(n + nReadPos); }
75 const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
76 reference operator[](size_type pos) { return vch[pos + nReadPos]; }
77 void clear() { vch.clear(); nReadPos = 0; }
78 iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
79 void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
81 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
83 assert(last - first >= 0);
84 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
86 // special case for inserting at the front when there's room
87 nReadPos -= (last - first);
88 memcpy(&vch[nReadPos], &first[0], last - first);
91 vch.insert(it, first, last);
94 void insert(iterator it, const char* first, const char* last)
96 assert(last - first >= 0);
97 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
99 // special case for inserting at the front when there's room
100 nReadPos -= (unsigned int)(last - first);
101 memcpy(&vch[nReadPos], &first[0], last - first);
104 vch.insert(it, first, last);
107 iterator erase(iterator it)
109 if (it == vch.begin() + nReadPos)
111 // special case for erasing from the front
112 if (++nReadPos >= vch.size())
114 // whenever we reach the end, we take the opportunity to clear the buffer
116 return vch.erase(vch.begin(), vch.end());
118 return vch.begin() + nReadPos;
121 return vch.erase(it);
124 iterator erase(iterator first, iterator last)
126 if (first == vch.begin() + nReadPos)
128 // special case for erasing from the front
129 if (last == vch.end())
132 return vch.erase(vch.begin(), vch.end());
136 nReadPos = (unsigned int)(last - vch.begin());
141 return vch.erase(first, last);
144 inline void Compact()
146 vch.erase(vch.begin(), vch.begin() + nReadPos);
150 bool Rewind(size_type n);
155 void setstate(short bits, const char* psz);
157 bool eof() const { return size() == 0; }
158 bool fail() const { return (state & (std::ios::badbit | std::ios::failbit)) != 0; }
159 bool good() const { return !eof() && (state == 0); }
160 void clear(short n) { state = n; } // name conflict with vector clear()
161 short exceptions() { return exceptmask; }
162 short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
163 CDataStream* rdbuf() { return this; }
164 int in_avail() { return (int)(size()); }
166 void SetType(int n) { nType = n; }
167 int GetType() { return nType; }
168 void SetVersion(int n) { nVersion = n; }
169 int GetVersion() { return nVersion; }
170 void ReadVersion() { *this >> nVersion; }
171 void WriteVersion() { *this << nVersion; }
173 CDataStream& read(char* pch, int nSize);
174 CDataStream& ignore(int nSize);
175 CDataStream& write(const char* pch, int nSize);
177 template<typename Stream>
178 void Serialize(Stream& s, int nType, int nVersion) const
180 // Special case: stream << stream concatenates like stream += stream
182 s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
186 unsigned int GetSerializeSize(const T& obj)
188 // Tells the size of the object if serialized to this stream
189 return ::GetSerializeSize(obj, nType, nVersion);
193 CDataStream& operator<<(const T& obj)
195 // Serialize to this stream
196 ::Serialize(*this, obj, nType, nVersion);
201 CDataStream& operator>>(T& obj)
203 // Unserialize from this stream
204 ::Unserialize(*this, obj, nType, nVersion);
208 void GetAndClear(CSerializeData &data);
212 /** RAII wrapper for FILE*.
214 * Will automatically close the file when it goes out of scope if not null.
215 * If you're returning the file pointer, return file.release().
216 * If you need to close the file early, use file.fclose() instead of fclose(file).
228 CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn);
232 FILE* release() { FILE* ret = file; file = NULL; return ret; }
233 operator FILE*() { return file; }
234 FILE* operator->() { return file; }
235 FILE& operator*() { return *file; }
236 FILE** operator&() { return &file; }
237 FILE* operator=(FILE* pnew) { return file = pnew; }
238 bool operator!() { return (file == NULL); }
244 void setstate(short bits, const char* psz);
245 bool fail() const { return (state & (std::ios::badbit | std::ios::failbit)) != 0; }
246 bool good() const { return state == 0; }
247 void clear(short n = 0) { state = n; }
248 short exceptions() { return exceptmask; }
249 short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
251 void SetType(int n) { nType = n; }
252 int GetType() { return nType; }
253 void SetVersion(int n) { nVersion = n; }
254 int GetVersion() { return nVersion; }
255 void ReadVersion() { *this >> nVersion; }
256 void WriteVersion() { *this << nVersion; }
258 CAutoFile& read(char* pch, size_t nSize);
259 CAutoFile& write(const char* pch, size_t nSize);
262 unsigned int GetSerializeSize(const T& obj)
264 // Tells the size of the object if serialized to this stream
265 return ::GetSerializeSize(obj, nType, nVersion);
269 CAutoFile& operator<<(const T& obj)
271 // Serialize to this stream
273 throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
274 ::Serialize(*this, obj, nType, nVersion);
279 CAutoFile& operator>>(T& obj)
281 // Unserialize from this stream
283 throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
284 ::Unserialize(*this, obj, nType, nVersion);
289 /** Wrapper around a FILE* that implements a ring buffer to
290 * deserialize from. It guarantees the ability to rewind
291 * a given number of bytes. */
295 FILE *src; // source file
296 uint64_t nSrcPos; // how many bytes have been read from source
297 uint64_t nReadPos; // how many bytes have been read from this
298 uint64_t nReadLimit; // up to which position we're allowed to read
299 uint64_t nRewind; // how many bytes we guarantee to rewind
300 std::vector<char> vchBuf; // the buffer
306 void setstate(short bits, const char *psz);
308 // read data from the source to fill the buffer
315 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
316 src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0),
317 state(0), exceptmask(std::ios_base::badbit | std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) {
320 // check whether no error occurred
325 // check whether we're at the end of the source file
327 return nReadPos == nSrcPos && feof(src);
330 // read a number of bytes
331 CBufferedFile& read(char *pch, size_t nSize);
333 // return the current reading position
338 // rewind to a given reading position
339 bool SetPos(uint64_t nPos);
340 bool Seek(uint64_t nPos);
342 // prevent reading beyond a certain position
343 // no argument removes the limit
344 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max());
347 CBufferedFile& operator>>(T& obj) {
348 // Unserialize from this stream
349 ::Unserialize(*this, obj, nType, nVersion);
353 // search for a given byte in the stream, and remain positioned on it
354 void FindByte(char ch);
357 #endif // BITCOIN_STREAMS_H