1 /*
2 BStone: A Source port of
3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike
4 
5 Copyright (c) 1992-2013 Apogee Entertainment, LLC
6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23 
24 
25 //
26 // A text reader for a stream.
27 //
28 
29 
30 #ifndef BSTONE_TEXT_READER_INCLUDED
31 #define BSTONE_TEXT_READER_INCLUDED
32 
33 
34 #include <array>
35 #include <string>
36 #include "bstone_istream.h"
37 
38 
39 namespace bstone
40 {
41 
42 
43 class TextReader final
44 {
45 public:
46     TextReader();
47 
48     TextReader(
49         IStream* stream);
50 
51     TextReader(
52         const TextReader& that) = delete;
53 
54     TextReader& operator=(
55         const TextReader& that) = delete;
56 
57     ~TextReader();
58 
59 
60     bool open(
61         IStream* stream);
62 
63     void close();
64 
65     bool is_open() const;
66 
67 
68     bool is_eos() const;
69 
70 
71     std::string read_line();
72 
73 
74 private:
75     static constexpr int max_buffer_size = 4096;
76 
77 
78     using Buffer = std::array<char, max_buffer_size>;
79 
80 
81     IStream* stream_;
82     bool is_eos_;
83     int buffer_offset_;
84     int buffer_size_;
85     Buffer buffer_;
86     int char_buffer_;
87 
88     int peek_char();
89 }; // TextReader
90 
91 
92 } // bstone
93 
94 
95 #endif // BSTONE_TEXT_READER_INCLUDED
96