diff --git a/common/richio.cpp b/common/richio.cpp index 3b307d3b04..ca3dcae702 100644 --- a/common/richio.cpp +++ b/common/richio.cpp @@ -89,6 +89,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, bool doOwn, unsigned aStartingLineNumber, @@ -97,7 +118,7 @@ FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName, iOwn( doOwn ), fp( aFile ) { - if( doOwn ) + if( doOwn && ftell( aFile ) == 0L ) { setvbuf( fp, NULL, _IOFBF, BUFSIZ * 8 ); } diff --git a/include/richio.h b/include/richio.h index fb3c35d1db..e3ff677773 100644 --- a/include/richio.h +++ b/include/richio.h @@ -308,6 +308,27 @@ protected: 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 * takes an open FILE and the size of the desired line buffer and takes