KiCad PCB EDA Suite
sch_legacy_plugin.cpp File Reference
#include <ctype.h>
#include <algorithm>
#include <wx/mstream.h>
#include <wx/filename.h>
#include <wx/tokenzr.h>
#include <drawtxt.h>
#include <kiway.h>
#include <kicad_string.h>
#include <richio.h>
#include <core/typeinfo.h>
#include <properties.h>
#include <general.h>
#include <lib_field.h>
#include <sch_bus_entry.h>
#include <sch_marker.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_no_connect.h>
#include <sch_component.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <sch_bitmap.h>
#include <sch_legacy_plugin.h>
#include <template_fieldnames.h>
#include <class_sch_screen.h>
#include <class_libentry.h>
#include <class_library.h>
#include <lib_arc.h>
#include <lib_bezier.h>
#include <lib_circle.h>
#include <lib_pin.h>
#include <lib_polyline.h>
#include <lib_rectangle.h>
#include <lib_text.h>
#include <eeschema_id.h>

Go to the source code of this file.

Classes

class  SCH_LEGACY_PLUGIN_CACHE
 Class SCH_LEGACY_PLUGIN_CACHE is a cache assistant for the part library portion of the SCH_PLUGIN API, and only for the SCH_LEGACY_PLUGIN, so therefore is private to this implementation file, i.e. More...
 

Macros

#define DOCFILE_IDENT   "EESchema-DOCLIB Version 2.0"
 
#define SCH_PARSE_ERROR(text, reader, pos)
 

Functions

static bool is_eol (char c)
 
static bool strCompare (const char *aString, const char *aLine, const char **aOutput=NULL)
 Function strCompare. More...
 
static int parseInt (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 Function parseInt. More...
 
static unsigned long parseHex (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 Function parseHex. More...
 
static double parseDouble (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 Function parseDouble. More...
 
static char parseChar (FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL)
 Function parseChar. More...
 
static void parseUnquotedString (wxString &aString, FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL, bool aCanBeEmpty=false)
 Function parseUnquotedString. More...
 
static void parseQuotedString (wxString &aString, FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL, bool aCanBeEmpty=false)
 Function parseQuotedString. More...
 

Variables

const char * delims = " \t\r\n"
 
const wxChar traceSchLegacyPlugin [] = wxT( "KI_SCH_LEGACY_PLUGIN" )
 

Macro Definition Documentation

#define DOCFILE_IDENT   "EESchema-DOCLIB Version 2.0"

Function Documentation

static bool is_eol ( char  c)
static

Definition at line 78 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadText().

79 {
80  // The default file eol character used internally by KiCad.
81  // |
82  // | Possible eol if someone edited the file by hand on certain platforms.
83  // | |
84  // | | May have gone past eol with strtok().
85  // | | |
86  if( c == '\n' || c == '\r' || c == 0 )
87  return true;
88 
89  return false;
90 }
static char parseChar ( FILE_LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = NULL 
)
static

Function parseChar.

parses a single ASCII character and updates the pointer at aOutput if it is not NULL.

Parameters
aReader- The line reader used to generate exception throw information.
aCurrentToken- A pointer the current position in a string.
aNextToken- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
Returns
A valid ASCII character.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the parsed token is not a a single character token.

Definition at line 278 of file sch_legacy_plugin.cpp.

References next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN_CACHE::loadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN_CACHE::parseFillMode().

280 {
281  while( *aCurrentToken && isspace( *aCurrentToken ) )
282  aCurrentToken++;
283 
284  if( !*aCurrentToken )
285  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
286 
287  if( !isspace( *( aCurrentToken + 1 ) ) )
288  SCH_PARSE_ERROR( _( "expected single character token" ), aReader, aCurrentToken );
289 
290  if( aNextToken )
291  {
292  const char* next = aCurrentToken + 2;
293 
294  while( *next && isspace( *next ) )
295  next++;
296 
297  *aNextToken = next;
298  }
299 
300  return *aCurrentToken;
301 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define SCH_PARSE_ERROR(text, reader, pos)
static double parseDouble ( FILE_LINE_READER aReader,
const char *  aLine,
const char **  aOutput = NULL 
)
static

Function parseDouble.

parses an ASCII point string with possible leading whitespace into a double precision floating point number and updates the pointer at aOutput if it is not NULL, just like "man strtod".

Parameters
aReader- The line reader used to generate exception throw information.
aLine- A pointer the current position in a string.
aOutput- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
Returns
A valid double value.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the parsed token is not a valid integer.

Definition at line 235 of file sch_legacy_plugin.cpp.

References next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadBitmap(), PCB_PARSER::parse3DModel(), PCB_PARSER::parseD_PAD(), PCB_PARSER::parseDRAWSEGMENT(), PCB_PARSER::parseEDGE_MODULE(), PCB_PARSER::parseMODULE_unchecked(), PCB_PARSER::parsePAGE_INFO(), PCB_PARSER::parseSetup(), PCB_PARSER::parseTEXTE_MODULE(), and PCB_PARSER::parseTEXTE_PCB().

237 {
238  if( !*aLine )
239  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
240 
241  // Clear errno before calling strtod() in case some other crt call set it.
242  errno = 0;
243 
244  double retv = strtod( aLine, (char**) aOutput );
245 
246  // Make sure no error occurred when calling strtod().
247  if( errno == ERANGE )
248  SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
249 
250  // strtod does not strip off whitespace before the next token.
251  if( aOutput )
252  {
253  const char* next = *aOutput;
254 
255  while( *next && isspace( *next ) )
256  next++;
257 
258  *aOutput = next;
259  }
260 
261  return retv;
262 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define SCH_PARSE_ERROR(text, reader, pos)
static unsigned long parseHex ( FILE_LINE_READER aReader,
const char *  aLine,
const char **  aOutput = NULL 
)
static

Function parseHex.

parses an ASCII hex integer string with possible leading whitespace into a long integer and updates the pointer at aOutput if it is not NULL, just like "man strtol".

Parameters
aReader- The line reader used to generate exception throw information.
aLine- A pointer the current position in a string.
aOutput- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
Returns
A valid integer value.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the parsed token is not a valid integer.

Definition at line 187 of file sch_legacy_plugin.cpp.

References next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN::loadSheet(), PCB_PARSER::parseDIMENSION(), PCB_PARSER::parseDRAWSEGMENT(), PCB_PARSER::parseEDGE_MODULE(), PCB_PARSER::parseMODULE_unchecked(), PCB_PARSER::parsePCB_TARGET(), PCB_PARSER::parseSetup(), PCB_PARSER::parseTEXTE_PCB(), PCB_PARSER::parseTRACK(), PCB_PARSER::parseVIA(), and PCB_PARSER::parseZONE_CONTAINER().

189 {
190  if( !*aLine )
191  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
192 
193  unsigned long retv;
194 
195  // Clear errno before calling strtoul() in case some other crt call set it.
196  errno = 0;
197  retv = strtoul( aLine, (char**) aOutput, 16 );
198 
199  // Make sure no error occurred when calling strtoul().
200  if( errno == ERANGE )
201  SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
202 
203  // Strip off whitespace before the next token.
204  if( aOutput )
205  {
206  // const char* next = aLine + strlen( token );
207 
208  const char* next = *aOutput;
209 
210  while( *next && isspace( *next ) )
211  next++;
212 
213  *aOutput = next;
214  }
215 
216  return retv;
217 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define SCH_PARSE_ERROR(text, reader, pos)
static int parseInt ( FILE_LINE_READER aReader,
const char *  aLine,
const char **  aOutput = NULL 
)
static

Function parseInt.

parses an ASCII integer string with possible leading whitespace into an integer and updates the pointer at aOutput if it is not NULL, just like "man strtol()".

Parameters
aReader- The line reader used to generate exception throw information.
aLine- A pointer the current position in a string.
aOutput- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
Returns
A valid integer value.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the parsed token is not a valid integer.

Definition at line 143 of file sch_legacy_plugin.cpp.

References next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN_CACHE::Load(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), SCH_LEGACY_PLUGIN_CACHE::loadBezier(), SCH_LEGACY_PLUGIN::loadBitmap(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN_CACHE::loadCircle(), SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN::loadJunction(), SCH_LEGACY_PLUGIN::loadNoConnect(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::loadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), SCH_LEGACY_PLUGIN_CACHE::loadPolyLine(), SCH_LEGACY_PLUGIN_CACHE::loadRectangle(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

144 {
145  if( !*aLine )
146  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
147 
148  // Clear errno before calling strtol() in case some other crt call set it.
149  errno = 0;
150 
151  long retv = strtol( aLine, (char**) aOutput, 10 );
152 
153  // Make sure no error occurred when calling strtol().
154  if( errno == ERANGE )
155  SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
156 
157  // strtol does not strip off whitespace before the next token.
158  if( aOutput )
159  {
160  const char* next = *aOutput;
161 
162  while( *next && isspace( *next ) )
163  next++;
164 
165  *aOutput = next;
166  }
167 
168  return (int) retv;
169 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define SCH_PARSE_ERROR(text, reader, pos)
static void parseQuotedString ( wxString &  aString,
FILE_LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = NULL,
bool  aCanBeEmpty = false 
)
static

Function parseQuotedString.

parses an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL.

The parsed string must be contained within a single line. There are no multi-line quoted strings in the legacy schematic file format.

Parameters
aString- A reference to the parsed string.
aReader- The line reader used to generate exception throw information.
aCurrentToken- A pointer the current position in a string.
aNextToken- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
aCanBeEmpty- True if the parsed string is optional. False if it is mandatory.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the aCanBeEmpty is false and no string was parsed.

Definition at line 384 of file sch_legacy_plugin.cpp.

References FROM_UTF8(), next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN::loadSheet(), and SCH_LEGACY_PLUGIN_CACHE::loadText().

387 {
388  if( !*aCurrentToken )
389  {
390  if( aCanBeEmpty )
391  return;
392  else
393  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
394  }
395 
396  const char* tmp = aCurrentToken;
397 
398  while( *tmp && isspace( *tmp ) )
399  tmp++;
400 
401  if( !*tmp )
402  {
403  if( aCanBeEmpty )
404  return;
405  else
406  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
407  }
408 
409  // Verify opening quote.
410  if( *tmp != '"' )
411  SCH_PARSE_ERROR( _( "expecting opening quote" ), aReader, aCurrentToken );
412 
413  tmp++;
414 
415  std::string utf8; // utf8 without escapes and quotes.
416 
417  // Fetch everything up to closing quote.
418  while( *tmp )
419  {
420  if( *tmp == '\\' )
421  {
422  tmp++;
423 
424  if( !*tmp )
425  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
426 
427  // Do not copy the escape byte if it is followed by \ or "
428  if( *tmp != '"' && *tmp != '\\' )
429  utf8 += '\\';
430 
431  utf8 += *tmp;
432  }
433  else if( *tmp == '"' ) // Closing double quote.
434  {
435  break;
436  }
437  else
438  {
439  utf8 += *tmp;
440  }
441 
442  tmp++;
443  }
444 
445  aString = FROM_UTF8( utf8.c_str() );
446 
447  if( aString.IsEmpty() && !aCanBeEmpty )
448  SCH_PARSE_ERROR( _( "expected quoted string" ), aReader, aCurrentToken );
449 
450  if( *tmp && *tmp != '"' )
451  SCH_PARSE_ERROR( _( "no closing quote for string found" ), aReader, tmp );
452 
453  // Move past the closing quote.
454  tmp++;
455 
456  if( aNextToken )
457  {
458  const char* next = tmp;
459 
460  while( *next && *next == ' ' )
461  next++;
462 
463  *aNextToken = next;
464  }
465 }
CITER next(CITER it)
Definition: ptree.cpp:130
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 SCH_PARSE_ERROR(text, reader, pos)
static void parseUnquotedString ( wxString &  aString,
FILE_LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = NULL,
bool  aCanBeEmpty = false 
)
static

Function parseUnquotedString.

parses an unquoted utf8 string and updates the pointer at aOutput if it is not NULL.

The parsed string must be a continuous string with no white space.

Parameters
aString- A reference to the parsed string.
aReader- The line reader used to generate exception throw information.
aCurrentToken- A pointer the current position in a string.
aNextToken- The pointer to a string pointer to copy the string pointer position when the parsing is complete.
aCanBeEmpty- True if the parsed string is optional. False if it is mandatory.
Exceptions
AnIO_ERROR on an unexpected end of line.
APARSE_ERROR if the aCanBeEmpty is false and no string was parsed.

Definition at line 320 of file sch_legacy_plugin.cpp.

References FROM_UTF8(), next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadAliases(), SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadDocs(), SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters(), SCH_LEGACY_PLUGIN::loadJunction(), SCH_LEGACY_PLUGIN::loadNoConnect(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::loadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), and SCH_LEGACY_PLUGIN_CACHE::loadText().

323 {
324  if( !*aCurrentToken )
325  {
326  if( aCanBeEmpty )
327  return;
328  else
329  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
330  }
331 
332  const char* tmp = aCurrentToken;
333 
334  while( *tmp && isspace( *tmp ) )
335  tmp++;
336 
337  if( !*tmp )
338  {
339  if( aCanBeEmpty )
340  return;
341  else
342  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
343  }
344 
345  std::string utf8;
346 
347  while( *tmp && !isspace( *tmp ) )
348  utf8 += *tmp++;
349 
350  aString = FROM_UTF8( utf8.c_str() );
351 
352  if( aString.IsEmpty() && !aCanBeEmpty )
353  SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
354 
355  if( aNextToken )
356  {
357  const char* next = tmp;
358 
359  while( *next && isspace( *next ) )
360  next++;
361 
362  *aNextToken = next;
363  }
364 }
CITER next(CITER it)
Definition: ptree.cpp:130
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 SCH_PARSE_ERROR(text, reader, pos)
static bool strCompare ( const char *  aString,
const char *  aLine,
const char **  aOutput = NULL 
)
static

Function strCompare.

compares aString to the string starting at aLine and advances the character point to the end of String and returns the new pointer position in aOutput if it is not NULL.

Parameters
aString- A pointer to the string to compare.
aLine- A pointer to string to begin the comparison.
aOutput- A pointer to a string pointer to the end of the comparison if not NULL.
Returns
True if aString was found starting at aLine. Otherwise false.

Definition at line 104 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN_CACHE::Load(), SCH_LEGACY_PLUGIN_CACHE::loadAliases(), SCH_LEGACY_PLUGIN_CACHE::loadArc(), SCH_LEGACY_PLUGIN_CACHE::loadBezier(), SCH_LEGACY_PLUGIN::loadBitmap(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN_CACHE::loadCircle(), SCH_LEGACY_PLUGIN::loadComponent(), SCH_LEGACY_PLUGIN_CACHE::loadDocs(), SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries(), SCH_LEGACY_PLUGIN::loadFile(), SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), SCH_LEGACY_PLUGIN::loadJunction(), SCH_LEGACY_PLUGIN::loadNoConnect(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::loadPart(), SCH_LEGACY_PLUGIN_CACHE::loadPin(), SCH_LEGACY_PLUGIN_CACHE::loadPolyLine(), SCH_LEGACY_PLUGIN_CACHE::loadRectangle(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

105 {
106  size_t len = strlen( aString );
107  bool retv = ( strncasecmp( aLine, aString, len ) == 0 ) &&
108  ( isspace( aLine[ len ] ) || aLine[ len ] == 0 );
109 
110  if( retv && aOutput )
111  {
112  const char* tmp = aLine;
113 
114  // Move past the end of the token.
115  tmp += len;
116 
117  // Move to the beginning of the next token.
118  while( *tmp && isspace( *tmp ) )
119  tmp++;
120 
121  *aOutput = tmp;
122  }
123 
124  return retv;
125 }

Variable Documentation

const char* delims = " \t\r\n"

Definition at line 72 of file sch_legacy_plugin.cpp.

Referenced by SCH_SHEET_PIN::Load().

const wxChar traceSchLegacyPlugin[] = wxT( "KI_SCH_LEGACY_PLUGIN" )