KiCad PCB EDA Suite
IFSTREAM_LINE_READER Class Reference

LINE_READER interface backed by std::ifstream. More...

#include <stdstream_line_reader.h>

Inheritance diagram for IFSTREAM_LINE_READER:
STDISTREAM_LINE_READER LINE_READER

Public Member Functions

 IFSTREAM_LINE_READER (const wxFileName &aFileName)
 
void Rewind ()
 
char * ReadLine () override
 Read a line of text into the buffer and increments the line number counter. More...
 
void SetStream (std::istream &aStream)
 Set the stream for this line reader. More...
 
virtual const wxString & GetSource () const
 Returns the name of the source of the lines in an abstract sense. More...
 
char * Line () const
 Return a pointer to the last line that was read in. More...
 
 operator char * () const
 A casting operator that returns a char* pointer to the start of the line buffer. More...
 
virtual unsigned LineNumber () const
 Return the line number of the last line read from this LINE_READER. More...
 
unsigned Length () const
 Return the number of bytes in the last line read from this LINE_READER. More...
 

Protected Member Functions

void expandCapacity (unsigned aNewsize)
 Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assumptions of capacity after calling this. More...
 

Protected Attributes

unsigned m_length
 no. bytes in line before trailing nul. More...
 
unsigned m_lineNum
 
char * m_line
 the read line of UTF8 text More...
 
unsigned m_capacity
 no. bytes allocated for line. More...
 
unsigned m_maxLineLength
 maximum allowed capacity using resizing. More...
 
wxString m_source
 origin of text lines, e.g. filename or "clipboard" More...
 

Private Attributes

std::ifstream m_fStream
 
std::string m_buffer
 
std::istream * m_stream
 

Detailed Description

LINE_READER interface backed by std::ifstream.

Definition at line 61 of file stdstream_line_reader.h.

Constructor & Destructor Documentation

◆ IFSTREAM_LINE_READER()

IFSTREAM_LINE_READER::IFSTREAM_LINE_READER ( const wxFileName &  aFileName)

Definition at line 70 of file stdstream_line_reader.cpp.

70 :
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}
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.
#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

References _, Format(), m_fStream, LINE_READER::m_source, STDISTREAM_LINE_READER::SetStream(), and THROW_IO_ERROR.

Member Function Documentation

◆ expandCapacity()

void LINE_READER::expandCapacity ( unsigned  aNewsize)
protectedinherited

Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assumptions of capacity after calling this.

Definition at line 140 of file richio.cpp.

141{
142 // m_length can equal maxLineLength and nothing breaks, there's room for
143 // the terminating nul. cannot go over this.
144 if( aNewsize > m_maxLineLength+1 )
145 aNewsize = m_maxLineLength+1;
146
147 if( aNewsize > m_capacity )
148 {
149 m_capacity = aNewsize;
150
151 // resize the buffer, and copy the original data
152 // Be sure there is room for the null EOL char, so reserve capacity+1 bytes
153 // to ensure capacity line length. Use capacity+5 to cover and corner case
154 char* bigger = new char[m_capacity+5];
155
156 wxASSERT( m_capacity >= m_length+1 );
157
158 memcpy( bigger, m_line, m_length );
159 bigger[m_length] = 0;
160
161 delete[] m_line;
162 m_line = bigger;
163 }
164}
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
char * m_line
the read line of UTF8 text
Definition: richio.h:158

References LINE_READER::m_capacity, LINE_READER::m_length, LINE_READER::m_line, and LINE_READER::m_maxLineLength.

Referenced by FILE_LINE_READER::ReadLine(), STRING_LINE_READER::ReadLine(), and INPUTSTREAM_LINE_READER::ReadLine().

◆ GetSource()

virtual const wxString & LINE_READER::GetSource ( ) const
inlinevirtualinherited

◆ Length()

unsigned LINE_READER::Length ( ) const
inlineinherited

◆ Line()

char * LINE_READER::Line ( ) const
inlineinherited

Return a pointer to the last line that was read in.

Definition at line 117 of file richio.h.

118 {
119 return m_line;
120 }

References LINE_READER::m_line.

Referenced by bench_string_lr(), bench_string_lr_reuse(), bench_wxbis(), bench_wxbis_reuse(), bench_wxis(), bench_wxis_reuse(), LEGACY_PLUGIN::biuParse(), LEGACY_PLUGIN::checkVersion(), LEGACY_PLUGIN::degParse(), detect_file_type(), NETLIST_READER::GuessNetlistFileType(), SCH_LEGACY_PLUGIN_CACHE::Load(), CMP_READER::Load(), SCH_LEGACY_PLUGIN_CACHE::loadAliases(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), SCH_LEGACY_PLUGIN_CACHE::loadBezier(), SCH_LEGACY_PLUGIN::loadBitmap(), SCH_LEGACY_PLUGIN::loadBusAlias(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN_CACHE::loadCircle(), SCH_LEGACY_PLUGIN::LoadContent(), BITMAP_BASE::LoadData(), SCH_LEGACY_PLUGIN_CACHE::loadDocs(), SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries(), SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN::loadFile(), EXCELLON_IMAGE::LoadFile(), SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters(), LEGACY_PLUGIN::loadFP_SHAPE(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN::loadJunction(), LEGACY_PLUGIN::loadMODULE_TEXT(), LP_CACHE::LoadModules(), LEGACY_NETLIST_READER::LoadNetlist(), SCH_LEGACY_PLUGIN::loadNoConnect(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::LoadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), SCH_LEGACY_PLUGIN_CACHE::loadPolyLine(), SCH_LEGACY_PLUGIN_CACHE::loadRect(), SCH_LEGACY_PLUGIN::loadSymbol(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN_CACHE::loadText(), LEGACY_PLUGIN::loadTrackList(), SCH_LEGACY_PLUGIN::loadWire(), LINE_READER::operator char *(), KICAD_NETLIST_PARSER::parseNet(), GPCB_FPL_CACHE::parseParameters(), DSNLEXER::PopReader(), SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile(), MWAVE_POLYGONAL_SHAPE_DLG::ReadDataShapeDescr(), DSNLEXER::readLine(), FILTER_READER::ReadLine(), LP_CACHE::SkipIndex(), EXCELLON_IMAGE::TestFileIsExcellon(), and GERBER_FILE_IMAGE::TestFileIsRS274().

◆ LineNumber()

◆ operator char *()

LINE_READER::operator char * ( ) const
inlineinherited

A casting operator that returns a char* pointer to the start of the line buffer.

Definition at line 125 of file richio.h.

126 {
127 return Line();
128 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117

References LINE_READER::Line().

◆ ReadLine()

char * STDISTREAM_LINE_READER::ReadLine ( )
overridevirtualinherited

Read a line of text into the buffer and increments the line number counter.

If the line is larger than the maximum length passed to the constructor, then an exception is thrown. The line is nul terminated.

Returns
The beginning of the read line, or NULL if EOF.
Exceptions
IO_ERRORwhen a line is too long.

Implements LINE_READER.

Definition at line 44 of file stdstream_line_reader.cpp.

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}

References STDISTREAM_LINE_READER::m_buffer, LINE_READER::m_length, LINE_READER::m_line, LINE_READER::m_lineNum, and STDISTREAM_LINE_READER::m_stream.

◆ Rewind()

void IFSTREAM_LINE_READER::Rewind ( )

Definition at line 86 of file stdstream_line_reader.cpp.

87{
88 m_fStream.clear() ;
89 m_fStream.seekg(0, std::ios::beg );
90}

References m_fStream.

◆ SetStream()

void STDISTREAM_LINE_READER::SetStream ( std::istream &  aStream)
inherited

Set the stream for this line reader.

Parameters
aStreama stream to read

Definition at line 61 of file stdstream_line_reader.cpp.

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}

References STDISTREAM_LINE_READER::m_stream.

Referenced by IFSTREAM_LINE_READER(), parse(), KI_TEST::ReadBoardItemFromStream(), and KI_TEST::ReadSchematicFromStream().

Member Data Documentation

◆ m_buffer

std::string STDISTREAM_LINE_READER::m_buffer
privateinherited

Definition at line 53 of file stdstream_line_reader.h.

Referenced by STDISTREAM_LINE_READER::ReadLine().

◆ m_capacity

unsigned LINE_READER::m_capacity
protectedinherited

◆ m_fStream

std::ifstream IFSTREAM_LINE_READER::m_fStream
private

Definition at line 69 of file stdstream_line_reader.h.

Referenced by IFSTREAM_LINE_READER(), and Rewind().

◆ m_length

◆ m_line

◆ m_lineNum

◆ m_maxLineLength

unsigned LINE_READER::m_maxLineLength
protectedinherited

maximum allowed capacity using resizing.

Definition at line 161 of file richio.h.

Referenced by LINE_READER::expandCapacity(), FILE_LINE_READER::ReadLine(), STRING_LINE_READER::ReadLine(), and INPUTSTREAM_LINE_READER::ReadLine().

◆ m_source

wxString LINE_READER::m_source
protectedinherited

◆ m_stream

std::istream* STDISTREAM_LINE_READER::m_stream
privateinherited

The documentation for this class was generated from the following files: