template uint64_t ReadCompactSize<CDataStream>(CDataStream&);
//
+//CAutoFile
+//
+
+CAutoFile::CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
+{
+ file = filenew;
+ nType = nTypeIn;
+ nVersion = nVersionIn;
+ state = 0;
+ exceptmask = std::ios::badbit | std::ios::failbit;
+}
+
+CAutoFile::~CAutoFile()
+{
+ fclose();
+}
+
+void CAutoFile::fclose()
+{
+ if (file != NULL && file != stdin && file != stdout && file != stderr)
+ ::fclose(file);
+ file = NULL;
+}
+
+void CAutoFile::setstate(short bits, const char* psz)
+{
+ state |= bits;
+ if (state & exceptmask)
+ throw std::ios_base::failure(psz);
+}
+
+CAutoFile& CAutoFile::read(char* pch, size_t nSize)
+{
+ 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");
+ return (*this);
+}
+
+CAutoFile& CAutoFile::write(const char* pch, size_t nSize)
+{
+ 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");
+ return (*this);
+}
+
+//
//CBufferedFile
//
};
-
-
-
-
-
-
-
-
-/** RAII wrapper for FILE*.
- *
- * Will automatically close the file when it goes out of scope if not null.
- * If you're returning the file pointer, return file.release().
- * If you need to close the file early, use file.fclose() instead of fclose(file).
- */
+// RAII wrapper for FILE*.
+//
+// Will automatically close the file when it goes out of scope if not null.
+// If you're returning the file pointer, return file.release().
+// If you need to close the file early, use file.fclose() instead of fclose(file).
class CAutoFile
{
protected:
int nType;
int nVersion;
- CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
- {
- file = filenew;
- nType = nTypeIn;
- nVersion = nVersionIn;
- state = 0;
- exceptmask = std::ios::badbit | std::ios::failbit;
- }
-
- ~CAutoFile()
- {
- fclose();
- }
-
- void fclose()
- {
- if (file != NULL && file != stdin && file != stdout && file != stderr)
- ::fclose(file);
- file = NULL;
- }
+ CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn);
+ ~CAutoFile();
+ void fclose();
FILE* release() { FILE* ret = file; file = NULL; return ret; }
operator FILE*() { return file; }
//
// Stream subset
//
- void setstate(short bits, const char* psz)
- {
- state |= bits;
- if (state & exceptmask)
- throw std::ios_base::failure(psz);
- }
-
+ void setstate(short bits, const char* psz);
bool fail() const { return (state & (std::ios::badbit | std::ios::failbit)) != 0; }
bool good() const { return state == 0; }
void clear(short n = 0) { state = n; }
void ReadVersion() { *this >> nVersion; }
void WriteVersion() { *this << nVersion; }
- CAutoFile& read(char* pch, size_t nSize)
- {
- 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");
- return (*this);
- }
-
- CAutoFile& write(const char* pch, size_t nSize)
- {
- 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");
- return (*this);
- }
+ CAutoFile& read(char* pch, size_t nSize);
+ CAutoFile& write(const char* pch, size_t nSize);
template<typename T>
unsigned int GetSerializeSize(const T& obj)