KiCad PCB EDA Suite
LEGACY_NETLIST_READER Class Reference

Class LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats. More...

#include <netlist_reader.h>

Inheritance diagram for LEGACY_NETLIST_READER:
NETLIST_READER

Public Types

enum  NETLIST_FILE_T { UNKNOWN = -1, ORCAD, LEGACY, KICAD }
 

Public Member Functions

 LEGACY_NETLIST_READER (LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
 
virtual void LoadNetlist () override
 Function LoadNetlist read the netlist file in the legacy format into aNetlist. More...
 
LINE_READERGetLineReader ()
 Function GetLineReader() More...
 

Static Public Member Functions

static NETLIST_FILE_T GuessNetlistFileType (LINE_READER *aLineReader)
 Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file types it knows about. More...
 
static NETLIST_READERGetNetlistReader (NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
 Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_READER type. More...
 

Protected Attributes

NETLISTm_netlist
 The net list to read the file(s) into. More...
 
bool m_loadFootprintFilters
 Load the component footprint filters section if true. More...
 
bool m_loadNets
 Load the nets section of the netlist file if true. More...
 
LINE_READERm_lineReader
 The line reader of the netlist. More...
 
CMP_READERm_footprintReader
 The reader used to load the footprint links. If NULL, footprint links are not read. More...
 

Private Member Functions

COMPONENTloadComponent (char *aText)
 Function loadComponent read the aLine containing the description of a component from a legacy format netlist and add it to the netlist. More...
 
void loadFootprintFilters ()
 Function loadFootprintFilters loads the footprint filter section of netlist file. More...
 
void loadNet (char *aText, COMPONENT *aComponent)
 Function loadNet read a component net description from aText. More...
 

Detailed Description

Class LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.

The KiCad legacy netlist format was derived directly from an old Orcad netlist format. The primary difference is the header was changed so this reader can read both formats.

Definition at line 209 of file netlist_reader.h.

Member Enumeration Documentation

Enumerator
UNKNOWN 
ORCAD 
LEGACY 
KICAD 

Definition at line 115 of file netlist_reader.h.

116  {
117  UNKNOWN = -1,
118  ORCAD,
119  LEGACY,
120  KICAD,
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  };

Constructor & Destructor Documentation

LEGACY_NETLIST_READER::LEGACY_NETLIST_READER ( LINE_READER aLineReader,
NETLIST aNetlist,
CMP_READER aFootprintLinkReader = NULL 
)
inline

Definition at line 258 of file netlist_reader.h.

260  :
261  NETLIST_READER( aLineReader, aNetlist, aFootprintLinkReader )
262  {
263  }
NETLIST_READER(LINE_READER *aLineReader, NETLIST *aNetlist, CMP_READER *aFootprintLinkReader=NULL)
Constructor.

Member Function Documentation

LINE_READER* NETLIST_READER::GetLineReader ( )
inherited

Function GetLineReader()

Returns
the LINE_READER associated with the NETLIST_READER.
NETLIST_READER * NETLIST_READER::GetNetlistReader ( NETLIST aNetlist,
const wxString &  aNetlistFileName,
const wxString &  aCompFootprintFileName = wxEmptyString 
)
staticinherited

Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_READER type.

Parameters
aNetlistis the netlist to load aNetlistFileName into.
aNetlistFileNameis the full path and file name of the net list to read.
aCompFootprintFileNameis the full path and file name of the component footprint associations to read. Set to wxEmptyString if loading the footprint association file is not required.
Returns
the appropriate NETLIST_READER if aNetlistFileName is a valid netlist or NULL if aNetlistFileName is not a valid netlist files.

Definition at line 79 of file netlist_reader.cpp.

References NETLIST_READER::GuessNetlistFileType(), NETLIST_READER::KICAD, NETLIST_READER::LEGACY, and NETLIST_READER::ORCAD.

Referenced by PCB_EDIT_FRAME::ReadPcbNetlist(), and DIALOG_NETLIST::verifyFootprints().

82 {
83  wxASSERT( aNetlist != NULL );
84 
85  std::unique_ptr< FILE_LINE_READER > file_rdr(new FILE_LINE_READER( aNetlistFileName ) );
86 
87  NETLIST_FILE_T type = GuessNetlistFileType( file_rdr.get() );
88  file_rdr->Rewind();
89 
90  // The component footprint link reader is NULL if no file name was specified.
91  std::unique_ptr<CMP_READER> cmp_rdr( aCompFootprintFileName.IsEmpty() ?
92  NULL :
93  new CMP_READER( new FILE_LINE_READER( aCompFootprintFileName ) ) );
94 
95  switch( type )
96  {
97  case LEGACY:
98  case ORCAD:
99  return new LEGACY_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
100 
101  case KICAD:
102  return new KICAD_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
103 
104  default: // Unrecognized format:
105  break;
106  }
107 
108  return NULL;
109 }
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.
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
Class LEGACY_NETLIST_READER reads the KiCad legacy and the old Orcad netlist formats.
Class CMP_READER reads a component footprint link file (*.cmp) format.
NETLIST_READER::NETLIST_FILE_T NETLIST_READER::GuessNetlistFileType ( LINE_READER aLineReader)
staticinherited

Function GuessNetlistFileType looks at aFileHeaderLine to see if it matches any of the netlist file types it knows about.

Parameters
aLineReaderis the LINE_READER object containing lines from the netlist to test.
Returns
the NETLIST_FILE_T of aLineReader.

Definition at line 48 of file netlist_reader.cpp.

References FROM_UTF8(), NETLIST_READER::KICAD, NETLIST_READER::LEGACY, LINE_READER::Line(), NETLIST_READER::ORCAD, LINE_READER::ReadLine(), and NETLIST_READER::UNKNOWN.

Referenced by NETLIST_READER::GetNetlistReader().

49 {
50  // Orcad Pcb2 netlist format starts by "( {", followed by an unknown comment,
51  // depending on the tool which created the file
52  wxRegEx reOrcad( wxT( "(?i)[ ]*\\([ \t]+{+" ), wxRE_ADVANCED );
53  wxASSERT( reOrcad.IsValid() );
54  // Our legacy netlist format starts by "# EESchema Netlist "
55  wxRegEx reLegacy( wxT( "(?i)#[ \t]+EESchema[ \t]+Netlist[ \t]+" ), wxRE_ADVANCED );
56  wxASSERT( reLegacy.IsValid() );
57  // Our new netlist format starts by "(export (version "
58  wxRegEx reKicad( wxT( "[ ]*\\(export[ ]+" ), wxRE_ADVANCED );
59  wxASSERT( reKicad.IsValid() );
60 
61  wxString line;
62 
63  while( aLineReader->ReadLine() )
64  {
65  line = FROM_UTF8( aLineReader->Line() );
66 
67  if( reLegacy.Matches( line ) )
68  return LEGACY;
69  else if( reKicad.Matches( line ) )
70  return KICAD;
71  else if( reOrcad.Matches( line ) )
72  return ORCAD;
73  }
74 
75  return UNKNOWN;
76 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
COMPONENT * LEGACY_NETLIST_READER::loadComponent ( char *  aText)
private

Function loadComponent read the aLine containing the description of a component from a legacy format netlist and add it to the netlist.

Analyze the first line of a component description in netlist: ( /40C08647 $noname R20 4.7K {Lib=R}

Parameters
aTextcontains the first line of description
Returns
the new component created by parsing aLine
Exceptions
PARSE_ERRORwhen aLine is not a valid component description.

Definition at line 99 of file legacy_netlist_reader.cpp.

References NETLIST::AddComponent(), FROM_UTF8(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, NETLIST_READER::m_netlist, name, LIB_ID::SetLibItemName(), COMPONENT::SetName(), THROW_PARSE_ERROR, timeStamp(), and TO_UTF8.

Referenced by LoadNetlist().

100 {
101  char* text;
102  wxString msg;
103  wxString timeStamp; // the full time stamp read from netlist
104  wxString footprintName; // the footprint name read from netlist
105  wxString value; // the component value read from netlist
106  wxString reference; // the component schematic reference designator read from netlist
107  wxString name; // the name of component that was placed in the schematic
108  char line[1024];
109 
110  strncpy( line, aText, sizeof(line)-1 );
111  line[sizeof(line)-1] = '\0';
112 
113  value = wxT( "~" );
114 
115  // Sample component line: /40C08647 $noname R20 4.7K {Lib=R}
116 
117  // Read time stamp (first word)
118  if( ( text = strtok( line, " ()\t\n" ) ) == NULL )
119  {
120  msg = _( "Cannot parse time stamp in component section of netlist." );
122  m_lineReader->Length() );
123  }
124 
125  timeStamp = FROM_UTF8( text );
126 
127  // Read footprint name (second word)
128  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
129  {
130  msg = _( "Cannot parse footprint name in component section of netlist." );
132  m_lineReader->Length() );
133  }
134 
135  footprintName = FROM_UTF8( text );
136 
137  // The footprint name will have to be looked up in the *.cmp file.
138  if( footprintName == wxT( "$noname" ) )
139  footprintName = wxEmptyString;
140 
141  // Read schematic reference designator (third word)
142  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
143  {
144  msg = _( "Cannot parse reference designator in component section of netlist." );
146  m_lineReader->Length() );
147  }
148 
149  reference = FROM_UTF8( text );
150 
151  // Read schematic value (forth word)
152  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
153  {
154  msg = _( "Cannot parse value in component section of netlist." );
156  m_lineReader->Length() );
157  }
158 
159  value = FROM_UTF8( text );
160 
161  // Read component name (fifth word) {Lib=C}
162  // This is an optional field (a comment), which does not always exists
163  if( ( text = strtok( NULL, " ()\t\n" ) ) != NULL )
164  {
165  name = FROM_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
166  }
167 
168  LIB_ID fpid;
169 
170  if( !footprintName.IsEmpty() )
171  fpid.SetLibItemName( TO_UTF8( footprintName ) );
172 
173  COMPONENT* component = new COMPONENT( fpid, reference, value, timeStamp );
174  component->SetName( name );
175  m_netlist->AddComponent( component );
176  return component;
177 }
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
unsigned long timeStamp(wxXmlNode *aTree)
Make a unique time stamp.
NETLIST * m_netlist
The net list to read the file(s) into.
Class LIB_ID.
Definition: lib_id.h:56
void AddComponent(COMPONENT *aComponent)
Function AddComponent adds aComponent to the NETLIST.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:133
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:168
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
void SetName(const wxString &aName)
Definition: pcb_netlist.h:143
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:130
const char * name
LINE_READER * m_lineReader
The line reader of the netlist.
void LEGACY_NETLIST_READER::loadFootprintFilters ( )
private

Function loadFootprintFilters loads the footprint filter section of netlist file.

Sample legacy footprint filter section: { Allowed footprints by component: $component R11 R? SM0603 SM0805 R?-* SM1206 $endlist $endfootprintlist }

Exceptions
IO_ERRORif a file IO error occurs.
PARSE_ERRORif an error occurs while parsing the file.

Definition at line 214 of file legacy_netlist_reader.cpp.

References FROM_UTF8(), GetChars(), NETLIST::GetComponentByReference(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, NETLIST_READER::m_netlist, LINE_READER::ReadLine(), COMPONENT::SetFootprintFilters(), and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

215 {
216  wxArrayString filters;
217  wxString cmpRef;
218  char* line;
219  COMPONENT* component = NULL; // Suppress compil warning
220 
221  while( ( line = m_lineReader->ReadLine() ) != NULL )
222  {
223  if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
224  {
225  wxASSERT( component != NULL );
226  component->SetFootprintFilters( filters );
227  component = NULL;
228  filters.Clear();
229  continue;
230  }
231 
232  if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
233  // End of this section
234  return;
235 
236  if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
237  {
238  cmpRef = FROM_UTF8( line + 11 );
239  cmpRef.Trim( true );
240  cmpRef.Trim( false );
241 
242  component = m_netlist->GetComponentByReference( cmpRef );
243 
244  // Cannot happen if the netlist is valid.
245  if( component == NULL )
246  {
247  wxString msg;
248  msg.Printf( _( "Cannot find component \'%s\' in footprint filter section "
249  "of netlist." ), GetChars( cmpRef ) );
251  m_lineReader->Length() );
252  }
253  }
254  else
255  {
256  // Add new filter to list
257  wxString fp = FROM_UTF8( line + 1 );
258  fp.Trim( false );
259  fp.Trim( true );
260  filters.Add( fp );
261  }
262  }
263 }
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
NETLIST * m_netlist
The net list to read the file(s) into.
void SetFootprintFilters(const wxArrayString &aFilterList)
Definition: pcb_netlist.h:170
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:133
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:168
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:130
COMPONENT * GetComponentByReference(const wxString &aReference)
Function GetComponentByReference returns a COMPONENT by aReference.
LINE_READER * m_lineReader
The line reader of the netlist.
void LEGACY_NETLIST_READER::loadNet ( char *  aText,
COMPONENT aComponent 
)
private

Function loadNet read a component net description from aText.

Parameters
aTextis current line read from the netlist.
aComponentis the component to add the net to.
Exceptions
PARSE_ERRORif a error occurs reading aText.

Definition at line 180 of file legacy_netlist_reader.cpp.

References COMPONENT::AddNet(), FROM_UTF8(), LINE_READER::GetSource(), LINE_READER::Length(), LINE_READER::LineNumber(), NETLIST_READER::m_lineReader, and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

181 {
182  wxString msg;
183  char* p;
184  char line[256];
185 
186  strncpy( line, aText, sizeof( line ) );
187  line[ sizeof(line) - 1 ] = '\0';
188 
189  if( ( p = strtok( line, " ()\t\n" ) ) == NULL )
190  {
191  msg = _( "Cannot parse pin name in component net section of netlist." );
193  m_lineReader->Length() );
194  }
195 
196  wxString pinName = FROM_UTF8( p );
197 
198  if( ( p = strtok( NULL, " ()\t\n" ) ) == NULL )
199  {
200  msg = _( "Cannot parse net name in component net section of netlist." );
202  m_lineReader->Length() );
203  }
204 
205  wxString netName = FROM_UTF8( p );
206 
207  if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
208  netName = wxEmptyString;
209 
210  aComponent->AddNet( pinName, netName );
211 }
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:133
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:168
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:130
void AddNet(const wxString &aPinName, const wxString &aNetName)
Definition: pcb_netlist.h:130
LINE_READER * m_lineReader
The line reader of the netlist.
void LEGACY_NETLIST_READER::LoadNetlist ( )
overridevirtual

Function LoadNetlist read the netlist file in the legacy format into aNetlist.

The legacy netlist format is: # EESchema Netlist Version 1.0 generee le 18/5/2005-12:30:22 ( ( 40C08647 $noname R20 4,7K {Lib=R} ( 1 VCC ) ( 2 MODB_1 ) ) ( 40C0863F $noname R18 4,7_k {Lib=R} ( 1 VCC ) ( 2 MODA_1 ) ) } #End

Exceptions
IO_ERRORif a file IO error occurs.
PARSE_ERRORif an error occurs while parsing the file.

Implements NETLIST_READER.

Definition at line 37 of file legacy_netlist_reader.cpp.

References LINE_READER::Line(), CMP_READER::Load(), loadComponent(), loadFootprintFilters(), loadNet(), NETLIST_READER::m_footprintReader, NETLIST_READER::m_lineReader, NETLIST_READER::m_loadFootprintFilters, NETLIST_READER::m_netlist, LINE_READER::ReadLine(), and StrPurge().

38 {
39  int state = 0;
40  bool is_comment = false;
41  COMPONENT* component = NULL;
42 
43  while( m_lineReader->ReadLine() )
44  {
45  char* line = StrPurge( m_lineReader->Line() );
46 
47  if( is_comment ) // Comments in progress
48  {
49  // Test for end of the current comment
50  if( ( line = strchr( line, '}' ) ) == NULL )
51  continue;
52 
53  is_comment = false;
54  }
55 
56  if( *line == '{' ) // Start Comment or Pcbnew info section
57  {
58  is_comment = true;
59 
60  if( m_loadFootprintFilters && state == 0
61  && (strncasecmp( line, "{ Allowed footprints", 20 ) == 0) )
62  {
64  continue;
65  }
66 
67  if( ( line = strchr( line, '}' ) ) == NULL )
68  continue;
69  }
70 
71  if( *line == '(' )
72  state++;
73 
74  if( *line == ')' )
75  state--;
76 
77  if( state == 2 )
78  {
79  component = loadComponent( line );
80  continue;
81  }
82 
83  if( state >= 3 ) // Pad descriptions are read here.
84  {
85  wxASSERT( component != NULL );
86 
87  loadNet( line, component );
88  state--;
89  }
90  }
91 
92  if( m_footprintReader )
93  {
95  }
96 }
char * StrPurge(char *text)
Function StrPurge removes leading and training spaces, tabs and end of line chars in text return a po...
Definition: string.cpp:194
NETLIST * m_netlist
The net list to read the file(s) into.
bool Load(NETLIST *aNetlist)
Function Load read the *.cmp file format contains the component footprint assignments created by CvPc...
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
void loadNet(char *aText, COMPONENT *aComponent)
Function loadNet read a component net description from aText.
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
bool m_loadFootprintFilters
Load the component footprint filters section if true.
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
void loadFootprintFilters()
Function loadFootprintFilters loads the footprint filter section of netlist file. ...
LINE_READER * m_lineReader
The line reader of the netlist.
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.

Member Data Documentation

CMP_READER* NETLIST_READER::m_footprintReader
protectedinherited

The reader used to load the footprint links. If NULL, footprint links are not read.

Definition at line 198 of file netlist_reader.h.

Referenced by LoadNetlist(), NETLIST_READER::NETLIST_READER(), and NETLIST_READER::~NETLIST_READER().

LINE_READER* NETLIST_READER::m_lineReader
protectedinherited
bool NETLIST_READER::m_loadFootprintFilters
protectedinherited

Load the component footprint filters section if true.

Definition at line 193 of file netlist_reader.h.

Referenced by LoadNetlist(), and NETLIST_READER::NETLIST_READER().

bool NETLIST_READER::m_loadNets
protectedinherited

Load the nets section of the netlist file if true.

Definition at line 194 of file netlist_reader.h.

Referenced by NETLIST_READER::NETLIST_READER().

NETLIST* NETLIST_READER::m_netlist
protectedinherited

The net list to read the file(s) into.

Definition at line 192 of file netlist_reader.h.

Referenced by loadComponent(), loadFootprintFilters(), LoadNetlist(), and NETLIST_READER::NETLIST_READER().


The documentation for this class was generated from the following files: