Skip to content

Commit 000019e

Browse files
author
MarcoFalke
committed
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
1 parent fa7724b commit 000019e

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ libbitcoin_util_a_SOURCES = \
715715
logging.cpp \
716716
random.cpp \
717717
randomenv.cpp \
718+
streams.cpp \
718719
support/cleanse.cpp \
719720
sync.cpp \
720721
util/asmap.cpp \
@@ -958,6 +959,7 @@ libbitcoinkernel_la_SOURCES = \
958959
script/standard.cpp \
959960
shutdown.cpp \
960961
signet.cpp \
962+
streams.cpp \
961963
support/cleanse.cpp \
962964
support/lockedpool.cpp \
963965
sync.cpp \

src/streams.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2009-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://opensource.org/license/mit/.
4+
5+
#include <span.h>
6+
#include <streams.h>
7+
8+
std::size_t AutoFile::detail_fread(Span<std::byte> dst)
9+
{
10+
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
11+
return std::fread(dst.data(), 1, dst.size(), m_file);
12+
}
13+
14+
void AutoFile::read(Span<std::byte> dst)
15+
{
16+
if (detail_fread(dst) != dst.size()) {
17+
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
18+
}
19+
}
20+
21+
void AutoFile::ignore(size_t nSize)
22+
{
23+
if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
24+
unsigned char data[4096];
25+
while (nSize > 0) {
26+
size_t nNow = std::min<size_t>(nSize, sizeof(data));
27+
if (std::fread(data, 1, nNow, m_file) != nNow) {
28+
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
29+
}
30+
nSize -= nNow;
31+
}
32+
}
33+
34+
void AutoFile::write(Span<const std::byte> src)
35+
{
36+
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
37+
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
38+
throw std::ios_base::failure("AutoFile::write: write failed");
39+
}
40+
}

src/streams.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <algorithm>
1515
#include <assert.h>
16+
#include <cstddef>
1617
#include <cstdio>
1718
#include <ios>
1819
#include <limits>
@@ -520,37 +521,15 @@ class AutoFile
520521
*/
521522
bool IsNull() const { return m_file == nullptr; }
522523

524+
/** Implementation detail, only used internally. */
525+
std::size_t detail_fread(Span<std::byte> dst);
526+
523527
//
524528
// Stream subset
525529
//
526-
void read(Span<std::byte> dst)
527-
{
528-
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
529-
if (std::fread(dst.data(), 1, dst.size(), m_file) != dst.size()) {
530-
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
531-
}
532-
}
533-
534-
void ignore(size_t nSize)
535-
{
536-
if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
537-
unsigned char data[4096];
538-
while (nSize > 0) {
539-
size_t nNow = std::min<size_t>(nSize, sizeof(data));
540-
if (std::fread(data, 1, nNow, m_file) != nNow) {
541-
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
542-
}
543-
nSize -= nNow;
544-
}
545-
}
546-
547-
void write(Span<const std::byte> src)
548-
{
549-
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
550-
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
551-
throw std::ios_base::failure("AutoFile::write: write failed");
552-
}
553-
}
530+
void read(Span<std::byte> dst);
531+
void ignore(size_t nSize);
532+
void write(Span<const std::byte> src);
554533

555534
template <typename T>
556535
AutoFile& operator<<(const T& obj)

0 commit comments

Comments
 (0)