Bitcoin ABC 0.32.4
P2P Digital Currency
streams.cpp
Go to the documentation of this file.
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
9 if (!m_file) {
10 throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
11 }
12 return std::fread(dst.data(), 1, dst.size(), m_file);
13}
14
16 if (detail_fread(dst) != dst.size()) {
17 throw std::ios_base::failure(feof() ? "AutoFile::read: end of file"
18 : "AutoFile::read: fread failed");
19 }
20}
21
22void AutoFile::ignore(size_t nSize) {
23 if (!m_file) {
24 throw std::ios_base::failure(
25 "AutoFile::ignore: file handle is nullptr");
26 }
27 uint8_t data[4096];
28 while (nSize > 0) {
29 size_t nNow = std::min<size_t>(nSize, sizeof(data));
30 if (std::fread(data, 1, nNow, m_file) != nNow) {
31 throw std::ios_base::failure(
32 feof() ? "AutoFile::ignore: end of file"
33 : "AutoFile::ignore: fread failed");
34 }
35 nSize -= nNow;
36 }
37}
38
40 if (!m_file) {
41 throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
42 }
43 if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
44 throw std::ios_base::failure("AutoFile::write: write failed");
45 }
46}
bool feof() const
Definition: streams.h:539
void ignore(size_t nSize)
Definition: streams.cpp:22
std::FILE * m_file
Definition: streams.h:528
void read(Span< std::byte > dst)
Definition: streams.cpp:15
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:8
void write(Span< const std::byte > src)
Definition: streams.cpp:39
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
constexpr std::size_t size() const noexcept
Definition: span.h:210
constexpr C * data() const noexcept
Definition: span.h:199