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"
26 
27 #include <stddef.h>
28 
29 class Reader;
30 
32  static constexpr size_t MAX_SIZE = 512 * 1024;
33 
34  Reader &reader;
35 
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
gcc_pure WritableBuffer< void > Read() const noexcept
An interface that can read bytes from a stream until the stream ends.
Definition: Reader.hxx:35
BufferedReader(Reader &_reader)
constexpr Range Read() const
Return a buffer range which may be read.
void Consume(size_type n)
Marks a chunk as consumed.
unsigned GetLineNumber() const noexcept
bool Fill(bool need_more)
char * ReadLine()
void * ReadFull(size_t size)
Read a buffer of exactly the given size (without consuming it).
size_t ReadFromBuffer(WritableBuffer< void > dest) noexcept
Read (and consume) data from the input buffer into the given buffer.
void Reset() noexcept
Reset the internal state.
constexpr WritableBuffer< void > ToVoid() const
#define gcc_pure
Definition: Compiler.h:116
void Consume(size_t n) noexcept