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-2021 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 
49 {
50 public:
56  CMP_READER( LINE_READER* aLineReader )
57  {
58  m_lineReader = aLineReader;
59  }
60 
62  {
63  if( m_lineReader )
64  {
65  delete m_lineReader;
66  m_lineReader = nullptr;
67  }
68  }
69 
96  bool Load( NETLIST* aNetlist );
97 
98 private:
100 };
101 
102 
107 {
108 public:
109 
111  {
112  UNKNOWN = -1,
116 
117  // Add new types here. Don't forget to create the appropriate class derived from
118  // NETCLASS_READER and add the entry to the NETLIST_READER::GetNetlistReader()
119  // function.
120  };
121 
128  NETLIST* aNetlist,
129  CMP_READER* aFootprintLinkReader = nullptr )
130  {
131  wxASSERT( aLineReader != nullptr );
132 
133  m_lineReader = aLineReader;
134  m_footprintReader = aFootprintLinkReader;
135  m_netlist = aNetlist;
136  m_loadFootprintFilters = true;
137  m_loadNets = true;
138  }
139 
140  virtual ~NETLIST_READER();
141 
149  static NETLIST_FILE_T GuessNetlistFileType( LINE_READER* aLineReader );
150 
163  static NETLIST_READER* GetNetlistReader( NETLIST* aNetlist,
164  const wxString& aNetlistFileName,
165  const wxString& aCompFootprintFileName = wxEmptyString );
166 
173  virtual void LoadNetlist() = 0;
174 
179 
180 protected:
183  bool m_loadNets;
185 
188 };
189 
190 
198 {
210  COMPONENT* loadComponent( char* aText );
211 
230  void loadFootprintFilters();
231 
240  void loadNet( char* aText, COMPONENT* aComponent );
241 
242 public:
243 
245  NETLIST* aNetlist,
246  CMP_READER* aFootprintLinkReader = nullptr ) :
247  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
248  {
249  }
250 
271  virtual void LoadNetlist() override;
272 };
273 
274 
278 class KICAD_NETLIST_PARSER : public NETLIST_LEXER
279 {
280 public:
281  KICAD_NETLIST_PARSER( LINE_READER* aReader, NETLIST* aNetlist );
282 
283  void SetLineReader( LINE_READER* aLineReader );
284 
285  void SetNetlist( NETLIST* aNetlist ) { m_netlist = aNetlist; }
286 
291  void Parse();
292 
293  // Useful for debug only:
294  const char* getTokenName( NL_T::T aTok )
295  {
296  return NETLIST_LEXER::TokenName( aTok );
297  }
298 
299 private:
304  void skipCurrent();
305 
316  void parseComponent();
317 
327  void parseNet();
328 
350  void parseLibPartList();
351 
352  NL_T::T token;
355 };
356 
357 
362 {
363 public:
365  NETLIST* aNetlist,
366  CMP_READER* aFootprintLinkReader = nullptr ) :
367  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader ),
368  m_parser( new KICAD_NETLIST_PARSER( aLineReader, aNetlist ) )
369  {
370  }
371 
373  {
374  delete m_parser;
375  }
376 
377  virtual void LoadNetlist() override;
378 
379 private:
381 };
382 
383 
384 #endif // NETLIST_READER_H
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
static NETLIST_FILE_T GuessNetlistFileType(LINE_READER *aLineReader)
Look at aFileHeaderLine to see if it matches any of the netlist file types it knows about.
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)
Attempt to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_RE...
NETLIST * m_netlist
The net list to read the file(s) into.
void parseComponent()
Parse a component description: (comp (ref P1) (value DB25FEMELLE) (footprint DB25FC) (libsource (lib ...
CMP_READER(LINE_READER *aLineReader)
void skipCurrent()
Skip the current token level, i.e search for the RIGHT parenthesis which closes the current descripti...
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=nullptr)
LEGACY_NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=nullptr)
This file contains miscellaneous commonly used macros and functions.
The parser for reading the KiCad s-expression netlist format.
virtual ~KICAD_NETLIST_READER()
bool Load(NETLIST *aNetlist)
Read the *.cmp file format contains the component footprint assignments created by CvPcb into aNetlis...
virtual void LoadNetlist() override
Load the contents of the netlist file into aNetlist.
KICAD_NETLIST_PARSER(LINE_READER *aReader, NETLIST *aNetlist)
virtual void LoadNetlist()=0
Load the contents of the netlist file into aNetlist.
void parseLibPartList()
Read the section "libparts" in the netlist: (libparts (libpart (lib device) (part C) (description "Co...
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:206
LINE_READER * m_lineReader
The line reader used to parse the netlist. Not owned.
virtual ~NETLIST_READER()
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.
Read the KiCad legacy and the old Orcad netlist formats.
KICAD_NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=nullptr)
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
void Parse()
Function Parse parse the full netlist.
bool m_loadFootprintFilters
Load the component footprint filters section if true.
virtual void LoadNetlist() override
Read the netlist file in the legacy format into aNetlist.
const char * getTokenName(NL_T::T aTok)
Read a component footprint link file (*.cmp) format.
bool m_loadNets
Load the nets section of the netlist file if true.
void parseNet()
Parse a net section (net (code 20) (name /PC-A0) (node (ref BUS1) (pin 62)) (node (ref U3) (pin 3)) (...
void loadFootprintFilters()
Load the footprint filter section of netlist file.
LINE_READER * m_lineReader
The line reader of the netlist.
void SetLineReader(LINE_READER *aLineReader)
LINE_READER * GetLineReader()
COMPONENT * loadComponent(char *aText)
Read the aLine containing the description of a component from a legacy format netlist and add it to t...
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.