Cache PJ* for *fix
[survex.git] / src / datain.c
blob1d731c9e67ef086344093adead4303317f480d39
1 /* datain.c
2 * Reads in survey files, dealing with special characters, keywords & data
3 * Copyright (C) 1991-2003,2005,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 Olly Betts
4 * Copyright (C) 2004 Simeon Warner
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <limits.h>
26 #include <stdarg.h>
28 #include "debug.h"
29 #include "cavern.h"
30 #include "date.h"
31 #include "filename.h"
32 #include "message.h"
33 #include "filelist.h"
34 #include "netbits.h"
35 #include "netskel.h"
36 #include "readval.h"
37 #include "datain.h"
38 #include "commands.h"
39 #include "out.h"
40 #include "str.h"
41 #include "thgeomag.h"
43 #define EPSILON (REAL_EPSILON * 1000)
45 #define var(I) (pcs->Var[(I)])
47 /* true if x is not-a-number value in Compass (999.0 or -999.0) */
48 /* Compass uses 999.0 but understands Karst data which used -999.0 */
49 #define is_compass_NaN(x) ( fabs(fabs(x)-999.0) < EPSILON )
51 int ch;
53 typedef enum {
54 CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_INFERPLUMB, CTYPE_HORIZ
55 } clino_type;
57 /* Don't explicitly initialise as we can't set the jmp_buf - this has
58 * static scope so will be initialised like this anyway */
59 parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
61 bool f_export_ok;
63 static real value[Fr - 1];
64 #define VAL(N) value[(N)-1]
65 static real variance[Fr - 1];
66 #define VAR(N) variance[(N)-1]
67 static long location[Fr - 1];
68 #define LOC(N) location[(N)-1]
69 static int location_width[Fr - 1];
70 #define WID(N) location_width[(N)-1]
72 /* style functions */
73 static void data_normal(void);
74 static void data_cartesian(void);
75 static void data_passage(void);
76 static void data_nosurvey(void);
77 static void data_ignore(void);
79 void
80 get_pos(filepos *fp)
82 fp->ch = ch;
83 fp->offset = ftell(file.fh);
84 if (fp->offset == -1)
85 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
88 void
89 set_pos(const filepos *fp)
91 ch = fp->ch;
92 if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
93 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
96 static void
97 report_parent(parse * p) {
98 if (p->parent)
99 report_parent(p->parent);
100 /* Force re-report of include tree for further errors in
101 * parent files */
102 p->reported_where = fFalse;
103 /* TRANSLATORS: %s is replaced by the filename of the parent file, and %u
104 * by the line number in that file. Your translation should also contain
105 * %s:%u so that automatic parsing of error messages to determine the file
106 * and line number still works. */
107 fprintf(STDERR, msg(/*In file included from %s:%u:\n*/5), p->filename, p->line);
110 static void
111 error_list_parent_files(void)
113 if (!file.reported_where && file.parent) {
114 report_parent(file.parent);
115 /* Suppress reporting of full include tree for further errors
116 * in this file */
117 file.reported_where = fTrue;
121 static void
122 show_line(int col, int width)
124 /* Rewind to beginning of line. */
125 long cur_pos = ftell(file.fh);
126 int tabs = 0;
127 if (cur_pos < 0 || fseek(file.fh, file.lpos, SEEK_SET) == -1)
128 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
130 /* Read the whole line and write it out. */
131 PUTC(' ', STDERR);
132 while (1) {
133 int c = GETC(file.fh);
134 /* Note: isEol() is true for EOF */
135 if (isEol(c)) break;
136 if (c == '\t') ++tabs;
137 PUTC(c, STDERR);
139 fputnl(STDERR);
141 /* If we have a location in the line for the error, indicate it. */
142 if (col) {
143 PUTC(' ', STDERR);
144 if (tabs == 0) {
145 while (--col) PUTC(' ', STDERR);
146 } else {
147 /* Copy tabs from line, replacing other characters with spaces - this
148 * means that the caret should line up correctly. */
149 if (fseek(file.fh, file.lpos, SEEK_SET) == -1)
150 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
151 while (--col) {
152 int c = GETC(file.fh);
153 if (c != '\t') c = ' ';
154 PUTC(c, STDERR);
157 PUTC('^', STDERR);
158 while (width > 1) {
159 PUTC('~', STDERR);
160 --width;
162 fputnl(STDERR);
165 /* Revert to where we were. */
166 if (fseek(file.fh, cur_pos, SEEK_SET) == -1)
167 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
170 static int caret_width = 0;
172 static void
173 compile_v_report_fpos(int severity, long fpos, int en, va_list ap)
175 int col = 0;
176 error_list_parent_files();
177 if (fpos >= file.lpos)
178 col = fpos - file.lpos - caret_width;
179 v_report(severity, file.filename, file.line, col, en, ap);
180 if (file.fh) show_line(col, caret_width);
183 static void
184 compile_v_report(int diag_flags, int en, va_list ap)
186 int severity = (diag_flags & DIAG_SEVERITY_MASK);
187 if (diag_flags & (DIAG_COL|DIAG_BUF)) {
188 if (file.fh) {
189 if (diag_flags & DIAG_BUF) caret_width = strlen(buffer);
190 compile_v_report_fpos(severity, ftell(file.fh), en, ap);
191 if (diag_flags & DIAG_BUF) caret_width = 0;
192 if (diag_flags & DIAG_SKIP) skipline();
193 return;
196 error_list_parent_files();
197 v_report(severity, file.filename, file.line, 0, en, ap);
198 if (file.fh) {
199 if (diag_flags & DIAG_BUF) {
200 show_line(0, strlen(buffer));
201 } else {
202 show_line(0, caret_width);
205 if (diag_flags & DIAG_SKIP) skipline();
208 void
209 compile_diagnostic(int diag_flags, int en, ...)
211 va_list ap;
212 va_start(ap, en);
213 if (diag_flags & (DIAG_TOKEN|DIAG_UINT|DIAG_DATE|DIAG_NUM)) {
214 char *p = NULL;
215 int len = 0;
216 skipblanks();
217 if (diag_flags & DIAG_TOKEN) {
218 while (!isBlank(ch) && !isEol(ch)) {
219 s_catchar(&p, &len, (char)ch);
220 nextch();
222 } else if (diag_flags & DIAG_UINT) {
223 while (isdigit(ch)) {
224 s_catchar(&p, &len, (char)ch);
225 nextch();
227 } else if (diag_flags & DIAG_DATE) {
228 while (isdigit(ch) || ch == '.') {
229 s_catchar(&p, &len, (char)ch);
230 nextch();
232 } else {
233 if (isMinus(ch) || isPlus(ch)) {
234 s_catchar(&p, &len, (char)ch);
235 nextch();
237 while (isdigit(ch)) {
238 s_catchar(&p, &len, (char)ch);
239 nextch();
241 if (isDecimal(ch)) {
242 s_catchar(&p, &len, (char)ch);
243 nextch();
245 while (isdigit(ch)) {
246 s_catchar(&p, &len, (char)ch);
247 nextch();
250 if (p) {
251 caret_width = strlen(p);
252 osfree(p);
254 compile_v_report(diag_flags|DIAG_COL, en, ap);
255 caret_width = 0;
256 } else {
257 compile_v_report(diag_flags, en, ap);
259 va_end(ap);
262 static void
263 compile_diagnostic_reading(int diag_flags, reading r, int en, ...)
265 va_list ap;
266 int severity = (diag_flags & DIAG_SEVERITY_MASK);
267 va_start(ap, en);
268 caret_width = WID(r);
269 compile_v_report_fpos(severity, LOC(r) + caret_width, en, ap);
270 caret_width = 0;
271 va_end(ap);
274 static void
275 compile_error_reading_skip(reading r, int en, ...)
277 va_list ap;
278 va_start(ap, en);
279 caret_width = WID(r);
280 compile_v_report_fpos(1, LOC(r) + caret_width, en, ap);
281 caret_width = 0;
282 va_end(ap);
283 skipline();
286 void
287 compile_diagnostic_at(int diag_flags, const char * filename, unsigned line, int en, ...)
289 va_list ap;
290 int severity = (diag_flags & DIAG_SEVERITY_MASK);
291 va_start(ap, en);
292 v_report(severity, filename, line, 0, en, ap);
293 va_end(ap);
296 void
297 compile_diagnostic_pfx(int diag_flags, const prefix * pfx, int en, ...)
299 va_list ap;
300 int severity = (diag_flags & DIAG_SEVERITY_MASK);
301 va_start(ap, en);
302 v_report(severity, pfx->filename, pfx->line, 0, en, ap);
303 va_end(ap);
306 void
307 compile_diagnostic_token_show(int diag_flags, int en)
309 char *p = NULL;
310 int len = 0;
311 skipblanks();
312 while (!isBlank(ch) && !isEol(ch)) {
313 s_catchar(&p, &len, (char)ch);
314 nextch();
316 if (p) {
317 caret_width = strlen(p);
318 compile_diagnostic(diag_flags|DIAG_COL, en, p);
319 caret_width = 0;
320 osfree(p);
321 } else {
322 compile_diagnostic(DIAG_ERR|DIAG_COL, en, "");
326 static void
327 compile_error_string(const char * s, int en, ...)
329 va_list ap;
330 va_start(ap, en);
331 caret_width = strlen(s);
332 compile_v_report(DIAG_ERR|DIAG_COL, en, ap);
333 va_end(ap);
334 caret_width = 0;
337 /* This function makes a note where to put output files */
338 static void
339 using_data_file(const char *fnm)
341 if (!fnm_output_base) {
342 /* was: fnm_output_base = base_from_fnm(fnm); */
343 fnm_output_base = baseleaf_from_fnm(fnm);
344 } else if (fnm_output_base_is_dir) {
345 /* --output pointed to directory so use the leaf basename in that dir */
346 char *lf, *p;
347 lf = baseleaf_from_fnm(fnm);
348 p = use_path(fnm_output_base, lf);
349 osfree(lf);
350 osfree(fnm_output_base);
351 fnm_output_base = p;
352 fnm_output_base_is_dir = 0;
356 static void
357 skipword(void)
359 while (!isBlank(ch) && !isEol(ch)) nextch();
362 extern void
363 skipblanks(void)
365 while (isBlank(ch)) nextch();
368 extern void
369 skipline(void)
371 while (!isEol(ch)) nextch();
374 static void
375 process_eol(void)
377 int eolchar;
379 skipblanks();
381 if (!isEol(ch)) {
382 if (!isComm(ch))
383 compile_diagnostic(DIAG_ERR|DIAG_COL, /*End of line not blank*/15);
384 skipline();
387 eolchar = ch;
388 file.line++;
389 /* skip any different eol characters so we get line counts correct on
390 * DOS text files and similar, but don't count several adjacent blank
391 * lines as one */
392 while (ch != EOF) {
393 nextch();
394 if (ch == eolchar || !isEol(ch)) {
395 break;
397 if (ch == '\n') eolchar = ch;
399 file.lpos = ftell(file.fh) - 1;
402 static bool
403 process_non_data_line(void)
405 skipblanks();
407 if (isData(ch)) return fFalse;
409 if (isKeywd(ch)) {
410 nextch();
411 handle_command();
414 process_eol();
416 return fTrue;
419 static void
420 read_reading(reading r, bool f_optional)
422 int n_readings;
423 q_quantity q;
424 switch (r) {
425 case Tape: q = Q_LENGTH; break;
426 case BackTape: q = Q_BACKLENGTH; break;
427 case Comp: q = Q_BEARING; break;
428 case BackComp: q = Q_BACKBEARING; break;
429 case Clino: q = Q_GRADIENT; break;
430 case BackClino: q = Q_BACKGRADIENT; break;
431 case FrDepth: case ToDepth: q = Q_DEPTH; break;
432 case Dx: q = Q_DX; break;
433 case Dy: q = Q_DY; break;
434 case Dz: q = Q_DZ; break;
435 case FrCount: case ToCount: q = Q_COUNT; break;
436 case Left: q = Q_LEFT; break;
437 case Right: q = Q_RIGHT; break;
438 case Up: q = Q_UP; break;
439 case Down: q = Q_DOWN; break;
440 default:
441 q = Q_NULL; /* Suppress compiler warning */;
442 BUG("Unexpected case");
444 LOC(r) = ftell(file.fh);
445 /* since we don't handle bearings in read_readings, it's never quadrant */
446 VAL(r) = read_numeric_multi(f_optional, fFalse, &n_readings);
447 WID(r) = ftell(file.fh) - LOC(r);
448 VAR(r) = var(q);
449 if (n_readings > 1) VAR(r) /= sqrt(n_readings);
452 static void
453 read_bearing_or_omit(reading r)
455 int n_readings;
456 bool quadrants = fFalse;
457 q_quantity q = Q_NULL;
458 switch (r) {
459 case Comp:
460 q = Q_BEARING;
461 if (pcs->f_bearing_quadrants)
462 quadrants = fTrue;
463 break;
464 case BackComp:
465 q = Q_BACKBEARING;
466 if (pcs->f_backbearing_quadrants)
467 quadrants = fTrue;
468 break;
469 default:
470 q = Q_NULL; /* Suppress compiler warning */;
471 BUG("Unexpected case");
473 LOC(r) = ftell(file.fh);
474 VAL(r) = read_bearing_multi_or_omit(quadrants, &n_readings);
475 WID(r) = ftell(file.fh) - LOC(r);
476 VAR(r) = var(q);
477 if (n_readings > 1) VAR(r) /= sqrt(n_readings);
480 /* For reading Compass MAK files which have a freeform syntax */
481 static void
482 nextch_handling_eol(void)
484 nextch();
485 while (ch != EOF && isEol(ch)) {
486 process_eol();
490 #define LITLEN(S) (sizeof(S"") - 1)
491 #define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
492 (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
493 strcasecmp((F) + (L) - LITLEN(E), E) == 0)
494 extern void
495 data_file(const char *pth, const char *fnm)
497 int begin_lineno_store;
498 parse file_store;
499 volatile enum {FMT_SVX, FMT_DAT, FMT_MAK} fmt = FMT_SVX;
502 char *filename;
503 FILE *fh;
504 size_t len;
506 if (!pth) {
507 /* file specified on command line - don't do special translation */
508 fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
509 } else {
510 fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
513 if (fh == NULL) {
514 compile_error_string(fnm, /*Couldnā€™t open file ā€œ%sā€*/24, fnm);
515 return;
518 len = strlen(filename);
519 if (has_ext(filename, len, "dat")) {
520 fmt = FMT_DAT;
521 } else if (has_ext(filename, len, "mak")) {
522 fmt = FMT_MAK;
525 file_store = file;
526 if (file.fh) file.parent = &file_store;
527 file.fh = fh;
528 file.filename = filename;
529 file.line = 1;
530 file.lpos = 0;
531 file.reported_where = fFalse;
532 nextch();
533 if (fmt == FMT_SVX && ch == 0xef) {
534 /* Maybe a UTF-8 "BOM" - skip if so. */
535 if (nextch() == 0xbb && nextch() == 0xbf) {
536 nextch();
537 file.lpos = 3;
538 } else {
539 rewind(fh);
540 ch = 0xef;
545 using_data_file(file.filename);
547 begin_lineno_store = pcs->begin_lineno;
548 pcs->begin_lineno = 0;
550 if (fmt == FMT_DAT) {
551 short *t;
552 int i;
553 settings *pcsNew;
555 pcsNew = osnew(settings);
556 *pcsNew = *pcs; /* copy contents */
557 pcsNew->begin_lineno = 0;
558 pcsNew->next = pcs;
559 pcs = pcsNew;
560 default_units(pcs);
561 default_calib(pcs);
563 pcs->style = STYLE_NORMAL;
564 pcs->units[Q_LENGTH] = METRES_PER_FOOT;
565 t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
567 t[EOF] = SPECIAL_EOL;
568 memset(t, 0, sizeof(short) * 33);
569 for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
570 t[127] = 0;
571 for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
572 t['\t'] |= SPECIAL_BLANK;
573 t[' '] |= SPECIAL_BLANK;
574 t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
575 t['\n'] |= SPECIAL_EOL;
576 t['\r'] |= SPECIAL_EOL;
577 t['.'] |= SPECIAL_DECIMAL;
578 t['-'] |= SPECIAL_MINUS;
579 t['+'] |= SPECIAL_PLUS;
580 pcs->Translate = t;
581 pcs->Case = OFF;
582 pcs->Truncate = INT_MAX;
583 pcs->infer = BIT(INFER_EQUATES)|BIT(INFER_EXPORTS)|BIT(INFER_PLUMBS);
584 } else if (fmt == FMT_MAK) {
585 short *t;
586 int i;
587 settings *pcsNew;
589 pcsNew = osnew(settings);
590 *pcsNew = *pcs; /* copy contents */
591 pcsNew->begin_lineno = 0;
592 pcsNew->next = pcs;
593 pcs = pcsNew;
595 t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
597 t[EOF] = SPECIAL_EOL;
598 memset(t, 0, sizeof(short) * 33);
599 for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
600 t[127] = 0;
601 for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
602 t['['] = t[','] = t[';'] = 0;
603 t['\t'] |= SPECIAL_BLANK;
604 t[' '] |= SPECIAL_BLANK;
605 t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
606 t['\n'] |= SPECIAL_EOL;
607 t['\r'] |= SPECIAL_EOL;
608 t['.'] |= SPECIAL_DECIMAL;
609 t['-'] |= SPECIAL_MINUS;
610 t['+'] |= SPECIAL_PLUS;
611 pcs->Translate = t;
612 pcs->Case = OFF;
613 pcs->Truncate = INT_MAX;
616 #ifdef HAVE_SETJMP_H
617 /* errors in nested functions can longjmp here */
618 if (setjmp(file.jbSkipLine)) {
619 skipline();
620 process_eol();
622 #endif
624 if (fmt == FMT_DAT) {
625 while (ch != EOF && !ferror(file.fh)) {
626 static const reading compass_order[] = {
627 Fr, To, Tape, CompassDATComp, CompassDATClino,
628 CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
629 CompassDATFlags, IgnoreAll
631 static const reading compass_order_backsights[] = {
632 Fr, To, Tape, CompassDATComp, CompassDATClino,
633 CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
634 CompassDATBackComp, CompassDATBackClino,
635 CompassDATFlags, IgnoreAll
637 /* <Cave name> */
638 skipline();
639 process_eol();
640 /* SURVEY NAME: <Short name> */
641 get_token();
642 get_token();
643 /* if (ch != ':') ... */
644 nextch();
645 get_token();
646 skipline();
647 process_eol();
648 /* SURVEY DATE: 7 10 79 COMMENT:<Long name> */
649 get_token();
650 get_token();
651 copy_on_write_meta(pcs);
652 if (ch == ':') {
653 int year, month, day;
655 nextch();
657 /* NB order is *month* *day* year */
658 month = read_uint();
659 day = read_uint();
660 year = read_uint();
661 /* Note: Larry says a 2 digit year is always 19XX */
662 if (year < 100) year += 1900;
664 pcs->meta->days1 = pcs->meta->days2 = days_since_1900(year, month, day);
665 } else {
666 pcs->meta->days1 = pcs->meta->days2 = -1;
668 pcs->declination = HUGE_REAL;
669 skipline();
670 process_eol();
671 /* SURVEY TEAM: */
672 get_token();
673 get_token();
674 skipline();
675 process_eol();
676 /* <Survey team> */
677 skipline();
678 process_eol();
679 /* DECLINATION: 1.00 FORMAT: DDDDLUDRADLN CORRECTIONS: 2.00 3.00 4.00 */
680 get_token();
681 nextch(); /* : */
682 skipblanks();
683 pcs->z[Q_DECLINATION] = -read_numeric(fFalse);
684 pcs->z[Q_DECLINATION] *= pcs->units[Q_DECLINATION];
685 get_token();
686 pcs->ordering = compass_order;
687 if (strcmp(buffer, "FORMAT") == 0) {
688 nextch(); /* : */
689 get_token();
690 if (strlen(buffer) >= 12 && buffer[11] == 'B') {
691 /* We have backsights for compass and clino */
692 pcs->ordering = compass_order_backsights;
694 get_token();
696 if (strcmp(buffer, "CORRECTIONS") == 0) {
697 nextch(); /* : */
698 pcs->z[Q_BEARING] = -rad(read_numeric(fFalse));
699 pcs->z[Q_GRADIENT] = -rad(read_numeric(fFalse));
700 pcs->z[Q_LENGTH] = -read_numeric(fFalse);
701 } else {
702 pcs->z[Q_BEARING] = 0;
703 pcs->z[Q_GRADIENT] = 0;
704 pcs->z[Q_LENGTH] = 0;
706 skipline();
707 process_eol();
708 /* BLANK LINE */
709 skipline();
710 process_eol();
711 /* heading line */
712 skipline();
713 process_eol();
714 /* BLANK LINE */
715 skipline();
716 process_eol();
717 while (ch != EOF) {
718 if (ch == '\x0c') {
719 nextch();
720 process_eol();
721 break;
723 data_normal();
725 clear_last_leg();
727 pcs->ordering = NULL; /* Avoid free() of static array. */
728 pop_settings();
729 } else if (fmt == FMT_MAK) {
730 while (ch != EOF && !ferror(file.fh)) {
731 if (ch == '#') {
732 /* include a file */
733 int ch_store;
734 char *dat_pth = path_from_fnm(file.filename);
735 char *dat_fnm = NULL;
736 int dat_fnm_len;
737 nextch_handling_eol();
738 while (ch != ',' && ch != ';' && ch != EOF) {
739 while (isEol(ch)) process_eol();
740 s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
741 nextch_handling_eol();
743 while (ch != ';' && ch != EOF) {
744 prefix *name;
745 nextch_handling_eol();
746 name = read_prefix(PFX_STATION|PFX_OPT);
747 if (name) {
748 skipblanks();
749 if (ch == '[') {
750 /* fixed pt */
751 node *stn;
752 real x, y, z;
753 bool in_feet = fFalse;
754 name->sflags |= BIT(SFLAGS_FIXED);
755 nextch_handling_eol();
756 if (ch == 'F' || ch == 'f') {
757 in_feet = fTrue;
758 nextch_handling_eol();
759 } else if (ch == 'M' || ch == 'm') {
760 nextch_handling_eol();
761 } else {
762 compile_diagnostic(DIAG_ERR, /*Expecting ā€œFā€ or ā€œMā€*/103);
764 while (!isdigit(ch) && ch != '+' && ch != '-' &&
765 ch != '.' && ch != ']' && ch != EOF) {
766 nextch_handling_eol();
768 x = read_numeric(fFalse);
769 while (!isdigit(ch) && ch != '+' && ch != '-' &&
770 ch != '.' && ch != ']' && ch != EOF) {
771 nextch_handling_eol();
773 y = read_numeric(fFalse);
774 while (!isdigit(ch) && ch != '+' && ch != '-' &&
775 ch != '.' && ch != ']' && ch != EOF) {
776 nextch_handling_eol();
778 z = read_numeric(fFalse);
779 if (in_feet) {
780 x *= METRES_PER_FOOT;
781 y *= METRES_PER_FOOT;
782 z *= METRES_PER_FOOT;
784 stn = StnFromPfx(name);
785 if (!fixed(stn)) {
786 POS(stn, 0) = x;
787 POS(stn, 1) = y;
788 POS(stn, 2) = z;
789 fix(stn);
790 } else {
791 if (x != POS(stn, 0) || y != POS(stn, 1) ||
792 z != POS(stn, 2)) {
793 compile_diagnostic(DIAG_ERR, /*Station already fixed or equated to a fixed point*/46);
794 } else {
795 compile_diagnostic(DIAG_WARN, /*Station already fixed at the same coordinates*/55);
798 while (ch != ']' && ch != EOF) nextch_handling_eol();
799 if (ch == ']') {
800 nextch_handling_eol();
801 skipblanks();
803 } else {
804 /* FIXME: link station - ignore for now */
805 /* FIXME: perhaps issue warning? Other station names can be "reused", which is problematic... */
807 while (ch != ',' && ch != ';' && ch != EOF)
808 nextch_handling_eol();
811 if (dat_fnm) {
812 ch_store = ch;
813 data_file(dat_pth, dat_fnm);
814 ch = ch_store;
815 osfree(dat_fnm);
817 } else {
818 /* FIXME: also check for % and $ later */
819 nextch_handling_eol();
822 pop_settings();
823 } else {
824 while (ch != EOF && !ferror(file.fh)) {
825 if (!process_non_data_line()) {
826 f_export_ok = fFalse;
827 switch (pcs->style) {
828 case STYLE_NORMAL:
829 case STYLE_DIVING:
830 case STYLE_CYLPOLAR:
831 data_normal();
832 break;
833 case STYLE_CARTESIAN:
834 data_cartesian();
835 break;
836 case STYLE_PASSAGE:
837 data_passage();
838 break;
839 case STYLE_NOSURVEY:
840 data_nosurvey();
841 break;
842 case STYLE_IGNORE:
843 data_ignore();
844 break;
845 default:
846 BUG("bad style");
850 clear_last_leg();
853 /* don't allow *BEGIN at the end of a file, then *EXPORT in the
854 * including file */
855 f_export_ok = fFalse;
857 if (pcs->begin_lineno) {
858 error_in_file(file.filename, pcs->begin_lineno,
859 /*BEGIN with no matching END in this file*/23);
860 /* Implicitly close any unclosed BEGINs from this file */
861 do {
862 pop_settings();
863 } while (pcs->begin_lineno);
866 pcs->begin_lineno = begin_lineno_store;
868 if (ferror(file.fh))
869 fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
871 (void)fclose(file.fh);
873 file = file_store;
875 /* don't free this - it may be pointed to by prefix.file */
876 /* osfree(file.filename); */
879 static real
880 mod2pi(real a)
882 return a - floor(a / (2 * M_PI)) * (2 * M_PI);
885 static real
886 handle_plumb(clino_type *p_ctype)
888 typedef enum {
889 CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
890 } clino_tok;
891 static const sztok clino_tab[] = {
892 {"D", CLINO_DOWN},
893 {"DOWN", CLINO_DOWN},
894 {"H", CLINO_LEVEL},
895 {"LEVEL", CLINO_LEVEL},
896 {"U", CLINO_UP},
897 {"UP", CLINO_UP},
898 {NULL, CLINO_NULL}
900 static const real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
901 clino_tok tok;
903 skipblanks();
904 if (isalpha(ch)) {
905 filepos fp;
906 get_pos(&fp);
907 get_token();
908 tok = match_tok(clino_tab, TABSIZE(clino_tab));
909 if (tok != CLINO_NULL) {
910 *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
911 return clinos[tok];
913 set_pos(&fp);
914 } else if (isSign(ch)) {
915 int chOld = ch;
916 nextch();
917 if (toupper(ch) == 'V') {
918 nextch();
919 *p_ctype = CTYPE_PLUMB;
920 return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
923 if (isOmit(chOld)) {
924 *p_ctype = CTYPE_OMIT;
925 /* no clino reading, so assume 0 with large sd */
926 return (real)0.0;
928 } else if (isOmit(ch)) {
929 /* OMIT char may not be a SIGN char too so we need to check here as
930 * well as above... */
931 nextch();
932 *p_ctype = CTYPE_OMIT;
933 /* no clino reading, so assume 0 with large sd */
934 return (real)0.0;
936 return HUGE_REAL;
939 static void
940 warn_readings_differ(int msgno, real diff, int units)
942 char buf[64];
943 char *p;
944 diff /= get_units_factor(units);
945 sprintf(buf, "%.2f", fabs(diff));
946 for (p = buf; *p; ++p) {
947 if (*p == '.') {
948 char *z = p;
949 while (*++p) {
950 if (*p != '0') z = p + 1;
952 p = z;
953 break;
956 strcpy(p, get_units_string(units));
957 compile_diagnostic(DIAG_WARN, msgno, buf);
960 static bool
961 handle_comp_units(void)
963 bool fNoComp = fTrue;
964 if (VAL(Comp) != HUGE_REAL) {
965 fNoComp = fFalse;
966 VAL(Comp) *= pcs->units[Q_BEARING];
967 if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
968 /* TRANSLATORS: Suspicious means something like 410 degrees or -20
969 * degrees */
970 compile_diagnostic_reading(DIAG_WARN, Comp, /*Suspicious compass reading*/59);
971 VAL(Comp) = mod2pi(VAL(Comp));
974 if (VAL(BackComp) != HUGE_REAL) {
975 fNoComp = fFalse;
976 VAL(BackComp) *= pcs->units[Q_BACKBEARING];
977 if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
978 /* FIXME: different message for BackComp? */
979 compile_diagnostic_reading(DIAG_WARN, BackComp, /*Suspicious compass reading*/59);
980 VAL(BackComp) = mod2pi(VAL(BackComp));
983 return fNoComp;
986 static real
987 handle_compass(real *p_var)
989 real compvar = VAR(Comp);
990 real comp = VAL(Comp);
991 real backcomp = VAL(BackComp);
992 real declination;
993 if (pcs->z[Q_DECLINATION] != HUGE_REAL) {
994 declination = -pcs->z[Q_DECLINATION];
995 } else if (pcs->declination != HUGE_REAL) {
996 /* Cached value calculated for a previous compass reading taken on the
997 * same date (by the 'else' just below).
999 declination = pcs->declination;
1000 } else {
1001 if (!pcs->meta || pcs->meta->days1 == -1) {
1002 compile_diagnostic(DIAG_WARN, /*No survey date specified - using 0 for magnetic declination*/304);
1003 declination = 0;
1004 } else {
1005 int avg_days = (pcs->meta->days1 + pcs->meta->days2) / 2;
1006 double dat = julian_date_from_days_since_1900(avg_days);
1007 /* thgeomag() takes (lat, lon, h, dat) - i.e. (y, x, z, date). */
1008 declination = thgeomag(pcs->dec_lat, pcs->dec_lon, pcs->dec_alt, dat);
1010 declination -= pcs->convergence;
1011 /* We cache the calculated declination as the calculation is relatively
1012 * expensive. We also cache an "assumed 0" answer so that we only
1013 * warn once per such survey rather than for every line with a compass
1014 * reading. */
1015 pcs->declination = declination;
1017 if (comp != HUGE_REAL) {
1018 comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
1019 comp += declination;
1021 if (backcomp != HUGE_REAL) {
1022 backcomp = (backcomp - pcs->z[Q_BACKBEARING])
1023 * pcs->sc[Q_BACKBEARING];
1024 backcomp += declination;
1025 backcomp -= M_PI;
1026 if (comp != HUGE_REAL) {
1027 real diff = comp - backcomp;
1028 real adj = fabs(diff) > M_PI ? M_PI : 0;
1029 diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
1030 if (sqrd(diff / 3.0) > compvar + VAR(BackComp)) {
1031 /* fore and back readings differ by more than 3 sds */
1032 /* TRANSLATORS: %s is replaced by the amount the readings disagree
1033 * by, e.g. "2.5Ā°" or "3įµ". */
1034 warn_readings_differ(/*COMPASS reading and BACKCOMPASS reading disagree by %s*/98,
1035 diff, get_angle_units(Q_BEARING));
1037 comp = (comp / compvar + backcomp / VAR(BackComp));
1038 compvar = (compvar + VAR(BackComp)) / 4;
1039 comp *= compvar;
1040 comp += adj;
1041 } else {
1042 comp = backcomp;
1043 compvar = VAR(BackComp);
1046 *p_var = compvar;
1047 return comp;
1050 static real
1051 handle_clino(q_quantity q, reading r, real val, bool percent, clino_type *p_ctype)
1053 bool range_0_180;
1054 real z;
1055 real diff_from_abs90;
1056 val *= pcs->units[q];
1057 /* percentage scale */
1058 if (percent) val = atan(val);
1059 /* We want to warn if there's a reading which it would be impossible
1060 * to have read from the instrument (e.g. on a -90 to 90 degree scale
1061 * you can't read "96" (it's probably a typo for "69"). However, the
1062 * gradient reading from a topofil is typically in the range 0 to 180,
1063 * with 90 being horizontal.
1065 * Really we should allow the valid range to be specified, but for now
1066 * we infer it from the zero error - if this is within 45 degrees of
1067 * 90 then we assume the range is 0 to 180.
1069 z = pcs->z[q];
1070 range_0_180 = (z > M_PI_4 && z < 3*M_PI_4);
1071 diff_from_abs90 = fabs(val) - M_PI_2;
1072 if (diff_from_abs90 > EPSILON) {
1073 if (!range_0_180) {
1074 int clino_units = get_angle_units(q);
1075 const char * units = get_units_string(clino_units);
1076 real right_angle = M_PI_2 / get_units_factor(clino_units);
1077 /* FIXME: different message for BackClino? */
1078 /* TRANSLATORS: %.f%s will be replaced with a right angle in the
1079 * units currently in use, e.g. "90Ā°" or "100įµ". And "absolute
1080 * value" means the reading ignoring the sign (so it might be
1081 * < -90Ā° or > 90Ā°. */
1082 compile_diagnostic_reading(DIAG_WARN, r, /*Clino reading over %.f%s (absolute value)*/51,
1083 right_angle, units);
1085 } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
1086 diff_from_abs90 >= -EPSILON) {
1087 *p_ctype = CTYPE_INFERPLUMB;
1089 if (range_0_180 && *p_ctype != CTYPE_INFERPLUMB) {
1090 /* FIXME: Warning message not ideal... */
1091 if (val < 0.0 || val - M_PI > EPSILON) {
1092 int clino_units = get_angle_units(q);
1093 const char * units = get_units_string(clino_units);
1094 real right_angle = M_PI_2 / get_units_factor(clino_units);
1095 compile_diagnostic_reading(DIAG_WARN, r, /*Clino reading over %.f%s (absolute value)*/51,
1096 right_angle, units);
1099 return val;
1102 static int
1103 process_normal(prefix *fr, prefix *to, bool fToFirst,
1104 clino_type ctype, clino_type backctype)
1106 real tape = VAL(Tape);
1107 real clin = VAL(Clino);
1108 real backclin = VAL(BackClino);
1110 real dx, dy, dz;
1111 real vx, vy, vz;
1112 #ifndef NO_COVARIANCES
1113 real cxy, cyz, czx;
1114 #endif
1116 bool fNoComp;
1118 /* adjusted tape is negative -- probably the calibration is wrong */
1119 if (tape < (real)0.0) {
1120 /* TRANSLATE different message for topofil? */
1121 compile_diagnostic_reading(DIAG_WARN, Tape, /*Negative adjusted tape reading*/79);
1124 fNoComp = handle_comp_units();
1126 if (ctype == CTYPE_READING) {
1127 clin = handle_clino(Q_GRADIENT, Clino, clin,
1128 pcs->f_clino_percent, &ctype);
1131 if (backctype == CTYPE_READING) {
1132 backclin = handle_clino(Q_BACKGRADIENT, BackClino, backclin,
1133 pcs->f_backclino_percent, &backctype);
1136 /* un-infer the plumb if the backsight was just a reading */
1137 if (ctype == CTYPE_INFERPLUMB && backctype == CTYPE_READING) {
1138 ctype = CTYPE_READING;
1141 if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
1142 /* TRANSLATORS: In data with backsights, the user has tried to give a
1143 * plumb for the foresight and a clino reading for the backsight, or
1144 * something similar. */
1145 compile_error_reading_skip(Clino, /*CLINO and BACKCLINO readings must be of the same type*/84);
1146 return 0;
1149 if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB ||
1150 backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) {
1151 /* plumbed */
1152 if (!fNoComp) {
1153 if (ctype == CTYPE_PLUMB ||
1154 (ctype == CTYPE_INFERPLUMB && VAL(Comp) != 0.0) ||
1155 backctype == CTYPE_PLUMB ||
1156 (backctype == CTYPE_INFERPLUMB && VAL(BackComp) != 0.0)) {
1157 /* FIXME: Different message for BackComp? */
1158 /* TRANSLATORS: A "plumbed leg" is one measured using a plumbline
1159 * (a weight on a string). So the problem here is that the leg is
1160 * vertical, so a compass reading has no meaning! */
1161 compile_diagnostic(DIAG_WARN, /*Compass reading given on plumbed leg*/21);
1165 dx = dy = (real)0.0;
1166 if (ctype != CTYPE_OMIT) {
1167 if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
1168 /* TRANSLATORS: We've been told the foresight and backsight are
1169 * both "UP", or that they're both "DOWN". */
1170 compile_error_reading_skip(Clino, /*Plumbed CLINO and BACKCLINO readings can't be in the same direction*/92);
1171 return 0;
1173 dz = (clin > (real)0.0) ? tape : -tape;
1174 } else {
1175 dz = (backclin < (real)0.0) ? tape : -tape;
1177 vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1178 vz = var(Q_POS) / 3.0 + VAR(Tape);
1179 #ifndef NO_COVARIANCES
1180 /* Correct values - no covariances in this case! */
1181 cxy = cyz = czx = (real)0.0;
1182 #endif
1183 } else {
1184 /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
1185 * or CTYPE_OMIT */
1186 /* clino */
1187 real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
1188 if (fNoComp) {
1189 /* TRANSLATORS: Here "legs" are survey legs, i.e. measurements between
1190 * survey stations. */
1191 compile_error_reading_skip(Comp, /*Compass reading may not be omitted except on plumbed legs*/14);
1192 return 0;
1194 if (tape == (real)0.0) {
1195 dx = dy = dz = (real)0.0;
1196 vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
1197 #ifndef NO_COVARIANCES
1198 cxy = cyz = czx = (real)0.0;
1199 #endif
1200 #if DEBUG_DATAIN_1
1201 printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
1202 #endif
1203 } else {
1204 real sinGcosG;
1205 /* take into account variance in LEVEL case */
1206 real var_clin = var(Q_LEVEL);
1207 real var_comp;
1208 real comp = handle_compass(&var_comp);
1209 /* ctype != CTYPE_READING is LEVEL case */
1210 if (ctype == CTYPE_READING) {
1211 clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
1212 var_clin = VAR(Clino);
1214 if (backctype == CTYPE_READING) {
1215 backclin = (backclin - pcs->z[Q_BACKGRADIENT])
1216 * pcs->sc[Q_BACKGRADIENT];
1217 if (ctype == CTYPE_READING) {
1218 if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) {
1219 /* fore and back readings differ by more than 3 sds */
1220 /* TRANSLATORS: %s is replaced by the amount the readings disagree
1221 * by, e.g. "2.5Ā°" or "3įµ". */
1222 warn_readings_differ(/*CLINO reading and BACKCLINO reading disagree by %s*/99,
1223 clin + backclin, get_angle_units(Q_GRADIENT));
1225 clin = (clin / var_clin - backclin / VAR(BackClino));
1226 var_clin = (var_clin + VAR(BackClino)) / 4;
1227 clin *= var_clin;
1228 } else {
1229 clin = -backclin;
1230 var_clin = VAR(BackClino);
1234 #if DEBUG_DATAIN
1235 printf(" %4.2f %4.2f %4.2f\n", tape, comp, clin);
1236 #endif
1237 cosG = cos(clin);
1238 LcosG = tape * cosG;
1239 sinB = sin(comp);
1240 cosB = cos(comp);
1241 #if DEBUG_DATAIN_1
1242 printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
1243 #endif
1244 dx = LcosG * sinB;
1245 dy = LcosG * cosB;
1246 dz = tape * sin(clin);
1247 /* printf("%.2f\n",clin); */
1248 #if DEBUG_DATAIN_1
1249 printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
1250 #endif
1251 dx2 = dx * dx;
1252 L2 = tape * tape;
1253 V = VAR(Tape) / L2;
1254 dy2 = dy * dy;
1255 cosG2 = cosG * cosG;
1256 sinGcosG = sin(clin) * cosG;
1257 dz2 = dz * dz;
1258 v = dz2 * var_clin;
1259 #ifdef NO_COVARIANCES
1260 vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
1261 (.5 + sinB * sinB * cosG2) * v);
1262 vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
1263 (.5 + cosB * cosB * cosG2) * v);
1264 if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
1265 /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
1266 vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
1267 } else {
1268 vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
1270 /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
1271 #else
1272 vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
1273 (sinB * sinB * v);
1274 vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
1275 (cosB * cosB * v);
1276 if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
1277 /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
1278 vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
1279 } else {
1280 vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
1282 /* usual covariance formulae are fine in no clino case since
1283 * dz = 0 so value of var_clin is ignored */
1284 cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
1285 - var_comp * dx * dy;
1286 czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
1287 cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
1288 #if 0
1289 printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
1290 printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
1291 #endif
1292 #endif
1293 #if DEBUG_DATAIN_1
1294 printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
1295 #endif
1298 #if DEBUG_DATAIN_1
1299 printf("Just before addleg, vx = %f\n", vx);
1300 #endif
1301 /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
1302 addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1303 #ifndef NO_COVARIANCES
1304 , cyz, czx, cxy
1305 #endif
1307 return 1;
1310 static int
1311 process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1313 real tape = VAL(Tape);
1315 real dx, dy, dz;
1316 real vx, vy, vz;
1317 #ifndef NO_COVARIANCES
1318 real cxy = 0, cyz = 0, czx = 0;
1319 #endif
1321 handle_comp_units();
1323 /* depth gauge readings increase upwards with default calibration */
1324 if (fDepthChange) {
1325 SVX_ASSERT(VAL(FrDepth) == 0.0);
1326 dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1327 dz *= pcs->sc[Q_DEPTH];
1328 } else {
1329 dz = VAL(ToDepth) - VAL(FrDepth);
1330 dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1333 /* adjusted tape is negative -- probably the calibration is wrong */
1334 if (tape < (real)0.0) {
1335 compile_diagnostic(DIAG_WARN, /*Negative adjusted tape reading*/79);
1338 /* check if tape is less than depth change */
1339 if (tape < fabs(dz)) {
1340 /* FIXME: allow margin of error based on variances? */
1341 /* TRANSLATORS: This means that the data fed in said this.
1343 * It could be a gross error (e.g. the decimal point is missing from the
1344 * depth gauge reading) or it could just be due to random error on a near
1345 * vertical leg */
1346 compile_diagnostic(DIAG_WARN, /*Tape reading is less than change in depth*/62);
1349 if (tape == (real)0.0 && dz == 0.0) {
1350 dx = dy = dz = (real)0.0;
1351 vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
1352 } else if (VAL(Comp) == HUGE_REAL &&
1353 VAL(BackComp) == HUGE_REAL) {
1354 /* plumb */
1355 dx = dy = (real)0.0;
1356 if (dz < 0) tape = -tape;
1357 /* FIXME: Should use FrDepth sometimes... */
1358 dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1359 / (VAR(Tape) * 2 * VAR(ToDepth));
1360 vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1361 /* FIXME: Should use FrDepth sometimes... */
1362 vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1363 / (VAR(Tape) + VAR(ToDepth));
1364 } else {
1365 real L2, sinB, cosB, dz2, D2;
1366 real var_comp;
1367 real comp = handle_compass(&var_comp);
1368 sinB = sin(comp);
1369 cosB = cos(comp);
1370 L2 = tape * tape;
1371 dz2 = dz * dz;
1372 D2 = L2 - dz2;
1373 if (D2 <= (real)0.0) {
1374 /* FIXME: Should use FrDepth sometimes... */
1375 real vsum = VAR(Tape) + 2 * VAR(ToDepth);
1376 dx = dy = (real)0.0;
1377 vx = vy = var(Q_POS) / 3.0;
1378 /* FIXME: Should use FrDepth sometimes... */
1379 vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
1380 if (dz > 0) {
1381 /* FIXME: Should use FrDepth sometimes... */
1382 dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
1383 } else {
1384 dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
1386 } else {
1387 real D = sqrt(D2);
1388 /* FIXME: Should use FrDepth sometimes... */
1389 real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
1390 dx = D * sinB;
1391 dy = D * cosB;
1393 vx = var(Q_POS) / 3.0 +
1394 sinB * sinB * F / D2 + var_comp * dy * dy;
1395 vy = var(Q_POS) / 3.0 +
1396 cosB * cosB * F / D2 + var_comp * dx * dx;
1397 /* FIXME: Should use FrDepth sometimes... */
1398 vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1400 #ifndef NO_COVARIANCES
1401 cxy = sinB * cosB * (F / D2 + var_comp * D2);
1402 /* FIXME: Should use FrDepth sometimes... */
1403 cyz = -2 * VAR(ToDepth) * dy / D;
1404 czx = -2 * VAR(ToDepth) * dx / D;
1405 #endif
1407 /* FIXME: If there's a clino reading, check it against the depth reading,
1408 * and average.
1409 * if (VAL(Clino) != HUGE_REAL || VAL(BackClino) != HUGE_REAL) { ... }
1412 addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1413 #ifndef NO_COVARIANCES
1414 , cxy, cyz, czx
1415 #endif
1417 return 1;
1420 static int
1421 process_cartesian(prefix *fr, prefix *to, bool fToFirst)
1423 real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
1424 real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
1425 real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
1427 addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
1428 #ifndef NO_COVARIANCES
1429 , 0, 0, 0
1430 #endif
1432 return 1;
1435 static void
1436 data_cartesian(void)
1438 prefix *fr = NULL, *to = NULL;
1440 bool fMulti = fFalse;
1442 reading first_stn = End;
1444 const reading *ordering;
1446 again:
1448 for (ordering = pcs->ordering ; ; ordering++) {
1449 skipblanks();
1450 switch (*ordering) {
1451 case Fr:
1452 fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1453 if (first_stn == End) first_stn = Fr;
1454 break;
1455 case To:
1456 to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
1457 if (first_stn == End) first_stn = To;
1458 break;
1459 case Station:
1460 fr = to;
1461 to = read_prefix(PFX_STATION);
1462 first_stn = To;
1463 break;
1464 case Dx: case Dy: case Dz:
1465 read_reading(*ordering, fFalse);
1466 break;
1467 case Ignore:
1468 skipword(); break;
1469 case IgnoreAllAndNewLine:
1470 skipline();
1471 /* fall through */
1472 case Newline:
1473 if (fr != NULL) {
1474 if (!process_cartesian(fr, to, first_stn == To))
1475 skipline();
1477 fMulti = fTrue;
1478 while (1) {
1479 process_eol();
1480 skipblanks();
1481 if (isData(ch)) break;
1482 if (!isComm(ch)) {
1483 return;
1486 break;
1487 case IgnoreAll:
1488 skipline();
1489 /* fall through */
1490 case End:
1491 if (!fMulti) {
1492 process_cartesian(fr, to, first_stn == To);
1493 process_eol();
1494 return;
1496 do {
1497 process_eol();
1498 skipblanks();
1499 } while (isComm(ch));
1500 goto again;
1501 default: BUG("Unknown reading in ordering");
1506 static int
1507 process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1509 real tape = VAL(Tape);
1511 real dx, dy, dz;
1512 real vx, vy, vz;
1513 #ifndef NO_COVARIANCES
1514 real cxy = 0;
1515 #endif
1517 handle_comp_units();
1519 /* depth gauge readings increase upwards with default calibration */
1520 if (fDepthChange) {
1521 SVX_ASSERT(VAL(FrDepth) == 0.0);
1522 dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1523 dz *= pcs->sc[Q_DEPTH];
1524 } else {
1525 dz = VAL(ToDepth) - VAL(FrDepth);
1526 dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1529 /* adjusted tape is negative -- probably the calibration is wrong */
1530 if (tape < (real)0.0) {
1531 compile_diagnostic(DIAG_WARN, /*Negative adjusted tape reading*/79);
1534 if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
1535 /* plumb */
1536 dx = dy = (real)0.0;
1537 vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1538 /* FIXME: Should use FrDepth sometimes... */
1539 vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1540 } else {
1541 real sinB, cosB;
1542 real var_comp;
1543 real comp = handle_compass(&var_comp);
1544 sinB = sin(comp);
1545 cosB = cos(comp);
1547 dx = tape * sinB;
1548 dy = tape * cosB;
1550 vx = var(Q_POS) / 3.0 +
1551 VAR(Tape) * sinB * sinB + var_comp * dy * dy;
1552 vy = var(Q_POS) / 3.0 +
1553 VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1554 /* FIXME: Should use FrDepth sometimes... */
1555 vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1557 #ifndef NO_COVARIANCES
1558 cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
1559 #endif
1561 addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1562 #ifndef NO_COVARIANCES
1563 , cxy, 0, 0
1564 #endif
1566 return 1;
1569 /* Process tape/compass/clino, diving, and cylpolar styles of survey data
1570 * Also handles topofil (fromcount/tocount or count) in place of tape */
1571 static void
1572 data_normal(void)
1574 prefix *fr = NULL, *to = NULL;
1575 reading first_stn = End;
1577 bool fTopofil = fFalse, fMulti = fFalse;
1578 bool fRev;
1579 clino_type ctype, backctype;
1580 bool fDepthChange;
1581 unsigned long compass_dat_flags = 0;
1583 const reading *ordering;
1585 VAL(Tape) = VAL(BackTape) = HUGE_REAL;
1586 VAL(Comp) = VAL(BackComp) = HUGE_REAL;
1587 VAL(FrCount) = VAL(ToCount) = 0;
1588 VAL(FrDepth) = VAL(ToDepth) = 0;
1589 VAL(Left) = VAL(Right) = VAL(Up) = VAL(Down) = HUGE_REAL;
1591 fRev = fFalse;
1592 ctype = backctype = CTYPE_OMIT;
1593 fDepthChange = fFalse;
1595 /* ordering may omit clino reading, so set up default here */
1596 /* this is also used if clino reading is the omit character */
1597 VAL(Clino) = VAL(BackClino) = 0;
1599 again:
1601 /* We clear these flags in the normal course of events, but if there's an
1602 * error in a reading, we might not, so make sure it has been cleared here.
1604 pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1605 for (ordering = pcs->ordering; ; ordering++) {
1606 skipblanks();
1607 switch (*ordering) {
1608 case Fr:
1609 fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
1610 if (first_stn == End) first_stn = Fr;
1611 break;
1612 case To:
1613 to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
1614 if (first_stn == End) first_stn = To;
1615 break;
1616 case Station:
1617 fr = to;
1618 to = read_prefix(PFX_STATION);
1619 first_stn = To;
1620 break;
1621 case Dir: {
1622 typedef enum {
1623 DIR_NULL=-1, DIR_FORE, DIR_BACK
1624 } dir_tok;
1625 static const sztok dir_tab[] = {
1626 {"B", DIR_BACK},
1627 {"F", DIR_FORE},
1629 dir_tok tok;
1630 get_token();
1631 tok = match_tok(dir_tab, TABSIZE(dir_tab));
1632 switch (tok) {
1633 case DIR_FORE:
1634 break;
1635 case DIR_BACK:
1636 fRev = fTrue;
1637 break;
1638 default:
1639 compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Found ā€œ%sā€, expecting ā€œFā€ or ā€œBā€*/131, buffer);
1640 process_eol();
1641 return;
1643 break;
1645 case Tape: case BackTape: {
1646 reading r = *ordering;
1647 read_reading(r, fTrue);
1648 if (VAL(r) == HUGE_REAL) {
1649 if (!isOmit(ch)) {
1650 compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found ā€œ%sā€*/9);
1651 /* Avoid also warning about omitted tape reading. */
1652 VAL(r) = 0;
1653 } else {
1654 nextch();
1656 } else if (VAL(r) < (real)0.0) {
1657 compile_diagnostic_reading(DIAG_WARN, r, /*Negative tape reading*/60);
1659 break;
1661 case Count:
1662 VAL(FrCount) = VAL(ToCount);
1663 LOC(FrCount) = LOC(ToCount);
1664 WID(FrCount) = WID(ToCount);
1665 read_reading(ToCount, fFalse);
1666 fTopofil = fTrue;
1667 break;
1668 case FrCount:
1669 read_reading(FrCount, fFalse);
1670 break;
1671 case ToCount:
1672 read_reading(ToCount, fFalse);
1673 fTopofil = fTrue;
1674 break;
1675 case Comp: case BackComp:
1676 read_bearing_or_omit(*ordering);
1677 break;
1678 case Clino: case BackClino: {
1679 reading r = *ordering;
1680 clino_type * p_ctype = (r == Clino ? &ctype : &backctype);
1681 read_reading(r, fTrue);
1682 if (VAL(r) == HUGE_REAL) {
1683 VAL(r) = handle_plumb(p_ctype);
1684 if (VAL(r) != HUGE_REAL) break;
1685 compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found ā€œ%sā€*/9);
1686 skipline();
1687 process_eol();
1688 return;
1690 *p_ctype = CTYPE_READING;
1691 break;
1693 case FrDepth: case ToDepth:
1694 read_reading(*ordering, fFalse);
1695 break;
1696 case Depth:
1697 VAL(FrDepth) = VAL(ToDepth);
1698 LOC(FrDepth) = LOC(ToDepth);
1699 WID(FrDepth) = WID(ToDepth);
1700 read_reading(ToDepth, fFalse);
1701 break;
1702 case DepthChange:
1703 fDepthChange = fTrue;
1704 VAL(FrDepth) = 0;
1705 read_reading(ToDepth, fFalse);
1706 break;
1707 case CompassDATComp:
1708 read_bearing_or_omit(Comp);
1709 if (is_compass_NaN(VAL(Comp))) VAL(Comp) = HUGE_REAL;
1710 break;
1711 case CompassDATBackComp:
1712 read_bearing_or_omit(BackComp);
1713 if (is_compass_NaN(VAL(BackComp))) VAL(BackComp) = HUGE_REAL;
1714 break;
1715 case CompassDATClino: case CompassDATBackClino: {
1716 reading r;
1717 clino_type * p_ctype;
1718 if (*ordering == CompassDATClino) {
1719 r = Clino;
1720 p_ctype = &ctype;
1721 } else {
1722 r = BackClino;
1723 p_ctype = &backctype;
1725 read_reading(r, fFalse);
1726 if (is_compass_NaN(VAL(r))) {
1727 VAL(r) = HUGE_REAL;
1728 *p_ctype = CTYPE_OMIT;
1729 } else {
1730 *p_ctype = CTYPE_READING;
1732 break;
1734 case CompassDATLeft: case CompassDATRight:
1735 case CompassDATUp: case CompassDATDown: {
1736 /* FIXME: need to actually make use of these entries! */
1737 reading actual = Left + (*ordering - CompassDATLeft);
1738 read_reading(actual, fFalse);
1739 if (VAL(actual) < 0) VAL(actual) = HUGE_REAL;
1740 break;
1742 case CompassDATFlags:
1743 if (ch == '#') {
1744 filepos fp;
1745 get_pos(&fp);
1746 nextch();
1747 if (ch == '|') {
1748 nextch();
1749 while (ch >= 'A' && ch <= 'Z') {
1750 compass_dat_flags |= BIT(ch - 'A');
1751 /* We currently understand:
1752 * L (exclude from length)
1753 * X (exclude data)
1754 * FIXME: but should also handle at least some of:
1755 * C (no adjustment) (set all (co)variances to 0?)
1756 * P (no plot) (new flag in 3d for "hidden by default"?)
1758 nextch();
1760 if (ch == '#') {
1761 nextch();
1762 } else {
1763 compass_dat_flags = 0;
1764 set_pos(&fp);
1766 } else {
1767 set_pos(&fp);
1770 break;
1771 case Ignore:
1772 skipword(); break;
1773 case IgnoreAllAndNewLine:
1774 skipline();
1775 /* fall through */
1776 case Newline:
1777 if (fr != NULL) {
1778 int r;
1779 int save_flags;
1780 int implicit_splay;
1781 if (fTopofil) {
1782 VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1783 LOC(Tape) = LOC(ToCount);
1784 WID(Tape) = WID(ToCount);
1786 /* Note: frdepth == todepth test works regardless of fDepthChange
1787 * (frdepth always zero, todepth is change of depth) and also
1788 * works for STYLE_NORMAL (both remain 0) */
1789 if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1790 (VAL(Tape) == (real)0.0 || VAL(Tape) == HUGE_REAL) &&
1791 (VAL(BackTape) == (real)0.0 || VAL(BackTape) == HUGE_REAL) &&
1792 VAL(FrDepth) == VAL(ToDepth)) {
1793 process_equate(fr, to);
1794 goto inferred_equate;
1796 if (fRev) {
1797 prefix *t = fr;
1798 fr = to;
1799 to = t;
1801 if (fTopofil) {
1802 VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1803 } else if (VAL(Tape) != HUGE_REAL) {
1804 VAL(Tape) *= pcs->units[Q_LENGTH];
1805 VAL(Tape) -= pcs->z[Q_LENGTH];
1806 VAL(Tape) *= pcs->sc[Q_LENGTH];
1808 if (VAL(BackTape) != HUGE_REAL) {
1809 VAL(BackTape) *= pcs->units[Q_BACKLENGTH];
1810 VAL(BackTape) -= pcs->z[Q_BACKLENGTH];
1811 VAL(BackTape) *= pcs->sc[Q_BACKLENGTH];
1812 if (VAL(Tape) != HUGE_REAL) {
1813 real diff = VAL(Tape) - VAL(BackTape);
1814 if (sqrd(diff / 3.0) > VAR(Tape) + VAR(BackTape)) {
1815 /* fore and back readings differ by more than 3 sds */
1816 /* TRANSLATORS: %s is replaced by the amount the readings disagree
1817 * by, e.g. "0.12m" or "0.2ft". */
1818 warn_readings_differ(/*TAPE reading and BACKTAPE reading disagree by %s*/97,
1819 diff, get_length_units(Q_LENGTH));
1821 VAL(Tape) = VAL(Tape) / VAR(Tape) + VAL(BackTape) / VAR(BackTape);
1822 VAR(Tape) = (VAR(Tape) + VAR(BackTape)) / 4;
1823 VAL(Tape) *= VAR(Tape);
1824 } else {
1825 VAL(Tape) = VAL(BackTape);
1826 VAR(Tape) = VAR(BackTape);
1828 } else if (VAL(Tape) == HUGE_REAL) {
1829 compile_diagnostic_reading(DIAG_ERR, Tape, /*Tape reading may not be omitted*/94);
1830 goto inferred_equate;
1832 implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1833 pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1834 save_flags = pcs->flags;
1835 if (implicit_splay) {
1836 pcs->flags |= BIT(FLAGS_SPLAY);
1838 switch (pcs->style) {
1839 case STYLE_NORMAL:
1840 r = process_normal(fr, to, (first_stn == To) ^ fRev,
1841 ctype, backctype);
1842 break;
1843 case STYLE_DIVING:
1844 /* FIXME: Handle any clino readings */
1845 r = process_diving(fr, to, (first_stn == To) ^ fRev,
1846 fDepthChange);
1847 break;
1848 case STYLE_CYLPOLAR:
1849 r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1850 fDepthChange);
1851 break;
1852 default:
1853 r = 0; /* avoid warning */
1854 BUG("bad style");
1856 pcs->flags = save_flags;
1857 if (!r) skipline();
1859 /* Swap fr and to back to how they were for next line */
1860 if (fRev) {
1861 prefix *t = fr;
1862 fr = to;
1863 to = t;
1867 fRev = fFalse;
1868 ctype = backctype = CTYPE_OMIT;
1869 fDepthChange = fFalse;
1871 /* ordering may omit clino reading, so set up default here */
1872 /* this is also used if clino reading is the omit character */
1873 VAL(Clino) = VAL(BackClino) = 0;
1874 LOC(Clino) = LOC(BackClino) = -1;
1875 WID(Clino) = WID(BackClino) = 0;
1877 inferred_equate:
1879 fMulti = fTrue;
1880 while (1) {
1881 process_eol();
1882 skipblanks();
1883 if (isData(ch)) break;
1884 if (!isComm(ch)) {
1885 return;
1888 break;
1889 case IgnoreAll:
1890 skipline();
1891 /* fall through */
1892 case End:
1893 if (!fMulti) {
1894 int save_flags;
1895 int implicit_splay;
1896 /* Compass ignore flag is 'X' */
1897 if ((compass_dat_flags & BIT('X' - 'A'))) {
1898 process_eol();
1899 return;
1901 if (fRev) {
1902 prefix *t = fr;
1903 fr = to;
1904 to = t;
1906 if (fTopofil) {
1907 VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1908 LOC(Tape) = LOC(ToCount);
1909 WID(Tape) = WID(ToCount);
1911 /* Note: frdepth == todepth test works regardless of fDepthChange
1912 * (frdepth always zero, todepth is change of depth) and also
1913 * works for STYLE_NORMAL (both remain 0) */
1914 if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1915 (VAL(Tape) == (real)0.0 || VAL(Tape) == HUGE_REAL) &&
1916 (VAL(BackTape) == (real)0.0 || VAL(BackTape) == HUGE_REAL) &&
1917 VAL(FrDepth) == VAL(ToDepth)) {
1918 process_equate(fr, to);
1919 process_eol();
1920 return;
1922 if (fTopofil) {
1923 VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1924 } else if (VAL(Tape) != HUGE_REAL) {
1925 VAL(Tape) *= pcs->units[Q_LENGTH];
1926 VAL(Tape) -= pcs->z[Q_LENGTH];
1927 VAL(Tape) *= pcs->sc[Q_LENGTH];
1929 if (VAL(BackTape) != HUGE_REAL) {
1930 VAL(BackTape) *= pcs->units[Q_BACKLENGTH];
1931 VAL(BackTape) -= pcs->z[Q_BACKLENGTH];
1932 VAL(BackTape) *= pcs->sc[Q_BACKLENGTH];
1933 if (VAL(Tape) != HUGE_REAL) {
1934 real diff = VAL(Tape) - VAL(BackTape);
1935 if (sqrd(diff / 3.0) > VAR(Tape) + VAR(BackTape)) {
1936 /* fore and back readings differ by more than 3 sds */
1937 /* TRANSLATORS: %s is replaced by the amount the readings disagree
1938 * by, e.g. "0.12m" or "0.2ft". */
1939 warn_readings_differ(/*TAPE reading and BACKTAPE reading disagree by %s*/97,
1940 diff, get_length_units(Q_LENGTH));
1942 VAL(Tape) = VAL(Tape) / VAR(Tape) + VAL(BackTape) / VAR(BackTape);
1943 VAR(Tape) = (VAR(Tape) + VAR(BackTape)) / 4;
1944 VAL(Tape) *= VAR(Tape);
1945 } else {
1946 VAL(Tape) = VAL(BackTape);
1947 VAR(Tape) = VAR(BackTape);
1949 } else if (VAL(Tape) == HUGE_REAL) {
1950 compile_diagnostic_reading(DIAG_ERR, Tape, /*Tape reading may not be omitted*/94);
1951 process_eol();
1952 return;
1954 implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1955 pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1956 save_flags = pcs->flags;
1957 if (implicit_splay) {
1958 pcs->flags |= BIT(FLAGS_SPLAY);
1960 if ((compass_dat_flags & BIT('L' - 'A'))) {
1961 /* 'L' means "exclude from length" - map this to Survex's
1962 * FLAGS_DUPLICATE. */
1963 pcs->flags |= BIT(FLAGS_DUPLICATE);
1965 switch (pcs->style) {
1966 case STYLE_NORMAL:
1967 process_normal(fr, to, (first_stn == To) ^ fRev,
1968 ctype, backctype);
1969 break;
1970 case STYLE_DIVING:
1971 /* FIXME: Handle any clino readings */
1972 process_diving(fr, to, (first_stn == To) ^ fRev,
1973 fDepthChange);
1974 break;
1975 case STYLE_CYLPOLAR:
1976 process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1977 fDepthChange);
1978 break;
1979 default:
1980 BUG("bad style");
1982 pcs->flags = save_flags;
1984 process_eol();
1985 return;
1987 do {
1988 process_eol();
1989 skipblanks();
1990 } while (isComm(ch));
1991 goto again;
1992 default:
1993 BUG("Unknown reading in ordering");
1998 static int
1999 process_lrud(prefix *stn)
2001 SVX_ASSERT(next_lrud);
2002 lrud * xsect = osnew(lrud);
2003 xsect->stn = stn;
2004 xsect->l = (VAL(Left) * pcs->units[Q_LEFT] - pcs->z[Q_LEFT]) * pcs->sc[Q_LEFT];
2005 xsect->r = (VAL(Right) * pcs->units[Q_RIGHT] - pcs->z[Q_RIGHT]) * pcs->sc[Q_RIGHT];
2006 xsect->u = (VAL(Up) * pcs->units[Q_UP] - pcs->z[Q_UP]) * pcs->sc[Q_UP];
2007 xsect->d = (VAL(Down) * pcs->units[Q_DOWN] - pcs->z[Q_DOWN]) * pcs->sc[Q_DOWN];
2008 xsect->meta = pcs->meta;
2009 if (pcs->meta) ++pcs->meta->ref_count;
2010 xsect->next = NULL;
2011 *next_lrud = xsect;
2012 next_lrud = &(xsect->next);
2014 return 1;
2017 static void
2018 data_passage(void)
2020 prefix *stn = NULL;
2021 const reading *ordering;
2023 for (ordering = pcs->ordering ; ; ordering++) {
2024 skipblanks();
2025 switch (*ordering) {
2026 case Station:
2027 stn = read_prefix(PFX_STATION);
2028 break;
2029 case Left: case Right: case Up: case Down: {
2030 reading r = *ordering;
2031 read_reading(r, fTrue);
2032 if (VAL(r) == HUGE_REAL) {
2033 if (!isOmit(ch)) {
2034 compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found ā€œ%sā€*/9);
2035 } else {
2036 nextch();
2038 VAL(r) = -1;
2040 break;
2042 case Ignore:
2043 skipword(); break;
2044 case IgnoreAll:
2045 skipline();
2046 /* fall through */
2047 case End: {
2048 process_lrud(stn);
2049 process_eol();
2050 return;
2052 default: BUG("Unknown reading in ordering");
2057 static int
2058 process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
2060 nosurveylink *link;
2062 /* Suppress "unused fixed point" warnings for these stations */
2063 fr->sflags |= BIT(SFLAGS_USED);
2064 to->sflags |= BIT(SFLAGS_USED);
2066 /* add to linked list which is dealt with after network is solved */
2067 link = osnew(nosurveylink);
2068 if (fToFirst) {
2069 link->to = StnFromPfx(to);
2070 link->fr = StnFromPfx(fr);
2071 } else {
2072 link->fr = StnFromPfx(fr);
2073 link->to = StnFromPfx(to);
2075 link->flags = pcs->flags | (STYLE_NOSURVEY << FLAGS_STYLE_BIT0);
2076 link->meta = pcs->meta;
2077 if (pcs->meta) ++pcs->meta->ref_count;
2078 link->next = nosurveyhead;
2079 nosurveyhead = link;
2080 return 1;
2083 static void
2084 data_nosurvey(void)
2086 prefix *fr = NULL, *to = NULL;
2088 bool fMulti = fFalse;
2090 reading first_stn = End;
2092 const reading *ordering;
2094 again:
2096 for (ordering = pcs->ordering ; ; ordering++) {
2097 skipblanks();
2098 switch (*ordering) {
2099 case Fr:
2100 fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
2101 if (first_stn == End) first_stn = Fr;
2102 break;
2103 case To:
2104 to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
2105 if (first_stn == End) first_stn = To;
2106 break;
2107 case Station:
2108 fr = to;
2109 to = read_prefix(PFX_STATION);
2110 first_stn = To;
2111 break;
2112 case Ignore:
2113 skipword(); break;
2114 case IgnoreAllAndNewLine:
2115 skipline();
2116 /* fall through */
2117 case Newline:
2118 if (fr != NULL) {
2119 if (!process_nosurvey(fr, to, first_stn == To))
2120 skipline();
2122 if (ordering[1] == End) {
2123 do {
2124 process_eol();
2125 skipblanks();
2126 } while (isComm(ch));
2127 if (!isData(ch)) {
2128 return;
2130 goto again;
2132 fMulti = fTrue;
2133 while (1) {
2134 process_eol();
2135 skipblanks();
2136 if (isData(ch)) break;
2137 if (!isComm(ch)) {
2138 return;
2141 break;
2142 case IgnoreAll:
2143 skipline();
2144 /* fall through */
2145 case End:
2146 if (!fMulti) {
2147 (void)process_nosurvey(fr, to, first_stn == To);
2148 process_eol();
2149 return;
2151 do {
2152 process_eol();
2153 skipblanks();
2154 } while (isComm(ch));
2155 goto again;
2156 default: BUG("Unknown reading in ordering");
2161 /* totally ignore a line of survey data */
2162 static void
2163 data_ignore(void)
2165 skipline();
2166 process_eol();