3 CDataStream::CDataStream(int nTypeIn, int nVersionIn)
5 Init(nTypeIn, nVersionIn);
8 CDataStream::CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
10 Init(nTypeIn, nVersionIn);
13 CDataStream::CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
15 Init(nTypeIn, nVersionIn);
18 CDataStream::CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
20 Init(nTypeIn, nVersionIn);
23 CDataStream::CDataStream(const std::vector<char> &vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
25 Init(nTypeIn, nVersionIn);
28 CDataStream::CDataStream(const std::vector<unsigned char> &vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
30 Init(nTypeIn, nVersionIn);
33 void CDataStream::Init(int nTypeIn, int nVersionIn)
37 nVersion = nVersionIn;
39 exceptmask = std::ios::badbit | std::ios::failbit;
42 CDataStream &CDataStream::operator+=(const CDataStream &b)
44 vch.insert(vch.end(), b.begin(), b.end());
48 std::string CDataStream::str() const
50 return std::string(begin(), end());
53 bool CDataStream::Rewind(size_type n)
55 // Rewind by n characters if the buffer hasn't been compacted yet
58 nReadPos -= (unsigned int)n;
62 void CDataStream::setstate(short bits, const char *psz)
65 if (state & exceptmask)
66 throw std::ios_base::failure(psz);
69 CDataStream &CDataStream::read(char *pch, int nSize)
71 // Read from the beginning of the buffer
73 unsigned int nReadPosNext = nReadPos + nSize;
74 if (nReadPosNext >= vch.size())
76 if (nReadPosNext > vch.size())
78 setstate(std::ios::failbit, "CDataStream::read() : end of data");
79 memset(pch, 0, nSize);
80 nSize = (int)(vch.size() - nReadPos);
82 memcpy(pch, &vch[nReadPos], nSize);
87 memcpy(pch, &vch[nReadPos], nSize);
88 nReadPos = nReadPosNext;
92 CDataStream &CDataStream::ignore(int nSize)
94 // Ignore from the beginning of the buffer
96 unsigned int nReadPosNext = nReadPos + nSize;
97 if (nReadPosNext >= vch.size())
99 if (nReadPosNext > vch.size())
100 setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
105 nReadPos = nReadPosNext;
109 CDataStream &CDataStream::write(const char *pch, int nSize)
111 // Write to the end of the buffer
113 vch.insert(vch.end(), pch, pch + nSize);
117 void CDataStream::GetAndClear(CSerializeData &data) {
119 CSerializeData().swap(vch);
122 CAutoFile::CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
126 nVersion = nVersionIn;
128 exceptmask = std::ios::badbit | std::ios::failbit;
131 CAutoFile::~CAutoFile()
136 void CAutoFile::fclose()
138 if (file != NULL && file != stdin && file != stdout && file != stderr)
143 void CAutoFile::setstate(short bits, const char *psz)
146 if (state & exceptmask)
147 throw std::ios_base::failure(psz);
150 CAutoFile &CAutoFile::read(char *pch, size_t nSize)
153 throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
154 if (fread(pch, 1, nSize, file) != nSize)
155 setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
159 CAutoFile &CAutoFile::write(const char *pch, size_t nSize)
162 throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
163 if (fwrite(pch, 1, nSize, file) != nSize)
164 setstate(std::ios::failbit, "CAutoFile::write : write failed");
168 void CBufferedFile::setstate(short bits, const char *psz) {
170 if (state & exceptmask)
171 throw std::ios_base::failure(psz);
174 bool CBufferedFile::Fill() {
175 unsigned int pos = (unsigned int)(nSrcPos % vchBuf.size());
176 unsigned int readNow = (unsigned int)(vchBuf.size() - pos);
177 unsigned int nAvail = (unsigned int)(vchBuf.size() - (nSrcPos - nReadPos) - nRewind);
178 if (nAvail < readNow)
182 size_t read = fread((void*)&vchBuf[pos], 1, readNow, src);
184 setstate(std::ios_base::failbit, feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed");
192 CBufferedFile &CBufferedFile::read(char *pch, size_t nSize) {
193 if (nSize + nReadPos > nReadLimit)
194 throw std::ios_base::failure("Read attempted past buffer limit");
195 if (nSize + nRewind > vchBuf.size())
196 throw std::ios_base::failure("Read larger than buffer size");
198 if (nReadPos == nSrcPos)
200 unsigned int pos = (unsigned int)(nReadPos % vchBuf.size());
202 if (nNow + pos > vchBuf.size())
203 nNow = vchBuf.size() - pos;
204 if (nNow + nReadPos > nSrcPos)
205 nNow = (size_t)(nSrcPos - nReadPos);
206 memcpy(pch, &vchBuf[pos], nNow);
214 bool CBufferedFile::SetPos(uint64_t nPos) {
216 if (nReadPos + nRewind < nSrcPos) {
217 nReadPos = nSrcPos - nRewind;
219 } else if (nReadPos > nSrcPos) {
227 bool CBufferedFile::Seek(uint64_t nPos) {
228 long nLongPos = (long)nPos;
229 if (nPos != (uint64_t)nLongPos)
231 if (fseek(src, nLongPos, SEEK_SET))
233 nLongPos = ftell(src);
240 bool CBufferedFile::SetLimit(uint64_t nPos) {
247 void CBufferedFile::FindByte(char ch) {
249 if (nReadPos == nSrcPos)
251 if (vchBuf[nReadPos % vchBuf.size()] == ch)