Changeset View
Changeset View
Standalone View
Standalone View
src/streams.h
Show First 20 Lines • Show All 566 Lines • ▼ Show 20 Lines | |||||
* Non-refcounted RAII wrapper for FILE* | * Non-refcounted RAII wrapper for FILE* | ||||
* | * | ||||
* Will automatically close the file when it goes out of scope if not null. If | * 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 | * you're returning the file pointer, return file.release(). If you need to | ||||
* close the file early, use file.fclose() instead of fclose(file). | * close the file early, use file.fclose() instead of fclose(file). | ||||
*/ | */ | ||||
class CAutoFile { | class CAutoFile { | ||||
private: | private: | ||||
// Disallow copies | |||||
CAutoFile(const CAutoFile &); | |||||
CAutoFile &operator=(const CAutoFile &); | |||||
const int nType; | const int nType; | ||||
const int nVersion; | const int nVersion; | ||||
FILE *file; | FILE *file; | ||||
public: | public: | ||||
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn) | CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn) | ||||
: nType(nTypeIn), nVersion(nVersionIn) { | : nType(nTypeIn), nVersion(nVersionIn) { | ||||
file = filenew; | file = filenew; | ||||
} | } | ||||
~CAutoFile() { fclose(); } | ~CAutoFile() { fclose(); } | ||||
// Disallow copies | |||||
CAutoFile(const CAutoFile &) = delete; | |||||
CAutoFile &operator=(const CAutoFile &) = delete; | |||||
void fclose() { | void fclose() { | ||||
if (file) { | if (file) { | ||||
::fclose(file); | ::fclose(file); | ||||
file = nullptr; | file = nullptr; | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
* deserialize from. It guarantees the ability to rewind a given number of | * deserialize from. It guarantees the ability to rewind a given number of | ||||
* bytes. | * bytes. | ||||
* | * | ||||
* Will automatically close the file when it goes out of scope if not null. If | * Will automatically close the file when it goes out of scope if not null. If | ||||
* you need to close the file early, use file.fclose() instead of fclose(file). | * you need to close the file early, use file.fclose() instead of fclose(file). | ||||
*/ | */ | ||||
class CBufferedFile { | class CBufferedFile { | ||||
private: | private: | ||||
// Disallow copies | |||||
CBufferedFile(const CBufferedFile &); | |||||
CBufferedFile &operator=(const CBufferedFile &); | |||||
const int nType; | const int nType; | ||||
const int nVersion; | const int nVersion; | ||||
// source file | // source file | ||||
FILE *src; | FILE *src; | ||||
// how many bytes have been read from source | // how many bytes have been read from source | ||||
uint64_t nSrcPos; | uint64_t nSrcPos; | ||||
// how many bytes have been read from this | // how many bytes have been read from this | ||||
Show All 29 Lines | CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, | ||||
int nTypeIn, int nVersionIn) | int nTypeIn, int nVersionIn) | ||||
: nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), | : nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), | ||||
nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0) { | nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0) { | ||||
src = fileIn; | src = fileIn; | ||||
} | } | ||||
~CBufferedFile() { fclose(); } | ~CBufferedFile() { fclose(); } | ||||
// Disallow copies | |||||
CBufferedFile(const CBufferedFile &) = delete; | |||||
CBufferedFile &operator=(const CBufferedFile &) = delete; | |||||
int GetVersion() const { return nVersion; } | int GetVersion() const { return nVersion; } | ||||
int GetType() const { return nType; } | int GetType() const { return nType; } | ||||
void fclose() { | void fclose() { | ||||
if (src) { | if (src) { | ||||
::fclose(src); | ::fclose(src); | ||||
src = nullptr; | src = nullptr; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines |