KiCad PCB EDA Suite
FILE_LINE_READER Class Reference

FILE_LINE_READER is a LINE_READER that reads from an open file. More...

#include <richio.h>

Inheritance diagram for FILE_LINE_READER:
LINE_READER

Public Member Functions

 FILE_LINE_READER (const wxString &aFileName, unsigned aStartingLineNumber=0, unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
 Constructor FILE_LINE_READER takes aFileName and the size of the desired line buffer and opens the file and assumes the obligation to close it. More...
 
 FILE_LINE_READER (FILE *aFile, const wxString &aFileName, bool doOwn=true, unsigned aStartingLineNumber=0, unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
 Constructor FILE_LINE_READER takes an open FILE and the size of the desired line buffer and takes ownership of the open file, i.e. More...
 
 ~FILE_LINE_READER ()
 Destructor may or may not close the open file, depending on doOwn in constructor. More...
 
char * ReadLine () override
 Function ReadLine reads a line of text into the buffer and increments the line number counter. More...
 
void Rewind ()
 Function Rewind rewinds the file and resets the line number back to zero. More...
 
virtual const wxString & GetSource () const
 Function GetSource returns the name of the source of the lines in an abstract sense. More...
 
char * Line () const
 Function Line returns a pointer to the last line that was read in. More...
 
 operator char * () const
 Operator char* is a casting operator that returns a char* pointer to the start of the line buffer. More...
 
virtual unsigned LineNumber () const
 Function Line Number returns the line number of the last line read from this LINE_READER. More...
 
unsigned Length () const
 Function Length returns the number of bytes in the last line read from this LINE_READER. More...
 

Protected Member Functions

void expandCapacity (unsigned aNewsize)
 Function expandCapacity 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

bool m_iOwn
 if I own the file, I'll promise to close it, else not. More...
 
FILE * m_fp
 I may own this file, but might not. More...
 
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

FILE_LINE_READER is a LINE_READER that reads from an open file.

File must be already open so that this class can exist without any UI policy.

Definition at line 181 of file richio.h.

Constructor & Destructor Documentation

◆ FILE_LINE_READER() [1/2]

FILE_LINE_READER::FILE_LINE_READER ( const wxString &  aFileName,
unsigned  aStartingLineNumber = 0,
unsigned  aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX 
)

Constructor FILE_LINE_READER takes aFileName and the size of the desired line buffer and opens the file and assumes the obligation to close it.

Parameters
aFileNameis the name of the file to open and to use for error reporting purposes.
aStartingLineNumberis the initial line number to report on error, and is accessible here for the case where multiple DSNLEXERs are reading from the same file in sequence, all from the same open file (with doOwn = false). Internally it is incremented by one after each ReadLine(), so the first reported line number will always be one greater than what is provided here.
aMaxLineLengthis the number of bytes to use in the line buffer.
Exceptions
IO_ERRORif aFileName cannot be opened.

Definition at line 162 of file richio.cpp.

163  :
164  LINE_READER( aMaxLineLength ), m_iOwn( true )
165 {
166  m_fp = wxFopen( aFileName, wxT( "rt" ) );
167 
168  if( !m_fp )
169  {
170  wxString msg = wxString::Format(
171  _( "Unable to open filename \"%s\" for reading" ), aFileName.GetData() );
172  THROW_IO_ERROR( msg );
173  }
174 
175  m_source = aFileName;
176  m_lineNum = aStartingLineNumber;
177 }
unsigned m_lineNum
Definition: richio.h:86
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Constructor LINE_READER builds a line reader and fixes the length of the maximum supported line lengt...
Definition: richio.cpp:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:93
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
#define _(s)
Definition: 3d_actions.cpp:33
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:185
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References _, Format(), m_fp, LINE_READER::m_lineNum, LINE_READER::m_source, and THROW_IO_ERROR.

◆ FILE_LINE_READER() [2/2]

FILE_LINE_READER::FILE_LINE_READER ( FILE *  aFile,
const wxString &  aFileName,
bool  doOwn = true,
unsigned  aStartingLineNumber = 0,
unsigned  aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX 
)

Constructor FILE_LINE_READER takes an open FILE and the size of the desired line buffer and takes ownership of the open file, i.e.

assumes the obligation to close it.

Parameters
aFileis an open file.
aFileNameis the name of the file for error reporting purposes.
doOwnif true, means I should close the open file, else not.
aStartingLineNumberis the initial line number to report on error, and is accessible here for the case where multiple DSNLEXERs are reading from the same file in sequence, all from the same open file (with doOwn = false). Internally it is incremented by one after each ReadLine(), so the first reported line number will always be one greater than what is provided here.
aMaxLineLengthis the number of bytes to use in the line buffer.

Definition at line 180 of file richio.cpp.

183  :
184  LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
185 {
186  m_source = aFileName;
187  m_lineNum = aStartingLineNumber;
188 }
unsigned m_lineNum
Definition: richio.h:86
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Constructor LINE_READER builds a line reader and fixes the length of the maximum supported line lengt...
Definition: richio.cpp:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:93
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:185

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

◆ ~FILE_LINE_READER()

FILE_LINE_READER::~FILE_LINE_READER ( )

Destructor may or may not close the open file, depending on doOwn in constructor.

Definition at line 191 of file richio.cpp.

192 {
193  if( m_iOwn && m_fp )
194  fclose( m_fp );
195 }
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:185

References m_fp, and m_iOwn.

Member Function Documentation

◆ expandCapacity()

void LINE_READER::expandCapacity ( unsigned  aNewsize)
protectedinherited

Function expandCapacity 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 lenght. 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:88
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:89
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:85
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:91

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

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

◆ GetSource()

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

Function GetSource 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 131 of file richio.h.

132  {
133  return m_source;
134  }
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:93

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(), SCH_LEGACY_PLUGIN::loadComponent(), 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(), GPCB_FPL_CACHE::parseMODULE(), KICAD_NETLIST_PARSER::parseNet(), and WRLPROC::WRLPROC().

◆ Length()

unsigned LINE_READER::Length ( ) const
inlineinherited

Function Length returns the number of bytes in the last line read from this LINE_READER.

Definition at line 169 of file richio.h.

170  {
171  return m_length;
172  }
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:85

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

Function Line returns a pointer to the last line that was read in.

Definition at line 140 of file richio.h.

141  {
142  return m_line;
143  }
char * m_line
the read line of UTF8 text
Definition: richio.h:88

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::loadComponent(), 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(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), SCH_LEGACY_PLUGIN::loadJunction(), LEGACY_PLUGIN::loadMODULE_EDGE(), 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::loadRectangle(), 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

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

Definition at line 150 of file richio.h.

151  {
152  return Line();
153  }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140

References LINE_READER::Line().

◆ ReadLine()

char * FILE_LINE_READER::ReadLine ( )
overridevirtual

Function ReadLine reads a line of text into the buffer and increments the line number counter.

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

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

Implements LINE_READER.

Definition at line 198 of file richio.cpp.

199 {
200  m_length = 0;
201 
202  for(;;)
203  {
204  if( m_length >= m_maxLineLength )
205  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
206 
207  if( m_length >= m_capacity )
208  expandCapacity( m_capacity * 2 );
209 
210  // faster, POSIX compatible fgetc(), no locking.
211  int cc = getc_unlocked( m_fp );
212 
213  if( cc == EOF )
214  break;
215 
216  m_line[ m_length++ ] = (char) cc;
217 
218  if( cc == '\n' )
219  break;
220  }
221 
222  m_line[ m_length ] = 0;
223 
224  // m_lineNum is incremented even if there was no line read, because this
225  // leads to better error reporting when we hit an end of file.
226  ++m_lineNum;
227 
228  return m_length ? m_line : NULL;
229 }
char * m_line
the read line of UTF8 text
Definition: richio.h:88
unsigned m_lineNum
Definition: richio.h:86
#define NULL
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:89
void expandCapacity(unsigned aNewsize)
Function expandCapacity will expand the capacity of line up to maxLineLength but not greater,...
Definition: richio.cpp:135
#define getc_unlocked
Definition: richio.cpp:38
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186
#define _(s)
Definition: 3d_actions.cpp:33
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:85
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:91

References _, LINE_READER::expandCapacity(), getc_unlocked, LINE_READER::m_capacity, m_fp, LINE_READER::m_length, LINE_READER::m_line, LINE_READER::m_lineNum, LINE_READER::m_maxLineLength, NULL, and THROW_IO_ERROR.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadDocs(), EXCELLON_IMAGE::LoadFile(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile(), and GERBER_JOBFILE_READER::ReadGerberJobFile().

◆ Rewind()

void FILE_LINE_READER::Rewind ( )
inline

Function Rewind rewinds the file and resets the line number back to zero.

Line number will go to 1 on first ReadLine().

Definition at line 243 of file richio.h.

244  {
245  rewind( m_fp );
246  m_lineNum = 0;
247  }
unsigned m_lineNum
Definition: richio.h:86
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186

References m_fp, and LINE_READER::m_lineNum.

Member Data Documentation

◆ m_capacity

unsigned LINE_READER::m_capacity
protectedinherited

◆ m_fp

FILE* FILE_LINE_READER::m_fp
protected

I may own this file, but might not.

Definition at line 186 of file richio.h.

Referenced by FILE_LINE_READER(), ReadLine(), Rewind(), and ~FILE_LINE_READER().

◆ m_iOwn

bool FILE_LINE_READER::m_iOwn
protected

if I own the file, I'll promise to close it, else not.

Definition at line 185 of file richio.h.

Referenced by ~FILE_LINE_READER().

◆ m_length

unsigned LINE_READER::m_length
protectedinherited

◆ m_line

◆ m_lineNum

◆ m_maxLineLength

unsigned LINE_READER::m_maxLineLength
protectedinherited

maximum allowed capacity using resizing.

Definition at line 91 of file richio.h.

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

◆ m_source

wxString LINE_READER::m_source
protectedinherited

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