KiCad PCB EDA Suite
sch_legacy_plugin_helpers.cpp File Reference
#include <errno.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <macros.h>
#include <richio.h>
#include <config.h>
#include "sch_legacy_plugin_helpers.h"

Go to the source code of this file.

Functions

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

Variables

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

Function Documentation

◆ is_eol()

bool is_eol ( char  c)

Definition at line 36 of file sch_legacy_plugin_helpers.cpp.

37{
38 // The default file eol character used internally by KiCad.
39 // |
40 // | Possible eol if someone edited the file by hand on certain platforms.
41 // | |
42 // | | May have gone past eol with strtok().
43 // | | |
44 if( c == '\n' || c == '\r' || c == 0 )
45 return true;
46
47 return false;
48}

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

◆ parseChar()

char parseChar ( LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = nullptr 
)

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 single character token.

Definition at line 165 of file sch_legacy_plugin_helpers.cpp.

166{
167 while( *aCurrentToken && isspace( *aCurrentToken ) )
168 aCurrentToken++;
169
170 if( !*aCurrentToken )
171 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
172
173 if( !isspace( *( aCurrentToken + 1 ) ) )
174 SCH_PARSE_ERROR( "expected single character token", aReader, aCurrentToken );
175
176 if( aNextToken )
177 {
178 const char* next = aCurrentToken + 2;
179
180 while( *next && isspace( *next ) )
181 next++;
182
183 *aNextToken = next;
184 }
185
186 return *aCurrentToken;
187}
#define _(s)
CITER next(CITER it)
Definition: ptree.cpp:126
#define SCH_PARSE_ERROR(text, reader, pos)

References _, next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN_CACHE::loadField(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadSymbol(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN_CACHE::parseFillMode().

◆ parseDouble()

double parseDouble ( LINE_READER aReader,
const char *  aLine,
const char **  aOutput = nullptr 
)

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 136 of file sch_legacy_plugin_helpers.cpp.

137{
138 if( !*aLine )
139 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
140
141 // Clear errno before calling strtod() in case some other crt call set it.
142 errno = 0;
143
144 double retv = strtod( aLine, (char**) aOutput );
145
146 // Make sure no error occurred when calling strtod().
147 if( errno == ERANGE )
148 SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
149
150 // strtod does not strip off whitespace before the next token.
151 if( aOutput )
152 {
153 const char* next = *aOutput;
154
155 while( *next && isspace( *next ) )
156 next++;
157
158 *aOutput = next;
159 }
160
161 return retv;
162}

References _, next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadBitmap(), PCB_PARSER::parse3DModel(), SCH_SEXPR_PARSER::parseArc(), PCB_PARSER::parseBoardStackup(), PCB_PARSER::parseBoardUnits(), PCB_PARSER::parseEDA_TEXT(), SCH_SEXPR_PARSER::parseEDA_TEXT(), SCH_SEXPR_PARSER::parseFill(), PCB_PARSER::parseFOOTPRINT_unchecked(), PCB_PARSER::parseFP_SHAPE(), PCB_PARSER::parseFP_TEXT(), PCB_PARSER::parseFP_TEXTBOX(), SCH_SEXPR_PARSER::parseImage(), SCH_SEXPR_PARSER::parseInternalUnits(), SCH_SEXPR_PARSER::parseJunction(), PCB_PARSER::parsePAD(), PCB_PARSER::parsePAGE_INFO(), SCH_SEXPR_PARSER::parsePAGE_INFO(), PCB_PARSER::parsePCB_BITMAP(), PCB_PARSER::parsePCB_SHAPE(), PCB_PARSER::parsePCB_TEXT(), PCB_PARSER::parsePCB_TEXTBOX(), SCH_SEXPR_PARSER::parseProperty(), PCB_PARSER::parseRenderCache(), SCH_SEXPR_PARSER::parseSchematicSymbol(), SCH_SEXPR_PARSER::parseSchField(), SCH_SEXPR_PARSER::parseSchSheetPin(), SCH_SEXPR_PARSER::parseSchText(), SCH_SEXPR_PARSER::parseSchTextBox(), PCB_PARSER::parseSetup(), SCH_SEXPR_PARSER::parseText(), SCH_SEXPR_PARSER::parseTextBox(), and PCB_PARSER::parseZONE().

◆ parseHex()

uint32_t parseHex ( LINE_READER aReader,
const char *  aLine,
const char **  aOutput = nullptr 
)

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 strtoll".

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

Definition at line 104 of file sch_legacy_plugin_helpers.cpp.

105{
106 if( !*aLine )
107 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
108
109 // Due to some issues between some files created by a 64 bits version and those
110 // created by a 32 bits version, we use here a temporary at least 64 bits storage:
111 unsigned long long retv;
112
113 // Clear errno before calling strtoull() in case some other crt call set it.
114 errno = 0;
115 retv = strtoull( aLine, (char**) aOutput, 16 );
116
117 // Make sure no error occurred when calling strtoull().
118 if( errno == ERANGE )
119 SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
120
121 // Strip off whitespace before the next token.
122 if( aOutput )
123 {
124 const char* next = *aOutput;
125
126 while( *next && isspace( *next ) )
127 next++;
128
129 *aOutput = next;
130 }
131
132 return (uint32_t)retv;
133}

References _, next(), and SCH_PARSE_ERROR.

Referenced by SCH_LEGACY_PLUGIN::loadSymbol().

◆ parseInt()

int parseInt ( LINE_READER aReader,
const char *  aLine,
const char **  aOutput = nullptr 
)

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 75 of file sch_legacy_plugin_helpers.cpp.

76{
77 if( !*aLine )
78 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
79
80 // Clear errno before calling strtol() in case some other crt call set it.
81 errno = 0;
82
83 long retv = strtol( aLine, (char**) aOutput, 10 );
84
85 // Make sure no error occurred when calling strtol().
86 if( errno == ERANGE )
87 SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
88
89 // strtol does not strip off whitespace before the next token.
90 if( aOutput )
91 {
92 const char* next = *aOutput;
93
94 while( *next && isspace( *next ) )
95 next++;
96
97 *aOutput = next;
98 }
99
100 return (int) retv;
101}

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_CACHE::loadField(), SCH_LEGACY_PLUGIN::loadHeader(), SCH_LEGACY_PLUGIN::loadJunction(), SCH_LEGACY_PLUGIN::loadNoConnect(), SCH_LEGACY_PLUGIN::loadPageSettings(), SCH_LEGACY_PLUGIN_CACHE::loadPolyLine(), SCH_LEGACY_PLUGIN_CACHE::loadRect(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadSymbol(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

◆ parseQuotedString()

void parseQuotedString ( wxString &  aString,
LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = nullptr,
bool  aCanBeEmpty = false 
)

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 236 of file sch_legacy_plugin_helpers.cpp.

238{
239 if( !*aCurrentToken )
240 {
241 if( aCanBeEmpty )
242 return;
243 else
244 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
245 }
246
247 const char* tmp = aCurrentToken;
248
249 while( *tmp && isspace( *tmp ) )
250 tmp++;
251
252 if( !*tmp )
253 {
254 if( aCanBeEmpty )
255 return;
256 else
257 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
258 }
259
260 // Verify opening quote.
261 if( *tmp != '"' )
262 SCH_PARSE_ERROR( "expecting opening quote", aReader, aCurrentToken );
263
264 tmp++;
265
266 std::string utf8; // utf8 without escapes and quotes.
267
268 // Fetch everything up to closing quote.
269 while( *tmp )
270 {
271 if( *tmp == '\\' )
272 {
273 tmp++;
274
275 if( !*tmp )
276 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
277
278 // Do not copy the escape byte if it is followed by \ or "
279 if( *tmp != '"' && *tmp != '\\' )
280 utf8 += '\\';
281
282 utf8 += *tmp;
283 }
284 else if( *tmp == '"' ) // Closing double quote.
285 {
286 break;
287 }
288 else
289 {
290 utf8 += *tmp;
291 }
292
293 tmp++;
294 }
295
296 aString = FROM_UTF8( utf8.c_str() );
297
298 if( aString.IsEmpty() && !aCanBeEmpty )
299 SCH_PARSE_ERROR( "expected quoted string", aReader, aCurrentToken );
300
301 if( *tmp && *tmp != '"' )
302 SCH_PARSE_ERROR( "no closing quote for string found", aReader, tmp );
303
304 // Move past the closing quote.
305 tmp++;
306
307 if( aNextToken )
308 {
309 const char* next = tmp;
310
311 while( *next == ' ' )
312 next++;
313
314 *aNextToken = next;
315 }
316}
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110

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

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

◆ parseUnquotedString()

void parseUnquotedString ( wxString &  aString,
LINE_READER aReader,
const char *  aCurrentToken,
const char **  aNextToken = nullptr,
bool  aCanBeEmpty = false 
)

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 190 of file sch_legacy_plugin_helpers.cpp.

192{
193 if( !*aCurrentToken )
194 {
195 if( aCanBeEmpty )
196 return;
197 else
198 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
199 }
200
201 const char* tmp = aCurrentToken;
202
203 while( *tmp && isspace( *tmp ) )
204 tmp++;
205
206 if( !*tmp )
207 {
208 if( aCanBeEmpty )
209 return;
210 else
211 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
212 }
213
214 std::string utf8;
215
216 while( *tmp && !isspace( *tmp ) )
217 utf8 += *tmp++;
218
219 aString = FROM_UTF8( utf8.c_str() );
220
221 if( aString.IsEmpty() && !aCanBeEmpty )
222 SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
223
224 if( aNextToken )
225 {
226 const char* next = tmp;
227
228 while( *next && isspace( *next ) )
229 next++;
230
231 *aNextToken = next;
232 }
233}

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

Referenced by SCH_LEGACY_PLUGIN::loadBusAlias(), 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::loadSheet(), SCH_LEGACY_PLUGIN::loadSymbol(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

◆ strCompare()

bool strCompare ( const char *  aString,
const char *  aLine,
const char **  aOutput = nullptr 
)

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 51 of file sch_legacy_plugin_helpers.cpp.

52{
53 size_t len = strlen( aString );
54 bool retv = ( strncasecmp( aLine, aString, len ) == 0 ) &&
55 ( isspace( aLine[ len ] ) || aLine[ len ] == 0 );
56
57 if( retv && aOutput )
58 {
59 const char* tmp = aLine;
60
61 // Move past the end of the token.
62 tmp += len;
63
64 // Move to the beginning of the next token.
65 while( *tmp && isspace( *tmp ) )
66 tmp++;
67
68 *aOutput = tmp;
69 }
70
71 return retv;
72}

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::loadBusAlias(), SCH_LEGACY_PLUGIN::loadBusEntry(), SCH_LEGACY_PLUGIN_CACHE::loadCircle(), SCH_LEGACY_PLUGIN::LoadContent(), SCH_LEGACY_PLUGIN_CACHE::loadDocs(), SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries(), SCH_LEGACY_PLUGIN::loadFile(), SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters(), SCH_LEGACY_PLUGIN_CACHE::loadHeader(), 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::loadRect(), SCH_LEGACY_PLUGIN::loadSheet(), SCH_LEGACY_PLUGIN::loadSymbol(), SCH_LEGACY_PLUGIN::loadText(), SCH_LEGACY_PLUGIN_CACHE::loadText(), and SCH_LEGACY_PLUGIN::loadWire().

Variable Documentation

◆ delims

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

Definition at line 33 of file sch_legacy_plugin_helpers.cpp.