KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_legacy_helpers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <wx/intl.h>
21#include <wx/string.h>
22
23#include <fast_float/fast_float.h>
24
25#include <string_utils.h>
26#include <richio.h>
27
28#include <config.h> // contains strncasecmp for msvc
30
31
32// Token delimiters.
33const char* delims = " \t\r\n";
34
35
36bool is_eol( char c )
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}
49
50
51bool strCompare( const char* aString, const char* aLine, const char** aOutput )
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}
73
74
75int parseInt( LINE_READER& aReader, const char* aLine, const char** aOutput )
76{
77 if( !aLine )
78 THROW_PARSE_ERROR( _( "unexpected end of file" ), aReader.GetSource(), aReader.Line(), aReader.LineNumber(),
79 0 );
80
81 if( !*aLine )
82 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
83
84 // Clear errno before calling strtol() in case some other crt call set it.
85 errno = 0;
86
87 long retv = strtol( aLine, (char**) aOutput, 10 );
88
89 // Make sure no error occurred when calling strtol().
90 if( errno == ERANGE )
91 SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
92
93 // strtol does not strip off whitespace before the next token.
94 if( aOutput )
95 {
96 const char* next = *aOutput;
97
98 while( *next && isspace( *next ) )
99 next++;
100
101 *aOutput = next;
102 }
103
104 return (int) retv;
105}
106
107
108uint32_t parseHex( LINE_READER& aReader, const char* aLine, const char** aOutput )
109{
110 if( !*aLine )
111 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
112
113 // Due to some issues between some files created by a 64 bits version and those
114 // created by a 32 bits version, we use here a temporary at least 64 bits storage:
115 unsigned long long retv;
116
117 // Clear errno before calling strtoull() in case some other crt call set it.
118 errno = 0;
119 retv = strtoull( aLine, (char**) aOutput, 16 );
120
121 // Make sure no error occurred when calling strtoull().
122 if( errno == ERANGE )
123 SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
124
125 // Strip off whitespace before the next token.
126 if( aOutput )
127 {
128 const char* next = *aOutput;
129
130 while( *next && isspace( *next ) )
131 next++;
132
133 *aOutput = next;
134 }
135
136 return (uint32_t)retv;
137}
138
139
140double parseDouble( LINE_READER& aReader, const char* aLine, const char** aOutput )
141{
142 if( !*aLine )
143 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
144
145 double retv{};
146
147 const char* end = aLine;
148 fast_float::from_chars_result result =
149 fast_float::from_chars( aLine, aLine + strlen( aLine ), retv, fast_float::chars_format::skip_white_space );
150 end = result.ptr;
151
152 if( aOutput )
153 *aOutput = end;
154
155 // Make sure no error occurred when calling from_chars.
156 if( result.ec != std::errc() )
157 SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
158
159 // strtod does not strip off whitespace before the next token.
160 if( aOutput )
161 {
162 const char* next = *aOutput;
163
164 while( *next && isspace( *next ) )
165 next++;
166
167 *aOutput = next;
168 }
169
170 return retv;
171}
172
173
174char parseChar( LINE_READER& aReader, const char* aCurrentToken, const char** aNextToken )
175{
176 while( *aCurrentToken && isspace( *aCurrentToken ) )
177 aCurrentToken++;
178
179 if( !*aCurrentToken )
180 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
181
182 if( !isspace( *( aCurrentToken + 1 ) ) )
183 SCH_PARSE_ERROR( "expected single character token", aReader, aCurrentToken );
184
185 if( aNextToken )
186 {
187 const char* next = aCurrentToken + 2;
188
189 while( *next && isspace( *next ) )
190 next++;
191
192 *aNextToken = next;
193 }
194
195 return *aCurrentToken;
196}
197
198
199void parseUnquotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
200 const char** aNextToken, bool aCanBeEmpty )
201{
202 if( !*aCurrentToken )
203 {
204 if( aCanBeEmpty )
205 return;
206 else
207 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
208 }
209
210 const char* tmp = aCurrentToken;
211
212 while( *tmp && isspace( *tmp ) )
213 tmp++;
214
215 if( !*tmp )
216 {
217 if( aCanBeEmpty )
218 return;
219 else
220 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
221 }
222
223 std::string utf8;
224
225 while( *tmp && !isspace( *tmp ) )
226 utf8 += *tmp++;
227
228 aString = From_UTF8( utf8.c_str() );
229
230 if( aString.IsEmpty() && !aCanBeEmpty )
231 SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
232
233 if( aNextToken )
234 {
235 const char* next = tmp;
236
237 while( *next && isspace( *next ) )
238 next++;
239
240 *aNextToken = next;
241 }
242}
243
244
245void parseQuotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
246 const char** aNextToken, bool aCanBeEmpty )
247{
248 if( !*aCurrentToken )
249 {
250 if( aCanBeEmpty )
251 return;
252 else
253 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
254 }
255
256 const char* tmp = aCurrentToken;
257
258 while( *tmp && isspace( *tmp ) )
259 tmp++;
260
261 if( !*tmp )
262 {
263 if( aCanBeEmpty )
264 return;
265 else
266 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
267 }
268
269 // Verify opening quote.
270 if( *tmp != '"' )
271 SCH_PARSE_ERROR( "expecting opening quote", aReader, aCurrentToken );
272
273 tmp++;
274
275 std::string utf8; // utf8 without escapes and quotes.
276
277 // Fetch everything up to closing quote.
278 while( *tmp )
279 {
280 if( *tmp == '\\' )
281 {
282 tmp++;
283
284 if( !*tmp )
285 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
286
287 // Do not copy the escape byte if it is followed by \ or "
288 if( *tmp != '"' && *tmp != '\\' )
289 utf8 += '\\';
290
291 utf8 += *tmp;
292 }
293 else if( *tmp == '"' ) // Closing double quote.
294 {
295 break;
296 }
297 else
298 {
299 utf8 += *tmp;
300 }
301
302 tmp++;
303 }
304
305 aString = From_UTF8( utf8.c_str() );
306
307 if( aString.IsEmpty() && !aCanBeEmpty )
308 SCH_PARSE_ERROR( "expected quoted string", aReader, aCurrentToken );
309
310 if( *tmp && *tmp != '"' )
311 SCH_PARSE_ERROR( "no closing quote for string found", aReader, tmp );
312
313 // Move past the closing quote.
314 tmp++;
315
316 if( aNextToken )
317 {
318 const char* next = tmp;
319
320 while( *next == ' ' )
321 next++;
322
323 *aNextToken = next;
324 }
325}
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:65
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition richio.h:93
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:119
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:101
#define _(s)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
static wxString From_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition idf_helpers.h:44
CITER next(CITER it)
Definition ptree.cpp:120
const char * delims
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 pointe...
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...
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.
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.
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 th...
bool is_eol(char c)
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.
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 ...
#define SCH_PARSE_ERROR(text, reader, pos)
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.