2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2014-08-07 18:54:06 +02:00
|
|
|
|
2022-05-10 16:05:27 +02:00
|
|
|
#pragma once
|
2014-08-07 18:54:06 +02:00
|
|
|
|
|
|
|
#include "util/DynamicFifoBuffer.hxx"
|
|
|
|
|
2020-03-13 00:46:28 +01:00
|
|
|
#include <cstddef>
|
2022-05-10 16:05:27 +02:00
|
|
|
#include <span>
|
2014-08-07 18:54:06 +02:00
|
|
|
|
|
|
|
class Reader;
|
|
|
|
|
|
|
|
class BufferedReader {
|
2021-12-07 11:49:59 +01:00
|
|
|
static constexpr std::size_t MAX_SIZE = 512 * 1024;
|
2014-08-07 18:54:06 +02:00
|
|
|
|
|
|
|
Reader &reader;
|
|
|
|
|
|
|
|
DynamicFifoBuffer<char> buffer;
|
|
|
|
|
2017-01-04 10:37:34 +01:00
|
|
|
bool eof = false;
|
2014-08-07 18:54:06 +02:00
|
|
|
|
2017-01-04 10:37:34 +01:00
|
|
|
unsigned line_number = 0;
|
2015-01-21 23:43:32 +01:00
|
|
|
|
2014-08-07 18:54:06 +02:00
|
|
|
public:
|
2018-08-21 18:52:57 +02:00
|
|
|
explicit BufferedReader(Reader &_reader) noexcept
|
2020-04-02 17:17:06 +02:00
|
|
|
:reader(_reader), buffer(16384) {}
|
2014-08-07 18:54:06 +02:00
|
|
|
|
2016-08-27 12:09:57 +02:00
|
|
|
/**
|
|
|
|
* Reset the internal state. Should be called after rewinding
|
|
|
|
* the underlying #Reader.
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void Reset() noexcept {
|
2016-08-27 12:09:57 +02:00
|
|
|
buffer.Clear();
|
|
|
|
eof = false;
|
|
|
|
line_number = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-07 18:54:06 +02:00
|
|
|
bool Fill(bool need_more);
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2022-05-10 16:05:27 +02:00
|
|
|
std::span<std::byte> Read() const noexcept {
|
2022-05-10 17:26:41 +02:00
|
|
|
return std::as_writable_bytes(buffer.Read());
|
2014-08-07 18:54:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:04 +02:00
|
|
|
/**
|
|
|
|
* Read a buffer of exactly the given size (without consuming
|
|
|
|
* it). Throws std::runtime_error if not enough data is
|
|
|
|
* available.
|
|
|
|
*/
|
2021-12-07 11:49:59 +01:00
|
|
|
void *ReadFull(std::size_t size);
|
2016-08-16 12:09:04 +02:00
|
|
|
|
2021-12-07 11:49:59 +01:00
|
|
|
void Consume(std::size_t n) noexcept {
|
2014-08-07 18:54:06 +02:00
|
|
|
buffer.Consume(n);
|
|
|
|
}
|
|
|
|
|
2016-08-16 08:46:44 +02:00
|
|
|
/**
|
|
|
|
* Read (and consume) data from the input buffer into the
|
|
|
|
* given buffer. Does not attempt to refill the buffer.
|
|
|
|
*/
|
2022-05-10 16:05:27 +02:00
|
|
|
std::size_t ReadFromBuffer(std::span<std::byte> dest) noexcept;
|
2016-08-16 08:46:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read data into the given buffer and consume it from our
|
|
|
|
* buffer. Throw an exception if the request cannot be
|
|
|
|
* forfilled.
|
|
|
|
*/
|
2022-05-10 16:05:27 +02:00
|
|
|
void ReadFull(std::span<std::byte> dest);
|
2016-08-16 08:46:44 +02:00
|
|
|
|
2022-05-11 11:53:48 +02:00
|
|
|
template<typename T>
|
|
|
|
void ReadFullT(T &dest) {
|
2023-09-12 09:32:09 +02:00
|
|
|
ReadFull(std::as_writable_bytes(std::span{&dest, 1}));
|
2022-05-11 11:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T ReadFullT() {
|
|
|
|
T dest;
|
|
|
|
ReadFullT<T>(dest);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2014-08-07 18:54:06 +02:00
|
|
|
char *ReadLine();
|
2015-01-21 23:43:32 +01:00
|
|
|
|
2017-06-03 21:33:44 +02:00
|
|
|
unsigned GetLineNumber() const noexcept {
|
2015-01-21 23:43:32 +01:00
|
|
|
return line_number;
|
|
|
|
}
|
2014-08-07 18:54:06 +02:00
|
|
|
};
|