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 along
17 * with this program. If not, see <http://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 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}
102
103
104uint32_t parseHex( LINE_READER& aReader, const char* aLine, const char** aOutput )
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}
134
135
136double parseDouble( LINE_READER& aReader, const char* aLine, const char** aOutput )
137{
138 if( !*aLine )
139 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
140
141 double retv{};
142
143 const char* end = aLine;
144 fast_float::from_chars_result result =
145 fast_float::from_chars( aLine, aLine + strlen( aLine ), retv, fast_float::chars_format::skip_white_space );
146 end = result.ptr;
147
148 if( aOutput )
149 *aOutput = end;
150
151 // Make sure no error occurred when calling from_chars.
152 if( result.ec != std::errc() )
153 SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
154
155 // strtod does not strip off whitespace before the next token.
156 if( aOutput )
157 {
158 const char* next = *aOutput;
159
160 while( *next && isspace( *next ) )
161 next++;
162
163 *aOutput = next;
164 }
165
166 return retv;
167}
168
169
170char parseChar( LINE_READER& aReader, const char* aCurrentToken, const char** aNextToken )
171{
172 while( *aCurrentToken && isspace( *aCurrentToken ) )
173 aCurrentToken++;
174
175 if( !*aCurrentToken )
176 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
177
178 if( !isspace( *( aCurrentToken + 1 ) ) )
179 SCH_PARSE_ERROR( "expected single character token", aReader, aCurrentToken );
180
181 if( aNextToken )
182 {
183 const char* next = aCurrentToken + 2;
184
185 while( *next && isspace( *next ) )
186 next++;
187
188 *aNextToken = next;
189 }
190
191 return *aCurrentToken;
192}
193
194
195void parseUnquotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
196 const char** aNextToken, bool aCanBeEmpty )
197{
198 if( !*aCurrentToken )
199 {
200 if( aCanBeEmpty )
201 return;
202 else
203 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
204 }
205
206 const char* tmp = aCurrentToken;
207
208 while( *tmp && isspace( *tmp ) )
209 tmp++;
210
211 if( !*tmp )
212 {
213 if( aCanBeEmpty )
214 return;
215 else
216 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
217 }
218
219 std::string utf8;
220
221 while( *tmp && !isspace( *tmp ) )
222 utf8 += *tmp++;
223
224 aString = From_UTF8( utf8.c_str() );
225
226 if( aString.IsEmpty() && !aCanBeEmpty )
227 SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
228
229 if( aNextToken )
230 {
231 const char* next = tmp;
232
233 while( *next && isspace( *next ) )
234 next++;
235
236 *aNextToken = next;
237 }
238}
239
240
241void parseQuotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
242 const char** aNextToken, bool aCanBeEmpty )
243{
244 if( !*aCurrentToken )
245 {
246 if( aCanBeEmpty )
247 return;
248 else
249 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
250 }
251
252 const char* tmp = aCurrentToken;
253
254 while( *tmp && isspace( *tmp ) )
255 tmp++;
256
257 if( !*tmp )
258 {
259 if( aCanBeEmpty )
260 return;
261 else
262 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
263 }
264
265 // Verify opening quote.
266 if( *tmp != '"' )
267 SCH_PARSE_ERROR( "expecting opening quote", aReader, aCurrentToken );
268
269 tmp++;
270
271 std::string utf8; // utf8 without escapes and quotes.
272
273 // Fetch everything up to closing quote.
274 while( *tmp )
275 {
276 if( *tmp == '\\' )
277 {
278 tmp++;
279
280 if( !*tmp )
281 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
282
283 // Do not copy the escape byte if it is followed by \ or "
284 if( *tmp != '"' && *tmp != '\\' )
285 utf8 += '\\';
286
287 utf8 += *tmp;
288 }
289 else if( *tmp == '"' ) // Closing double quote.
290 {
291 break;
292 }
293 else
294 {
295 utf8 += *tmp;
296 }
297
298 tmp++;
299 }
300
301 aString = From_UTF8( utf8.c_str() );
302
303 if( aString.IsEmpty() && !aCanBeEmpty )
304 SCH_PARSE_ERROR( "expected quoted string", aReader, aCurrentToken );
305
306 if( *tmp && *tmp != '"' )
307 SCH_PARSE_ERROR( "no closing quote for string found", aReader, tmp );
308
309 // Move past the closing quote.
310 tmp++;
311
312 if( aNextToken )
313 {
314 const char* next = tmp;
315
316 while( *next == ' ' )
317 next++;
318
319 *aNextToken = next;
320 }
321}
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:93
#define _(s)
CITER next(CITER it)
Definition ptree.cpp:124
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)
wxString From_UTF8(const char *cstring)
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.