4 #include "allocators.h" 15 typedef CSerializeData vector_type;
17 unsigned int nReadPos;
22 typedef vector_type::allocator_type allocator_type;
23 typedef vector_type::size_type size_type;
24 typedef vector_type::difference_type difference_type;
25 typedef vector_type::reference reference;
26 typedef vector_type::const_reference const_reference;
27 typedef vector_type::value_type value_type;
28 typedef vector_type::iterator iterator;
29 typedef vector_type::const_iterator const_iterator;
30 typedef vector_type::reverse_iterator reverse_iterator;
34 Init(nTypeIn, nVersionIn);
37 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
39 Init(nTypeIn, nVersionIn);
42 #if !defined(_MSC_VER) || _MSC_VER >= 1300 43 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
45 Init(nTypeIn, nVersionIn);
49 CDataStream(
const vector_type& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
51 Init(nTypeIn, nVersionIn);
54 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
56 Init(nTypeIn, nVersionIn);
59 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
61 Init(nTypeIn, nVersionIn);
64 void Init(
int nTypeIn,
int nVersionIn)
68 nVersion = nVersionIn;
73 vch.insert(vch.end(), b.begin(), b.end());
84 std::string str()
const 86 return (std::string(begin(), end()));
93 const_iterator begin()
const {
return vch.begin() + nReadPos; }
94 iterator begin() {
return vch.begin() + nReadPos; }
95 const_iterator end()
const {
return vch.end(); }
96 iterator end() {
return vch.end(); }
97 size_type size()
const {
return vch.size() - nReadPos; }
98 bool empty()
const {
return vch.size() == nReadPos; }
99 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
100 void reserve(size_type n) { vch.reserve(n + nReadPos); }
101 const_reference operator[](size_type pos)
const {
return vch[pos + nReadPos]; }
102 reference operator[](size_type pos) {
return vch[pos + nReadPos]; }
103 void clear() { vch.clear(); nReadPos = 0; }
104 iterator insert(iterator it,
const char& x=
char()) {
return vch.insert(it, x); }
105 void insert(iterator it, size_type n,
const char& x) { vch.insert(it, n, x); }
107 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
109 assert(last - first >= 0);
110 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
113 nReadPos -= (last - first);
114 memcpy(&vch[nReadPos], &first[0], last - first);
117 vch.insert(it, first, last);
120 #if !defined(_MSC_VER) || _MSC_VER >= 1300 121 void insert(iterator it,
const char* first,
const char* last)
123 assert(last - first >= 0);
124 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
127 nReadPos -= (last - first);
128 memcpy(&vch[nReadPos], &first[0], last - first);
131 vch.insert(it, first, last);
135 iterator erase(iterator it)
137 if (it == vch.begin() + nReadPos)
140 if (++nReadPos >= vch.size())
144 return vch.erase(vch.begin(), vch.end());
146 return vch.begin() + nReadPos;
149 return vch.erase(it);
152 iterator erase(iterator first, iterator last)
154 if (first == vch.begin() + nReadPos)
157 if (last == vch.end())
160 return vch.erase(vch.begin(), vch.end());
164 nReadPos = (last - vch.begin());
169 return vch.erase(first, last);
172 inline void Compact()
174 vch.erase(vch.begin(), vch.begin() + nReadPos);
178 bool Rewind(size_type n)
191 bool eof()
const {
return size() == 0; }
193 int in_avail() {
return size(); }
195 void SetType(
int n) { nType = n; }
196 int GetType() {
return nType; }
197 void SetVersion(
int n) { nVersion = n; }
198 int GetVersion() {
return nVersion; }
199 void ReadVersion() { *
this >> nVersion; }
200 void WriteVersion() { *
this << nVersion; }
205 unsigned int nReadPosNext = nReadPos + nSize;
206 if (nReadPosNext >= vch.size())
208 if (nReadPosNext > vch.size())
210 throw std::ios_base::failure(
"CDataStream::read() : end of data");
212 memcpy(pch, &vch[nReadPos], nSize);
217 memcpy(pch, &vch[nReadPos], nSize);
218 nReadPos = nReadPosNext;
226 unsigned int nReadPosNext = nReadPos + nSize;
227 if (nReadPosNext >= vch.size())
229 if (nReadPosNext > vch.size())
230 throw std::ios_base::failure(
"CDataStream::ignore() : end of data");
235 nReadPos = nReadPosNext;
242 vch.insert(vch.end(), pch, pch + nSize);
246 template<
typename Stream>
247 void Serialize(Stream& s,
int nType,
int nVersion)
const 251 s.write((
char*)&vch[0], vch.size() *
sizeof(vch[0]));
255 unsigned int GetSerializeSize(
const T& obj)
258 return ::GetSerializeSize(obj, nType, nVersion);
265 ::Serialize(*
this, obj, nType, nVersion);
273 ::Unserialize(*
this, obj, nType, nVersion);
277 void GetAndClear(CSerializeData &data) {
278 data.insert(data.end(), begin(), end());
311 CAutoFile(FILE* filenew,
int nTypeIn,
int nVersionIn)
315 nVersion = nVersionIn;
335 FILE*
release() { FILE* ret = file; file = NULL;
return ret; }
341 FILE*
Get()
const {
return file; }
345 bool IsNull()
const {
return (file == NULL); }
350 void SetType(
int n) { nType = n; }
351 int GetType() {
return nType; }
352 void SetVersion(
int n) { nVersion = n; }
353 int GetVersion() {
return nVersion; }
354 void ReadVersion() { *
this >> nVersion; }
355 void WriteVersion() { *
this << nVersion; }
360 throw std::ios_base::failure(
"CAutoFile::read : file handle is NULL");
361 if (fread(pch, 1, nSize, file) != nSize)
362 throw std::ios_base::failure(feof(file) ?
"CAutoFile::read : end of file" :
"CAutoFile::read : fread failed");
366 CAutoFile& write(
const char* pch,
size_t nSize)
369 throw std::ios_base::failure(
"CAutoFile::write : file handle is NULL");
370 if (fwrite(pch, 1, nSize, file) != nSize)
371 throw std::ios_base::failure(
"CAutoFile::write : write failed");
376 unsigned int GetSerializeSize(
const T& obj)
379 return ::GetSerializeSize(obj, nType, nVersion);
387 throw std::ios_base::failure(
"CAutoFile::operator<< : file handle is NULL");
388 ::Serialize(*
this, obj, nType, nVersion);
397 throw std::ios_base::failure(
"CAutoFile::operator>> : file handle is NULL");
398 ::Unserialize(*
this, obj, nType, nVersion);
424 std::vector<char> vchBuf;
429 unsigned int pos = nSrcPos % vchBuf.size();
430 unsigned int readNow = vchBuf.size() - pos;
431 unsigned int nAvail = (
unsigned int)(vchBuf.size() - (nSrcPos - nReadPos) - nRewind);
432 if (nAvail < readNow)
436 size_t read = fread((
void*)&vchBuf[pos], 1, readNow, src);
438 throw std::ios_base::failure(feof(src) ?
"CBufferedFile::Fill : end of file" :
"CBufferedFile::Fill : fread failed");
446 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
447 nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf((
unsigned int)nBufSize, 0)
451 nVersion = nVersionIn;
469 return nReadPos == nSrcPos && feof(src);
474 if (nSize + nReadPos > nReadLimit)
475 throw std::ios_base::failure(
"Read attempted past buffer limit");
476 if (nSize + nRewind > vchBuf.size())
477 throw std::ios_base::failure(
"Read larger than buffer size");
479 if (nReadPos == nSrcPos)
481 unsigned int pos = nReadPos % vchBuf.size();
483 if (nNow + pos > vchBuf.size())
484 nNow = vchBuf.size() - pos;
485 if (nNow + nReadPos > nSrcPos)
486 nNow = (size_t)(nSrcPos - nReadPos);
487 memcpy(pch, &vchBuf[pos], nNow);
501 bool SetPos(uint64_t nPos) {
503 if (nReadPos + nRewind < nSrcPos) {
504 nReadPos = nSrcPos - nRewind;
506 }
else if (nReadPos > nSrcPos) {
514 bool Seek(uint64_t nPos) {
515 long nLongPos = (long)nPos;
516 if (nPos != (uint64_t)nLongPos)
518 if (fseek(src, nLongPos, SEEK_SET))
520 nLongPos = ftell(src);
528 bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
538 ::Unserialize(*
this, obj, nType, nVersion);
543 void FindByte(
char ch) {
545 if (nReadPos == nSrcPos)
547 if (vchBuf[nReadPos % vchBuf.size()] == ch)
FILE * release()
Definition: streams.h:335
bool IsNull() const
Definition: streams.h:345
FILE * Get() const
Definition: streams.h:341
Definition: streams.h:409
Definition: streams.h:298