KiCad PCB EDA Suite
netlist_reader.h
Go to the documentation of this file.
1 #ifndef NETLIST_READER_H
2 #define NETLIST_READER_H
3 
8 /*
9  * This program source code file is part of KiCad, a free EDA CAD application.
10  *
11  * Copyright (C) 2012 Jean-Pierre Charras.
12  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>.
13  * Copyright (C) 2012-2015 KiCad Developers, see CHANGELOG.TXT for contributors.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, you may find one here:
27  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
28  * or you may search the http://www.gnu.org website for the version 2 license,
29  * or you may write to the Free Software Foundation, Inc.,
30  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31  */
32 
33 #include <boost/ptr_container/ptr_vector.hpp>
34 
35 #include <fctsys.h>
36 #include <macros.h>
37 #include <lib_id.h>
38 
39 #include <netlist_lexer.h> // netlist_lexer is common to Eeschema and Pcbnew
40 
41 
42 class NETLIST;
43 class COMPONENT;
44 
45 
51 {
53 
54 public:
61  CMP_READER( LINE_READER* aLineReader )
62  {
63  m_lineReader = aLineReader;
64  }
65 
67  {
68  if( m_lineReader )
69  {
70  delete m_lineReader;
71  m_lineReader = NULL;
72  }
73  }
74 
103  bool Load( NETLIST* aNetlist ) throw( IO_ERROR, PARSE_ERROR );
104 };
105 
106 
112 {
113 public:
114 
116  {
117  UNKNOWN = -1,
121 
122  // Add new types here. Don't forget to create the appropriate class derived from
123  // NETCLASS_READER and add the entry to the NETLIST_READER::GetNetlistReader()
124  // function.
125  };
126 
127 
134  NETLIST* aNetlist,
135  CMP_READER* aFootprintLinkReader = NULL )
136  {
137  wxASSERT( aLineReader != NULL );
138 
139  m_lineReader = aLineReader;
140  m_footprintReader = aFootprintLinkReader;
141  m_netlist = aNetlist;
142  m_loadFootprintFilters = true;
143  m_loadNets = true;
144  }
145 
146  virtual ~NETLIST_READER();
147 
156  static NETLIST_FILE_T GuessNetlistFileType( LINE_READER* aLineReader );
157 
171  static NETLIST_READER* GetNetlistReader( NETLIST* aNetlist,
172  const wxString& aNetlistFileName,
173  const wxString& aCompFootprintFileName = wxEmptyString )
174  throw( IO_ERROR );
175 
183  virtual void LoadNetlist() throw( IO_ERROR, PARSE_ERROR, boost::bad_pointer ) = 0;
184 
190 
191 protected:
194  bool m_loadNets;
195  LINE_READER* m_lineReader;
196 
199 };
200 
201 
210 {
223  COMPONENT* loadComponent( char* aText ) throw( PARSE_ERROR, boost::bad_pointer );
224 
244  void loadFootprintFilters() throw( IO_ERROR, PARSE_ERROR );
245 
254  void loadNet( char* aText, COMPONENT* aComponent ) throw( PARSE_ERROR );
255 
256 public:
257 
259  NETLIST* aNetlist,
260  CMP_READER* aFootprintLinkReader = NULL ) :
261  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
262  {
263  }
264 
286  virtual void LoadNetlist() throw ( IO_ERROR, PARSE_ERROR, boost::bad_pointer ) override;
287 };
288 
289 
295 {
296 private:
300 
306  void skipCurrent() throw( IO_ERROR, PARSE_ERROR );
307 
318  void parseComponent() throw( IO_ERROR, PARSE_ERROR, boost::bad_pointer );
319 
330  void parseNet() throw( IO_ERROR, PARSE_ERROR );
331 
354  void parseLibPartList() throw( IO_ERROR, PARSE_ERROR );
355 
356 
357 public:
358  KICAD_NETLIST_PARSER( LINE_READER* aReader, NETLIST* aNetlist );
359 
360  void SetLineReader( LINE_READER* aLineReader );
361 
362  void SetNetlist( NETLIST* aNetlist ) { m_netlist = aNetlist; }
363 
368  void Parse() throw( IO_ERROR, PARSE_ERROR, boost::bad_pointer );
369 
370  // Useful for debug only:
371  const char* getTokenName( NL_T::T aTok )
372  {
373  return NETLIST_LEXER::TokenName( aTok );
374  }
375 };
376 
377 
382 class KICAD_NETLIST_READER : public NETLIST_READER
383 {
385 
386 public:
388  NETLIST* aNetlist,
389  CMP_READER* aFootprintLinkReader = NULL ) :
390  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader ),
391  m_parser( new KICAD_NETLIST_PARSER( aLineReader, aNetlist ) )
392  {
393  }
394 
396  {
397  delete m_parser;
398  }
399 
400  virtual void LoadNetlist() throw ( IO_ERROR, PARSE_ERROR, boost::bad_pointer ) override;
401 };
402 
403 
404 #endif // NETLIST_READER_H
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
static NETLIST_FILE_T GuessNetlistFileType(LINE_READER *aLineReader)
Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file t...
Class KICAD_NETLIST_READER read the new s-expression based KiCad netlist format.
static const char * TokenName(NL_T::T aTok)
Function TokenName returns the name of the token in ASCII form.
LINE_READER * m_lineReader
The line reader to read.
T
enum T contains all this lexer's tokens.
Class NETLIST_LEXER is an automatically generated class using the TokenList2DnsLexer.cmake technology, based on keywords provided by file: /home/kicad/workspace/kicad-doxygen/common/netlist.keywords.
Definition: netlist_lexer.h:92
NETLIST * m_netlist
The netlist to parse into. Not owned.
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return...
NETLIST * m_netlist
The net list to read the file(s) into.
CMP_READER(LINE_READER *aLineReader)
CMP_READER constructor.
This file contains miscellaneous commonly used macros and functions.
Class KICAD_NETLIST_PARSER is the parser for reading the KiCad s-expression netlist format...
virtual ~KICAD_NETLIST_READER()
bool Load(NETLIST *aNetlist)
Function Load read the *.cmp file format contains the component footprint assignments created by CvPc...
virtual void LoadNetlist()=0
Function LoadNetlist loads the contents of the netlist file into aNetlist.
KICAD_NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:205
LINE_READER * m_lineReader
The line reader used to parse the netlist. Not owned.
T
enum T contains all this lexer's tokens.
Definition: netlist_lexer.h:26
virtual ~NETLIST_READER()
Class NETLIST_READER is a pure virtual class to derive a specific type of netlist reader from...
KICAD_NETLIST_PARSER * m_parser
The s-expression format parser.
static const char * getTokenName(T aTok)
Class LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
LEGACY_NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
bool m_loadFootprintFilters
Load the component footprint filters section if true.
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller's report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
Class CMP_READER reads a component footprint link file (*.cmp) format.
bool m_loadNets
Load the nets section of the netlist file if true.
C++ does not put enum values in separate namespaces unless the enum itself is in a separate namespace...
Definition: netlist_lexer.h:23
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
Constructor.
LINE_READER * GetLineReader()
Function GetLineReader()
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.