KiCad PCB EDA Suite
STRING_LINE_READER Class Reference

Is a LINE_READER that reads from a multiline 8 bit wide std::string. More...

#include <richio.h>

Inheritance diagram for STRING_LINE_READER:
LINE_READER

Public Member Functions

 STRING_LINE_READER (const std::string &aString, const wxString &aSource)
 Construct a string line reader. More...
 
 STRING_LINE_READER (const STRING_LINE_READER &aStartingPoint)
 Construct a string line reader. More...
 
char * ReadLine () override
 Read a line of text into the buffer and increments the line number counter. 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

std::string m_lines
 
size_t m_ndx
 
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...
 

Detailed Description

Is a LINE_READER that reads from a multiline 8 bit wide std::string.

Definition at line 240 of file richio.h.

Constructor & Destructor Documentation

◆ STRING_LINE_READER() [1/2]

STRING_LINE_READER::STRING_LINE_READER ( const std::string &  aString,
const wxString &  aSource 
)

Construct a string line reader.

Parameters
aStringis a source string consisting of one or more lines of text, where multiple lines are separated with a '
' character. The last line does not necessarily need a trailing '
'.
aSourcedescribes the source of aString for error reporting purposes can be anything meaningful, such as wxT( "clipboard" ).

Definition at line 248 of file richio.cpp.

248  :
250  m_lines( aString ), m_ndx( 0 )
251 {
252  // Clipboard text should be nice and _use multiple lines_ so that
253  // we can report _line number_ oriented error messages when parsing.
254  m_source = aSource;
255 }
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:73
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:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163
std::string m_lines
Definition: richio.h:243

References LINE_READER::m_source.

◆ STRING_LINE_READER() [2/2]

STRING_LINE_READER::STRING_LINE_READER ( const STRING_LINE_READER aStartingPoint)

Construct a string line reader.

Allows for a continuation of the reading of a stream started by another STRING_LINE_READER. Any stream offset and source name are used from

Definition at line 258 of file richio.cpp.

258  :
260  m_lines( aStartingPoint.m_lines ),
261  m_ndx( aStartingPoint.m_ndx )
262 {
263  // since we are keeping the same "source" name, for error reporting purposes
264  // we need to have the same notion of line number and offset.
265 
266  m_source = aStartingPoint.m_source;
267  m_lineNum = aStartingPoint.m_lineNum;
268 }
unsigned m_lineNum
Definition: richio.h:156
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:73
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:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163
std::string m_lines
Definition: richio.h:243

References LINE_READER::m_lineNum, and LINE_READER::m_source.

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 135 of file richio.cpp.

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

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

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

◆ GetSource()

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

Returns the name of the source of the lines in an abstract sense.

This may be a file or it may be the clipboard or any other source of lines of text. The returned string is useful for reporting error messages.

Reimplemented in WHITESPACE_FILTER_READER, and FILTER_READER.

Definition at line 109 of file richio.h.

110  {
111  return m_source;
112  }
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163

References LINE_READER::m_source.

Referenced by LEGACY_PLUGIN::biuParse(), LEGACY_PLUGIN::checkVersion(), DSNLEXER::CurSource(), LEGACY_PLUGIN::degParse(), WRLPROC::GetFileName(), FILTER_READER::GetSource(), WHITESPACE_FILTER_READER::GetSource(), CMP_READER::Load(), LEGACY_NETLIST_READER::loadComponent(), LEGACY_NETLIST_READER::loadFootprintFilters(), LEGACY_NETLIST_READER::loadNet(), SCH_LEGACY_PLUGIN_CACHE::LoadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), LEGACY_PLUGIN::loadSETUP(), SCH_LEGACY_PLUGIN::loadSymbol(), GPCB_FPL_CACHE::parseFOOTPRINT(), KICAD_NETLIST_PARSER::parseNet(), and WRLPROC::WRLPROC().

◆ Length()

unsigned LINE_READER::Length ( ) const
inlineinherited

Return the number of bytes in the last line read from this LINE_READER.

Definition at line 143 of file richio.h.

144  {
145  return m_length;
146  }
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155

References LINE_READER::m_length.

Referenced by LEGACY_NETLIST_READER::loadComponent(), LEGACY_NETLIST_READER::loadFootprintFilters(), LEGACY_NETLIST_READER::loadNet(), KICAD_NETLIST_PARSER::parseNet(), FILTER_READER::ReadLine(), WHITESPACE_FILTER_READER::ReadLine(), and DSNLEXER::readLine().

◆ 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  }
char * m_line
the read line of UTF8 text
Definition: richio.h:158

References LINE_READER::m_line.

Referenced by LEGACY_PLUGIN::biuParse(), LEGACY_PLUGIN::checkVersion(), LEGACY_PLUGIN::degParse(), NETLIST_READER::GuessNetlistFileType(), 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::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::loadHeader(), SCH_LEGACY_PLUGIN_CACHE::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(), FILTER_READER::ReadLine(), DSNLEXER::readLine(), and LP_CACHE::SkipIndex().

◆ 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 * STRING_LINE_READER::ReadLine ( )
overridevirtual

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 271 of file richio.cpp.

272 {
273  size_t nlOffset = m_lines.find( '\n', m_ndx );
274 
275  if( nlOffset == std::string::npos )
276  m_length = m_lines.length() - m_ndx;
277  else
278  m_length = nlOffset - m_ndx + 1; // include the newline, so +1
279 
280  if( m_length )
281  {
282  if( m_length >= m_maxLineLength )
283  THROW_IO_ERROR( _("Line length exceeded") );
284 
285  if( m_length+1 > m_capacity ) // +1 for terminating nul
287 
288  wxASSERT( m_ndx + m_length <= m_lines.length() );
289 
290  memcpy( m_line, &m_lines[m_ndx], m_length );
291  m_ndx += m_length;
292  }
293 
294  ++m_lineNum; // this gets incremented even if no bytes were read
295  m_line[m_length] = 0;
296 
297  return m_length ? m_line : nullptr;
298 }
char * m_line
the read line of UTF8 text
Definition: richio.h:158
unsigned m_lineNum
Definition: richio.h:156
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
void expandCapacity(unsigned aNewsize)
Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assu...
Definition: richio.cpp:135
#define _(s)
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
std::string m_lines
Definition: richio.h:243
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161

References _, LINE_READER::expandCapacity(), LINE_READER::m_capacity, LINE_READER::m_length, LINE_READER::m_line, LINE_READER::m_lineNum, m_lines, LINE_READER::m_maxLineLength, m_ndx, and THROW_IO_ERROR.

Member Data Documentation

◆ m_capacity

unsigned LINE_READER::m_capacity
protectedinherited

◆ m_length

unsigned LINE_READER::m_length
protectedinherited

◆ m_line

◆ m_lineNum

◆ m_lines

std::string STRING_LINE_READER::m_lines
protected

Definition at line 243 of file richio.h.

Referenced by ReadLine().

◆ 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(), ReadLine(), and INPUTSTREAM_LINE_READER::ReadLine().

◆ m_ndx

size_t STRING_LINE_READER::m_ndx
protected

Definition at line 244 of file richio.h.

Referenced by ReadLine().

◆ m_source

wxString LINE_READER::m_source
protectedinherited

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