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 <draw_graphic_text.h>
#include <kiway.h>
#include <kicad_string.h>
#include <richio.h>
#include <core/typeinfo.h>
#include <properties.h>
#include <trace_helpers.h>
#include <general.h>
#include <sch_bitmap.h>
#include <sch_bus_entry.h>
#include <sch_component.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_marker.h>
#include <sch_no_connect.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <sch_legacy_plugin.h>
#include <template_fieldnames.h>
#include <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_field.h>
#include <lib_pin.h>
#include <lib_polyline.h>
#include <lib_rectangle.h>
#include <lib_text.h>
#include <eeschema_id.h>
#include <symbol_lib_table.h>

Go to the source code of this file.

Classes

class  SCH_LEGACY_PLUGIN_CACHE
 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)
 
#define T_STYLE   "style"
 
#define T_COLOR   "rgb"
 
#define T_COLORA   "rgba"
 
#define T_WIDTH   "width"
 

Functions

static bool is_eol (char c)
 
static bool strCompare (const char *aString, const char *aLine, const char **aOutput=NULL)
 Compare 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. More...
 
static int parseInt (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 Parse 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()". More...
 
static unsigned long parseHex (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 Parse 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". More...
 
static double parseDouble (FILE_LINE_READER &aReader, const char *aLine, const char **aOutput=NULL)
 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". More...
 
static char parseChar (FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL)
 Parse a single ASCII character and updates the pointer at aOutput if it is not NULL. More...
 
static void parseUnquotedString (wxString &aString, FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL, bool aCanBeEmpty=false)
 Parse an unquoted utf8 string and updates the pointer at aOutput if it is not NULL. More...
 
static void parseQuotedString (wxString &aString, FILE_LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=NULL, bool aCanBeEmpty=false)
 Parse an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL. More...
 

Variables

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

Macro Definition Documentation

#define DOCFILE_IDENT   "EESchema-DOCLIB Version 2.0"
#define T_COLOR   "rgb"

Definition at line 78 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN::loadWire().

#define T_COLORA   "rgba"

Definition at line 79 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN::loadWire().

#define T_STYLE   "style"
#define T_WIDTH   "width"

Function Documentation

static bool is_eol ( char  c)
static

Definition at line 83 of file sch_legacy_plugin.cpp.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

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

Parse 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
IO_ERRORon an unexpected end of line.
PARSE_ERRORif the parsed token is not a a single character token.

Definition at line 273 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().

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

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
IO_ERRORon an unexpected end of line.
PARSE_ERRORif the parsed token is not a valid integer.

Definition at line 232 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().

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

Parse 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
IO_ERRORon an unexpected end of line.
PARSE_ERRORif 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

Parse 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 144 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().

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

Parse 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
IO_ERRORon an unexpected end of line.
PARSE_ERRORif the aCanBeEmpty is false and no string was parsed.

Definition at line 375 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().

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

Parse 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
IO_ERRORon an unexpected end of line.
PARSE_ERRORif the aCanBeEmpty is false and no string was parsed.

Definition at line 313 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(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

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

Compare 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 107 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().

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

Variable Documentation

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

Definition at line 74 of file sch_legacy_plugin.cpp.