Commit 0267b059 authored by Dick Hollenbeck's avatar Dick Hollenbeck
Browse files

add PushReader and PopReader to DSNLEXER, make FILE_LINE_READER own its FILE*

parent 3335ccd9
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -4,6 +4,29 @@ KiCad ChangeLog 2010
Please add newer entries at the top, list the date and your name with
email address.

2010-Oct-5 UPDATE Dick Hollenbeck <dick@softplc.com>
================================================================================
++richio:
  * LINE_READER now has a GetSource() function which is used in error
    reporting.  This is typically the name of the file which is supplying the
    lines of text, or string "clipboard" if the text is coming from the clipboard.
    Derived classes FILE_LINE_READER and STRING_LINE_READER's constructors both
    need an additional parameter which identifies the source.
  * FILE_LINE_READER now owns the source FILE and will close it in its destructor.
    This resulted in the removal of several fclose() statements that had been
    there to close a file associated with a FILE_LINE_READER.
  * DSNLEXER now supports an internal LINE_READER* stack which is used to handle
    nested s-expression files, with the ability to resume from the proper place
    in the containing file.  There is now PushReader() and PopReader() functions
    in DSNLEXER to handle this.  No protection is provided against circular
    inclusions, but this could be done by searching the stack and comparing
    GetSource() values for anything already on the stack before pushing.
    Each s-expression grammar is free to define one or more keywords that cause
    nesting to occur. That policy choice is not part of DSNLEXER's job.
    One example might be:
    (inherit (footprint library_uri ftprintname))


2010-oct-03, UPDATE Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr>
================================================================================
++gerbview:
+22 −13
Original line number Diff line number Diff line
@@ -58,24 +58,18 @@ void DSNLEXER::init()

    commentsAreTokens = false;

    // "start" should never change until we change the reader.  The DSN
    // format spec supports an include file mechanism but we can add that later
    // using a std::stack to hold a stack of LINE_READERs to track nesting.
    start = (char*) (*reader);

    limit = start;
    next  = start;
}


DSNLEXER::DSNLEXER( FILE* aFile, const wxString& aFilename,
        const KEYWORD* aKeywordTable, unsigned aKeywordCount ) :
    keywords( aKeywordTable ),
    keywordCount( aKeywordCount )
{
    filename = aFilename;

    reader = new FILE_LINE_READER( aFile, 4096 );

    FILE_LINE_READER* fileReader = new FILE_LINE_READER( aFile, aFilename, 4096 );
    PushReader( fileReader );
    init();
}

@@ -85,11 +79,26 @@ DSNLEXER::DSNLEXER( const std::string& aClipboardTxt,
    keywords( aKeywordTable ),
    keywordCount( aKeywordCount )
{
    filename = _( "clipboard" );
    STRING_LINE_READER* stringReader = new STRING_LINE_READER( aClipboardTxt, _( "clipboard" ) );
    PushReader( stringReader );
    init();
}


void DSNLEXER::PushReader( LINE_READER* aLineReader )
{
    readerStack.push_back( aLineReader );
    reader = aLineReader;
    start = (char*) (*aLineReader);
}

    reader = new STRING_LINE_READER( aClipboardTxt );

    init();
void DSNLEXER::PopReader()
{
    readerStack.pop_back();
    reader = &readerStack.back();
    if( reader )
        start = (char*) (*reader);
}


@@ -207,7 +216,7 @@ bool DSNLEXER::IsSymbol( int aTok )
void DSNLEXER::ThrowIOError( wxString aText, int charOffset ) throw (IOError)
{
    // append to aText, do not overwrite
    aText << wxT(" ") << _("in") << wxT(" \"") << filename
    aText << wxT(" ") << _("in") << wxT(" \"") << CurSource()
          << wxT("\" ") << _("on line") << wxT(" ") << reader->LineNumber()
          << wxT(" ") << _("at offset") << wxT(" ") << charOffset;

+7 −6
Original line number Diff line number Diff line
@@ -49,10 +49,11 @@ LINE_READER::LINE_READER( unsigned aMaxLineLength )
}


FILE_LINE_READER::FILE_LINE_READER( FILE* aFile,  unsigned aMaxLineLength ) :
    LINE_READER( aMaxLineLength )
FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName, unsigned aMaxLineLength ) :
    LINE_READER( aMaxLineLength ),
    fp( aFile )
{
    fp = aFile;
    source = aFileName;
}


@@ -81,11 +82,11 @@ int FILE_LINE_READER::ReadLine() throw (IOError)

int STRING_LINE_READER::ReadLine() throw (IOError)
{
    size_t      nlOffset = source.find( '\n', ndx );
    size_t      nlOffset = lines.find( '\n', ndx );
    size_t      advance;

    if( nlOffset == std::string::npos )
        advance = source.length() - ndx;
        advance = lines.length() - ndx;
    else
        advance = nlOffset - ndx + 1;     // include the newline, so +1

@@ -94,7 +95,7 @@ int STRING_LINE_READER::ReadLine() throw (IOError)
        if( advance > maxLineLength )
            throw IOError( _("Line length exceeded") );

        wxASSERT( ndx + advance <= source.length() );
        wxASSERT( ndx + advance <= lines.length() );

        memcpy( line, &source[ndx], advance );

+9 −11
Original line number Diff line number Diff line
@@ -113,7 +113,8 @@ int WinEDA_CvpcbFrame::ReadSchematicNetlist()
        return -1;
    }

    FILE_LINE_READER netlistReader( source,  BUFFER_CHAR_SIZE );
    // FILE_LINE_READER will close the file.
    FILE_LINE_READER netlistReader( source, m_NetlistFileName.GetFullPath(), BUFFER_CHAR_SIZE );
    char* Line = netlistReader;

    /* Read the file header (must be  "( { OrCAD PCB" or "({ OrCAD PCB" )
@@ -137,7 +138,6 @@ int WinEDA_CvpcbFrame::ReadSchematicNetlist()
        wxString msg, Lineconv = CONV_FROM_UTF8( Line );
        msg.Printf( _( "Unknown file format <%s>" ), Lineconv.GetData() );
        DisplayError( this, msg );
        fclose( source );
        return -3;
    }

@@ -263,8 +263,6 @@ int WinEDA_CvpcbFrame::ReadSchematicNetlist()
        ReadPinConnection( netlistReader, Cmp );
    }

    fclose( source );

    m_components.sort();

    return 0;
+28 −7
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@

#include <cstdio>
#include <string>
#include <boost/ptr_container/ptr_vector.hpp>

#include "fctsys.h"

@@ -81,12 +82,14 @@ class DSNLEXER
    char*               start;
    char*               limit;

    LINE_READER*        reader;
    typedef boost::ptr_vector<LINE_READER>  READER_STACK;

    READER_STACK        readerStack;            ///< owns all the LINE_READERs by pointer.
    LINE_READER*        reader;                 ///< no ownership. ownership is via readerStack.
    int                 stringDelimiter;
    bool                space_in_quoted_tokens; ///< blank spaces within quoted strings
    bool                commentsAreTokens;      ///< true if should return comments as tokens

    wxString            filename;
    int                 prevTok;        ///< curTok from previous NextTok() call.
    int                 curOffset;      ///< offset within current line of the current token

@@ -164,9 +167,26 @@ public:

    ~DSNLEXER()
    {
        delete reader;
    }

    /**
     * Function PushReader
     * manages a stack of LINE_READERs in order to handle nested file inclusion.
     * Pushes aLineReader onto the top of a stack of LINE_READERs and makes
     * it the current LINE_READER with its own GetSource(), line number and line text.
     */
    void PushReader( LINE_READER* aLineReader );

    /**
     * Function PopReader
     * deletes the top most LINE_READER from an internal stack of LINE_READERs and
     * in the case of FILE_LINE_READER this means the associated FILE is closed.
     * The most recently used former LINE_READER on the stack becomes the
     * current LINE_READER and its previous position in its input stream and the
     * its latest line number should pertain.
     */
    void PopReader();

    // Some functions whose return value is best overloaded to return an enum
    // in a derived class.
    //-----<overload return values to tokens>------------------------------
@@ -364,12 +384,13 @@ public:

    /**
     * Function CurFilename
     * returns the current input filename.
     * @return const wxString& - the filename.
     * returns the current LINE_READER source.
     * @return const wxString& - the source of the lines of text,
     *   e.g. a filename or "clipboard".
     */
    const wxString& CurFilename()
    const wxString& CurSource()
    {
        return filename;
        return reader->GetSource();
    }

    /**
Loading