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>

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 77 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadText().

78 {
79  // The default file eol character used internally by KiCad.
80  // |
81  // | Possible eol if someone edited the file by hand on certain platforms.
82  // | |
83  // | | May have gone past eol with strtok().
84  // | | |
85  if( c == '\n' || c == '\r' || c == 0 )
86  return true;
87 
88  return false;
89 }
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 277 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().

279 {
280  while( *aCurrentToken && isspace( *aCurrentToken ) )
281  aCurrentToken++;
282 
283  if( !*aCurrentToken )
284  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
285 
286  if( !isspace( *( aCurrentToken + 1 ) ) )
287  SCH_PARSE_ERROR( _( "expected single character token" ), aReader, aCurrentToken );
288 
289  if( aNextToken )
290  {
291  const char* next = aCurrentToken + 2;
292 
293  while( *next && isspace( *next ) )
294  next++;
295 
296  *aNextToken = next;
297  }
298 
299  return *aCurrentToken;
300 }
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 234 of file sch_legacy_plugin.cpp.

References next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadBitmap(), PCB_PLOT_PARAMS_PARSER::Parse(), PCB_PARSER::parse3DModel(), PAGE_LAYOUT_READER_PARSER::parseBitmap(), PAGE_LAYOUT_READER_PARSER::parseCoordinate(), PCB_PARSER::parseD_PAD(), PCB_PARSER::parseDRAWSEGMENT(), PCB_PARSER::parseEDGE_MODULE(), PAGE_LAYOUT_READER_PARSER::parseGraphic(), PCB_PARSER::parseMODULE_unchecked(), PCB_PARSER::parsePAGE_INFO(), PAGE_LAYOUT_READER_PARSER::parsePolygon(), PAGE_LAYOUT_READER_PARSER::parsePolyOutline(), PAGE_LAYOUT_READER_PARSER::parseSetup(), PCB_PARSER::parseSetup(), PAGE_LAYOUT_READER_PARSER::parseText(), PCB_PARSER::parseTEXTE_MODULE(), and PCB_PARSER::parseTEXTE_PCB().

236 {
237  if( !*aLine )
238  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
239 
240  // Clear errno before calling strtod() in case some other crt call set it.
241  errno = 0;
242 
243  double retv = strtod( aLine, (char**) aOutput );
244 
245  // Make sure no error occurred when calling strtod().
246  if( errno == ERANGE )
247  SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
248 
249  // strtod does not strip off whitespace before the next token.
250  if( aOutput )
251  {
252  const char* next = *aOutput;
253 
254  while( *next && isspace( *next ) )
255  next++;
256 
257  *aOutput = next;
258  }
259 
260  return retv;
261 }
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 186 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().

188 {
189  if( !*aLine )
190  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
191 
192  unsigned long retv;
193 
194  // Clear errno before calling strtoul() in case some other crt call set it.
195  errno = 0;
196  retv = strtoul( aLine, (char**) aOutput, 16 );
197 
198  // Make sure no error occurred when calling strtoul().
199  if( errno == ERANGE )
200  SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
201 
202  // Strip off whitespace before the next token.
203  if( aOutput )
204  {
205  // const char* next = aLine + strlen( token );
206 
207  const char* next = *aOutput;
208 
209  while( *next && isspace( *next ) )
210  next++;
211 
212  *aOutput = next;
213  }
214 
215  return retv;
216 }
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 142 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().

143 {
144  if( !*aLine )
145  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
146 
147  // Clear errno before calling strtol() in case some other crt call set it.
148  errno = 0;
149 
150  long retv = strtol( aLine, (char**) aOutput, 10 );
151 
152  // Make sure no error occurred when calling strtol().
153  if( errno == ERANGE )
154  SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
155 
156  // strtol does not strip off whitespace before the next token.
157  if( aOutput )
158  {
159  const char* next = *aOutput;
160 
161  while( *next && isspace( *next ) )
162  next++;
163 
164  *aOutput = next;
165  }
166 
167  return (int) retv;
168 }
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 383 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().

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

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

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

Variable Documentation

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

Definition at line 71 of file sch_legacy_plugin.cpp.

Referenced by SCH_SHEET_PIN::Load().

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