6 #ifndef FABCOIN_STREAMS_H 7 #define FABCOIN_STREAMS_H 25 template<
typename Stream>
34 OverrideStream(Stream* stream_,
int nType_,
int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
52 void write(
const char* pch,
size_t nSize)
54 stream->write(pch, nSize);
57 void read(
char* pch,
size_t nSize)
59 stream->read(pch, nSize);
87 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn) :
nType(nTypeIn),
nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
89 if(nPos > vchData.size())
96 template <
typename... Args>
97 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn, Args&&... args) :
CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
101 void write(
const char* pch,
size_t nSize)
103 assert(nPos <= vchData.size());
104 size_t nOverwrite =
std::min(nSize, vchData.size() - nPos);
106 memcpy(vchData.data() + nPos,
reinterpret_cast<const unsigned char*
>(pch), nOverwrite);
108 if (nOverwrite < nSize) {
109 vchData.insert(vchData.end(),
reinterpret_cast<const unsigned char*
>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
131 if(nPos > vchData.size())
132 vchData.resize(nPos);
169 Init(nTypeIn, nVersionIn);
172 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
174 Init(nTypeIn, nVersionIn);
177 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
179 Init(nTypeIn, nVersionIn);
182 CDataStream(
const vector_type& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
184 Init(nTypeIn, nVersionIn);
187 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
189 Init(nTypeIn, nVersionIn);
192 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
194 Init(nTypeIn, nVersionIn);
197 template <
typename... Args>
200 Init(nTypeIn, nVersionIn);
204 void Init(
int nTypeIn,
int nVersionIn)
208 nVersion = nVersionIn;
226 return (std::string(begin(), end()));
233 const_iterator
begin()
const {
return vch.begin() + nReadPos; }
234 iterator
begin() {
return vch.begin() + nReadPos; }
235 const_iterator
end()
const {
return vch.end(); }
236 iterator
end() {
return vch.end(); }
237 size_type
size()
const {
return vch.size() - nReadPos; }
238 bool empty()
const {
return vch.size() == nReadPos; }
239 void resize(size_type n, value_type
c=0) { vch.resize(n + nReadPos,
c); }
240 void reserve(size_type n) { vch.reserve(n + nReadPos); }
241 const_reference
operator[](size_type pos)
const {
return vch[pos + nReadPos]; }
242 reference
operator[](size_type pos) {
return vch[pos + nReadPos]; }
243 void clear() { vch.clear(); nReadPos = 0; }
244 iterator
insert(iterator it,
const char&
x=
char()) {
return vch.insert(it,
x); }
245 void insert(iterator it, size_type n,
const char&
x) { vch.insert(it, n, x); }
246 value_type*
data() {
return vch.data() + nReadPos; }
247 const value_type*
data()
const {
return vch.data() + nReadPos; }
249 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
251 if (last == first)
return;
253 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
256 nReadPos -= (last - first);
257 memcpy(&vch[nReadPos], &first[0], last - first);
260 vch.insert(it, first, last);
263 void insert(iterator it,
const char* first,
const char* last)
265 if (last == first)
return;
267 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
270 nReadPos -= (last - first);
271 memcpy(&vch[nReadPos], &first[0], last - first);
274 vch.insert(it, first, last);
279 if (it == vch.begin() + nReadPos)
282 if (++nReadPos >= vch.size())
286 return vch.erase(vch.begin(), vch.end());
288 return vch.begin() + nReadPos;
291 return vch.erase(it);
294 iterator
erase(iterator first, iterator last)
296 if (first == vch.begin() + nReadPos)
299 if (last == vch.end())
302 return vch.erase(vch.begin(), vch.end());
306 nReadPos = (last - vch.begin());
311 return vch.erase(first, last);
316 vch.erase(vch.begin(), vch.begin() + nReadPos);
342 void read(
char* pch,
size_t nSize)
344 if (nSize == 0)
return;
347 unsigned int nReadPosNext = nReadPos + nSize;
348 if (nReadPosNext >= vch.size())
350 if (nReadPosNext > vch.size())
352 throw std::ios_base::failure(
"CDataStream::read(): end of data");
354 memcpy(pch, &vch[nReadPos], nSize);
359 memcpy(pch, &vch[nReadPos], nSize);
360 nReadPos = nReadPosNext;
367 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
369 unsigned int nReadPosNext = nReadPos + nSize;
370 if (nReadPosNext >= vch.size())
372 if (nReadPosNext > vch.size())
373 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
378 nReadPos = nReadPosNext;
381 void write(
const char* pch,
size_t nSize)
384 vch.insert(vch.end(), pch, pch + nSize);
387 template<
typename Stream>
392 s.write((
char*)vch.data(), vch.size() *
sizeof(value_type));
412 d.insert(d.end(), begin(), end());
421 void Xor(
const std::vector<unsigned char>& key)
423 if (key.size() == 0) {
427 for (size_type i = 0, j = 0; i !=
size(); i++) {
468 CAutoFile(FILE* filenew,
int nTypeIn,
int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
490 FILE*
release() { FILE* ret = file; file =
nullptr;
return ret; }
496 FILE*
Get()
const {
return file; }
500 bool IsNull()
const {
return (file ==
nullptr); }
508 void read(
char* pch,
size_t nSize)
511 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
512 if (fread(pch, 1, nSize, file) != nSize)
513 throw std::ios_base::failure(feof(file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
519 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
520 unsigned char data[4096];
522 size_t nNow = std::min<size_t>(nSize,
sizeof(
data));
523 if (fread(data, 1, nNow, file) != nNow)
524 throw std::ios_base::failure(feof(file) ?
"CAutoFile::ignore: end of file" :
"CAutoFile::read: fread failed");
529 void write(
const char* pch,
size_t nSize)
532 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
533 if (fwrite(pch, 1, nSize, file) != nSize)
534 throw std::ios_base::failure(
"CAutoFile::write: write failed");
542 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
552 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
584 unsigned int pos = nSrcPos % vchBuf.size();
585 unsigned int readNow = vchBuf.size() - pos;
586 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
587 if (nAvail < readNow)
591 size_t nBytes = fread((
void*)&vchBuf[pos], 1, readNow, src);
593 throw std::ios_base::failure(feof(src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
601 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
602 nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
625 return nReadPos == nSrcPos && feof(src);
629 void read(
char *pch,
size_t nSize) {
630 if (nSize + nReadPos > nReadLimit)
631 throw std::ios_base::failure(
"Read attempted past buffer limit");
632 if (nSize + nRewind > vchBuf.size())
633 throw std::ios_base::failure(
"Read larger than buffer size");
635 if (nReadPos == nSrcPos)
637 unsigned int pos = nReadPos % vchBuf.size();
639 if (nNow + pos > vchBuf.size())
640 nNow = vchBuf.size() - pos;
641 if (nNow + nReadPos > nSrcPos)
642 nNow = nSrcPos - nReadPos;
643 memcpy(pch, &vchBuf[pos], nNow);
658 if (nReadPos + nRewind < nSrcPos) {
659 nReadPos = nSrcPos - nRewind;
661 }
else if (nReadPos > nSrcPos) {
670 long nLongPos = nPos;
671 if (nPos != (uint64_t)nLongPos)
673 if (fseek(src, nLongPos, SEEK_SET))
675 nLongPos = ftell(src);
700 if (nReadPos == nSrcPos)
702 if (vchBuf[nReadPos % vchBuf.size()] ==
ch)
709 #endif // FABCOIN_STREAMS_H
CVectorWriter & operator<<(const T &obj)
void Init(int nTypeIn, int nVersionIn)
CSerializeData vector_type
void ignore(size_t nSize)
vector_type::iterator iterator
vector_type::allocator_type allocator_type
std::vector< unsigned char > & vchData
const_iterator begin() const
CAutoFile & operator>>(T &obj)
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&...args)
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
void write(const char *pch, size_t nSize)
vector_type::size_type size_type
void resize(size_type n, value_type c=0)
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
vector_type::reference reference
vector_type::value_type value_type
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
OverrideStream< Stream > & operator<<(const T &obj)
assert(len-trim+(2 *lenIndices)<=WIDTH)
Double ended buffer combining vector and stream-like interfaces.
void write(const char *pch, size_t nSize)
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
const value_type * data() const
vector_type::reverse_iterator reverse_iterator
iterator erase(iterator it)
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
CDataStream(int nTypeIn, int nVersionIn)
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
FILE * release()
Get wrapped FILE* with transfer of ownership.
void write(const char *pch, size_t nSize)
void Serialize(Stream &s, char a)
void read(char *pch, size_t nSize)
vector_type::const_reference const_reference
void read(char *pch, size_t nSize)
bool SetLimit(uint64_t nPos=(uint64_t)(-1))
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
vector_type::const_iterator const_iterator
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
CDataStream(int nTypeIn, int nVersionIn, Args &&...args)
CDataStream & operator+=(const CDataStream &b)
CDataStream & operator>>(T &obj)
void GetAndClear(CSerializeData &d)
void write(const char *pch, size_t nSize)
void read(char *pch, size_t nSize)
void read(char *pch, size_t nSize)
reference operator[](size_type pos)
bool SetPos(uint64_t nPos)
OverrideStream(Stream *stream_, int nType_, int nVersion_)
void SerializeMany(Stream &s)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
iterator insert(iterator it, const char &x=char())
void Serialize(Stream &s) const
CDataStream & operator<<(const T &obj)
void reserve(size_type n)
uint8_t const size_t const size
void * memcpy(void *a, const void *b, size_t c)
void insert(iterator it, size_type n, const char &x)
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
void Unserialize(Stream &s, char &a)
vector_type::difference_type difference_type
const_reference operator[](size_type pos) const
void insert(iterator it, const char *first, const char *last)
std::vector< char, zero_after_free_allocator< char > > CSerializeData
std::vector< char > vchBuf
iterator erase(iterator first, iterator last)
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
CBufferedFile & operator>>(T &obj)
CAutoFile & operator<<(const T &obj)
Non-refcounted RAII wrapper for FILE*.
const_iterator end() const
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
OverrideStream< Stream > & operator>>(T &obj)