MPD: fs/io/BufferedReader.hxx Source File

MPD 0.20.15

BufferedReader.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_BUFFERED_READER_HXX
21 #define MPD_BUFFERED_READER_HXX
22
23 #include "check.h"
24 #include "Compiler.h"
25 #include "util/DynamicFifoBuffer.hxx"
26
27 #include <stddef.h>
28
29 class Reader;
30
31 class BufferedReader {
32 static constexpr size_t MAX_SIZE = 512 * 1024;
33
34 Reader &reader;
35
36 DynamicFifoBuffer<char> buffer;
37
38 bool eof = false;
39
40 unsigned line_number = 0;
41
42 public:
43 explicit BufferedReader(Reader &_reader)
44 :reader(_reader), buffer(4096) {}
45
50 void Reset() noexcept {
51 buffer.Clear();
52 eof = false;
53 line_number = 0;
54 }
55
56 bool Fill(bool need_more);
57
58 gcc_pure
59 WritableBuffer<void> Read() const noexcept {
60 return buffer.Read().ToVoid();
61 }
62
68 void *ReadFull(size_t size);
69
70 void Consume(size_t n) noexcept {
71 buffer.Consume(n);
72 }
73
78 size_t ReadFromBuffer(WritableBuffer<void> dest) noexcept;
79
85 void ReadFull(WritableBuffer<void> dest);
86
87 char *ReadLine();
88
89 unsigned GetLineNumber() const noexcept {
90 return line_number;
91 }
92 };
93
94 #endif
BufferedReader::Read
gcc_pure WritableBuffer< void > Read() const noexcept
Definition: BufferedReader.hxx:59
Reader
An interface that can read bytes from a stream until the stream ends.
Definition: Reader.hxx:35
check.h
BufferedReader::BufferedReader
BufferedReader(Reader &_reader)
Definition: BufferedReader.hxx:43
ForeignFifoBuffer::Read
constexpr Range Read() const
Return a buffer range which may be read.
Definition: ForeignFifoBuffer.hxx:193
ForeignFifoBuffer::Clear
void Clear()
Definition: ForeignFifoBuffer.hxx:133
ForeignFifoBuffer::Consume
void Consume(size_type n)
Marks a chunk as consumed.
Definition: ForeignFifoBuffer.hxx:200
BufferedReader::GetLineNumber
unsigned GetLineNumber() const noexcept
Definition: BufferedReader.hxx:89
BufferedReader::Fill
bool Fill(bool need_more)
BufferedReader::ReadLine
char * ReadLine()
BufferedReader::ReadFull
void * ReadFull(size_t size)
Read a buffer of exactly the given size (without consuming it).
Compiler.h
WritableBuffer< void >
Definition: WritableBuffer.hxx:45
BufferedReader::ReadFromBuffer
size_t ReadFromBuffer(WritableBuffer< void > dest) noexcept
Read (and consume) data from the input buffer into the given buffer.
DynamicFifoBuffer.hxx
BufferedReader::Reset
void Reset() noexcept
Reset the internal state.
Definition: BufferedReader.hxx:50
DynamicFifoBuffer< char >
WritableBuffer::ToVoid
constexpr WritableBuffer< void > ToVoid() const
Definition: WritableBuffer.hxx:122
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
BufferedReader
Definition: BufferedReader.hxx:31
BufferedReader::Consume
void Consume(size_t n) noexcept
Definition: BufferedReader.hxx:70