KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew/netlist_reader/legacy_netlist_reader.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2011 Jean-Pierre Charras.
5 * Copyright (C) 2013 Wayne Stambaugh <[email protected]>.
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <config.h> // to define strncasecmp for some platforms
23#include <richio.h>
24#include <string_utils.h>
25
26#include "pcb_netlist.h"
27#include <netlist_reader/netlist_reader.h>
28
29
31{
32 int state = 0;
33 bool is_comment = false;
34 COMPONENT* component = nullptr;
35
36 while( m_lineReader->ReadLine() )
37 {
38 char* line = StrPurge( m_lineReader->Line() );
39
40 if( is_comment ) // Comments in progress
41 {
42 // Test for end of the current comment
43 if( ( line = strchr( line, '}' ) ) == nullptr )
44 continue;
45
46 is_comment = false;
47 }
48
49 if( *line == '{' ) // Start Comment or Pcbnew info section
50 {
51 is_comment = true;
52
53 if( m_loadFootprintFilters && state == 0
54 && (strncasecmp( line, "{ Allowed footprints", 20 ) == 0) )
55 {
57 continue;
58 }
59
60 if( ( line = strchr( line, '}' ) ) == nullptr )
61 continue;
62 }
63
64 if( *line == '(' )
65 state++;
66
67 if( *line == ')' )
68 state--;
69
70 if( state == 2 )
71 {
72 component = loadComponent( line );
73 continue;
74 }
75
76 if( state >= 3 ) // Pad descriptions are read here.
77 {
78 wxASSERT( component != nullptr );
79
80 loadNet( line, component );
81 state--;
82 }
83 }
84
86 {
88 }
89}
90
91
93{
94 char* text;
95 wxString msg;
96 wxString footprintName; // the footprint name read from netlist
97 wxString value; // the component value read from netlist
98 wxString reference; // the component schematic reference designator read from netlist
99 wxString name; // the name of component that was placed in the schematic
100 char line[1024];
101
102 strncpy( line, aText, sizeof(line)-1 );
103 line[sizeof(line)-1] = '\0';
104
105 value = wxT( "~" );
106
107 // Sample component line: /68183921-93a5-49ac-91b0-49d05a0e1647 $noname R20 4.7K {Lib=R}
108
109 // Read time stamp (first word)
110 if( ( text = strtok( line, " ()\t\n" ) ) == nullptr )
111 {
112 msg = _( "Cannot parse time stamp in symbol section of netlist." );
113 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
114 m_lineReader->Length() );
115 }
116
117 KIID_PATH path( From_UTF8( text ) );
118
119 // Read footprint name (second word)
120 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
121 {
122 msg = _( "Cannot parse footprint name in symbol section of netlist." );
123 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
124 m_lineReader->Length() );
125 }
126
127 footprintName = From_UTF8( text );
128
129 // The footprint name will have to be looked up in the *.cmp file.
130 if( footprintName == wxT( "$noname" ) )
131 footprintName = wxEmptyString;
132
133 // Read schematic reference designator (third word)
134 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
135 {
136 msg = _( "Cannot parse reference designator in symbol section of netlist." );
137 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
138 m_lineReader->Length() );
139 }
140
141 reference = From_UTF8( text );
142
143 // Read schematic value (forth word)
144 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
145 {
146 msg = _( "Cannot parse value in symbol section of netlist." );
147 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
148 m_lineReader->Length() );
149 }
150
151 value = From_UTF8( text );
152
153 // Read component name (fifth word) {Lib=C}
154 // This is an optional field (a comment), which does not always exists
155 if( ( text = strtok( nullptr, " ()\t\n" ) ) != nullptr )
156 {
157 name = From_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
158 }
159
160 LIB_ID fpid;
161
162 if( !footprintName.IsEmpty() )
163 fpid.SetLibItemName( footprintName );
164
165 COMPONENT* component = new COMPONENT( fpid, reference, value, path, {} );
166 component->SetName( name );
167 m_netlist->AddComponent( component );
168 return component;
169}
170
171
172void LEGACY_NETLIST_READER::loadNet( char* aText, COMPONENT* aComponent )
173{
174 wxString msg;
175 char* p;
176 char line[256];
177
178 strncpy( line, aText, sizeof( line ) );
179 line[ sizeof(line) - 1 ] = '\0';
180
181 if( ( p = strtok( line, " ()\t\n" ) ) == nullptr )
182 {
183 msg = _( "Cannot parse pin name in symbol net section of netlist." );
184 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
185 m_lineReader->Length() );
186 }
187
188 wxString pinName = From_UTF8( p );
189
190 if( ( p = strtok( nullptr, " ()\t\n" ) ) == nullptr )
191 {
192 msg = _( "Cannot parse net name in symbol net section of netlist." );
193 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
194 m_lineReader->Length() );
195 }
196
197 wxString netName = From_UTF8( p );
198
199 if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
200 netName = wxEmptyString;
201
202 aComponent->AddNet( pinName, netName, wxEmptyString, wxEmptyString );
203}
204
205
207{
208 wxArrayString filters;
209 wxString cmpRef;
210 char* line;
211 COMPONENT* component = nullptr; // Suppress compile warning
212
213 while( ( line = m_lineReader->ReadLine() ) != nullptr )
214 {
215 if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
216 {
217 wxASSERT( component != nullptr );
218 component->SetFootprintFilters( filters );
219 component = nullptr;
220 filters.Clear();
221 continue;
222 }
223
224 if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
225 // End of this section
226 return;
227
228 if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
229 {
230 cmpRef = From_UTF8( line + 11 );
231 cmpRef.Trim( true );
232 cmpRef.Trim( false );
233
234 component = m_netlist->GetComponentByReference( cmpRef );
235
236 // Cannot happen if the netlist is valid.
237 if( component == nullptr )
238 {
239 wxString msg;
240 msg.Printf( _( "Cannot find symbol %s in footprint filter section of netlist." ),
241 cmpRef );
242 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
243 m_lineReader->Length() );
244 }
245 }
246 else
247 {
248 // Add new filter to list
249 wxString fp = From_UTF8( line + 1 );
250 fp.Trim( false );
251 fp.Trim( true );
252 filters.Add( fp );
253 }
254 }
255}
256
257// LocalWords: EDA Charras pcb netlist noname cmp endlist
258// LocalWords: endfootprintlist
const char * name
Store all of the related component information found in a netlist.
void SetFootprintFilters(const wxArrayString &aFilters)
void AddNet(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
void SetName(const wxString &aName)
COMPONENT * loadComponent(char *aText)
Read the aLine containing the description of a component from a legacy format netlist and add it to t...
void loadFootprintFilters()
Load the footprint filter section of netlist file.
void loadNet(char *aText, COMPONENT *aComponent)
Function loadNet read a component net description from aText.
virtual void LoadNetlist() override
Read the netlist file in the legacy format into aNetlist.
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:107
bool m_loadFootprintFilters
Load the component footprint filters section if true.
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.
LINE_READER * m_lineReader
The line reader of the netlist.
NETLIST * m_netlist
The net list to read the file(s) into.
#define _(s)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
wxString From_UTF8(const char *cstring)
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
std::string path