KiCad PCB EDA Suite
FILE_LINE_READER Class Reference

Class 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 newsize)
 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 iOwn
 if I own the file, I'll promise to close it, else not. More...
 
FILE * fp
 I may own this file, but might not. More...
 
unsigned length
 no. bytes in line before trailing nul. More...
 
unsigned lineNum
 
char * line
 the read line of UTF8 text More...
 
unsigned capacity
 no. bytes allocated for line. More...
 
unsigned maxLineLength
 maximum allowed capacity using resizing. More...
 
wxString source
 origin of text lines, e.g. filename or "clipboard" More...
 

Detailed Description

Class 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 180 of file richio.h.

Constructor & Destructor Documentation

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

References Format(), fp, LINE_READER::lineNum, LINE_READER::source, and THROW_IO_ERROR.

159  :
160  LINE_READER( aMaxLineLength ),
161  iOwn( true )
162 {
163  fp = wxFopen( aFileName, wxT( "rt" ) );
164  if( !fp )
165  {
166  wxString msg = wxString::Format(
167  _( "Unable to open filename '%s' for reading" ), aFileName.GetData() );
168  THROW_IO_ERROR( msg );
169  }
170 
171  source = aFileName;
172  lineNum = aStartingLineNumber;
173 }
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
unsigned lineNum
Definition: richio.h:85
bool iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:184
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:103
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:205
wxString source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:92
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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 176 of file richio.cpp.

References LINE_READER::lineNum, and LINE_READER::source.

179  :
180  LINE_READER( aMaxLineLength ),
181  iOwn( doOwn ),
182  fp( aFile )
183 {
184  source = aFileName;
185  lineNum = aStartingLineNumber;
186 }
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
unsigned lineNum
Definition: richio.h:85
bool iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:184
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:103
wxString source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:92
FILE_LINE_READER::~FILE_LINE_READER ( )

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

Definition at line 189 of file richio.cpp.

References fp, and iOwn.

190 {
191  if( iOwn && fp )
192  fclose( fp );
193 }
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
bool iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:184

Member Function Documentation

void LINE_READER::expandCapacity ( unsigned  newsize)
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 132 of file richio.cpp.

References LINE_READER::capacity, LINE_READER::length, LINE_READER::line, and LINE_READER::maxLineLength.

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

133 {
134  // length can equal maxLineLength and nothing breaks, there's room for
135  // the terminating nul. cannot go over this.
136  if( newsize > maxLineLength+1 )
137  newsize = maxLineLength+1;
138 
139  if( newsize > capacity )
140  {
141  capacity = newsize;
142 
143  // resize the buffer, and copy the original data
144  char* bigger = new char[capacity];
145 
146  wxASSERT( capacity >= length+1 );
147 
148  memcpy( bigger, line, length );
149  bigger[length] = 0;
150 
151  delete[] line;
152  line = bigger;
153  }
154 }
unsigned maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:90
char * line
the read line of UTF8 text
Definition: richio.h:87
unsigned length
no. bytes in line before trailing nul.
Definition: richio.h:84
unsigned capacity
no. bytes allocated for line.
Definition: richio.h:88
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 130 of file richio.h.

References LINE_READER::source.

Referenced by LEGACY_PLUGIN::biuParse(), LEGACY_PLUGIN::checkVersion(), DSNLEXER::CurSource(), LEGACY_PLUGIN::degParse(), FILTER_READER::GetSource(), WHITESPACE_FILTER_READER::GetSource(), CMP_READER::Load(), LEGACY_NETLIST_READER::loadComponent(), LEGACY_NETLIST_READER::loadFootprintFilters(), LEGACY_NETLIST_READER::loadNet(), LEGACY_PLUGIN::loadSETUP(), GPCB_FPL_CACHE::parseMODULE(), and KICAD_NETLIST_PARSER::parseNet().

131  {
132  return source;
133  }
wxString source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:92
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 168 of file richio.h.

References LINE_READER::length.

Referenced by LIB_FIELD::Load(), 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().

169  {
170  return length;
171  }
unsigned length
no. bytes in line before trailing nul.
Definition: richio.h:84
char* LINE_READER::Line ( ) const
inlineinherited

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

Definition at line 139 of file richio.h.

References LINE_READER::line.

Referenced by LEGACY_PLUGIN::biuParse(), LEGACY_PLUGIN::checkVersion(), LEGACY_PLUGIN::degParse(), NETLIST_READER::GuessNetlistFileType(), CMP_READER::Load(), SCH_BITMAP::Load(), LIB_PIN::Load(), SCH_SHEET_PIN::Load(), SCH_COMPONENT::Load(), LIB_PART::Load(), SCH_LEGACY_PLUGIN_CACHE::loadAliases(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), SCH_LEGACY_PLUGIN_CACHE::loadBezier(), SCH_LEGACY_PLUGIN::loadBitmap(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN_CACHE::loadCircle(), SCH_LEGACY_PLUGIN::loadComponent(), BITMAP_BASE::LoadData(), SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries(), SCH_LEGACY_PLUGIN_CACHE::loadField(), EXCELLON_IMAGE::LoadFile(), SCH_LEGACY_PLUGIN::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_EDIT_FRAME::LoadOneEEFile(), 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_EDIT_FRAME::ProcessCmpToFootprintLinkFile(), MWAVE_POLYGONAL_SHAPE_DLG::ReadDataShapeDescr(), FILTER_READER::ReadLine(), DSNLEXER::readLine(), ReadSchemaDescr(), and LP_CACHE::SkipIndex().

140  {
141  return line;
142  }
char * line
the read line of UTF8 text
Definition: richio.h:87
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 149 of file richio.h.

References LINE_READER::Line().

150  {
151  return Line();
152  }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
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 196 of file richio.cpp.

References LINE_READER::capacity, LINE_READER::expandCapacity(), fp, getc_unlocked, LINE_READER::length, LINE_READER::line, LINE_READER::lineNum, LINE_READER::maxLineLength, and THROW_IO_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadBitmap(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadDocs(), SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries(), EXCELLON_IMAGE::LoadFile(), SCH_LEGACY_PLUGIN::loadFile(), SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), SCH_EDIT_FRAME::LoadOneEEFile(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::loadPart(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN::loadWire(), SCH_EDIT_FRAME::ProcessCmpToFootprintLinkFile(), and GERBER_JOBFILE_READER::ReadGerberJobFile().

197 {
198  length = 0;
199 
200  for(;;)
201  {
202  if( length >= maxLineLength )
203  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
204 
205  if( length >= capacity )
206  expandCapacity( capacity * 2 );
207 
208  // faster, POSIX compatible fgetc(), no locking.
209  int cc = getc_unlocked( fp );
210  if( cc == EOF )
211  break;
212 
213  line[ length++ ] = (char) cc;
214 
215  if( cc == '\n' )
216  break;
217  }
218 
219  line[ length ] = 0;
220 
221  // lineNum is incremented even if there was no line read, because this
222  // leads to better error reporting when we hit an end of file.
223  ++lineNum;
224 
225  return length ? line : NULL;
226 }
unsigned maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:90
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
unsigned lineNum
Definition: richio.h:85
char * line
the read line of UTF8 text
Definition: richio.h:87
void expandCapacity(unsigned newsize)
Function expandCapacity will expand the capacity of line up to maxLineLength but not greater...
Definition: richio.cpp:132
#define getc_unlocked
Definition: richio.cpp:34
unsigned length
no. bytes in line before trailing nul.
Definition: richio.h:84
unsigned capacity
no. bytes allocated for line.
Definition: richio.h:88
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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 242 of file richio.h.

References LINE_READER::lineNum.

243  {
244  rewind( fp );
245  lineNum = 0;
246  }
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
unsigned lineNum
Definition: richio.h:85

Member Data Documentation

unsigned LINE_READER::capacity
protectedinherited
FILE* FILE_LINE_READER::fp
protected

I may own this file, but might not.

Definition at line 185 of file richio.h.

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

bool FILE_LINE_READER::iOwn
protected

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

Definition at line 184 of file richio.h.

Referenced by ~FILE_LINE_READER().

unsigned LINE_READER::length
protectedinherited
unsigned LINE_READER::maxLineLength
protectedinherited

maximum allowed capacity using resizing.

Definition at line 90 of file richio.h.

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

wxString LINE_READER::source
protectedinherited

origin of text lines, e.g. filename or "clipboard"

Definition at line 92 of file richio.h.

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


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