From 5634bc3381ad0cce293fe439cdafb7331b6f1775 Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Wed, 24 Oct 2012 13:20:33 -0500 Subject: [PATCH] move PCB_PARSER into PCB_IO as a member --- include/dsnlexer.h | 6 ++++-- pcbnew/kicad_plugin.cpp | 22 ++++++++++++++-------- pcbnew/kicad_plugin.h | 4 ++++ pcbnew/pcb_parser.cpp | 9 +++++++++ pcbnew/pcb_parser.h | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/include/dsnlexer.h b/include/dsnlexer.h index 757e1156e6..aa3c3e99b1 100644 --- a/include/dsnlexer.h +++ b/include/dsnlexer.h @@ -204,12 +204,14 @@ public: * * @param aKeywordTable is an array of KEYWORDS holding \a aKeywordCount. This * token table need not contain the lexer separators such as '(' ')', etc. + * * @param aKeywordCount is the count of tokens in aKeywordTable. + * * @param aLineReader is any subclassed instance of LINE_READER, such as - * STRING_LINE_READER or FILE_LINE_READER. + * STRING_LINE_READER or FILE_LINE_READER. No ownership is taken. */ DSNLEXER( const KEYWORD* aKeywordTable, unsigned aKeywordCount, - LINE_READER* aLineReader ); + LINE_READER* aLineReader = NULL ); virtual ~DSNLEXER(); diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index 9f68431530..1eecdb10b5 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -241,11 +241,12 @@ void FP_CACHE::Load() // reader now owns fp, will close on exception or return FILE_LINE_READER reader( fp, fpFileName ); - PCB_PARSER parser( &reader ); + + m_owner->m_parser->SetLineReader( &reader ); std::string name = TO_UTF8( fpFileName ); - m_modules.insert( name, new FP_CACHE_ITEM( (MODULE*) parser.Parse(), fpFileName ) ); + m_modules.insert( name, new FP_CACHE_ITEM( (MODULE*) m_owner->m_parser->Parse(), fpFileName ) ); } while( dir.GetNext( &fpFileName ) ); @@ -339,9 +340,9 @@ BOARD_ITEM* PCB_IO::Parse( const wxString& aClipboardSourceInput ) throw( IO_ERR STRING_LINE_READER reader( input, wxT( "clipboard" ) ); - PCB_PARSER parser( &reader ); + m_parser->SetLineReader( &reader ); - return parser.Parse(); + return m_parser->Parse(); } @@ -1466,7 +1467,8 @@ void PCB_IO::format( ZONE_CONTAINER* aZone, int aNestLevel ) const PCB_IO::PCB_IO() : m_cache( 0 ), - m_ctl( 0 ) + m_ctl( 0 ), + m_parser( new PCB_PARSER() ) { init( 0 ); m_out = &m_sf; @@ -1475,7 +1477,8 @@ PCB_IO::PCB_IO() : PCB_IO::PCB_IO( int aControlFlags ) : m_cache( 0 ), - m_ctl( aControlFlags ) + m_ctl( aControlFlags ), + m_parser( new PCB_PARSER() ) { init( 0 ); m_out = &m_sf; @@ -1485,6 +1488,7 @@ PCB_IO::PCB_IO( int aControlFlags ) : PCB_IO::~PCB_IO() { delete m_cache; + delete m_parser; } @@ -1499,9 +1503,11 @@ BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, PROPERTIES* } FILE_LINE_READER reader( file.fp(), aFileName ); - PCB_PARSER parser( &reader, aAppendToMe ); - BOARD* board = dynamic_cast( parser.Parse() ); + m_parser->SetLineReader( &reader ); + m_parser->SetBoard( aAppendToMe ); + + BOARD* board = dynamic_cast( m_parser->Parse() ); wxASSERT( board ); // Give the filename to the board if it's new diff --git a/pcbnew/kicad_plugin.h b/pcbnew/kicad_plugin.h index 67f4865122..7a52ae27d7 100644 --- a/pcbnew/kicad_plugin.h +++ b/pcbnew/kicad_plugin.h @@ -30,6 +30,7 @@ class BOARD; class BOARD_ITEM; class FP_CACHE; +class PCB_PARSER; /** Current s-expression file format version. 2 was the last legacy format version. */ @@ -47,6 +48,8 @@ class FP_CACHE; */ class PCB_IO : public PLUGIN { + friend class FP_CACHE; + public: @@ -133,6 +136,7 @@ protected: STRING_FORMATTER m_sf; OUTPUTFORMATTER* m_out; ///< output any Format()s to this, no ownership int m_ctl; + PCB_PARSER* m_parser; private: diff --git a/pcbnew/pcb_parser.cpp b/pcbnew/pcb_parser.cpp index aaf5a32661..db49eaff72 100644 --- a/pcbnew/pcb_parser.cpp +++ b/pcbnew/pcb_parser.cpp @@ -51,6 +51,15 @@ using namespace std; + +void PCB_PARSER::init() +{ + m_layerMap.clear(); + + // @todo add default layernames here. +} + + double PCB_PARSER::parseDouble() throw( IO_ERROR ) { char* tmp; diff --git a/pcbnew/pcb_parser.h b/pcbnew/pcb_parser.h index e9ad61f4eb..243fae811a 100644 --- a/pcbnew/pcb_parser.h +++ b/pcbnew/pcb_parser.h @@ -66,6 +66,15 @@ class PCB_PARSER : public PCB_LEXER BOARD* m_board; LAYER_HASH_MAP m_layerMap; //< Map layer name to it's index saved in BOARD::m_Layer. + + /** + * Function init + * clears and re-establishes m_layerMap with the default layer names. + * m_layerMap will have some of its entries overwritten whenever a (new) board + * is encountered. + */ + void init(); + void parseHeader() throw( IO_ERROR, PARSE_ERROR ); void parseGeneralSection() throw( IO_ERROR, PARSE_ERROR ); void parsePAGE_INFO() throw( IO_ERROR, PARSE_ERROR ); @@ -204,13 +213,35 @@ class PCB_PARSER : public PCB_LEXER bool parseBool() throw( PARSE_ERROR ); + public: - PCB_PARSER( LINE_READER* aReader, BOARD* aBoard = NULL ) : + + PCB_PARSER( LINE_READER* aReader = NULL ) : PCB_LEXER( aReader ), - m_board( aBoard ) + m_board( 0 ) { } + + /** + * Functoin SetLineReader + * sets @a aLineReader into the parser, and returns the previous one, if any. + * @param aReader is what to read from for tokens, no ownership is received. + * @return LINE_READER* - previous LINE_READER or NULL if none. + */ + LINE_READER* SetLineReader( LINE_READER* aReader ) + { + LINE_READER* ret = PopReader(); + PushReader( aReader ); + return ret; + } + + void SetBoard( BOARD* aBoard ) + { + init(); + m_board = aBoard; + } + BOARD_ITEM* Parse() throw( IO_ERROR, PARSE_ERROR ); };