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-2017 Wayne Stambaugh <stambaughw@gmail.com>.
13  * Copyright (C) 2012-2017 KiCad Developers, see AUTHORS.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 <macros.h>
36 #include <lib_id.h>
37 
38 #include <netlist_lexer.h> // netlist_lexer is common to Eeschema and Pcbnew
39 
40 
41 class NETLIST;
42 class COMPONENT;
43 
44 
50 {
52 
53 public:
60  CMP_READER( LINE_READER* aLineReader )
61  {
62  m_lineReader = aLineReader;
63  }
64 
66  {
67  if( m_lineReader )
68  {
69  delete m_lineReader;
71  }
72  }
73 
102  bool Load( NETLIST* aNetlist );
103 };
104 
105 
111 {
112 public:
113 
115  {
116  UNKNOWN = -1,
120 
121  // Add new types here. Don't forget to create the appropriate class derived from
122  // NETCLASS_READER and add the entry to the NETLIST_READER::GetNetlistReader()
123  // function.
124  };
125 
126 
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 
182  virtual void LoadNetlist() = 0;
183 
189 
190 protected:
193  bool m_loadNets;
195 
198 };
199 
200 
209 {
222  COMPONENT* loadComponent( char* aText );
223 
243  void loadFootprintFilters();
244 
253  void loadNet( char* aText, COMPONENT* aComponent );
254 
255 public:
256 
258  NETLIST* aNetlist,
259  CMP_READER* aFootprintLinkReader = NULL ) :
260  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
261  {
262  }
263 
285  virtual void LoadNetlist() override;
286 };
287 
288 
293 class KICAD_NETLIST_PARSER : public NETLIST_LEXER
294 {
295 private:
296  NL_T::T token;
299 
305  void skipCurrent();
306 
318  void parseComponent();
319 
330  void parseNet();
331 
354  void parseLibPartList();
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();
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 
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() override;
401 };
402 
403 
404 #endif // NETLIST_READER_H
LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived to re...
Definition: richio.h:82
static NETLIST_FILE_T GuessNetlistFileType(LINE_READER *aLineReader)
Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file t...
KICAD_NETLIST_READER read the new s-expression based KiCad netlist format.
void SetNetlist(NETLIST *aNetlist)
LINE_READER * m_lineReader
The line reader to read.
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.
void parseComponent()
Function parseComponent parse a component description: (comp (ref P1) (value DB25FEMELLE) (footprint ...
CMP_READER(LINE_READER *aLineReader)
CMP_READER constructor.
void skipCurrent()
Function skipCurrent Skip the current token level, i.e search for the RIGHT parenthesis which closes ...
This file contains miscellaneous commonly used macros and functions.
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() override
Function LoadNetlist loads the contents of the netlist file into aNetlist.
KICAD_NETLIST_PARSER(LINE_READER *aReader, NETLIST *aNetlist)
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)
void parseLibPartList()
Function parseLibPartList reads the section "libparts" in the netlist: (libparts (libpart (lib device...
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:194
#define NULL
LINE_READER * m_lineReader
The line reader used to parse the netlist. Not owned.
virtual ~NETLIST_READER()
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.
void loadNet(char *aText, COMPONENT *aComponent)
Function loadNet read a component net description from aText.
LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
LEGACY_NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
void Parse()
Function Parse parse the full netlist.
bool m_loadFootprintFilters
Load the component footprint filters section if true.
virtual void LoadNetlist() override
Function LoadNetlist read the netlist file in the legacy format into aNetlist.
const char * getTokenName(NL_T::T aTok)
CMP_READER reads a component footprint link file (*.cmp) format.
bool m_loadNets
Load the nets section of the netlist file if true.
void parseNet()
Function parseNet Parses a section like (net (code 20) (name /PC-A0) (node (ref BUS1) (pin 62)) (node...
void loadFootprintFilters()
Function loadFootprintFilters loads the footprint filter section of netlist file.
LINE_READER * m_lineReader
The line reader of the netlist.
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
Constructor.
void SetLineReader(LINE_READER *aLineReader)
LINE_READER * GetLineReader()
Function GetLineReader()
COMPONENT * loadComponent(char *aText)
Function loadComponent read the aLine containing the description of a component from a legacy format ...
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.