aboutsummaryrefslogtreecommitdiff
path: root/src/streams.h
diff options
context:
space:
mode:
authorMarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>2023-07-12 09:40:24 +0200
committerMarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>2023-07-13 11:50:55 +0200
commitfa7724bc9d94c08d8facccd0a067d6a3b27fbbc6 (patch)
treec0b16f9136cfb779aa2338a31a43f61aa118df31 /src/streams.h
parentfa8d227d58f7baa5a9be1b88930f4813bf6eedb1 (diff)
downloadbitcoin-fa7724bc9d94c08d8facccd0a067d6a3b27fbbc6.tar.xz
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.
Diffstat (limited to 'src/streams.h')
-rw-r--r--src/streams.h47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/streams.h b/src/streams.h
index 7806eeaf98..f9bcf0fe14 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -480,73 +480,74 @@ public:
class AutoFile
{
protected:
- FILE* file;
+ std::FILE* m_file;
public:
- explicit AutoFile(FILE* filenew) : file{filenew} {}
+ explicit AutoFile(std::FILE* file) : m_file{file} {}
- ~AutoFile()
- {
- fclose();
- }
+ ~AutoFile() { fclose(); }
// Disallow copies
AutoFile(const AutoFile&) = delete;
AutoFile& operator=(const AutoFile&) = delete;
+ bool feof() const { return std::feof(m_file); }
+
int fclose()
{
- int retval{0};
- if (file) {
- retval = ::fclose(file);
- file = nullptr;
- }
- return retval;
+ if (auto rel{release()}) return std::fclose(rel);
+ return 0;
}
/** Get wrapped FILE* with transfer of ownership.
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
* 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.
* @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.
*/
- FILE* Get() const { return file; }
+ std::FILE* Get() const { return m_file; }
/** 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
//
void read(Span<std::byte> dst)
{
- if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
- if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
- throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
+ if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
+ if (std::fread(dst.data(), 1, dst.size(), m_file) != dst.size()) {
+ throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
}
}
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];
while (nSize > 0) {
size_t nNow = std::min<size_t>(nSize, sizeof(data));
- if (fread(data, 1, nNow, file) != nNow)
- throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
+ if (std::fread(data, 1, nNow, m_file) != nNow) {
+ throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
+ }
nSize -= nNow;
}
}
void write(Span<const std::byte> src)
{
- if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
- if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
+ if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
+ if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure("AutoFile::write: write failed");
}
}