KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common/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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <richio.h>
27#include <string_utils.h>
28
30#include <netlist_reader/netlist_reader.h>
31#include <config.h> // to define strncasecmp for some platforms
32
34{
35 int state = 0;
36 bool is_comment = false;
37 COMPONENT* component = nullptr;
38
39 while( m_lineReader->ReadLine() )
40 {
41 char* line = StrPurge( m_lineReader->Line() );
42
43 if( is_comment ) // Comments in progress
44 {
45 // Test for end of the current comment
46 if( ( line = strchr( line, '}' ) ) == nullptr )
47 continue;
48
49 is_comment = false;
50 }
51
52 if( *line == '{' ) // Start Comment or Pcbnew info section
53 {
54 is_comment = true;
55
56 if( m_loadFootprintFilters && state == 0
57 && (strncasecmp( line, "{ Allowed footprints", 20 ) == 0) )
58 {
60 continue;
61 }
62
63 if( ( line = strchr( line, '}' ) ) == nullptr )
64 continue;
65 }
66
67 if( *line == '(' )
68 state++;
69
70 if( *line == ')' )
71 state--;
72
73 if( state == 2 )
74 {
75 component = loadComponent( line );
76 continue;
77 }
78
79 if( state >= 3 ) // Pad descriptions are read here.
80 {
81 wxASSERT( component != nullptr );
82
83 loadNet( line, component );
84 state--;
85 }
86 }
87
89 {
91 }
92}
93
94
96{
97 char* text;
98 wxString msg;
99 wxString footprintName; // the footprint name read from netlist
100 wxString value; // the component value read from netlist
101 wxString reference; // the component schematic reference designator read from netlist
102 wxString name; // the name of component that was placed in the schematic
103 char line[1024];
104
105 strncpy( line, aText, sizeof(line)-1 );
106 line[sizeof(line)-1] = '\0';
107
108 value = wxT( "~" );
109
110 // Sample component line: /68183921-93a5-49ac-91b0-49d05a0e1647 $noname R20 4.7K {Lib=R}
111
112 // Read time stamp (first word)
113 if( ( text = strtok( line, " ()\t\n" ) ) == nullptr )
114 {
115 msg = _( "Cannot parse time stamp in symbol section of netlist." );
116 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
117 m_lineReader->Length() );
118 }
119
121
122 // Read footprint name (second word)
123 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
124 {
125 msg = _( "Cannot parse footprint name in symbol section of netlist." );
126 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
127 m_lineReader->Length() );
128 }
129
130 footprintName = From_UTF8( text );
131
132 // The footprint name will have to be looked up in the *.cmp file.
133 if( footprintName == wxT( "$noname" ) )
134 footprintName = wxEmptyString;
135
136 // Read schematic reference designator (third word)
137 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
138 {
139 msg = _( "Cannot parse reference designator in symbol section of netlist." );
140 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
141 m_lineReader->Length() );
142 }
143
144 reference = From_UTF8( text );
145
146 // Read schematic value (forth word)
147 if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
148 {
149 msg = _( "Cannot parse value in symbol section of netlist." );
150 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
151 m_lineReader->Length() );
152 }
153
154 value = From_UTF8( text );
155
156 // Read component name (fifth word) {Lib=C}
157 // This is an optional field (a comment), which does not always exists
158 if( ( text = strtok( nullptr, " ()\t\n" ) ) != nullptr )
159 {
160 name = From_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
161 }
162
163 LIB_ID fpid;
164
165 if( !footprintName.IsEmpty() )
166 fpid.SetLibItemName( footprintName );
167
168 COMPONENT* component = new COMPONENT( fpid, reference, value, path, {} );
169 component->SetName( name );
170 m_netlist->AddComponent( component );
171 return component;
172}
173
174
175void LEGACY_NETLIST_READER::loadNet( char* aText, COMPONENT* aComponent )
176{
177 wxString msg;
178 char* p;
179 char line[256];
180
181 strncpy( line, aText, sizeof( line ) );
182 line[ sizeof(line) - 1 ] = '\0';
183
184 if( ( p = strtok( line, " ()\t\n" ) ) == nullptr )
185 {
186 msg = _( "Cannot parse pin name in symbol net section of netlist." );
187 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
188 m_lineReader->Length() );
189 }
190
191 wxString pinName = From_UTF8( p );
192
193 if( ( p = strtok( nullptr, " ()\t\n" ) ) == nullptr )
194 {
195 msg = _( "Cannot parse net name in symbol net section of netlist." );
196 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
197 m_lineReader->Length() );
198 }
199
200 wxString netName = From_UTF8( p );
201
202 if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
203 netName = wxEmptyString;
204
205 aComponent->AddNet( pinName, netName, wxEmptyString, wxEmptyString );
206}
207
208
210{
211 wxArrayString filters;
212 wxString cmpRef;
213 char* line;
214 COMPONENT* component = nullptr; // Suppress compile warning
215
216 while( ( line = m_lineReader->ReadLine() ) != nullptr )
217 {
218 if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
219 {
220 wxASSERT( component != nullptr );
221 component->SetFootprintFilters( filters );
222 component = nullptr;
223 filters.Clear();
224 continue;
225 }
226
227 if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
228 // End of this section
229 return;
230
231 if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
232 {
233 cmpRef = From_UTF8( line + 11 );
234 cmpRef.Trim( true );
235 cmpRef.Trim( false );
236
237 component = m_netlist->GetComponentByReference( cmpRef );
238
239 // Cannot happen if the netlist is valid.
240 if( component == nullptr )
241 {
242 wxString msg;
243 msg.Printf( _( "Cannot find symbol %s in footprint filter section of netlist." ),
244 cmpRef );
245 THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
246 m_lineReader->Length() );
247 }
248 }
249 else
250 {
251 // Add new filter to list
252 wxString fp = From_UTF8( line + 1 );
253 fp.Trim( false );
254 fp.Trim( true );
255 filters.Add( fp );
256 }
257 }
258}
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.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:111
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.