KiCad PCB EDA Suite
Loading...
Searching...
No Matches
stdstream_line_reader.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <ios>
23
25 LINE_READER( 0 ),
26 m_stream( nullptr )
27{
28 m_line = nullptr;
29 m_lineNum = 0;
30}
31
32
34{
35 // this is only a view into a string, it can't be deleted by the base
36 m_line = nullptr;
37}
38
39
41{
42 getline( *m_stream, m_buffer );
43
44 m_buffer.append( 1, '\n' );
45
46 m_length = m_buffer.size();
47 m_line = (char*) m_buffer.data(); //ew why no const??
48
49 // lineNum is incremented even if there was no line read, because this
50 // leads to better error reporting when we hit an end of file.
51 ++m_lineNum;
52
53 return m_stream->eof() ? nullptr : m_line;
54}
55
56
57void STDISTREAM_LINE_READER::SetStream( std::istream& aStream )
58{
59 // Could be done with a virtual getStream function, but the
60 // virtual function call is a noticeable (but minor) penalty within
61 // ReadLine() in tight loops
62 m_stream = &aStream;
63}
64
65
66IFSTREAM_LINE_READER::IFSTREAM_LINE_READER( const wxFileName& aFileName ) :
67 m_fStream( aFileName.GetFullPath().fn_str() )
68{
69 if( !m_fStream.is_open() )
70 {
71 wxString msg = wxString::Format(
72 _( "Unable to open filename '%s' for reading" ), aFileName.GetFullPath().GetData() );
73 THROW_IO_ERROR( msg );
74 }
75
77
78 m_source = aFileName.GetFullPath();
79}
80
81
83{
84 m_fStream.clear() ;
85 m_fStream.seekg(0, std::ios::beg );
86}
IFSTREAM_LINE_READER(const wxFileName &aFileName)
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Build a line reader and fixes the length of the maximum supported line length to aMaxLineLength.
Definition richio.cpp:100
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:136
char * m_line
the read line of UTF8 text
Definition richio.h:139
unsigned m_lineNum
Definition richio.h:137
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:144
void SetStream(std::istream &aStream)
Set the stream for this line reader.
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.