From 000019e158ef01f2bedc3fc1589f95e106e817ea Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Wed, 5 Jul 2023 12:44:12 +0200 Subject: [PATCH] Add AutoFile::detail_fread member function New code can call the method without having first to retrieve the raw FILE* pointer via Get(). Also, move implementation to the cpp file. Can be reviewed with: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --- src/Makefile.am | 2 ++ src/streams.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/streams.h | 35 +++++++---------------------------- 3 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 src/streams.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 4e9c161c57..cdf2df4f2e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -715,6 +715,7 @@ libbitcoin_util_a_SOURCES = \ logging.cpp \ random.cpp \ randomenv.cpp \ + streams.cpp \ support/cleanse.cpp \ sync.cpp \ util/asmap.cpp \ @@ -958,6 +959,7 @@ libbitcoinkernel_la_SOURCES = \ script/standard.cpp \ shutdown.cpp \ signet.cpp \ + streams.cpp \ support/cleanse.cpp \ support/lockedpool.cpp \ sync.cpp \ diff --git a/src/streams.cpp b/src/streams.cpp new file mode 100644 index 0000000000..16a8e51722 --- /dev/null +++ b/src/streams.cpp @@ -0,0 +1,40 @@ +// Copyright (c) 2009-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://opensource.org/license/mit/. + +#include +#include + +std::size_t AutoFile::detail_fread(Span dst) +{ + if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr"); + return std::fread(dst.data(), 1, dst.size(), m_file); +} + +void AutoFile::read(Span dst) +{ + if (detail_fread(dst) != dst.size()) { + throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed"); + } +} + +void AutoFile::ignore(size_t nSize) +{ + 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(nSize, sizeof(data)); + 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 AutoFile::write(Span src) +{ + 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"); + } +} diff --git a/src/streams.h b/src/streams.h index f9bcf0fe14..27875775a7 100644 --- a/src/streams.h +++ b/src/streams.h @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -520,37 +521,15 @@ public: */ bool IsNull() const { return m_file == nullptr; } + /** Implementation detail, only used internally. */ + std::size_t detail_fread(Span dst); + // // Stream subset // - void read(Span dst) - { - 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 (!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(nSize, sizeof(data)); - 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 src) - { - 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"); - } - } + void read(Span dst); + void ignore(size_t nSize); + void write(Span src); template AutoFile& operator<<(const T& obj)