Loading common/filter_reader.cpp +32 −5 Original line number Original line Diff line number Diff line Loading @@ -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; } while( ( ret = reader.ReadLine() ) != 0 ) FILTER_READER::~FILTER_READER() { { if( !strchr( "#\n\r", reader[0] ) ) // 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; } char* FILTER_READER::ReadLine() throw( IO_ERROR ) { char* s; while( ( s = reader.ReadLine() ) != NULL ) { if( !strchr( "#\n\r", s[0] ) ) break; break; } } return ret; line = reader.Line(); length = reader.Length(); return length ? line : NULL; } } common/richio.cpp +13 −39 Original line number Original line Diff line number Diff line Loading @@ -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 Loading Loading @@ -134,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; Loading Loading @@ -196,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 ) : Loading @@ -224,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 ); Loading Loading @@ -252,7 +226,7 @@ unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR ) line[length] = 0; line[length] = 0; return length; return length ? line : NULL; } } Loading @@ -263,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; Loading Loading @@ -293,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; } } Loading include/dsnlexer.h +3 −1 Original line number Original line Diff line number Diff line Loading @@ -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. Loading include/filter_reader.h +4 −15 Original line number Original line Diff line number Diff line Loading @@ -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_ include/richio.h +8 −11 Original line number Original line Diff line number Diff line Loading @@ -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 Loading @@ -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; } } Loading Loading @@ -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; } } Loading Loading @@ -354,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 Loading Loading @@ -401,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 }; }; Loading @@ -423,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 }; }; Loading Loading
common/filter_reader.cpp +32 −5 Original line number Original line Diff line number Diff line Loading @@ -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; } while( ( ret = reader.ReadLine() ) != 0 ) FILTER_READER::~FILTER_READER() { { if( !strchr( "#\n\r", reader[0] ) ) // 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; } char* FILTER_READER::ReadLine() throw( IO_ERROR ) { char* s; while( ( s = reader.ReadLine() ) != NULL ) { if( !strchr( "#\n\r", s[0] ) ) break; break; } } return ret; line = reader.Line(); length = reader.Length(); return length ? line : NULL; } }
common/richio.cpp +13 −39 Original line number Original line Diff line number Diff line Loading @@ -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 Loading Loading @@ -134,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; Loading Loading @@ -196,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 ) : Loading @@ -224,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 ); Loading Loading @@ -252,7 +226,7 @@ unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR ) line[length] = 0; line[length] = 0; return length; return length ? line : NULL; } } Loading @@ -263,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; Loading Loading @@ -293,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; } } Loading
include/dsnlexer.h +3 −1 Original line number Original line Diff line number Diff line Loading @@ -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. Loading
include/filter_reader.h +4 −15 Original line number Original line Diff line number Diff line Loading @@ -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_
include/richio.h +8 −11 Original line number Original line Diff line number Diff line Loading @@ -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 Loading @@ -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; } } Loading Loading @@ -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; } } Loading Loading @@ -354,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 Loading Loading @@ -401,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 }; }; Loading @@ -423,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 }; }; Loading