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 throw ( IO_ERROR, PARSE_ERROR, boost::bad_pointer )
 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) throw ( IO_ERROR )
 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) throw ( PARSE_ERROR, boost::bad_pointer )
 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 () throw ( IO_ERROR, PARSE_ERROR )
 Function loadFootprintFilters loads the footprint filter section of netlist file. More...
 
void loadNet (char *aText, COMPONENT *aComponent) throw ( PARSE_ERROR )
 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 
)
throw (IO_ERROR
)
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.

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

83 {
84  wxASSERT( aNetlist != NULL );
85 
86  std::unique_ptr< FILE_LINE_READER > file_rdr(new FILE_LINE_READER( aNetlistFileName ) );
87 
88  NETLIST_FILE_T type = GuessNetlistFileType( file_rdr.get() );
89  file_rdr->Rewind();
90 
91  // The component footprint link reader is NULL if no file name was specified.
92  std::unique_ptr<CMP_READER> cmp_rdr( aCompFootprintFileName.IsEmpty() ?
93  NULL :
94  new CMP_READER( new FILE_LINE_READER( aCompFootprintFileName ) ) );
95 
96  switch( type )
97  {
98  case LEGACY:
99  case ORCAD:
100  return new LEGACY_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
101 
102  case KICAD:
103  return new KICAD_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
104 
105  default: // Unrecognized format:
106  break;
107  }
108 
109  return NULL;
110 }
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.

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)
throw ( PARSE_ERROR,
boost::bad_pointer
)
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 FROM_UTF8(), LIB_ID::SetLibItemName(), COMPONENT::SetName(), THROW_PARSE_ERROR, timeStamp(), and TO_UTF8.

Referenced by LoadNetlist().

101 {
102  char* text;
103  wxString msg;
104  wxString timeStamp; // the full time stamp read from netlist
105  wxString footprintName; // the footprint name read from netlist
106  wxString value; // the component value read from netlist
107  wxString reference; // the component schematic reference designator read from netlist
108  wxString name; // the name of component that was placed in the schematic
109  char line[1024];
110 
111  strncpy( line, aText, sizeof(line)-1 );
112  line[sizeof(line)-1] = '\0';
113 
114  value = wxT( "~" );
115 
116  // Sample component line: /40C08647 $noname R20 4.7K {Lib=R}
117 
118  // Read time stamp (first word)
119  if( ( text = strtok( line, " ()\t\n" ) ) == NULL )
120  {
121  msg = _( "Cannot parse time stamp in component section of netlist." );
123  m_lineReader->Length() );
124  }
125 
126  timeStamp = FROM_UTF8( text );
127 
128  // Read footprint name (second word)
129  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
130  {
131  msg = _( "Cannot parse footprint name in component section of netlist." );
133  m_lineReader->Length() );
134  }
135 
136  footprintName = FROM_UTF8( text );
137 
138  // The footprint name will have to be looked up in the *.cmp file.
139  if( footprintName == wxT( "$noname" ) )
140  footprintName = wxEmptyString;
141 
142  // Read schematic reference designator (third word)
143  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
144  {
145  msg = _( "Cannot parse reference designator in component section of netlist." );
147  m_lineReader->Length() );
148  }
149 
150  reference = FROM_UTF8( text );
151 
152  // Read schematic value (forth word)
153  if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
154  {
155  msg = _( "Cannot parse value in component section of netlist." );
157  m_lineReader->Length() );
158  }
159 
160  value = FROM_UTF8( text );
161 
162  // Read component name (fifth word) {Lib=C}
163  // This is an optional field (a comment), which does not always exists
164  if( ( text = strtok( NULL, " ()\t\n" ) ) != NULL )
165  {
166  name = FROM_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
167  }
168 
169  LIB_ID fpid;
170 
171  if( !footprintName.IsEmpty() )
172  fpid.SetLibItemName( TO_UTF8( footprintName ) );
173 
174  COMPONENT* component = new COMPONENT( fpid, reference, value, timeStamp );
175  component->SetName( name );
176  m_netlist->AddComponent( component );
177  return component;
178 }
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.
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
LINE_READER * m_lineReader
The line reader of the netlist.
static unsigned long timeStamp(CPTREE &aTree)
Make a unique time stamp.
void LEGACY_NETLIST_READER::loadFootprintFilters ( )
throw ( IO_ERROR,
PARSE_ERROR
)
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 215 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().

216 {
217  wxArrayString filters;
218  wxString cmpRef;
219  char* line;
220  COMPONENT* component = NULL; // Suppress compil warning
221 
222  while( ( line = m_lineReader->ReadLine() ) != NULL )
223  {
224  if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
225  {
226  wxASSERT( component != NULL );
227  component->SetFootprintFilters( filters );
228  component = NULL;
229  filters.Clear();
230  continue;
231  }
232 
233  if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
234  // End of this section
235  return;
236 
237  if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
238  {
239  cmpRef = FROM_UTF8( line + 11 );
240  cmpRef.Trim( true );
241  cmpRef.Trim( false );
242 
243  component = m_netlist->GetComponentByReference( cmpRef );
244 
245  // Cannot happen if the netlist is valid.
246  if( component == NULL )
247  {
248  wxString msg;
249  msg.Printf( _( "Cannot find component \'%s\' in footprint filter section "
250  "of netlist." ), GetChars( cmpRef ) );
252  m_lineReader->Length() );
253  }
254  }
255  else
256  {
257  // Add new filter to list
258  wxString fp = FROM_UTF8( line + 1 );
259  fp.Trim( false );
260  fp.Trim( true );
261  filters.Add( fp );
262  }
263  }
264 }
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 
)
throw (PARSE_ERROR
)
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 181 of file legacy_netlist_reader.cpp.

References FROM_UTF8(), and THROW_PARSE_ERROR.

Referenced by LoadNetlist().

182 {
183  wxString msg;
184  char* p;
185  char line[256];
186 
187  strncpy( line, aText, sizeof( line ) );
188  line[ sizeof(line) - 1 ] = '\0';
189 
190  if( ( p = strtok( line, " ()\t\n" ) ) == NULL )
191  {
192  msg = _( "Cannot parse pin name in component net section of netlist." );
194  m_lineReader->Length() );
195  }
196 
197  wxString pinName = FROM_UTF8( p );
198 
199  if( ( p = strtok( NULL, " ()\t\n" ) ) == NULL )
200  {
201  msg = _( "Cannot parse net name in component net section of netlist." );
203  m_lineReader->Length() );
204  }
205 
206  wxString netName = FROM_UTF8( p );
207 
208  if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
209  netName = wxEmptyString;
210 
211  aComponent->AddNet( pinName, netName );
212 }
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 ( )
throw ( IO_ERROR,
PARSE_ERROR,
boost::bad_pointer
)
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

The line reader of the netlist.

Definition at line 195 of file netlist_reader.h.

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

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 loadFootprintFilters(), LoadNetlist(), and NETLIST_READER::NETLIST_READER().


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