PIVX Core  5.6.99
P2P Digital Currency
streams.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2017-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_STREAMS_H
8 #define PIVX_STREAMS_H
9 
10 #include "serialize.h"
12 
13 #include <algorithm>
14 #include <assert.h>
15 #include <ios>
16 #include <limits>
17 #include <map>
18 #include <set>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 template<typename Stream>
28 {
29  Stream* stream;
30 
31  const int nType;
32  const int nVersion;
33 
34 public:
35  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
36 
37  template<typename T>
39  {
40  // Serialize to this stream
41  ::Serialize(*this, obj);
42  return (*this);
43  }
44 
45  template<typename T>
47  {
48  // Unserialize from this stream
49  ::Unserialize(*this, obj);
50  return (*this);
51  }
52 
53  void write(const char* pch, size_t nSize)
54  {
55  stream->write(pch, nSize);
56  }
57 
58  void read(char* pch, size_t nSize)
59  {
60  stream->read(pch, nSize);
61  }
62 
63  int GetVersion() const { return nVersion; }
64  int GetType() const { return nType; }
65  size_t size() const { return stream->size(); }
66  void ignore(size_t size) { return stream->ignore(size); }
67 };
68 
74 template<typename SerializeType>
76 {
77 protected:
78  typedef SerializeType vector_type;
80  unsigned int nReadPos;
81  int nType;
82  int nVersion;
83 
84 public:
85  typedef typename vector_type::allocator_type allocator_type;
86  typedef typename vector_type::size_type size_type;
87  typedef typename vector_type::difference_type difference_type;
88  typedef typename vector_type::reference reference;
89  typedef typename vector_type::const_reference const_reference;
90  typedef typename vector_type::value_type value_type;
91  typedef typename vector_type::iterator iterator;
92  typedef typename vector_type::const_iterator const_iterator;
93  typedef typename vector_type::reverse_iterator reverse_iterator;
94 
95  explicit CBaseDataStream(int nTypeIn, int nVersionIn)
96  {
97  Init(nTypeIn, nVersionIn);
98  }
99 
100  CBaseDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
101  {
102  Init(nTypeIn, nVersionIn);
103  }
104 
105  CBaseDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
106  {
107  Init(nTypeIn, nVersionIn);
108  }
109 
110  CBaseDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
111  {
112  Init(nTypeIn, nVersionIn);
113  }
114 
115  CBaseDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
116  {
117  Init(nTypeIn, nVersionIn);
118  }
119 
120  CBaseDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
121  {
122  Init(nTypeIn, nVersionIn);
123  }
124 
125  template <typename... Args>
126  CBaseDataStream(int nTypeIn, int nVersionIn, Args&&... args)
127  {
128  Init(nTypeIn, nVersionIn);
129  ::SerializeMany(*this, std::forward<Args>(args)...);
130  }
131 
132  void Init(int nTypeIn, int nVersionIn)
133  {
134  nReadPos = 0;
135  nType = nTypeIn;
136  nVersion = nVersionIn;
137  }
138 
140  {
141  vch.insert(vch.end(), b.begin(), b.end());
142  return *this;
143  }
144 
146  {
147  CBaseDataStream ret = a;
148  ret += b;
149  return (ret);
150  }
151 
152  std::string str() const
153  {
154  return (std::string(begin(), end()));
155  }
156 
157 
158  //
159  // Vector subset
160  //
161  const_iterator begin() const { return vch.begin() + nReadPos; }
162  iterator begin() { return vch.begin() + nReadPos; }
163  const_iterator end() const { return vch.end(); }
164  iterator end() { return vch.end(); }
165  size_type size() const { return vch.size() - nReadPos; }
166  bool empty() const { return vch.size() == nReadPos; }
167  void resize(size_type n, value_type c = 0) { vch.resize(n + nReadPos, c); }
168  void reserve(size_type n) { vch.reserve(n + nReadPos); }
169  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
170  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
171  void clear()
172  {
173  vch.clear();
174  nReadPos = 0;
175  }
176  iterator insert(iterator it, const char x = char()) { return vch.insert(it, x); }
177  void insert(iterator it, size_type n, const char x) { vch.insert(it, n, x); }
178  value_type* data() { return vch.data() + nReadPos; }
179  const value_type* data() const { return vch.data() + nReadPos; }
180 
181  void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
182  {
183  if (last == first) return;
184  assert(last - first > 0);
185  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos) {
186  // special case for inserting at the front when there's room
187  nReadPos -= (last - first);
188  memcpy(&vch[nReadPos], &first[0], last - first);
189  } else
190  vch.insert(it, first, last);
191  }
192 
193  void insert(iterator it, const char* first, const char* last)
194  {
195  if (last == first) return;
196  assert(last - first > 0);
197  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos) {
198  // special case for inserting at the front when there's room
199  nReadPos -= (last - first);
200  memcpy(&vch[nReadPos], &first[0], last - first);
201  } else
202  vch.insert(it, first, last);
203  }
204 
206  {
207  if (it == vch.begin() + nReadPos) {
208  // special case for erasing from the front
209  if (++nReadPos >= vch.size()) {
210  // whenever we reach the end, we take the opportunity to clear the buffer
211  nReadPos = 0;
212  return vch.erase(vch.begin(), vch.end());
213  }
214  return vch.begin() + nReadPos;
215  } else
216  return vch.erase(it);
217  }
218 
220  {
221  if (first == vch.begin() + nReadPos) {
222  // special case for erasing from the front
223  if (last == vch.end()) {
224  nReadPos = 0;
225  return vch.erase(vch.begin(), vch.end());
226  } else {
227  nReadPos = (last - vch.begin());
228  return last;
229  }
230  } else
231  return vch.erase(first, last);
232  }
233 
234  inline void Compact()
235  {
236  vch.erase(vch.begin(), vch.begin() + nReadPos);
237  nReadPos = 0;
238  }
239 
241  {
242  // Rewind by n characters if the buffer hasn't been compacted yet
243  if (n > nReadPos)
244  return false;
245  nReadPos -= n;
246  return true;
247  }
248 
249 
250  //
251  // Stream subset
252  //
253  bool eof() const { return size() == 0; }
254  CBaseDataStream* rdbuf() { return this; }
255  int in_avail() { return size(); }
256 
257  void SetType(int n) { nType = n; }
258  int GetType() const { return nType; }
259  void SetVersion(int n) { nVersion = n; }
260  int GetVersion() const { return nVersion; }
261 
262  void read(char* pch, size_t nSize)
263  {
264  if (nSize == 0) return;
265 
266  // Read from the beginning of the buffer
267  unsigned int nReadPosNext = nReadPos + nSize;
268  if (nReadPosNext > vch.size()) {
269  throw std::ios_base::failure("CDataStream::read(): end of data");
270  }
271  memcpy(pch, &vch[nReadPos], nSize);
272  if (nReadPosNext == vch.size()) {
273  nReadPos = 0;
274  vch.clear();
275  return;
276  }
277  nReadPos = nReadPosNext;
278  }
279 
280  CBaseDataStream& movePos(size_t nSize){
281  nReadPos = nReadPos + nSize;
282  return (*this);
283  }
284 
285  void ignore(int nSize)
286  {
287  // Ignore from the beginning of the buffer
288  if (nSize < 0) {
289  throw std::ios_base::failure("CBaseDataStream::ignore(): nSize negative");
290  }
291  unsigned int nReadPosNext = nReadPos + nSize;
292  if (nReadPosNext >= vch.size()) {
293  if (nReadPosNext > vch.size())
294  throw std::ios_base::failure("CBaseDataStream::ignore() : end of data");
295  nReadPos = 0;
296  vch.clear();
297  return;
298  }
299  nReadPos = nReadPosNext;
300  }
301 
302  void write(const char* pch, size_t nSize)
303  {
304  // Write to the end of the buffer
305  vch.insert(vch.end(), pch, pch + nSize);
306  }
307 
308  template <typename Stream>
309  void Serialize(Stream& s) const
310  {
311  // Special case: stream << stream concatenates like stream += stream
312  if (!vch.empty())
313  s.write((char*)vch.data(), vch.size() * sizeof(value_type));
314  }
315 
316  template <typename T>
318  {
319  // Serialize to this stream
320  ::Serialize(*this, obj);
321  return (*this);
322  }
323 
324  template <typename T>
326  {
327  // Unserialize from this stream
328  ::Unserialize(*this, obj);
329  return (*this);
330  }
331 
333  {
334  data.insert(data.end(), begin(), end());
335  clear();
336  }
337 };
338 
339 /* Minimal stream for overwriting and/or appending to an existing byte vector
340  *
341  * The referenced vector will grow as necessary
342  */
344 {
345  public:
346 
347 /*
348  * @param[in] nTypeIn Serialization Type
349  * @param[in] nVersionIn Serialization Version (including any flags)
350  * @param[in] vchDataIn Referenced byte vector to overwrite/append
351  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
352  * grow as necessary to max(index, vec.size()). So to append, use vec.size().
353 */
354  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
355  {
356  if(nPos > vchData.size())
357  vchData.resize(nPos);
358  }
359 /*
360  * (other params same as above)
361  * @param[in] args A list of items to serialize starting at nPos.
362 */
363  template <typename... Args>
364  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
365  {
366  ::SerializeMany(*this, std::forward<Args>(args)...);
367  }
368  void write(const char* pch, size_t nSize)
369  {
370  assert(nPos <= vchData.size());
371  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
372  if (nOverwrite) {
373  memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
374  }
375  if (nOverwrite < nSize) {
376  vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
377  }
378  nPos += nSize;
379  }
380  template<typename T>
382  {
383  // Serialize to this stream
384  ::Serialize(*this, obj);
385  return (*this);
386  }
387  int GetVersion() const
388  {
389  return nVersion;
390  }
391  int GetType() const
392  {
393  return nType;
394  }
395  void seek(size_t nSize)
396  {
397  nPos += nSize;
398  if(nPos > vchData.size())
399  vchData.resize(nPos);
400  }
401 private:
402  const int nType;
403  const int nVersion;
404  std::vector<unsigned char>& vchData;
405  size_t nPos;
406 };
407 
408 class CDataStream : public CBaseDataStream<CSerializeData>
409 {
410 public:
411  explicit CDataStream(int nTypeIn, int nVersionIn) : CBaseDataStream(nTypeIn, nVersionIn) { }
412 
413  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) :
414  CBaseDataStream(pbegin, pend, nTypeIn, nVersionIn) { }
415 
416 #if !defined(_MSC_VER) || _MSC_VER >= 1300
417  CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) :
418  CBaseDataStream(pbegin, pend, nTypeIn, nVersionIn) { }
419 #endif
420 
421  CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) :
422  CBaseDataStream(vchIn, nTypeIn, nVersionIn) { }
423 
424  CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) :
425  CBaseDataStream(vchIn, nTypeIn, nVersionIn) { }
426 
427  CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) :
428  CBaseDataStream(vchIn, nTypeIn, nVersionIn) { }
429 
430  template <typename... Args>
431  CDataStream(int nTypeIn, int nVersionIn, Args&&... args) :
432  CBaseDataStream(nTypeIn, nVersionIn, args...) { }
433 
434 };
435 
436 
437 
438 
439 
440 
441 
442 
443 
444 
452 {
453 private:
454  const int nType;
455  const int nVersion;
456 
457  FILE* file;
458 
459 public:
460  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
461  {
462  file = filenew;
463  }
464 
466  {
467  fclose();
468  }
469 
470  // Disallow copies
471  CAutoFile(const CAutoFile&) = delete;
472  CAutoFile& operator=(const CAutoFile&) = delete;
473 
474  void fclose()
475  {
476  if (file) {
477  ::fclose(file);
478  file = nullptr;
479  }
480  }
481 
486  FILE* release()
487  {
488  FILE* ret = file;
489  file = nullptr;
490  return ret;
491  }
492 
497  FILE* Get() const { return file; }
498 
501  bool IsNull() const { return (file == nullptr); }
502 
503  //
504  // Stream subset
505  //
506  int GetType() const { return nType; }
507  int GetVersion() const { return nVersion; }
508 
509  void read(char* pch, size_t nSize)
510  {
511  if (!file)
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");
515  }
516 
517  void ignore(size_t nSize)
518  {
519  if (!file)
520  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
521  unsigned char data[4096];
522  while (nSize > 0) {
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");
526  nSize -= nNow;
527  }
528  }
529 
530 
531  void write(const char* pch, size_t nSize)
532  {
533  if (!file)
534  throw std::ios_base::failure("CAutoFile::write : file handle is nullptr");
535  if (fwrite(pch, 1, nSize, file) != nSize)
536  throw std::ios_base::failure("CAutoFile::write : write failed");
537  }
538 
539  template <typename T>
540  CAutoFile& operator<<(const T& obj)
541  {
542  // Serialize to this stream
543  if (!file)
544  throw std::ios_base::failure("CAutoFile::operator<< : file handle is nullptr");
545  ::Serialize(*this, obj);
546  return (*this);
547  }
548 
549  template<typename T>
551  {
552  // Unserialize from this stream
553  if (!file)
554  throw std::ios_base::failure("CAutoFile::operator>> : file handle is nullptr");
555  ::Unserialize(*this, obj);
556  return (*this);
557  }
558 };
559 
567 {
568 private:
569  const int nType;
570  const int nVersion;
571 
572  FILE* src; // source file
573  uint64_t nSrcPos; // how many bytes have been read from source
574  uint64_t nReadPos; // how many bytes have been read from this
575  uint64_t nReadLimit; // up to which position we're allowed to read
576  uint64_t nRewind; // how many bytes we guarantee to rewind
577  std::vector<char> vchBuf; // the buffer
578 
579 protected:
580  // read data from the source to fill the buffer
581  bool Fill()
582  {
583  unsigned int pos = nSrcPos % vchBuf.size();
584  unsigned int readNow = vchBuf.size() - pos;
585  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
586  if (nAvail < readNow)
587  readNow = nAvail;
588  if (readNow == 0)
589  return false;
590  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
591  if (nBytes == 0) {
592  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
593  }
594  nSrcPos += nBytes;
595  return true;
596  }
597 
598 public:
599  CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
600  nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
601  {
602  if (nRewindIn >= nBufSize)
603  throw std::ios_base::failure("Rewind limit must be less than buffer size");
604  src = fileIn;
605  }
606 
608  {
609  fclose();
610  }
611 
612  int GetVersion() const { return nVersion; }
613  int GetType() const { return nType; }
614 
615  // Disallow copies
616  CBufferedFile(const CBufferedFile&) = delete;
618 
619  void fclose()
620  {
621  if (src) {
622  ::fclose(src);
623  src = nullptr;
624  }
625  }
626 
627  // check whether we're at the end of the source file
628  bool eof() const
629  {
630  return nReadPos == nSrcPos && feof(src);
631  }
632 
633  // read a number of bytes
634  void read(char* pch, size_t nSize)
635  {
636  if (nSize + nReadPos > nReadLimit)
637  throw std::ios_base::failure("Read attempted past buffer limit");
638  while (nSize > 0) {
639  if (nReadPos == nSrcPos)
640  Fill();
641  unsigned int pos = nReadPos % vchBuf.size();
642  size_t nNow = nSize;
643  if (nNow + pos > vchBuf.size())
644  nNow = vchBuf.size() - pos;
645  if (nNow + nReadPos > nSrcPos)
646  nNow = nSrcPos - nReadPos;
647  memcpy(pch, &vchBuf[pos], nNow);
648  nReadPos += nNow;
649  pch += nNow;
650  nSize -= nNow;
651  }
652  }
653 
654  // return the current reading position
655  uint64_t GetPos()
656  {
657  return nReadPos;
658  }
659 
661  bool SetPos(uint64_t nPos) {
662  size_t bufsize = vchBuf.size();
663  if (nPos + bufsize < nSrcPos) {
664  // rewinding too far, rewind as far as possible
665  nReadPos = nSrcPos - bufsize;
666  return false;
667  }
668  if (nPos > nSrcPos) {
669  // can't go this far forward, go as far as possible
670  nReadPos = nSrcPos;
671  return false;
672  }
673  nReadPos = nPos;
674  return true;
675  }
676 
679  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
680  if (nPos < nReadPos)
681  return false;
682  nReadLimit = nPos;
683  return true;
684  }
685 
686  template<typename T>
688  // Unserialize from this stream
689  ::Unserialize(*this, obj);
690  return (*this);
691  }
692 
693  // search for a given byte in the stream, and remain positioned on it
694  void FindByte(char ch)
695  {
696  while (true) {
697  if (nReadPos == nSrcPos)
698  Fill();
699  if (vchBuf[nReadPos % vchBuf.size()] == ch)
700  break;
701  nReadPos++;
702  }
703  }
704 };
705 
706 #endif // PIVX_STREAMS_H
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:452
FILE * file
Definition: streams.h:457
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:486
const int nType
Definition: streams.h:454
void ignore(size_t nSize)
Definition: streams.h:517
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:497
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:460
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:501
void write(const char *pch, size_t nSize)
Definition: streams.h:531
CAutoFile & operator=(const CAutoFile &)=delete
int GetType() const
Definition: streams.h:506
CAutoFile & operator<<(const T &obj)
Definition: streams.h:540
const int nVersion
Definition: streams.h:455
CAutoFile(const CAutoFile &)=delete
~CAutoFile()
Definition: streams.h:465
void fclose()
Definition: streams.h:474
int GetVersion() const
Definition: streams.h:507
CAutoFile & operator>>(T &&obj)
Definition: streams.h:550
void read(char *pch, size_t nSize)
Definition: streams.h:509
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:76
void SetType(int n)
Definition: streams.h:257
SerializeType vector_type
Definition: streams.h:78
CBaseDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:126
value_type * data()
Definition: streams.h:178
const_reference operator[](size_type pos) const
Definition: streams.h:169
iterator erase(iterator first, iterator last)
Definition: streams.h:219
CBaseDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:110
vector_type vch
Definition: streams.h:79
vector_type::iterator iterator
Definition: streams.h:91
CBaseDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:105
CBaseDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:95
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:132
void Serialize(Stream &s) const
Definition: streams.h:309
CBaseDataStream * rdbuf()
Definition: streams.h:254
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:193
CBaseDataStream & operator<<(const T &obj)
Definition: streams.h:317
void SetVersion(int n)
Definition: streams.h:259
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: streams.h:181
int GetType() const
Definition: streams.h:258
void resize(size_type n, value_type c=0)
Definition: streams.h:167
bool eof() const
Definition: streams.h:253
void write(const char *pch, size_t nSize)
Definition: streams.h:302
iterator begin()
Definition: streams.h:162
vector_type::reference reference
Definition: streams.h:88
const_iterator end() const
Definition: streams.h:163
CBaseDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:120
vector_type::difference_type difference_type
Definition: streams.h:87
void read(char *pch, size_t nSize)
Definition: streams.h:262
void GetAndClear(CSerializeData &data)
Definition: streams.h:332
vector_type::value_type value_type
Definition: streams.h:90
vector_type::const_iterator const_iterator
Definition: streams.h:92
CBaseDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:115
reference operator[](size_type pos)
Definition: streams.h:170
CBaseDataStream & movePos(size_t nSize)
Definition: streams.h:280
vector_type::const_reference const_reference
Definition: streams.h:89
iterator insert(iterator it, const char x=char())
Definition: streams.h:176
void ignore(int nSize)
Definition: streams.h:285
void Compact()
Definition: streams.h:234
unsigned int nReadPos
Definition: streams.h:80
const_iterator begin() const
Definition: streams.h:161
CBaseDataStream & operator+=(const CBaseDataStream &b)
Definition: streams.h:139
bool empty() const
Definition: streams.h:166
CBaseDataStream & operator>>(T &&obj)
Definition: streams.h:325
iterator end()
Definition: streams.h:164
vector_type::size_type size_type
Definition: streams.h:86
CBaseDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:100
std::string str() const
Definition: streams.h:152
vector_type::allocator_type allocator_type
Definition: streams.h:85
iterator erase(iterator it)
Definition: streams.h:205
const value_type * data() const
Definition: streams.h:179
bool Rewind(size_type n)
Definition: streams.h:240
size_type size() const
Definition: streams.h:165
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:93
void reserve(size_type n)
Definition: streams.h:168
void clear()
Definition: streams.h:171
int GetVersion() const
Definition: streams.h:260
friend CBaseDataStream operator+(const CBaseDataStream &a, const CBaseDataStream &b)
Definition: streams.h:145
int in_avail()
Definition: streams.h:255
void insert(iterator it, size_type n, const char x)
Definition: streams.h:177
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:567
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:687
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
Definition: streams.h:679
uint64_t nReadLimit
Definition: streams.h:575
void FindByte(char ch)
Definition: streams.h:694
int GetVersion() const
Definition: streams.h:612
bool Fill()
Definition: streams.h:581
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:599
uint64_t nRewind
Definition: streams.h:576
std::vector< char > vchBuf
Definition: streams.h:577
void read(char *pch, size_t nSize)
Definition: streams.h:634
FILE * src
Definition: streams.h:572
~CBufferedFile()
Definition: streams.h:607
int GetType() const
Definition: streams.h:613
uint64_t nSrcPos
Definition: streams.h:573
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:661
const int nType
Definition: streams.h:569
uint64_t nReadPos
Definition: streams.h:574
CBufferedFile(const CBufferedFile &)=delete
CBufferedFile & operator=(const CBufferedFile &)=delete
const int nVersion
Definition: streams.h:570
void fclose()
Definition: streams.h:619
bool eof() const
Definition: streams.h:628
uint64_t GetPos()
Definition: streams.h:655
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:413
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:421
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:411
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:424
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:431
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:417
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:427
const int nVersion
Definition: streams.h:403
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:381
const int nType
Definition: streams.h:402
void seek(size_t nSize)
Definition: streams.h:395
void write(const char *pch, size_t nSize)
Definition: streams.h:368
int GetType() const
Definition: streams.h:391
size_t nPos
Definition: streams.h:405
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:364
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:354
int GetVersion() const
Definition: streams.h:387
std::vector< unsigned char > & vchData
Definition: streams.h:404
const int nVersion
Definition: streams.h:32
void ignore(size_t size)
Definition: streams.h:66
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:35
Stream * stream
Definition: streams.h:29
void read(char *pch, size_t nSize)
Definition: streams.h:58
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:38
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:46
size_t size() const
Definition: streams.h:65
int GetVersion() const
Definition: streams.h:63
const int nType
Definition: streams.h:31
int GetType() const
Definition: streams.h:64
void write(const char *pch, size_t nSize)
Definition: streams.h:53
void * memcpy(void *a, const void *b, size_t c)
#define T(expected, seed, data)
void SerializeMany(Stream &s)
Definition: serialize.h:1372
void Serialize(Stream &s, char a)
Definition: serialize.h:237
void Unserialize(Stream &s, char &a)
Definition: serialize.h:253
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:46