1 | /* readval.c |
---|
2 | * Routines to read a prefix or number from the current input file |
---|
3 | * Copyright (C) 1991-2003,2005,2006,2010,2011,2012,2013,2014,2015,2016,2018,2019 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <limits.h> |
---|
25 | #include <stddef.h> /* for offsetof */ |
---|
26 | |
---|
27 | #include "cavern.h" |
---|
28 | #include "date.h" |
---|
29 | #include "debug.h" |
---|
30 | #include "filename.h" |
---|
31 | #include "message.h" |
---|
32 | #include "readval.h" |
---|
33 | #include "datain.h" |
---|
34 | #include "netbits.h" |
---|
35 | #include "osalloc.h" |
---|
36 | #include "str.h" |
---|
37 | |
---|
38 | #ifdef HAVE_SETJMP_H |
---|
39 | # define LONGJMP(JB) longjmp((JB), 1) |
---|
40 | #else |
---|
41 | # define LONGJMP(JB) exit(1) |
---|
42 | #endif |
---|
43 | |
---|
44 | int root_depr_count = 0; |
---|
45 | |
---|
46 | static prefix * |
---|
47 | new_anon_station(void) |
---|
48 | { |
---|
49 | prefix *name = osnew(prefix); |
---|
50 | name->pos = NULL; |
---|
51 | name->ident = NULL; |
---|
52 | name->shape = 0; |
---|
53 | name->stn = NULL; |
---|
54 | name->up = pcs->Prefix; |
---|
55 | name->down = NULL; |
---|
56 | name->filename = file.filename; |
---|
57 | name->line = file.line; |
---|
58 | name->min_export = name->max_export = 0; |
---|
59 | name->sflags = BIT(SFLAGS_ANON); |
---|
60 | /* Keep linked list of anon stations for node stats. */ |
---|
61 | name->right = anon_list; |
---|
62 | anon_list = name; |
---|
63 | return name; |
---|
64 | } |
---|
65 | |
---|
66 | /* if prefix is omitted: if PFX_OPT set return NULL, otherwise use longjmp */ |
---|
67 | extern prefix * |
---|
68 | read_prefix(unsigned pfx_flags) |
---|
69 | { |
---|
70 | bool f_optional = !!(pfx_flags & PFX_OPT); |
---|
71 | bool fSurvey = !!(pfx_flags & PFX_SURVEY); |
---|
72 | bool fSuspectTypo = !!(pfx_flags & PFX_SUSPECT_TYPO); |
---|
73 | prefix *back_ptr, *ptr; |
---|
74 | char *name; |
---|
75 | size_t name_len = 32; |
---|
76 | size_t i; |
---|
77 | bool fNew; |
---|
78 | bool fImplicitPrefix = fTrue; |
---|
79 | int depth = -1; |
---|
80 | filepos fp_firstsep; |
---|
81 | |
---|
82 | skipblanks(); |
---|
83 | #ifndef NO_DEPRECATED |
---|
84 | if (isRoot(ch)) { |
---|
85 | if (!(pfx_flags & PFX_ALLOW_ROOT)) { |
---|
86 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*ROOT is deprecated*/25); |
---|
87 | LONGJMP(file.jbSkipLine); |
---|
88 | } |
---|
89 | if (root_depr_count < 5) { |
---|
90 | compile_diagnostic(DIAG_WARN|DIAG_COL, /*ROOT is deprecated*/25); |
---|
91 | if (++root_depr_count == 5) |
---|
92 | compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95); |
---|
93 | } |
---|
94 | nextch(); |
---|
95 | ptr = root; |
---|
96 | if (!isNames(ch)) { |
---|
97 | if (!isSep(ch)) return ptr; |
---|
98 | /* Allow optional SEPARATOR after ROOT */ |
---|
99 | get_pos(&fp_firstsep); |
---|
100 | nextch(); |
---|
101 | } |
---|
102 | fImplicitPrefix = fFalse; |
---|
103 | #else |
---|
104 | if (0) { |
---|
105 | #endif |
---|
106 | } else { |
---|
107 | if ((pfx_flags & PFX_ANON) && |
---|
108 | (isSep(ch) || (pcs->dash_for_anon_wall_station && ch == '-'))) { |
---|
109 | int first_ch = ch; |
---|
110 | filepos here; |
---|
111 | get_pos(&here); |
---|
112 | nextch(); |
---|
113 | if (isBlank(ch) || isEol(ch)) { |
---|
114 | if (!isSep(first_ch)) |
---|
115 | goto anon_wall_station; |
---|
116 | /* A single separator alone ('.' by default) is an anonymous |
---|
117 | * station which is on a point inside the passage and implies |
---|
118 | * the leg to it is a splay. |
---|
119 | */ |
---|
120 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
121 | set_pos(&here); |
---|
122 | compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Can't have a leg between two anonymous stations*/3); |
---|
123 | LONGJMP(file.jbSkipLine); |
---|
124 | } |
---|
125 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
---|
126 | return new_anon_station(); |
---|
127 | } |
---|
128 | if (isSep(first_ch) && ch == first_ch) { |
---|
129 | nextch(); |
---|
130 | if (isBlank(ch) || isEol(ch)) { |
---|
131 | /* A double separator ('..' by default) is an anonymous station |
---|
132 | * which is on the wall and implies the leg to it is a splay. |
---|
133 | */ |
---|
134 | prefix * pfx; |
---|
135 | anon_wall_station: |
---|
136 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
137 | set_pos(&here); |
---|
138 | compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Can't have a leg between two anonymous stations*/3); |
---|
139 | LONGJMP(file.jbSkipLine); |
---|
140 | } |
---|
141 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
---|
142 | pfx = new_anon_station(); |
---|
143 | pfx->sflags |= BIT(SFLAGS_WALL); |
---|
144 | return pfx; |
---|
145 | } |
---|
146 | if (ch == first_ch) { |
---|
147 | nextch(); |
---|
148 | if (isBlank(ch) || isEol(ch)) { |
---|
149 | /* A triple separator ('...' by default) is an anonymous |
---|
150 | * station, but otherwise not handled specially (e.g. for |
---|
151 | * a single leg down an unexplored side passage to a station |
---|
152 | * which isn't refindable). |
---|
153 | */ |
---|
154 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
155 | set_pos(&here); |
---|
156 | compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Can't have a leg between two anonymous stations*/3); |
---|
157 | LONGJMP(file.jbSkipLine); |
---|
158 | } |
---|
159 | pcs->flags |= BIT(FLAGS_ANON_ONE_END); |
---|
160 | return new_anon_station(); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | set_pos(&here); |
---|
165 | } |
---|
166 | ptr = pcs->Prefix; |
---|
167 | } |
---|
168 | |
---|
169 | i = 0; |
---|
170 | name = NULL; |
---|
171 | do { |
---|
172 | fNew = fFalse; |
---|
173 | if (name == NULL) { |
---|
174 | /* Need a new name buffer */ |
---|
175 | name = osmalloc(name_len); |
---|
176 | } |
---|
177 | /* i==0 iff this is the first pass */ |
---|
178 | if (i) { |
---|
179 | i = 0; |
---|
180 | nextch(); |
---|
181 | } |
---|
182 | while (isNames(ch)) { |
---|
183 | if (i < pcs->Truncate) { |
---|
184 | /* truncate name */ |
---|
185 | name[i++] = (pcs->Case == LOWER ? tolower(ch) : |
---|
186 | (pcs->Case == OFF ? ch : toupper(ch))); |
---|
187 | if (i >= name_len) { |
---|
188 | name_len = name_len + name_len; |
---|
189 | name = osrealloc(name, name_len); |
---|
190 | } |
---|
191 | } |
---|
192 | nextch(); |
---|
193 | } |
---|
194 | if (isSep(ch)) { |
---|
195 | fImplicitPrefix = fFalse; |
---|
196 | get_pos(&fp_firstsep); |
---|
197 | } |
---|
198 | if (i == 0) { |
---|
199 | osfree(name); |
---|
200 | if (!f_optional) { |
---|
201 | if (isEol(ch)) { |
---|
202 | if (fSurvey) { |
---|
203 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting survey name*/89); |
---|
204 | } else { |
---|
205 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting station name*/28); |
---|
206 | } |
---|
207 | } else { |
---|
208 | /* TRANSLATORS: Here "station" is a survey station, not a train station. */ |
---|
209 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Character “%c” not allowed in station name (use *SET NAMES to set allowed characters)*/7, ch); |
---|
210 | } |
---|
211 | LONGJMP(file.jbSkipLine); |
---|
212 | } |
---|
213 | return (prefix *)NULL; |
---|
214 | } |
---|
215 | |
---|
216 | name[i++] = '\0'; |
---|
217 | |
---|
218 | back_ptr = ptr; |
---|
219 | ptr = ptr->down; |
---|
220 | if (ptr == NULL) { |
---|
221 | /* Special case first time around at each level */ |
---|
222 | name = osrealloc(name, i); |
---|
223 | ptr = osnew(prefix); |
---|
224 | ptr->ident = name; |
---|
225 | name = NULL; |
---|
226 | ptr->right = ptr->down = NULL; |
---|
227 | ptr->pos = NULL; |
---|
228 | ptr->shape = 0; |
---|
229 | ptr->stn = NULL; |
---|
230 | ptr->up = back_ptr; |
---|
231 | ptr->filename = file.filename; |
---|
232 | ptr->line = file.line; |
---|
233 | ptr->min_export = ptr->max_export = 0; |
---|
234 | ptr->sflags = BIT(SFLAGS_SURVEY); |
---|
235 | if (fSuspectTypo && !fImplicitPrefix) |
---|
236 | ptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
---|
237 | back_ptr->down = ptr; |
---|
238 | fNew = fTrue; |
---|
239 | } else { |
---|
240 | /* Use caching to speed up adding an increasing sequence to a |
---|
241 | * large survey */ |
---|
242 | static prefix *cached_survey = NULL, *cached_station = NULL; |
---|
243 | prefix *ptrPrev = NULL; |
---|
244 | int cmp = 1; /* result of strcmp ( -ve for <, 0 for =, +ve for > ) */ |
---|
245 | if (cached_survey == back_ptr) { |
---|
246 | cmp = strcmp(cached_station->ident, name); |
---|
247 | if (cmp <= 0) ptr = cached_station; |
---|
248 | } |
---|
249 | while (ptr && (cmp = strcmp(ptr->ident, name))<0) { |
---|
250 | ptrPrev = ptr; |
---|
251 | ptr = ptr->right; |
---|
252 | } |
---|
253 | if (cmp) { |
---|
254 | /* ie we got to one that was higher, or the end */ |
---|
255 | prefix *newptr; |
---|
256 | name = osrealloc(name, i); |
---|
257 | newptr = osnew(prefix); |
---|
258 | newptr->ident = name; |
---|
259 | name = NULL; |
---|
260 | if (ptrPrev == NULL) |
---|
261 | back_ptr->down = newptr; |
---|
262 | else |
---|
263 | ptrPrev->right = newptr; |
---|
264 | newptr->right = ptr; |
---|
265 | newptr->down = NULL; |
---|
266 | newptr->pos = NULL; |
---|
267 | newptr->shape = 0; |
---|
268 | newptr->stn = NULL; |
---|
269 | newptr->up = back_ptr; |
---|
270 | newptr->filename = file.filename; |
---|
271 | newptr->line = file.line; |
---|
272 | newptr->min_export = newptr->max_export = 0; |
---|
273 | newptr->sflags = BIT(SFLAGS_SURVEY); |
---|
274 | if (fSuspectTypo && !fImplicitPrefix) |
---|
275 | newptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
---|
276 | ptr = newptr; |
---|
277 | fNew = fTrue; |
---|
278 | } |
---|
279 | cached_survey = back_ptr; |
---|
280 | cached_station = ptr; |
---|
281 | } |
---|
282 | depth++; |
---|
283 | f_optional = fFalse; /* disallow after first level */ |
---|
284 | if (isSep(ch)) get_pos(&fp_firstsep); |
---|
285 | } while (isSep(ch)); |
---|
286 | if (name) osfree(name); |
---|
287 | |
---|
288 | /* don't warn about a station that is referred to twice */ |
---|
289 | if (!fNew) ptr->sflags &= ~BIT(SFLAGS_SUSPECTTYPO); |
---|
290 | |
---|
291 | if (fNew) { |
---|
292 | /* fNew means SFLAGS_SURVEY is currently set */ |
---|
293 | SVX_ASSERT(TSTBIT(ptr->sflags, SFLAGS_SURVEY)); |
---|
294 | if (!fSurvey) { |
---|
295 | ptr->sflags &= ~BIT(SFLAGS_SURVEY); |
---|
296 | if (TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
---|
297 | } |
---|
298 | } else { |
---|
299 | /* check that the same name isn't being used for a survey and station */ |
---|
300 | if (fSurvey ^ TSTBIT(ptr->sflags, SFLAGS_SURVEY)) { |
---|
301 | /* TRANSLATORS: Here "station" is a survey station, not a train station. |
---|
302 | * |
---|
303 | * Here "survey" is a "cave map" rather than list of questions - it should be |
---|
304 | * translated to the terminology that cavers using the language would use. |
---|
305 | */ |
---|
306 | compile_diagnostic(DIAG_ERR, /*“%s” can’t be both a station and a survey*/27, |
---|
307 | sprint_prefix(ptr)); |
---|
308 | } |
---|
309 | if (!fSurvey && TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
---|
310 | } |
---|
311 | |
---|
312 | /* check the export level */ |
---|
313 | #if 0 |
---|
314 | printf("R min %d max %d depth %d pfx %s\n", |
---|
315 | ptr->min_export, ptr->max_export, depth, sprint_prefix(ptr)); |
---|
316 | #endif |
---|
317 | if (ptr->min_export == 0 || ptr->min_export == USHRT_MAX) { |
---|
318 | if (depth > ptr->max_export) ptr->max_export = depth; |
---|
319 | } else if (ptr->max_export < depth) { |
---|
320 | prefix *survey = ptr; |
---|
321 | char *s; |
---|
322 | const char *p; |
---|
323 | int level; |
---|
324 | for (level = ptr->max_export + 1; level; level--) { |
---|
325 | survey = survey->up; |
---|
326 | SVX_ASSERT(survey); |
---|
327 | } |
---|
328 | s = osstrdup(sprint_prefix(survey)); |
---|
329 | p = sprint_prefix(ptr); |
---|
330 | if (survey->filename) { |
---|
331 | compile_diagnostic_pfx(DIAG_ERR, survey, |
---|
332 | /*Station “%s” not exported from survey “%s”*/26, |
---|
333 | p, s); |
---|
334 | } else { |
---|
335 | compile_diagnostic(DIAG_ERR, /*Station “%s” not exported from survey “%s”*/26, p, s); |
---|
336 | } |
---|
337 | osfree(s); |
---|
338 | #if 0 |
---|
339 | printf(" *** pfx %s warning not exported enough depth %d " |
---|
340 | "ptr->max_export %d\n", sprint_prefix(ptr), |
---|
341 | depth, ptr->max_export); |
---|
342 | #endif |
---|
343 | } |
---|
344 | if (!fImplicitPrefix && (pfx_flags & PFX_WARN_SEPARATOR)) { |
---|
345 | filepos fp_tmp; |
---|
346 | get_pos(&fp_tmp); |
---|
347 | set_pos(&fp_firstsep); |
---|
348 | compile_diagnostic(DIAG_WARN|DIAG_COL, /*Separator in survey name*/392); |
---|
349 | set_pos(&fp_tmp); |
---|
350 | } |
---|
351 | return ptr; |
---|
352 | } |
---|
353 | |
---|
354 | /* if numeric expr is omitted: if f_optional return HUGE_REAL, else longjmp */ |
---|
355 | static real |
---|
356 | read_number(bool f_optional) |
---|
357 | { |
---|
358 | bool fPositive, fDigits = fFalse; |
---|
359 | real n = (real)0.0; |
---|
360 | filepos fp; |
---|
361 | int ch_old; |
---|
362 | |
---|
363 | get_pos(&fp); |
---|
364 | ch_old = ch; |
---|
365 | fPositive = !isMinus(ch); |
---|
366 | if (isSign(ch)) nextch(); |
---|
367 | |
---|
368 | while (isdigit(ch)) { |
---|
369 | n = n * (real)10.0 + (char)(ch - '0'); |
---|
370 | nextch(); |
---|
371 | fDigits = fTrue; |
---|
372 | } |
---|
373 | |
---|
374 | if (isDecimal(ch)) { |
---|
375 | real mult = (real)1.0; |
---|
376 | nextch(); |
---|
377 | while (isdigit(ch)) { |
---|
378 | mult *= (real).1; |
---|
379 | n += (char)(ch - '0') * mult; |
---|
380 | fDigits = fTrue; |
---|
381 | nextch(); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | /* !'fRead' => !fDigits so fDigits => 'fRead' */ |
---|
386 | if (fDigits) return (fPositive ? n : -n); |
---|
387 | |
---|
388 | /* didn't read a valid number. If it's optional, reset filepos & return */ |
---|
389 | set_pos(&fp); |
---|
390 | if (f_optional) { |
---|
391 | return HUGE_REAL; |
---|
392 | } |
---|
393 | |
---|
394 | if (isOmit(ch_old)) { |
---|
395 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Field may not be omitted*/8); |
---|
396 | } else { |
---|
397 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
398 | } |
---|
399 | LONGJMP(file.jbSkipLine); |
---|
400 | return 0.0; /* for brain-fried compilers */ |
---|
401 | } |
---|
402 | |
---|
403 | extern real |
---|
404 | read_quadrant(bool f_optional) |
---|
405 | { |
---|
406 | const int quad = 90; |
---|
407 | filepos fp; |
---|
408 | get_pos(&fp); |
---|
409 | int ch_old; |
---|
410 | ch_old = ch; |
---|
411 | real v = 0; |
---|
412 | /* Handle case where bearings are in Quadrants.*/ |
---|
413 | switch (ch) { |
---|
414 | case 'e': case 'E': v = quad * 1; nextch(); break; |
---|
415 | case 'w': case 'W': v = quad * 3; nextch(); break; |
---|
416 | case 's': case 'S': v = quad * 2; nextch(); break; |
---|
417 | case 'n': case 'N': v = 0; nextch(); break; |
---|
418 | default: |
---|
419 | /*TODO better error */ |
---|
420 | if (f_optional) { |
---|
421 | return HUGE_REAL; |
---|
422 | } |
---|
423 | if (isOmit(ch_old)) { |
---|
424 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Field may not be omitted*/8); |
---|
425 | } else { |
---|
426 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/590); |
---|
427 | } |
---|
428 | LONGJMP(file.jbSkipLine); |
---|
429 | return 0.0; /* for brain-fried compilers */ |
---|
430 | } |
---|
431 | real r = read_number(fTrue); |
---|
432 | if (r != HUGE_REAL && r <= quad) { |
---|
433 | if (ch == 'e' || ch == 'E') { |
---|
434 | if ( v == quad * 2) /* south half */ |
---|
435 | v = v - r; |
---|
436 | else if (!v) |
---|
437 | v = r; |
---|
438 | } else if (ch == 'w' || ch == 'W') { |
---|
439 | if ( v == quad * 2) |
---|
440 | v = v + r; |
---|
441 | else if (!v) |
---|
442 | v = quad * 4 - r; |
---|
443 | } else { |
---|
444 | /*TODO better error */ |
---|
445 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/590); |
---|
446 | LONGJMP(file.jbSkipLine); |
---|
447 | return 0.0; /* for brain-fried compilers */ |
---|
448 | } |
---|
449 | nextch(); |
---|
450 | return v; |
---|
451 | } else if ( r == HUGE_REAL ) { |
---|
452 | return v; |
---|
453 | } else { |
---|
454 | /* TODO r > quad; suspcious */ |
---|
455 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/590); |
---|
456 | LONGJMP(file.jbSkipLine); |
---|
457 | return 0.0; /* for brain-fried compilers */ |
---|
458 | } |
---|
459 | /* didn't read a valid quadrant. If it's optional, reset filepos & return */ |
---|
460 | set_pos(&fp); |
---|
461 | if (f_optional) { |
---|
462 | return HUGE_REAL; |
---|
463 | } |
---|
464 | if (isOmit(ch_old)) { |
---|
465 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Field may not be omitted*/8); |
---|
466 | } else { |
---|
467 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/590); |
---|
468 | } |
---|
469 | LONGJMP(file.jbSkipLine); |
---|
470 | return 0.0; /* for brain-fried compilers */ |
---|
471 | |
---|
472 | } |
---|
473 | |
---|
474 | extern real |
---|
475 | read_numeric(bool f_optional) |
---|
476 | { |
---|
477 | skipblanks(); |
---|
478 | return read_number(f_optional); |
---|
479 | } |
---|
480 | |
---|
481 | extern real |
---|
482 | read_numeric_multi(bool f_optional, bool f_quadrants, int *p_n_readings) |
---|
483 | { |
---|
484 | size_t n_readings = 0; |
---|
485 | real tot = (real)0.0; |
---|
486 | |
---|
487 | skipblanks(); |
---|
488 | if (!isOpen(ch)) { |
---|
489 | real r = 0; |
---|
490 | if (!f_quadrants) |
---|
491 | r = read_number(f_optional); |
---|
492 | else |
---|
493 | r = read_quadrant(f_optional); |
---|
494 | if (p_n_readings) *p_n_readings = (r == HUGE_REAL ? 0 : 1); |
---|
495 | return r; |
---|
496 | } |
---|
497 | nextch(); |
---|
498 | |
---|
499 | skipblanks(); |
---|
500 | do { |
---|
501 | if (! f_quadrants) |
---|
502 | tot += read_number(fFalse); |
---|
503 | else |
---|
504 | tot += read_quadrant(fFalse); |
---|
505 | ++n_readings; |
---|
506 | skipblanks(); |
---|
507 | } while (!isClose(ch)); |
---|
508 | nextch(); |
---|
509 | |
---|
510 | if (p_n_readings) *p_n_readings = n_readings; |
---|
511 | /* FIXME: special averaging for bearings ... */ |
---|
512 | /* And for percentage gradient */ |
---|
513 | return tot / n_readings; |
---|
514 | } |
---|
515 | |
---|
516 | /* read numeric expr or omit (return HUGE_REAL); else longjmp */ |
---|
517 | extern real |
---|
518 | read_bearing_multi_or_omit(bool f_quadrants, int *p_n_readings) |
---|
519 | { |
---|
520 | real v; |
---|
521 | v = read_numeric_multi(fTrue, f_quadrants, p_n_readings); |
---|
522 | if (v == HUGE_REAL) { |
---|
523 | if (!isOmit(ch)) { |
---|
524 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
525 | LONGJMP(file.jbSkipLine); |
---|
526 | return 0.0; /* for brain-fried compilers */ |
---|
527 | } |
---|
528 | nextch(); |
---|
529 | } |
---|
530 | return v; |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | /* Don't skip blanks, variable error code */ |
---|
535 | static unsigned int |
---|
536 | read_uint_internal(int errmsg, const filepos *fp) |
---|
537 | { |
---|
538 | unsigned int n = 0; |
---|
539 | if (!isdigit(ch)) { |
---|
540 | if (fp) set_pos(fp); |
---|
541 | compile_diagnostic_token_show(DIAG_ERR, errmsg); |
---|
542 | LONGJMP(file.jbSkipLine); |
---|
543 | } |
---|
544 | while (isdigit(ch)) { |
---|
545 | n = n * 10 + (char)(ch - '0'); |
---|
546 | nextch(); |
---|
547 | } |
---|
548 | return n; |
---|
549 | } |
---|
550 | |
---|
551 | extern unsigned int |
---|
552 | read_uint(void) |
---|
553 | { |
---|
554 | skipblanks(); |
---|
555 | return read_uint_internal(/*Expecting numeric field, found “%s”*/9, NULL); |
---|
556 | } |
---|
557 | |
---|
558 | extern void |
---|
559 | read_string(char **pstr, int *plen) |
---|
560 | { |
---|
561 | s_zero(pstr); |
---|
562 | |
---|
563 | skipblanks(); |
---|
564 | if (ch == '\"') { |
---|
565 | /* String quoted in "" */ |
---|
566 | nextch(); |
---|
567 | while (1) { |
---|
568 | if (isEol(ch)) { |
---|
569 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Missing \"*/69); |
---|
570 | LONGJMP(file.jbSkipLine); |
---|
571 | } |
---|
572 | |
---|
573 | if (ch == '\"') break; |
---|
574 | |
---|
575 | s_catchar(pstr, plen, ch); |
---|
576 | nextch(); |
---|
577 | } |
---|
578 | if (!*pstr) { |
---|
579 | /* Return empty string for "", not NULL. */ |
---|
580 | s_catchar(pstr, plen, '\0'); |
---|
581 | } |
---|
582 | nextch(); |
---|
583 | } else { |
---|
584 | /* Unquoted string */ |
---|
585 | while (1) { |
---|
586 | if (isEol(ch) || isComm(ch)) { |
---|
587 | if (!*pstr || !(*pstr)[0]) { |
---|
588 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting string field*/121); |
---|
589 | LONGJMP(file.jbSkipLine); |
---|
590 | } |
---|
591 | return; |
---|
592 | } |
---|
593 | |
---|
594 | if (isBlank(ch)) break; |
---|
595 | |
---|
596 | s_catchar(pstr, plen, ch); |
---|
597 | nextch(); |
---|
598 | } |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | extern void |
---|
603 | read_date(int *py, int *pm, int *pd) |
---|
604 | { |
---|
605 | unsigned int y = 0, m = 0, d = 0; |
---|
606 | filepos fp_date; |
---|
607 | |
---|
608 | skipblanks(); |
---|
609 | |
---|
610 | get_pos(&fp_date); |
---|
611 | y = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
612 | /* Two digit year is 19xx. */ |
---|
613 | if (y < 100) { |
---|
614 | filepos fp_save; |
---|
615 | get_pos(&fp_save); |
---|
616 | y += 1900; |
---|
617 | set_pos(&fp_date); |
---|
618 | /* TRANSLATORS: %d will be replaced by the assumed year, e.g. 1918 */ |
---|
619 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Assuming 2 digit year is %d*/76, y); |
---|
620 | set_pos(&fp_save); |
---|
621 | } |
---|
622 | if (y < 1900 || y > 2078) { |
---|
623 | set_pos(&fp_date); |
---|
624 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid year (< 1900 or > 2078)*/58); |
---|
625 | LONGJMP(file.jbSkipLine); |
---|
626 | return; /* for brain-fried compilers */ |
---|
627 | } |
---|
628 | if (ch == '.') { |
---|
629 | filepos fp; |
---|
630 | nextch(); |
---|
631 | get_pos(&fp); |
---|
632 | m = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
633 | if (m < 1 || m > 12) { |
---|
634 | set_pos(&fp); |
---|
635 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid month*/86); |
---|
636 | LONGJMP(file.jbSkipLine); |
---|
637 | return; /* for brain-fried compilers */ |
---|
638 | } |
---|
639 | if (ch == '.') { |
---|
640 | nextch(); |
---|
641 | get_pos(&fp); |
---|
642 | d = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
643 | if (d < 1 || d > last_day(y, m)) { |
---|
644 | set_pos(&fp); |
---|
645 | /* TRANSLATORS: e.g. 31st of April, or 32nd of any month */ |
---|
646 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid day of the month*/87); |
---|
647 | LONGJMP(file.jbSkipLine); |
---|
648 | return; /* for brain-fried compilers */ |
---|
649 | } |
---|
650 | } |
---|
651 | } |
---|
652 | if (py) *py = y; |
---|
653 | if (pm) *pm = m; |
---|
654 | if (pd) *pd = d; |
---|
655 | } |
---|