diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2014-08-24 01:57:52 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2014-08-24 02:06:09 +0200 |
commit | 5cd00bc8cb6820d3b41bea329fcf0c26c03db64a (patch) | |
tree | 174862fcca9125ed73cd6ececd9aca0d4b4305d4 /src | |
parent | 57fe1eaadc7c404b199239351e7bd80f48f3bcba (diff) | |
parent | eb0b56b19017ab5c16c745e6da39c53126924ed6 (diff) |
Merge pull request #4618
eb0b56b Simplify serialize.h's exception handling (Pieter Wuille)
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/serialize.h | 63 | ||||
-rw-r--r-- | src/test/alert_tests.cpp | 2 |
3 files changed, 8 insertions, 59 deletions
diff --git a/src/main.cpp b/src/main.cpp index d7543e3f13..01f3e06a60 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3237,7 +3237,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp) } } uint64_t nRewind = blkdat.GetPos(); - while (blkdat.good() && !blkdat.eof()) { + while (!blkdat.eof()) { boost::this_thread::interruption_point(); blkdat.SetPos(nRewind); diff --git a/src/serialize.h b/src/serialize.h index 3e5eff469d..17cb724bee 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -905,8 +905,6 @@ protected: typedef CSerializeData vector_type; vector_type vch; unsigned int nReadPos; - short state; - short exceptmask; public: int nType; int nVersion; @@ -958,8 +956,6 @@ public: nReadPos = 0; nType = nTypeIn; nVersion = nVersionIn; - state = 0; - exceptmask = std::ios::badbit | std::ios::failbit; } CDataStream& operator+=(const CDataStream& b) @@ -1082,19 +1078,7 @@ public: // // Stream subset // - void setstate(short bits, const char* psz) - { - state |= bits; - if (state & exceptmask) - throw std::ios_base::failure(psz); - } - bool eof() const { return size() == 0; } - bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } - bool good() const { return !eof() && (state == 0); } - void clear(short n) { state = n; } // name conflict with vector clear() - short exceptions() { return exceptmask; } - short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; } CDataStream* rdbuf() { return this; } int in_avail() { return size(); } @@ -1113,9 +1097,7 @@ public: { if (nReadPosNext > vch.size()) { - setstate(std::ios::failbit, "CDataStream::read() : end of data"); - memset(pch, 0, nSize); - nSize = vch.size() - nReadPos; + throw std::ios_base::failure("CDataStream::read() : end of data"); } memcpy(pch, &vch[nReadPos], nSize); nReadPos = 0; @@ -1135,7 +1117,7 @@ public: if (nReadPosNext >= vch.size()) { if (nReadPosNext > vch.size()) - setstate(std::ios::failbit, "CDataStream::ignore() : end of data"); + throw std::ios_base::failure("CDataStream::ignore() : end of data"); nReadPos = 0; vch.clear(); return (*this); @@ -1207,8 +1189,6 @@ class CAutoFile { protected: FILE* file; - short state; - short exceptmask; public: int nType; int nVersion; @@ -1218,8 +1198,6 @@ public: file = filenew; nType = nTypeIn; nVersion = nVersionIn; - state = 0; - exceptmask = std::ios::badbit | std::ios::failbit; } ~CAutoFile() @@ -1246,19 +1224,6 @@ public: // // Stream subset // - void setstate(short bits, const char* psz) - { - state |= bits; - if (state & exceptmask) - throw std::ios_base::failure(psz); - } - - bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } - bool good() const { return state == 0; } - void clear(short n = 0) { state = n; } - short exceptions() { return exceptmask; } - short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; } - void SetType(int n) { nType = n; } int GetType() { return nType; } void SetVersion(int n) { nVersion = n; } @@ -1271,7 +1236,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::read : file handle is NULL"); if (fread(pch, 1, nSize, file) != nSize) - setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); + throw std::ios_base::failure(feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); return (*this); } @@ -1280,7 +1245,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::write : file handle is NULL"); if (fwrite(pch, 1, nSize, file) != nSize) - setstate(std::ios::failbit, "CAutoFile::write : write failed"); + throw std::ios_base::failure("CAutoFile::write : write failed"); return (*this); } @@ -1325,16 +1290,7 @@ private: uint64_t nRewind; // how many bytes we guarantee to rewind std::vector<char> vchBuf; // the buffer - short state; - short exceptmask; - protected: - void setstate(short bits, const char *psz) { - state |= bits; - if (state & exceptmask) - throw std::ios_base::failure(psz); - } - // read data from the source to fill the buffer bool Fill() { unsigned int pos = nSrcPos % vchBuf.size(); @@ -1346,8 +1302,7 @@ protected: return false; size_t read = fread((void*)&vchBuf[pos], 1, readNow, src); if (read == 0) { - setstate(std::ios_base::failbit, feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed"); - return false; + throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed"); } else { nSrcPos += read; return true; @@ -1360,12 +1315,7 @@ public: CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) : src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0), - state(0), exceptmask(std::ios_base::badbit | std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) { - } - - // check whether no error occurred - bool good() const { - return state == 0; + nType(nTypeIn), nVersion(nVersionIn) { } // check whether we're at the end of the source file @@ -1424,7 +1374,6 @@ public: nLongPos = ftell(src); nSrcPos = nLongPos; nReadPos = nLongPos; - state = 0; return true; } diff --git a/src/test/alert_tests.cpp b/src/test/alert_tests.cpp index b16f3f7f57..e3066a51ab 100644 --- a/src/test/alert_tests.cpp +++ b/src/test/alert_tests.cpp @@ -83,7 +83,7 @@ struct ReadAlerts std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests)); CDataStream stream(vch, SER_DISK, CLIENT_VERSION); try { - while (stream.good()) + while (!stream.eof()) { CAlert alert; stream >> alert; |