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 (C) 2022 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 <errno.h>
21
22#include <wx/intl.h>
23#include <wx/string.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 // 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}
163
164
165char parseChar( LINE_READER& aReader, const char* aCurrentToken, const char** aNextToken )
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}
188
189
190void parseUnquotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
191 const char** aNextToken, bool aCanBeEmpty )
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}
234
235
236void parseQuotedString( wxString& aString, LINE_READER& aReader, const char* aCurrentToken,
237 const char** aNextToken, bool aCanBeEmpty )
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}
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:126
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)