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