cfgfile 0.2.11
Loading...
Searching...
No Matches
input_stream.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__INPUT_STREAM_HPP__INCLUDED
32#define CFGFILE__INPUT_STREAM_HPP__INCLUDED
33
34// cfgfile include.
35#include "types.hpp"
36#include "const.hpp"
37
38// C++ include.
39#include <stack>
40
41
42namespace cfgfile {
43
45static const std::size_t c_buff_size = 512;
46
47
48//
49// input_stream_t
50//
51
53template< typename Trait = string_trait_t >
55public:
56 input_stream_t( const typename Trait::string_t & file_name,
57 typename Trait::istream_t & input )
58 : m_stream( input )
59 , m_line_number( 1 )
60 , m_column_number( 1 )
61 , m_file_name( file_name )
62 , m_buf_pos( 0 )
63 , m_stream_pos( 0 )
64 {
65 Trait::noskipws( m_stream );
66
67 m_stream_size = Trait::size_of_file( m_stream );
68
69 if( m_stream_size < 0 )
70 m_stream_size = 0;
71
72 Trait::fill_buf( m_stream, m_buf, c_buff_size, m_stream_pos, m_stream_size );
73 }
74
76 {
77 }
78
80 typename Trait::char_t get()
81 {
82 if( !at_end() )
83 {
84 m_prev_positions.push( { m_column_number, m_line_number } );
85
86 m_column_number += 1;
87
88 typename Trait::char_t ch( 0x00 );
89
90 if( !m_returned_char.empty() )
91 {
92 ch = m_returned_char.top();
93
94 m_returned_char.pop();
95
96 if( is_new_line( ch ) )
97 {
98 m_line_number += 1;
99 m_column_number = 1;
100 }
101
102 return ch;
103 }
104
105 if( m_buf_pos == c_buff_size )
106 {
107 Trait::fill_buf( m_stream, m_buf, c_buff_size, m_stream_pos, m_stream_size );
108 m_buf_pos = 0;
109 }
110
111 ch = m_buf[ m_buf_pos ];
112
113 ++m_buf_pos;
114
115 if( is_new_line( ch ) )
116 {
117 m_line_number += 1;
118 m_column_number = 1;
119 }
120
121 return ch;
122 }
123 else
124 return typename Trait::char_t( 0x00 );
125 }
126
128 void put_back( typename Trait::char_t ch )
129 {
130 if( !m_prev_positions.empty() )
131 {
132 const auto prev = m_prev_positions.top();
133 m_prev_positions.pop();
134
135 m_column_number = prev.m_column_number;
136 m_line_number = prev.m_line_number;
137
138 m_returned_char.push( ch );
139 }
140 }
141
143 typename Trait::pos_t line_number() const
144 {
145 return m_line_number;
146 }
147
149 typename Trait::pos_t column_number() const
150 {
151 return m_column_number;
152 }
153
155 bool at_end() const
156 {
157 if( m_returned_char.empty() )
158 return ( Trait::is_at_end( m_stream ) && m_buf_pos == (size_t) m_buf.size() );
159 else
160 return false;
161 }
162
164 const typename Trait::string_t & file_name() const
165 {
166 return m_file_name;
167 }
168
169private:
170 bool is_new_line( typename Trait::char_t & ch )
171 {
173 return true;
175 {
176 typename Trait::char_t next_char( 0x00 );
177
178 next_char = simple_get();
179
181 {
182 ch = next_char;
183
184 return true;
185 }
186 else
187 {
188 m_returned_char.push( next_char );
189
190 return true;
191 }
192 }
193 else
194 return false;
195 }
196
197 typename Trait::char_t simple_get()
198 {
199 if( !m_returned_char.empty() )
200 {
201 auto ch = m_returned_char.top();
202
203 m_returned_char.pop();
204
205 return ch;
206 }
207 else
208 {
209 typename Trait::char_t ch( 0x00 );
210
211 if( m_buf_pos == c_buff_size )
212 {
213 Trait::fill_buf( m_stream, m_buf, c_buff_size, m_stream_pos, m_stream_size );
214 m_buf_pos = 0;
215 }
216
217 ch = m_buf[ m_buf_pos ];
218
219 ++m_buf_pos;
220
221 return ch;
222 }
223 }
224
225private:
226 DISABLE_COPY( input_stream_t )
227
228 struct position_t {
229 typename Trait::pos_t m_column_number;
230 typename Trait::pos_t m_line_number;
231 };
232
234 typename Trait::istream_t & m_stream;
236 typename Trait::pos_t m_line_number;
238 typename Trait::pos_t m_column_number;
240 std::stack< position_t > m_prev_positions;
242 typename Trait::string_t m_file_name;
244 std::stack< typename Trait::char_t > m_returned_char;
246 typename Trait::buf_t m_buf;
248 std::size_t m_buf_pos;
250 typename Trait::pos_t m_stream_size;
252 typename Trait::pos_t m_stream_pos;
253}; // class input_stream_t
254
255} /* namespace cfgfile */
256
257#endif // CFGFILE__INPUT_STREAM_HPP__INCLUDED
Exception in the library.
Definition exceptions.hpp:51
Input stream for parser.
Definition input_stream.hpp:54
Trait::char_t get()
Get a symbol from the stream.
Definition input_stream.hpp:80
~input_stream_t()
Definition input_stream.hpp:75
Trait::pos_t column_number() const
Definition input_stream.hpp:149
void put_back(typename Trait::char_t ch)
Put symbol back in the stream.
Definition input_stream.hpp:128
input_stream_t(const typename Trait::string_t &file_name, typename Trait::istream_t &input)
Definition input_stream.hpp:56
bool at_end() const
Definition input_stream.hpp:155
const Trait::string_t & file_name() const
Definition input_stream.hpp:164
Trait::pos_t line_number() const
Definition input_stream.hpp:143
Definition const.hpp:38
Definition const.hpp:45
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition types.hpp:580