Commit b065b37a authored by Dick Hollenbeck's avatar Dick Hollenbeck

richio improvements, fix dangling open file

parents 2d1a7e09 9ae4e609
...@@ -27,14 +27,41 @@ ...@@ -27,14 +27,41 @@
#include <richio.h> #include <richio.h>
#include <filter_reader.h> #include <filter_reader.h>
unsigned FILTER_READER::ReadLine() throw( IO_ERROR )
FILTER_READER::FILTER_READER( LINE_READER& aReader ) :
LINE_READER( 1 ),
reader( aReader )
{ {
unsigned ret; // Not using our own line buffer, will be using aReader's. This changes
// the meaning of this->line to be merely a pointer to aReader's line, which of course
// is not owned here.
delete [] line;
line = 0;
}
FILTER_READER::~FILTER_READER()
{
// Our 'line' points to aReader's, and he will delete that buffer.
// Prevent subsequent call to ~LINE_READER() from deleting a buffer we do not own.
line = 0;
}
while( ( ret = reader.ReadLine() ) != 0 )
char* FILTER_READER::ReadLine() throw( IO_ERROR )
{
char* s;
while( ( s = reader.ReadLine() ) != NULL )
{ {
if( !strchr( "#\n\r", reader[0] ) ) if( !strchr( "#\n\r", s[0] ) )
break; break;
} }
return ret;
line = reader.Line();
length = reader.Length();
return length ? line : NULL;
} }
...@@ -64,6 +64,12 @@ LINE_READER::LINE_READER( unsigned aMaxLineLength ) ...@@ -64,6 +64,12 @@ LINE_READER::LINE_READER( unsigned aMaxLineLength )
} }
LINE_READER::~LINE_READER()
{
delete[] line;
}
void LINE_READER::expandCapacity( unsigned newsize ) void LINE_READER::expandCapacity( unsigned newsize )
{ {
// length can equal maxLineLength and nothing breaks, there's room for // length can equal maxLineLength and nothing breaks, there's room for
...@@ -89,6 +95,27 @@ void LINE_READER::expandCapacity( unsigned newsize ) ...@@ -89,6 +95,27 @@ void LINE_READER::expandCapacity( unsigned newsize )
} }
FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName,
unsigned aStartingLineNumber,
unsigned aMaxLineLength ) throw( IO_ERROR ) :
LINE_READER( aMaxLineLength ),
iOwn( true )
{
fp = wxFopen( aFileName, wxT( "rt" ) );
if( !fp )
{
wxString msg = wxString::Format(
_( "Unable to open filename '%s' for reading" ), aFileName.GetData() );
THROW_IO_ERROR( msg );
}
setvbuf( fp, NULL, _IOFBF, BUFSIZ * 8 );
source = aFileName;
lineNum = aStartingLineNumber;
}
FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName, FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
bool doOwn, bool doOwn,
unsigned aStartingLineNumber, unsigned aStartingLineNumber,
...@@ -97,7 +124,7 @@ FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName, ...@@ -97,7 +124,7 @@ FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
iOwn( doOwn ), iOwn( doOwn ),
fp( aFile ) fp( aFile )
{ {
if( doOwn ) if( doOwn && ftell( aFile ) == 0L )
{ {
setvbuf( fp, NULL, _IOFBF, BUFSIZ * 8 ); setvbuf( fp, NULL, _IOFBF, BUFSIZ * 8 );
} }
...@@ -113,40 +140,8 @@ FILE_LINE_READER::~FILE_LINE_READER() ...@@ -113,40 +140,8 @@ FILE_LINE_READER::~FILE_LINE_READER()
fclose( fp ); fclose( fp );
} }
#if 0
// The strlen() will trip on embedded nuls which can come in via bad data files.
// Try an alternate technique below.
unsigned FILE_LINE_READER::ReadLine() throw( IO_ERROR ) char* FILE_LINE_READER::ReadLine() throw( IO_ERROR )
{
length = 0;
line[0] = 0;
// fgets always puts a terminating nul at end of its read.
while( fgets( line + length, capacity - length, fp ) )
{
length += strlen( line + length );
if( length >= maxLineLength )
THROW_IO_ERROR( _("Line length exceeded") );
// a normal line breaks here, once through while loop
if( length+1 < capacity || line[length-1] == '\n' )
break;
expandCapacity( capacity * 2 );
}
// lineNum is incremented even if there was no line read, because this
// leads to better error reporting when we hit an end of file.
++lineNum;
return length;
}
#else
unsigned FILE_LINE_READER::ReadLine() throw( IO_ERROR )
{ {
length = 0; length = 0;
...@@ -175,9 +170,8 @@ unsigned FILE_LINE_READER::ReadLine() throw( IO_ERROR ) ...@@ -175,9 +170,8 @@ unsigned FILE_LINE_READER::ReadLine() throw( IO_ERROR )
// leads to better error reporting when we hit an end of file. // leads to better error reporting when we hit an end of file.
++lineNum; ++lineNum;
return length; return length ? line : NULL;
} }
#endif
STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ) : STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ) :
...@@ -203,7 +197,8 @@ STRING_LINE_READER::STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint ...@@ -203,7 +197,8 @@ STRING_LINE_READER::STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint
lineNum = aStartingPoint.lineNum; lineNum = aStartingPoint.lineNum;
} }
unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR )
char* STRING_LINE_READER::ReadLine() throw( IO_ERROR )
{ {
size_t nlOffset = lines.find( '\n', ndx ); size_t nlOffset = lines.find( '\n', ndx );
...@@ -231,7 +226,7 @@ unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR ) ...@@ -231,7 +226,7 @@ unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR )
line[length] = 0; line[length] = 0;
return length; return length ? line : NULL;
} }
...@@ -242,7 +237,7 @@ INPUTSTREAM_LINE_READER::INPUTSTREAM_LINE_READER( wxInputStream* aStream ) : ...@@ -242,7 +237,7 @@ INPUTSTREAM_LINE_READER::INPUTSTREAM_LINE_READER( wxInputStream* aStream ) :
} }
unsigned INPUTSTREAM_LINE_READER::ReadLine() throw( IO_ERROR ) char* INPUTSTREAM_LINE_READER::ReadLine() throw( IO_ERROR )
{ {
length = 0; length = 0;
...@@ -272,7 +267,7 @@ unsigned INPUTSTREAM_LINE_READER::ReadLine() throw( IO_ERROR ) ...@@ -272,7 +267,7 @@ unsigned INPUTSTREAM_LINE_READER::ReadLine() throw( IO_ERROR )
// leads to better error reporting when we hit an end of file. // leads to better error reporting when we hit an end of file.
++lineNum; ++lineNum;
return length; return length ? line : NULL;
} }
......
...@@ -117,7 +117,9 @@ protected: ...@@ -117,7 +117,9 @@ protected:
{ {
if( reader ) if( reader )
{ {
unsigned len = reader->ReadLine(); reader->ReadLine();
unsigned len = reader->Length();
// start may have changed in ReadLine(), which can resize and // start may have changed in ReadLine(), which can resize and
// relocate reader's line buffer. // relocate reader's line buffer.
......
...@@ -45,32 +45,21 @@ public: ...@@ -45,32 +45,21 @@ public:
* Constructor ( LINE_READER& ) * Constructor ( LINE_READER& )
* does not take ownership over @a aReader, so will not destroy it. * does not take ownership over @a aReader, so will not destroy it.
*/ */
FILTER_READER( LINE_READER& aReader ) : FILTER_READER( LINE_READER& aReader );
reader( aReader )
{ ~FILTER_READER();
}
unsigned ReadLine() throw( IO_ERROR ); char* ReadLine() throw( IO_ERROR );
const wxString& GetSource() const const wxString& GetSource() const
{ {
return reader.GetSource(); return reader.GetSource();
} }
char* Line() const
{
return reader.Line();
}
unsigned LineNumber() const unsigned LineNumber() const
{ {
return reader.LineNumber(); return reader.LineNumber();
} }
unsigned Length() const
{
return reader.Length();
}
}; };
#endif // FILTER_READER_H_ #endif // FILTER_READER_H_
...@@ -227,20 +227,17 @@ public: ...@@ -227,20 +227,17 @@ public:
*/ */
LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX ); LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
virtual ~LINE_READER() virtual ~LINE_READER();
{
delete[] line;
}
/** /**
* Function ReadLine * Function ReadLine
* reads a line of text into the buffer and increments the line number * reads a line of text into the buffer and increments the line number
* counter. If the line is larger than aMaxLineLength passed to the * counter. If the line is larger than aMaxLineLength passed to the
* constructor, then an exception is thrown. The line is nul terminated. * constructor, then an exception is thrown. The line is nul terminated.
* @return unsigned - The number of bytes read, 0 at end of file. * @return char* - The beginning of the read line, or NULL if EOF.
* @throw IO_ERROR when a line is too long. * @throw IO_ERROR when a line is too long.
*/ */
virtual unsigned ReadLine() throw( IO_ERROR ) = 0; virtual char* ReadLine() throw( IO_ERROR ) = 0;
/** /**
* Function GetSource * Function GetSource
...@@ -258,7 +255,7 @@ public: ...@@ -258,7 +255,7 @@ public:
* Function Line * Function Line
* returns a pointer to the last line that was read in. * returns a pointer to the last line that was read in.
*/ */
virtual char* Line() const char* Line() const
{ {
return line; return line;
} }
...@@ -287,7 +284,7 @@ public: ...@@ -287,7 +284,7 @@ public:
* Function Length * Function Length
* returns the number of bytes in the last line read from this LINE_READER. * returns the number of bytes in the last line read from this LINE_READER.
*/ */
virtual unsigned Length() const unsigned Length() const
{ {
return length; return length;
} }
...@@ -308,6 +305,27 @@ protected: ...@@ -308,6 +305,27 @@ protected:
public: public:
/**
* Constructor FILE_LINE_READER
* takes @a aFileName and the size of the desired line buffer and opens
* the file and assumes the obligation to close it.
*
* @param aFileName is the name of the file to open and to use for error reporting purposes.
*
* @param aStartingLineNumber is 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 @a 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.
*
* @param aMaxLineLength is the number of bytes to use in the line buffer.
*
* @throw IO_ERROR if @a aFileName cannot be opened.
*/
FILE_LINE_READER( const wxString& aFileName,
unsigned aStartingLineNumber = 0,
unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX ) throw( IO_ERROR );
/** /**
* Constructor FILE_LINE_READER * Constructor FILE_LINE_READER
* takes an open FILE and the size of the desired line buffer and takes * takes an open FILE and the size of the desired line buffer and takes
...@@ -333,7 +351,7 @@ public: ...@@ -333,7 +351,7 @@ public:
*/ */
~FILE_LINE_READER(); ~FILE_LINE_READER();
unsigned ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
/** /**
* Function Rewind * Function Rewind
...@@ -380,7 +398,7 @@ public: ...@@ -380,7 +398,7 @@ public:
*/ */
STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint ); STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
unsigned ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
}; };
...@@ -402,7 +420,7 @@ public: ...@@ -402,7 +420,7 @@ public:
*/ */
INPUTSTREAM_LINE_READER( wxInputStream* aStream ); INPUTSTREAM_LINE_READER( wxInputStream* aStream );
unsigned ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
}; };
......
...@@ -222,16 +222,8 @@ void FP_CACHE::Load() ...@@ -222,16 +222,8 @@ void FP_CACHE::Load()
do do
{ {
FILE* fp = wxFopen( fpFileName, wxT( "r" ) );
if( !fp )
{
THROW_IO_ERROR( wxString::Format( _( "cannot open footprint library file '%s'" ),
fpFileName.GetData() ) );
}
// reader now owns fp, will close on exception or return // reader now owns fp, will close on exception or return
FILE_LINE_READER reader( fp, fpFileName ); FILE_LINE_READER reader( fpFileName );
m_owner->m_parser->SetLineReader( &reader ); m_owner->m_parser->SetLineReader( &reader );
...@@ -1551,15 +1543,7 @@ PCB_IO::~PCB_IO() ...@@ -1551,15 +1543,7 @@ PCB_IO::~PCB_IO()
BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, PROPERTIES* aProperties ) BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, PROPERTIES* aProperties )
{ {
wxFFile file( aFileName, wxT("r") ); FILE_LINE_READER reader( aFileName );
if( !file.IsOpened() )
{
wxString msg = wxString::Format( _( "Unable to read file \"%s\"" ), GetChars( aFileName ) );
THROW_IO_ERROR( msg );
}
FILE_LINE_READER reader( file.fp(), aFileName, false /* wxFFile owns fp */ );
m_parser->SetLineReader( &reader ); m_parser->SetLineReader( &reader );
m_parser->SetBoard( aAppendToMe ); m_parser->SetBoard( aAppendToMe );
......
...@@ -127,9 +127,9 @@ static bool inline isSpace( int c ) { return strchr( delims, c ) != 0; } ...@@ -127,9 +127,9 @@ static bool inline isSpace( int c ) { return strchr( delims, c ) != 0; }
/// The function and macro which follow comprise a shim which can be a /// The function and macro which follow comprise a shim which can be a
/// monitor on lines of text read in from the input file. /// monitor on lines of text read in from the input file.
/// And it can be used as a trap. /// And it can be used as a trap.
static inline unsigned ReadLine( LINE_READER* rdr, const char* caller ) static inline char* ReadLine( LINE_READER* rdr, const char* caller )
{ {
unsigned ret = rdr->ReadLine(); char* ret = rdr->ReadLine();
const char* line = rdr->Line(); const char* line = rdr->Line();
printf( "%-6u %s: %s", rdr->LineNumber(), caller, line ); printf( "%-6u %s: %s", rdr->LineNumber(), caller, line );
...@@ -238,15 +238,7 @@ BOARD* LEGACY_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe, PROPE ...@@ -238,15 +238,7 @@ BOARD* LEGACY_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe, PROPE
// delete on exception, iff I own m_board, according to aAppendToMe // delete on exception, iff I own m_board, according to aAppendToMe
auto_ptr<BOARD> deleter( aAppendToMe ? NULL : m_board ); auto_ptr<BOARD> deleter( aAppendToMe ? NULL : m_board );
FILE* fp = wxFopen( aFileName, wxT( "r" ) ); FILE_LINE_READER reader( aFileName );
if( !fp )
{
m_error.Printf( _( "Unable to open file '%s'" ), aFileName.GetData() );
THROW_IO_ERROR( m_error );
}
// reader now owns fp, will close on exception or return
FILE_LINE_READER reader( fp, aFileName );
m_reader = &reader; // member function accessibility m_reader = &reader; // member function accessibility
...@@ -268,11 +260,10 @@ void LEGACY_PLUGIN::loadAllSections( bool doAppend ) ...@@ -268,11 +260,10 @@ void LEGACY_PLUGIN::loadAllSections( bool doAppend )
// $SETUP section is next // $SETUP section is next
// Then follows $EQUIPOT and all the rest // Then follows $EQUIPOT and all the rest
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
// put the more frequent ones at the top, but realize TRACKs are loaded as a group // put the more frequent ones at the top, but realize TRACKs are loaded as a group
if( TESTLINE( "$MODULE" ) ) if( TESTLINE( "$MODULE" ) )
...@@ -344,10 +335,9 @@ void LEGACY_PLUGIN::loadAllSections( bool doAppend ) ...@@ -344,10 +335,9 @@ void LEGACY_PLUGIN::loadAllSections( bool doAppend )
} }
else else
{ {
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
line = m_reader->Line(); // gobble until $EndSetup // gobble until $EndSetup
if( TESTLINE( "$EndSETUP" ) ) if( TESTLINE( "$EndSETUP" ) )
break; break;
} }
...@@ -395,9 +385,10 @@ void LEGACY_PLUGIN::checkVersion() ...@@ -395,9 +385,10 @@ void LEGACY_PLUGIN::checkVersion()
void LEGACY_PLUGIN::loadGENERAL() void LEGACY_PLUGIN::loadGENERAL()
{ {
while( READLINE( m_reader ) ) char* line;
while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
const char* data; const char* data;
if( TESTLINE( "Units" ) ) if( TESTLINE( "Units" ) )
...@@ -516,11 +507,10 @@ void LEGACY_PLUGIN::loadSHEET() ...@@ -516,11 +507,10 @@ void LEGACY_PLUGIN::loadSHEET()
{ {
char buf[260]; char buf[260];
TITLE_BLOCK tb; TITLE_BLOCK tb;
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
if( TESTLINE( "Sheet" ) ) if( TESTLINE( "Sheet" ) )
{ {
// e.g. "Sheet A3 16535 11700" // e.g. "Sheet A3 16535 11700"
...@@ -631,11 +621,11 @@ void LEGACY_PLUGIN::loadSETUP() ...@@ -631,11 +621,11 @@ void LEGACY_PLUGIN::loadSETUP()
NETCLASS* netclass_default = m_board->m_NetClasses.GetDefault(); NETCLASS* netclass_default = m_board->m_NetClasses.GetDefault();
BOARD_DESIGN_SETTINGS bds = m_board->GetDesignSettings(); BOARD_DESIGN_SETTINGS bds = m_board->GetDesignSettings();
ZONE_SETTINGS zs = m_board->GetZoneSettings(); ZONE_SETTINGS zs = m_board->GetZoneSettings();
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "PcbPlotParams" ) ) if( TESTLINE( "PcbPlotParams" ) )
{ {
...@@ -929,11 +919,10 @@ void LEGACY_PLUGIN::loadSETUP() ...@@ -929,11 +919,10 @@ void LEGACY_PLUGIN::loadSETUP()
MODULE* LEGACY_PLUGIN::LoadMODULE() MODULE* LEGACY_PLUGIN::LoadMODULE()
{ {
auto_ptr<MODULE> module( new MODULE( m_board ) ); auto_ptr<MODULE> module( new MODULE( m_board ) );
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
const char* data; const char* data;
// most frequently encountered ones at the top // most frequently encountered ones at the top
...@@ -1139,11 +1128,11 @@ MODULE* LEGACY_PLUGIN::LoadMODULE() ...@@ -1139,11 +1128,11 @@ MODULE* LEGACY_PLUGIN::LoadMODULE()
void LEGACY_PLUGIN::loadPAD( MODULE* aModule ) void LEGACY_PLUGIN::loadPAD( MODULE* aModule )
{ {
auto_ptr<D_PAD> pad( new D_PAD( aModule ) ); auto_ptr<D_PAD> pad( new D_PAD( aModule ) );
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "Sh" ) ) // (Sh)ape and padname if( TESTLINE( "Sh" ) ) // (Sh)ape and padname
{ {
...@@ -1450,13 +1439,11 @@ void LEGACY_PLUGIN::loadMODULE_EDGE( MODULE* aModule ) ...@@ -1450,13 +1439,11 @@ void LEGACY_PLUGIN::loadMODULE_EDGE( MODULE* aModule )
for( int ii = 0; ii<ptCount; ++ii ) for( int ii = 0; ii<ptCount; ++ii )
{ {
if( !READLINE( m_reader ) ) if( ( line = READLINE( m_reader ) ) == NULL )
{ {
THROW_IO_ERROR( "S_POLGON point count mismatch." ); THROW_IO_ERROR( "S_POLGON point count mismatch." );
} }
line = m_reader->Line();
// e.g. "Dl 23 44\n" // e.g. "Dl 23 44\n"
if( !TESTLINE( "Dl" ) ) if( !TESTLINE( "Dl" ) )
...@@ -1625,10 +1612,9 @@ void LEGACY_PLUGIN::load3D( MODULE* aModule ) ...@@ -1625,10 +1612,9 @@ void LEGACY_PLUGIN::load3D( MODULE* aModule )
t3D = n3D; t3D = n3D;
} }
while( READLINE( m_reader ) ) char* line;
while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
if( TESTLINE( "Na" ) ) // Shape File Name if( TESTLINE( "Na" ) ) // Shape File Name
{ {
char buf[512]; char buf[512];
...@@ -1678,11 +1664,11 @@ void LEGACY_PLUGIN::loadPCB_LINE() ...@@ -1678,11 +1664,11 @@ void LEGACY_PLUGIN::loadPCB_LINE()
*/ */
auto_ptr<DRAWSEGMENT> dseg( new DRAWSEGMENT( m_board ) ); auto_ptr<DRAWSEGMENT> dseg( new DRAWSEGMENT( m_board ) );
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "Po" ) ) if( TESTLINE( "Po" ) )
{ {
...@@ -1784,11 +1770,11 @@ void LEGACY_PLUGIN::loadNETINFO_ITEM() ...@@ -1784,11 +1770,11 @@ void LEGACY_PLUGIN::loadNETINFO_ITEM()
char buf[1024]; char buf[1024];
NETINFO_ITEM* net = new NETINFO_ITEM( m_board ); NETINFO_ITEM* net = new NETINFO_ITEM( m_board );
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "Na" ) ) if( TESTLINE( "Na" ) )
{ {
...@@ -1845,10 +1831,11 @@ void LEGACY_PLUGIN::loadPCB_TEXT() ...@@ -1845,10 +1831,11 @@ void LEGACY_PLUGIN::loadPCB_TEXT()
TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board ); TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board );
m_board->Add( pcbtxt, ADD_APPEND ); m_board->Add( pcbtxt, ADD_APPEND );
while( READLINE( m_reader ) ) char* line;
while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "Te" ) ) // Text line (or first line for multi line texts) if( TESTLINE( "Te" ) ) // Text line (or first line for multi line texts)
{ {
...@@ -1947,7 +1934,9 @@ void LEGACY_PLUGIN::loadPCB_TEXT() ...@@ -1947,7 +1934,9 @@ void LEGACY_PLUGIN::loadPCB_TEXT()
void LEGACY_PLUGIN::loadTrackList( int aStructType ) void LEGACY_PLUGIN::loadTrackList( int aStructType )
{ {
while( READLINE( m_reader ) ) char* line;
while( ( line = READLINE( m_reader ) ) != NULL )
{ {
// read two lines per loop iteration, each loop is one TRACK or VIA // read two lines per loop iteration, each loop is one TRACK or VIA
// example first line: // example first line:
...@@ -1955,7 +1944,6 @@ void LEGACY_PLUGIN::loadTrackList( int aStructType ) ...@@ -1955,7 +1944,6 @@ void LEGACY_PLUGIN::loadTrackList( int aStructType )
// e.g. "Po 3 21086 17586 21086 17586 180 -1" for a via (uses sames start and end) // e.g. "Po 3 21086 17586 21086 17586 180 -1" for a via (uses sames start and end)
const char* data; const char* data;
char* line = m_reader->Line();
if( line[0] == '$' ) // $EndTRACK if( line[0] == '$' ) // $EndTRACK
return; // preferred exit return; // preferred exit
...@@ -2065,6 +2053,7 @@ void LEGACY_PLUGIN::loadNETCLASS() ...@@ -2065,6 +2053,7 @@ void LEGACY_PLUGIN::loadNETCLASS()
{ {
char buf[1024]; char buf[1024];
wxString netname; wxString netname;
char* line;
// create an empty NETCLASS without a name, but do not add it to the BOARD // create an empty NETCLASS without a name, but do not add it to the BOARD
// yet since that would bypass duplicate netclass name checking within the BOARD. // yet since that would bypass duplicate netclass name checking within the BOARD.
...@@ -2072,10 +2061,8 @@ void LEGACY_PLUGIN::loadNETCLASS() ...@@ -2072,10 +2061,8 @@ void LEGACY_PLUGIN::loadNETCLASS()
// just before returning. // just before returning.
auto_ptr<NETCLASS> nc( new NETCLASS( m_board, wxEmptyString ) ); auto_ptr<NETCLASS> nc( new NETCLASS( m_board, wxEmptyString ) );
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
char* line = m_reader->Line();
if( TESTLINE( "AddNet" ) ) // most frequent type of line if( TESTLINE( "AddNet" ) ) // most frequent type of line
{ {
// e.g. "AddNet "V3.3D"\n" // e.g. "AddNet "V3.3D"\n"
...@@ -2164,11 +2151,11 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER() ...@@ -2164,11 +2151,11 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER()
CPolyLine::HATCH_STYLE outline_hatch = CPolyLine::NO_HATCH; CPolyLine::HATCH_STYLE outline_hatch = CPolyLine::NO_HATCH;
bool sawCorner = false; bool sawCorner = false;
char buf[1024]; char buf[1024];
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "ZCorner" ) ) // new corner found if( TESTLINE( "ZCorner" ) ) // new corner found
{ {
...@@ -2349,10 +2336,8 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER() ...@@ -2349,10 +2336,8 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER()
// Read the PolysList (polygons used for fill areas in the zone) // Read the PolysList (polygons used for fill areas in the zone)
std::vector<CPolyPt> polysList; std::vector<CPolyPt> polysList;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
line = m_reader->Line();
if( TESTLINE( "$endPOLYSCORNERS" ) ) if( TESTLINE( "$endPOLYSCORNERS" ) )
break; break;
...@@ -2370,10 +2355,8 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER() ...@@ -2370,10 +2355,8 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER()
else if( TESTLINE( "$FILLSEGMENTS" ) ) else if( TESTLINE( "$FILLSEGMENTS" ) )
{ {
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
line = m_reader->Line();
if( TESTLINE( "$endFILLSEGMENTS" ) ) if( TESTLINE( "$endFILLSEGMENTS" ) )
break; break;
...@@ -2422,11 +2405,11 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER() ...@@ -2422,11 +2405,11 @@ void LEGACY_PLUGIN::loadZONE_CONTAINER()
void LEGACY_PLUGIN::loadDIMENSION() void LEGACY_PLUGIN::loadDIMENSION()
{ {
auto_ptr<DIMENSION> dim( new DIMENSION( m_board ) ); auto_ptr<DIMENSION> dim( new DIMENSION( m_board ) );
char* line;
while( READLINE( m_reader ) ) while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "$endCOTATION" ) ) if( TESTLINE( "$endCOTATION" ) )
{ {
...@@ -2617,10 +2600,11 @@ void LEGACY_PLUGIN::loadDIMENSION() ...@@ -2617,10 +2600,11 @@ void LEGACY_PLUGIN::loadDIMENSION()
void LEGACY_PLUGIN::loadPCB_TARGET() void LEGACY_PLUGIN::loadPCB_TARGET()
{ {
while( READLINE( m_reader ) ) char* line;
while( ( line = READLINE( m_reader ) ) != NULL )
{ {
const char* data; const char* data;
char* line = m_reader->Line();
if( TESTLINE( "$EndPCB_TARGET" ) || TESTLINE( "$EndMIREPCB" ) ) if( TESTLINE( "$EndPCB_TARGET" ) || TESTLINE( "$EndMIREPCB" ) )
{ {
...@@ -3928,15 +3912,7 @@ wxDateTime FPL_CACHE::GetLibModificationTime() ...@@ -3928,15 +3912,7 @@ wxDateTime FPL_CACHE::GetLibModificationTime()
void FPL_CACHE::Load() void FPL_CACHE::Load()
{ {
FILE* fp = wxFopen( m_lib_name, wxT( "r" ) ); FILE_LINE_READER reader( m_lib_name );
if( !fp )
{
THROW_IO_ERROR( wxString::Format(
_( "Unable to open legacy library file '%s'" ), m_lib_name.GetData() ) );
}
// reader now owns fp, will close on exception or return
FILE_LINE_READER reader( fp, m_lib_name );
ReadAndVerifyHeader( &reader ); ReadAndVerifyHeader( &reader );
SkipIndex( &reader ); SkipIndex( &reader );
...@@ -3951,18 +3927,16 @@ void FPL_CACHE::Load() ...@@ -3951,18 +3927,16 @@ void FPL_CACHE::Load()
void FPL_CACHE::ReadAndVerifyHeader( LINE_READER* aReader ) void FPL_CACHE::ReadAndVerifyHeader( LINE_READER* aReader )
{ {
char* line; char* line = aReader->ReadLine();
if( !aReader->ReadLine() ) if( !line )
goto L_bad_library; goto L_bad_library;
line = aReader->Line();
if( !TESTLINE( "PCBNEW-LibModule-V1" ) ) if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
goto L_bad_library; goto L_bad_library;
while( aReader->ReadLine() ) while( ( line = aReader->ReadLine() ) != NULL )
{ {
line = aReader->Line();
if( TESTLINE( "Units" ) ) if( TESTLINE( "Units" ) )
{ {
const char* units = strtok( line + SZ( "Units" ), delims ); const char* units = strtok( line + SZ( "Units" ), delims );
...@@ -3993,19 +3967,16 @@ void FPL_CACHE::SkipIndex( LINE_READER* aReader ) ...@@ -3993,19 +3967,16 @@ void FPL_CACHE::SkipIndex( LINE_READER* aReader )
// So we must read the next line after $EndINDEX tag, // So we must read the next line after $EndINDEX tag,
// to see if this is not a new $INDEX tag. // to see if this is not a new $INDEX tag.
bool exit = false; bool exit = false;
char* line = aReader->Line();
do do
{ {
char* line = aReader->Line();
if( TESTLINE( "$INDEX" ) ) if( TESTLINE( "$INDEX" ) )
{ {
exit = false; exit = false;
while( aReader->ReadLine() ) while( ( line = aReader->ReadLine() ) != NULL )
{ {
line = aReader->Line();
if( TESTLINE( "$EndINDEX" ) ) if( TESTLINE( "$EndINDEX" ) )
{ {
exit = true; exit = true;
...@@ -4015,7 +3986,7 @@ void FPL_CACHE::SkipIndex( LINE_READER* aReader ) ...@@ -4015,7 +3986,7 @@ void FPL_CACHE::SkipIndex( LINE_READER* aReader )
} }
else if( exit ) else if( exit )
break; break;
} while( aReader->ReadLine() ); } while( ( line = aReader->ReadLine() ) != NULL );
} }
...@@ -4023,11 +3994,11 @@ void FPL_CACHE::LoadModules( LINE_READER* aReader ) ...@@ -4023,11 +3994,11 @@ void FPL_CACHE::LoadModules( LINE_READER* aReader )
{ {
m_owner->SetReader( aReader ); m_owner->SetReader( aReader );
char* line = aReader->Line();
do do
{ {
// test first for the $MODULE, even before reading because of INDEX bug. // test first for the $MODULE, even before reading because of INDEX bug.
char* line = aReader->Line();
if( TESTLINE( "$MODULE" ) ) if( TESTLINE( "$MODULE" ) )
{ {
MODULE* m = m_owner->LoadMODULE(); MODULE* m = m_owner->LoadMODULE();
...@@ -4089,7 +4060,7 @@ void FPL_CACHE::LoadModules( LINE_READER* aReader ) ...@@ -4089,7 +4060,7 @@ void FPL_CACHE::LoadModules( LINE_READER* aReader )
} }
} }
} while( aReader->ReadLine() ); } while( ( line = aReader->ReadLine() ) != NULL );
} }
......
...@@ -80,15 +80,10 @@ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile ) ...@@ -80,15 +80,10 @@ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile )
/* First, read the netlist: Build the list of footprints found in netlist /* First, read the netlist: Build the list of footprints found in netlist
*/ */
#ifndef __WXMAC__
// netlineReader dtor will close aFile // netlineReader dtor will close aFile
FILE_LINE_READER netlineReader( aFile, m_netlistFullName ); FILE_LINE_READER netlineReader( aFile, m_netlistFullName );
#else COMPONENT_INFO* curComponent = NULL;
//Seems that the setvbuf call destroys the FILE buffer (already allocated)
//And looses the first 4096 bytes so we set doOwn => false
FILE_LINE_READER netlineReader( aFile, m_netlistFullName, false );
#endif
COMPONENT_INFO *curComponent = NULL;
while( netlineReader.ReadLine() ) while( netlineReader.ReadLine() )
{ {
char* line = StrPurge( netlineReader.Line() ); char* line = StrPurge( netlineReader.Line() );
...@@ -372,23 +367,22 @@ bool NETLIST_READER::ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistR ...@@ -372,23 +367,22 @@ bool NETLIST_READER::ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistR
{ {
wxString cmpRef; wxString cmpRef;
COMPONENT_INFO* cmp_info = NULL; COMPONENT_INFO* cmp_info = NULL;
char* line;
while( aNetlistReader.ReadLine() ) while( ( line = aNetlistReader.ReadLine() ) != NULL )
{ {
const char* Line = aNetlistReader.Line(); if( strnicmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
if( strnicmp( Line, "$endlist", 8 ) == 0 ) // end of list for the current component
{ {
cmp_info = NULL; cmp_info = NULL;
continue; continue;
} }
if( strnicmp( Line, "$endfootprintlist", 4 ) == 0 ) if( strnicmp( line, "$endfootprintlist", 4 ) == 0 )
// End of this section // End of this section
return 0; return 0;
if( strnicmp( Line, "$component", 10 ) == 0 ) // New component reference found if( strnicmp( line, "$component", 10 ) == 0 ) // New component reference found
{ {
cmpRef = FROM_UTF8( Line + 11 ); cmpRef = FROM_UTF8( line + 11 );
cmpRef.Trim( true ); cmpRef.Trim( true );
cmpRef.Trim( false ); cmpRef.Trim( false );
...@@ -405,7 +399,7 @@ bool NETLIST_READER::ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistR ...@@ -405,7 +399,7 @@ bool NETLIST_READER::ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistR
else if( cmp_info ) else if( cmp_info )
{ {
// Add new filter to list // Add new filter to list
wxString fp = FROM_UTF8( Line + 1 ); wxString fp = FROM_UTF8( line + 1 );
fp.Trim( false ); fp.Trim( false );
fp.Trim( true ); fp.Trim( true );
cmp_info->m_FootprintFilter.Add( fp ); cmp_info->m_FootprintFilter.Add( fp );
......
...@@ -230,14 +230,9 @@ void SPECCTRA_DB::readTIME( time_t* time_stamp ) throw( IO_ERROR ) ...@@ -230,14 +230,9 @@ void SPECCTRA_DB::readTIME( time_t* time_stamp ) throw( IO_ERROR )
void SPECCTRA_DB::LoadPCB( const wxString& filename ) throw( IO_ERROR ) void SPECCTRA_DB::LoadPCB( const wxString& filename ) throw( IO_ERROR )
{ {
FILE* fp = wxFopen( filename, wxT("r") ); FILE_LINE_READER reader( filename );
if( !fp ) PushReader( &reader );
{
ThrowIOError( _("Unable to open file \"%s\""), GetChars(filename) );
}
PushReader( new FILE_LINE_READER( fp, filename ) );
if( NextTok() != T_LEFT ) if( NextTok() != T_LEFT )
Expecting( T_LEFT ); Expecting( T_LEFT );
...@@ -248,21 +243,15 @@ void SPECCTRA_DB::LoadPCB( const wxString& filename ) throw( IO_ERROR ) ...@@ -248,21 +243,15 @@ void SPECCTRA_DB::LoadPCB( const wxString& filename ) throw( IO_ERROR )
SetPCB( new PCB() ); SetPCB( new PCB() );
doPCB( pcb ); doPCB( pcb );
PopReader();
delete PopReader(); // close fp
} }
void SPECCTRA_DB::LoadSESSION( const wxString& filename ) throw( IO_ERROR ) void SPECCTRA_DB::LoadSESSION( const wxString& filename ) throw( IO_ERROR )
{ {
FILE* fp = wxFopen( filename, wxT("r") ); FILE_LINE_READER reader( filename );
if( !fp )
{
ThrowIOError( _("Unable to open file \"%s\""), GetChars(filename) );
}
PushReader( new FILE_LINE_READER( fp, filename ) ); PushReader( &reader );
if( NextTok() != T_LEFT ) if( NextTok() != T_LEFT )
Expecting( T_LEFT ); Expecting( T_LEFT );
...@@ -274,7 +263,7 @@ void SPECCTRA_DB::LoadSESSION( const wxString& filename ) throw( IO_ERROR ) ...@@ -274,7 +263,7 @@ void SPECCTRA_DB::LoadSESSION( const wxString& filename ) throw( IO_ERROR )
doSESSION( session ); doSESSION( session );
delete PopReader(); // close fp PopReader();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment