KiCad PCB EDA Suite
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 (C) 2018 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <ios>
27
29 LINE_READER( 0 ),
30 m_stream( nullptr )
31{
32 m_line = nullptr;
33 m_lineNum = 0;
34}
35
36
38{
39 // this is only a view into a string, it can't be deleted by the base
40 m_line = nullptr;
41}
42
43
45{
46 getline( *m_stream, m_buffer );
47
48 m_buffer.append( 1, '\n' );
49
50 m_length = m_buffer.size();
51 m_line = (char*) m_buffer.data(); //ew why no const??
52
53 // lineNum is incremented even if there was no line read, because this
54 // leads to better error reporting when we hit an end of file.
55 ++m_lineNum;
56
57 return m_stream->eof() ? nullptr : m_line;
58}
59
60
61void STDISTREAM_LINE_READER::SetStream( std::istream& aStream )
62{
63 // Could be done with a virtual getStream function, but the
64 // virtual function call is a noticeable (but minor) penalty within
65 // ReadLine() in tight loops
66 m_stream = &aStream;
67}
68
69
70IFSTREAM_LINE_READER::IFSTREAM_LINE_READER( const wxFileName& aFileName ) :
71 m_fStream( aFileName.GetFullPath().ToUTF8() )
72{
73 if( !m_fStream.is_open() )
74 {
75 wxString msg = wxString::Format(
76 _( "Unable to open filename \"%s\" for reading" ), aFileName.GetFullPath().GetData() );
77 THROW_IO_ERROR( msg );
78 }
79
81
82 m_source = aFileName.GetFullPath();
83}
84
85
87{
88 m_fStream.clear() ;
89 m_fStream.seekg(0, std::ios::beg );
90}
IFSTREAM_LINE_READER(const wxFileName &aFileName)
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
char * m_line
the read line of UTF8 text
Definition: richio.h:158
unsigned m_lineNum
Definition: richio.h:156
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163
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.
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200