7 #ifndef RAVEN_STREAMS_H 8 #define RAVEN_STREAMS_H 26 template<
typename Stream>
35 OverrideStream(Stream* stream_,
int nType_,
int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
53 void write(
const char* pch,
size_t nSize)
55 stream->write(pch, nSize);
58 void read(
char* pch,
size_t nSize)
60 stream->read(pch, nSize);
88 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn) :
nType(nTypeIn),
nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
90 if(nPos > vchData.size())
97 template <
typename... Args>
98 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn, Args&&... args) :
CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
102 void write(
const char* pch,
size_t nSize)
104 assert(nPos <= vchData.size());
105 size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
107 memcpy(vchData.data() + nPos,
reinterpret_cast<const unsigned char*
>(pch), nOverwrite);
109 if (nOverwrite < nSize) {
110 vchData.insert(vchData.end(),
reinterpret_cast<const unsigned char*
>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
132 if(nPos > vchData.size())
133 vchData.resize(nPos);
170 Init(nTypeIn, nVersionIn);
173 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
175 Init(nTypeIn, nVersionIn);
178 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
180 Init(nTypeIn, nVersionIn);
183 CDataStream(
const vector_type& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
185 Init(nTypeIn, nVersionIn);
188 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
190 Init(nTypeIn, nVersionIn);
193 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
195 Init(nTypeIn, nVersionIn);
198 template <
typename... Args>
201 Init(nTypeIn, nVersionIn);
205 void Init(
int nTypeIn,
int nVersionIn)
209 nVersion = nVersionIn;
227 return (std::string(begin(), end()));
234 const_iterator
begin()
const {
return vch.begin() + nReadPos; }
235 iterator
begin() {
return vch.begin() + nReadPos; }
236 const_iterator
end()
const {
return vch.end(); }
237 iterator
end() {
return vch.end(); }
238 size_type
size()
const {
return vch.size() - nReadPos; }
239 bool empty()
const {
return vch.size() == nReadPos; }
240 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
241 void reserve(size_type n) { vch.reserve(n + nReadPos); }
242 const_reference
operator[](size_type pos)
const {
return vch[pos + nReadPos]; }
243 reference
operator[](size_type pos) {
return vch[pos + nReadPos]; }
244 void clear() { vch.clear(); nReadPos = 0; }
245 iterator
insert(iterator it,
const char& x=
char()) {
return vch.insert(it, x); }
246 void insert(iterator it, size_type n,
const char& x) { vch.insert(it, n, x); }
247 value_type*
data() {
return vch.data() + nReadPos; }
248 const value_type*
data()
const {
return vch.data() + nReadPos; }
250 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
252 if (last == first)
return;
253 assert(last - first > 0);
254 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
257 nReadPos -= (last - first);
258 memcpy(&vch[nReadPos], &first[0], last - first);
261 vch.insert(it, first, last);
264 void insert(iterator it,
const char* first,
const char* last)
266 if (last == first)
return;
267 assert(last - first > 0);
268 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
271 nReadPos -= (last - first);
272 memcpy(&vch[nReadPos], &first[0], last - first);
275 vch.insert(it, first, last);
280 if (it == vch.begin() + nReadPos)
283 if (++nReadPos >= vch.size())
287 return vch.erase(vch.begin(), vch.end());
289 return vch.begin() + nReadPos;
292 return vch.erase(it);
295 iterator
erase(iterator first, iterator last)
297 if (first == vch.begin() + nReadPos)
300 if (last == vch.end())
303 return vch.erase(vch.begin(), vch.end());
307 nReadPos = (last - vch.begin());
312 return vch.erase(first, last);
317 vch.erase(vch.begin(), vch.begin() + nReadPos);
334 bool eof()
const {
return size() == 0; }
343 void read(
char* pch,
size_t nSize)
345 if (nSize == 0)
return;
348 unsigned int nReadPosNext = nReadPos + nSize;
349 if (nReadPosNext >= vch.size())
351 if (nReadPosNext > vch.size())
353 throw std::ios_base::failure(
"CDataStream::read(): end of data");
355 memcpy(pch, &vch[nReadPos], nSize);
360 memcpy(pch, &vch[nReadPos], nSize);
361 nReadPos = nReadPosNext;
368 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
370 unsigned int nReadPosNext = nReadPos + nSize;
371 if (nReadPosNext >= vch.size())
373 if (nReadPosNext > vch.size())
374 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
379 nReadPos = nReadPosNext;
382 void write(
const char* pch,
size_t nSize)
385 vch.insert(vch.end(), pch, pch + nSize);
388 template<
typename Stream>
393 s.write((
char*)vch.data(), vch.size() *
sizeof(value_type));
413 d.insert(d.end(), begin(), end());
422 void Xor(
const std::vector<unsigned char>& key)
424 if (key.size() == 0) {
428 for (size_type i = 0, j = 0; i != size(); i++) {
465 CAutoFile(FILE* filenew,
int nTypeIn,
int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
491 FILE*
release() { FILE* ret = file; file =
nullptr;
return ret; }
497 FILE*
Get()
const {
return file; }
501 bool IsNull()
const {
return (file ==
nullptr); }
509 void read(
char* pch,
size_t nSize)
512 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
513 if (fread(pch, 1, nSize, file) != nSize)
514 throw std::ios_base::failure(feof(file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
520 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
521 unsigned char data[4096];
523 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
524 if (fread(data, 1, nNow, file) != nNow)
525 throw std::ios_base::failure(feof(file) ?
"CAutoFile::ignore: end of file" :
"CAutoFile::read: fread failed");
530 void write(
const char* pch,
size_t nSize)
533 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
534 if (fwrite(pch, 1, nSize, file) != nSize)
535 throw std::ios_base::failure(
"CAutoFile::write: write failed");
543 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
553 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
581 unsigned int pos = nSrcPos % vchBuf.size();
582 unsigned int readNow = vchBuf.size() - pos;
583 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
584 if (nAvail < readNow)
588 size_t nBytes = fread((
void*)&vchBuf[pos], 1, readNow, src);
590 throw std::ios_base::failure(feof(src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
598 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
599 nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
626 return nReadPos == nSrcPos && feof(src);
630 void read(
char *pch,
size_t nSize) {
631 if (nSize + nReadPos > nReadLimit)
632 throw std::ios_base::failure(
"Read attempted past buffer limit");
633 if (nSize + nRewind > vchBuf.size())
634 throw std::ios_base::failure(
"Read larger than buffer size");
636 if (nReadPos == nSrcPos)
638 unsigned int pos = nReadPos % vchBuf.size();
640 if (nNow + pos > vchBuf.size())
641 nNow = vchBuf.size() - pos;
642 if (nNow + nReadPos > nSrcPos)
643 nNow = nSrcPos - nReadPos;
644 memcpy(pch, &vchBuf[pos], nNow);
659 if (nReadPos + nRewind < nSrcPos) {
660 nReadPos = nSrcPos - nRewind;
662 }
else if (nReadPos > nSrcPos) {
671 long nLongPos = nPos;
672 if (nPos != (uint64_t)nLongPos)
674 if (fseek(src, nLongPos, SEEK_SET))
676 nLongPos = ftell(src);
701 if (nReadPos == nSrcPos)
703 if (vchBuf[nReadPos % vchBuf.size()] == ch)
710 #endif // RAVEN_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
CAutoFile & operator>>(T &obj)
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
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)
void Serialize(Stream &s) const
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)
Double ended buffer combining vector and stream-like interfaces.
void write(const char *pch, size_t nSize)
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
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)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
FILE * release()
Get wrapped FILE* with transfer of ownership.
void write(const char *pch, size_t nSize)
void Serialize(Stream &s, char a)
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
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(int nTypeIn, int nVersionIn, Args &&... args)
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)
const value_type * data() const
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
CDataStream & operator+=(const CDataStream &b)
CDataStream & operator>>(T &obj)
void GetAndClear(CSerializeData &d)
#define S(x0, x1, x2, x3, cb, r)
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)
const_reference operator[](size_type pos) const
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
const_iterator end() const
const_iterator begin() const
iterator insert(iterator it, const char &x=char())
CDataStream & operator<<(const T &obj)
void reserve(size_type n)
void * memcpy(void *a, const void *b, size_t c)
void insert(iterator it, size_type n, const char &x)
void Unserialize(Stream &s, char &a)
vector_type::difference_type difference_type
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...
CBufferedFile & operator>>(T &obj)
CAutoFile & operator<<(const T &obj)
Non-refcounted RAII wrapper for FILE*.
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
OverrideStream< Stream > & operator>>(T &obj)