mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-30 05:52:33 +02:00
refactor: Modernize AutoFile
* Add m_ prefix to the std::FILE member variable * Add std namespace where possible, to avoid confusion with member functions of the same name. * Add AutoFile::feof() member function, to be used in place of std::feof(AutoFile::Get()) * Simplify fclose() in terms of release() * Fix typo in the error message in the ignore member function.
This commit is contained in:
parent
fa8d227d58
commit
fa7724bc9d
@ -480,73 +480,74 @@ public:
|
|||||||
class AutoFile
|
class AutoFile
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
FILE* file;
|
std::FILE* m_file;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AutoFile(FILE* filenew) : file{filenew} {}
|
explicit AutoFile(std::FILE* file) : m_file{file} {}
|
||||||
|
|
||||||
~AutoFile()
|
~AutoFile() { fclose(); }
|
||||||
{
|
|
||||||
fclose();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disallow copies
|
// Disallow copies
|
||||||
AutoFile(const AutoFile&) = delete;
|
AutoFile(const AutoFile&) = delete;
|
||||||
AutoFile& operator=(const AutoFile&) = delete;
|
AutoFile& operator=(const AutoFile&) = delete;
|
||||||
|
|
||||||
|
bool feof() const { return std::feof(m_file); }
|
||||||
|
|
||||||
int fclose()
|
int fclose()
|
||||||
{
|
{
|
||||||
int retval{0};
|
if (auto rel{release()}) return std::fclose(rel);
|
||||||
if (file) {
|
return 0;
|
||||||
retval = ::fclose(file);
|
|
||||||
file = nullptr;
|
|
||||||
}
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get wrapped FILE* with transfer of ownership.
|
/** Get wrapped FILE* with transfer of ownership.
|
||||||
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
|
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
|
||||||
* of this function to clean up the returned FILE*.
|
* of this function to clean up the returned FILE*.
|
||||||
*/
|
*/
|
||||||
FILE* release() { FILE* ret = file; file = nullptr; return ret; }
|
std::FILE* release()
|
||||||
|
{
|
||||||
|
std::FILE* ret{m_file};
|
||||||
|
m_file = nullptr;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/** Get wrapped FILE* without transfer of ownership.
|
/** Get wrapped FILE* without transfer of ownership.
|
||||||
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
|
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
|
||||||
* AutoFile outlives use of the passed pointer.
|
* AutoFile outlives use of the passed pointer.
|
||||||
*/
|
*/
|
||||||
FILE* Get() const { return file; }
|
std::FILE* Get() const { return m_file; }
|
||||||
|
|
||||||
/** Return true if the wrapped FILE* is nullptr, false otherwise.
|
/** Return true if the wrapped FILE* is nullptr, false otherwise.
|
||||||
*/
|
*/
|
||||||
bool IsNull() const { return (file == nullptr); }
|
bool IsNull() const { return m_file == nullptr; }
|
||||||
|
|
||||||
//
|
//
|
||||||
// Stream subset
|
// Stream subset
|
||||||
//
|
//
|
||||||
void read(Span<std::byte> dst)
|
void read(Span<std::byte> dst)
|
||||||
{
|
{
|
||||||
if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
|
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
|
||||||
if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
|
if (std::fread(dst.data(), 1, dst.size(), m_file) != dst.size()) {
|
||||||
throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
|
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ignore(size_t nSize)
|
void ignore(size_t nSize)
|
||||||
{
|
{
|
||||||
if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
|
if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
|
||||||
unsigned char data[4096];
|
unsigned char data[4096];
|
||||||
while (nSize > 0) {
|
while (nSize > 0) {
|
||||||
size_t nNow = std::min<size_t>(nSize, sizeof(data));
|
size_t nNow = std::min<size_t>(nSize, sizeof(data));
|
||||||
if (fread(data, 1, nNow, file) != nNow)
|
if (std::fread(data, 1, nNow, m_file) != nNow) {
|
||||||
throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
|
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
|
||||||
|
}
|
||||||
nSize -= nNow;
|
nSize -= nNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(Span<const std::byte> src)
|
void write(Span<const std::byte> src)
|
||||||
{
|
{
|
||||||
if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
|
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
|
||||||
if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
|
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
|
||||||
throw std::ios_base::failure("AutoFile::write: write failed");
|
throw std::ios_base::failure("AutoFile::write: write failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user