source: git/src/datain.c @ 85c0078

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 85c0078 was 85c0078, checked in by Olly Betts <olly@…>, 9 years ago

doc/TODO.htm,lib/,src/,tests/: In cavern, use the currently set units
when outputting measurements in warnings, errors, and the stats at
the end of the run.

  • Property mode set to 100644
File size: 51.1 KB
RevLine 
[b0d908e]1/* datain.c
[d1b1380]2 * Reads in survey files, dealing with special characters, keywords & data
[dda0ca7]3 * Copyright (C) 1991-2003,2005,2009,2010,2011,2012,2013,2014,2015 Olly Betts
[44bb30d]4 * Copyright (C) 2004 Simeon Warner
[846746e]5 *
[89231c4]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.
[846746e]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
[89231c4]13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
[846746e]15 *
[89231c4]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
[ecbc6c18]18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[d1b1380]19 */
20
[a420b49]21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
[d1b1380]24
25#include <limits.h>
[a420b49]26#include <stdarg.h>
[d1b1380]27
28#include "debug.h"
[a420b49]29#include "cavern.h"
[c60cb16]30#include "date.h"
[07025e3]31#include "filename.h"
32#include "message.h"
[d1b1380]33#include "filelist.h"
34#include "netbits.h"
[5853657]35#include "netskel.h"
[d1b1380]36#include "readval.h"
37#include "datain.h"
38#include "commands.h"
39#include "out.h"
[39fba51]40#include "str.h"
[d1b1380]41
[2b078c4]42#define EPSILON (REAL_EPSILON * 1000)
[d1b1380]43
[58cc1fb]44#define var(I) (pcs->Var[(I)])
[d1b1380]45
[44bb30d]46/* true if x is not-a-number value in Compass (999.0 or -999.0)    */
47/* Compass uses 999.0 but understands Karst data which used -999.0 */
48#define is_compass_NaN(x) ( fabs(fabs(x)-999.0) <  EPSILON )
49
[58cc1fb]50int ch;
[d1b1380]51
[be97baf]52typedef enum {
53    CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_INFERPLUMB, CTYPE_HORIZ
54} clino_type;
[5b7c1b7]55
[eb18f4d]56/* Don't explicitly initialise as we can't set the jmp_buf - this has
57 * static scope so will be initialised like this anyway */
[b0d908e]58parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
[a420b49]59
[932f7e9]60bool f_export_ok;
61
[21c226e]62static real value[Fr - 1];
63#define VAL(N) value[(N)-1]
64static real variance[Fr - 1];
65#define VAR(N) variance[(N)-1]
66
[ee05463]67/* style functions */
[e2d48d8]68static void data_normal(void);
69static void data_cartesian(void);
70static void data_passage(void);
71static void data_nosurvey(void);
72static void data_ignore(void);
[ee05463]73
[c7451a9]74void
[c80bd34]75get_pos(filepos *fp)
[c7451a9]76{
[c80bd34]77   fp->ch = ch;
78   fp->offset = ftell(file.fh);
79   if (fp->offset == -1)
[c7451a9]80      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
81}
82
[c80bd34]83void
84set_pos(const filepos *fp)
[c7451a9]85{
[c80bd34]86   ch = fp->ch;
87   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
[c7451a9]88      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
89}
90
[44bf1d9]91static void
[993454b]92push_back(int c)
93{
94   if (c != EOF && ungetc(c, file.fh) == EOF)
95      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
96}
97
[895f767]98static void
99report_parent(parse * p) {
100    if (p->parent)
101        report_parent(p->parent);
102    /* Force re-report of include tree for further errors in
103     * parent files */
104    p->reported_where = fFalse;
[736f7df]105    /* TRANSLATORS: %s is replaced by the filename of the parent file, and %u
[0b8c321]106     * by the line number in that file.  Your translation should also contain
107     * %s:%u so that automatic parsing of error messages to determine the file
108     * and line number still works. */
[895f767]109    fprintf(STDERR, msg(/*In file included from %s:%u:\n*/5), p->filename, p->line);
110}
111
[a420b49]112static void
113error_list_parent_files(void)
114{
[b0d908e]115   if (!file.reported_where && file.parent) {
[895f767]116      report_parent(file.parent);
[b0d908e]117      /* Suppress reporting of full include tree for further errors
118       * in this file */
119      file.reported_where = fTrue;
[a420b49]120   }
121}
122
123void
124compile_error(int en, ...)
125{
[da96015]126   int col = 0;
[a420b49]127   va_list ap;
128   va_start(ap, en);
129   error_list_parent_files();
[da96015]130   if (en < 0) {
131      en = -en;
[671c8f6]132      if (file.fh) col = ftell(file.fh) - file.lpos;
[da96015]133   }
134   v_report(1, file.filename, file.line, col, en, ap);
[a420b49]135   va_end(ap);
136}
137
[fa42426]138void
139compile_error_skip(int en, ...)
140{
[da96015]141   int col = 0;
[fa42426]142   va_list ap;
143   va_start(ap, en);
144   error_list_parent_files();
[da96015]145   if (en < 0) {
146      en = -en;
[671c8f6]147      if (file.fh) col = ftell(file.fh) - file.lpos;
[da96015]148   }
149   v_report(1, file.filename, file.line, col, en, ap);
[fa42426]150   va_end(ap);
151   skipline();
152}
153
[93e3492]154void
155compile_error_at(const char * filename, unsigned line, int en, ...)
156{
157   va_list ap;
158   va_start(ap, en);
159   v_report(1, filename, line, 0, en, ap);
160   va_end(ap);
161}
162
163void
164compile_error_pfx(const prefix * pfx, int en, ...)
165{
166   va_list ap;
167   va_start(ap, en);
168   v_report(1, pfx->filename, pfx->line, 0, en, ap);
169   va_end(ap);
170}
171
[39fba51]172void
173compile_error_token(int en)
174{
175   char *p = NULL;
[8cea1ad]176   int len = 0;
[39fba51]177   skipblanks();
178   while (!isBlank(ch) && !isEol(ch)) {
[63dc4eb]179      s_catchar(&p, &len, (char)ch);
[39fba51]180      nextch();
181   }
[4bc4d58]182   compile_error(en, p ? p : "");
[39fba51]183   osfree(p);
184}
185
[a420b49]186void
187compile_warning(int en, ...)
188{
[da96015]189   int col = 0;
[a420b49]190   va_list ap;
191   va_start(ap, en);
192   error_list_parent_files();
[da96015]193   if (en < 0) {
194      en = -en;
[671c8f6]195      if (file.fh) col = ftell(file.fh) - file.lpos;
[da96015]196   }
197   v_report(0, file.filename, file.line, col, en, ap);
[a420b49]198   va_end(ap);
199}
200
[93e3492]201void
202compile_warning_at(const char * filename, unsigned line, int en, ...)
203{
204   va_list ap;
205   va_start(ap, en);
206   v_report(0, filename, line, 0, en, ap);
207   va_end(ap);
208}
209
210void
211compile_warning_pfx(const prefix * pfx, int en, ...)
212{
213   va_list ap;
214   va_start(ap, en);
215   v_report(0, pfx->filename, pfx->line, 0, en, ap);
216   va_end(ap);
217}
218
[a420b49]219/* This function makes a note where to put output files */
220static void
221using_data_file(const char *fnm)
222{
223   if (!fnm_output_base) {
[4bab027]224      /* was: fnm_output_base = base_from_fnm(fnm); */
225      fnm_output_base = baseleaf_from_fnm(fnm);
226   } else if (fnm_output_base_is_dir) {
227      /* --output pointed to directory so use the leaf basename in that dir */
228      char *lf, *p;
229      lf = baseleaf_from_fnm(fnm);
230      p = use_path(fnm_output_base, lf);
231      osfree(lf);
232      osfree(fnm_output_base);
233      fnm_output_base = p;
234      fnm_output_base_is_dir = 0;
[58cc1fb]235   }
236}
237
[a420b49]238static void
239skipword(void)
240{
[58cc1fb]241   while (!isBlank(ch) && !isEol(ch)) nextch();
[d1b1380]242}
243
[a420b49]244extern void
245skipblanks(void)
246{
[58cc1fb]247   while (isBlank(ch)) nextch();
[d1b1380]248}
249
[a420b49]250extern void
251skipline(void)
252{
[58cc1fb]253   while (!isEol(ch)) nextch();
[d1b1380]254}
255
[90bb053]256#ifndef NO_PERCENTAGE
257static long int filelen;
258#endif
259
260static void
261process_bol(void)
262{
263#ifndef NO_PERCENTAGE
264   /* print %age of file done */
[be97baf]265   if (filelen > 0) {
266      filepos fp;
267      get_pos(&fp);
268      printf("%d%%\r", (int)(100 * fp.offset / filelen));
269   }
[90bb053]270#endif
271
272   nextch();
273   skipblanks();
274}
275
276static void
277process_eol(void)
278{
279   int eolchar;
280
281   skipblanks();
282
283   if (!isEol(ch)) {
[da96015]284      if (!isComm(ch)) compile_error(-/*End of line not blank*/15);
[90bb053]285      skipline();
286   }
287
288   eolchar = ch;
289   file.line++;
290   /* skip any different eol characters so we get line counts correct on
291    * DOS text files and similar, but don't count several adjacent blank
292    * lines as one */
293   while (ch != EOF) {
294      nextch();
295      if (ch == eolchar || !isEol(ch)) {
[993454b]296         push_back(ch);
[90bb053]297         break;
298      }
299   }
[da96015]300   file.lpos = ftell(file.fh);
[90bb053]301}
302
303static bool
304process_non_data_line(void)
305{
306   process_bol();
307
308   if (isData(ch)) return fFalse;
309
310   if (isKeywd(ch)) {
311      nextch();
312      handle_command();
313   }
314
315   process_eol();
316
317   return fTrue;
318}
319
[21c226e]320static void
321read_reading(reading r, bool f_optional)
322{
323   int n_readings;
324   q_quantity q;
325   switch (r) {
326      case Tape: q = Q_LENGTH; break;
327      case Comp: q = Q_BEARING; break;
328      case BackComp: q = Q_BACKBEARING; break;
329      case Clino: q = Q_GRADIENT; break;
330      case BackClino: q = Q_BACKGRADIENT; break;
331      case FrDepth: case ToDepth: q = Q_DEPTH; break;
332      case Dx: q = Q_DX; break;
333      case Dy: q = Q_DY; break;
334      case Dz: q = Q_DZ; break;
335      case FrCount: case ToCount: q = Q_COUNT; break;
[07442af]336      case Left: q = Q_LEFT; break;
337      case Right: q = Q_RIGHT; break;
338      case Up: q = Q_UP; break;
339      case Down: q = Q_DOWN; break;
[74a0681]340      default:
[44bb30d]341        q = Q_NULL; /* Suppress compiler warning */;
[74a0681]342        BUG("Unexpected case");
[21c226e]343   }
[e3b03de]344   VAL(r) = read_numeric(f_optional, &n_readings);
[21c226e]345   VAR(r) = var(q);
346   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
347}
348
349static void
350read_bearing_or_omit(reading r)
351{
352   int n_readings;
[74a0681]353   q_quantity q = Q_NULL;
[21c226e]354   VAL(r) = read_numeric_or_omit(&n_readings);
355   switch (r) {
356      case Comp: q = Q_BEARING; break;
357      case BackComp: q = Q_BACKBEARING; break;
[74a0681]358      default:
[44bb30d]359        q = Q_NULL; /* Suppress compiler warning */;
[74a0681]360        BUG("Unexpected case");
[21c226e]361   }
362   VAR(r) = var(q);
363   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
364}
365
[be97baf]366/* For reading Compass MAK files which have a freeform syntax */
367static void
368nextch_handling_eol(void)
369{
370   nextch();
371   while (ch != EOF && isEol(ch)) {
372      process_eol();
373      nextch();
374   }
375}
376
377#define LITLEN(S) (sizeof(S"") - 1)
378#define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
379                        (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
380                        strcasecmp((F) + (L) - LITLEN(E), E) == 0)
[a420b49]381extern void
382data_file(const char *pth, const char *fnm)
383{
[47c7a94]384   int begin_lineno_store;
[7f08c83]385   parse file_store;
[b4fe9fb]386   volatile enum {FMT_SVX, FMT_DAT, FMT_MAK} fmt = FMT_SVX;
[d1b1380]387
[7f08c83]388   {
389      char *filename;
[f4b609d]390      FILE *fh;
[be97baf]391      size_t len;
392
[f4b609d]393      if (!pth) {
394         /* file specified on command line - don't do special translation */
395         fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
396      } else {
397         fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
398      }
[bd1913f]399
[7f08c83]400      if (fh == NULL) {
[66f5fc4]401         compile_error(-/*Couldn’t open file “%s”*/24, fnm);
[7f08c83]402         return;
403      }
404
[be97baf]405      len = strlen(filename);
406      if (has_ext(filename, len, "dat")) {
407         fmt = FMT_DAT;
408      } else if (has_ext(filename, len, "mak")) {
409         fmt = FMT_MAK;
410      }
411
[7f08c83]412      file_store = file;
413      if (file.fh) file.parent = &file_store;
414      file.fh = fh;
415      file.filename = filename;
416      file.line = 1;
[da96015]417      file.lpos = 0;
[b0d908e]418      file.reported_where = fFalse;
[a420b49]419   }
[cb3d1e2]420
[5b68ae1]421   if (fPercent) printf("%s:\n", fnm);
[d1b1380]422
[a420b49]423   using_data_file(file.filename);
[d1b1380]424
[47c7a94]425   begin_lineno_store = pcs->begin_lineno;
426   pcs->begin_lineno = 0;
[cb3d1e2]427
[d1b1380]428#ifndef NO_PERCENTAGE
[58cc1fb]429   /* Try to find how long the file is...
430    * However, under ANSI fseek( ..., SEEK_END) may not be supported */
431   filelen = 0;
432   if (fPercent) {
[c80bd34]433      if (fseek(file.fh, 0l, SEEK_END) == 0) {
434         filepos fp;
435         get_pos(&fp);
436         filelen = fp.offset;
437      }
[58cc1fb]438      rewind(file.fh); /* reset file ptr to start & clear any error state */
439   }
[d1b1380]440#endif
441
[be97baf]442   if (fmt == FMT_DAT) {
443      short *t;
444      int i;
445      settings *pcsNew;
446
447      pcsNew = osnew(settings);
448      *pcsNew = *pcs; /* copy contents */
449      pcsNew->begin_lineno = 0;
450      pcsNew->next = pcs;
451      pcs = pcsNew;
452      default_units(pcs);
453      default_calib(pcs);
454
455      pcs->style = STYLE_NORMAL;
456      pcs->units[Q_LENGTH] = METRES_PER_FOOT;
457      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
458
459      t[EOF] = SPECIAL_EOL;
460      memset(t, 0, sizeof(short) * 33);
461      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
462      t[127] = 0;
463      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
464      t['\t'] |= SPECIAL_BLANK;
465      t[' '] |= SPECIAL_BLANK;
466      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
467      t['\n'] |= SPECIAL_EOL;
468      t['\r'] |= SPECIAL_EOL;
469      t['.'] |= SPECIAL_DECIMAL;
470      t['-'] |= SPECIAL_MINUS;
471      t['+'] |= SPECIAL_PLUS;
472      pcs->Translate = t;
473      pcs->Case = OFF;
474      pcs->Truncate = INT_MAX;
[e9ea53b]475      pcs->infer = BIT(INFER_EQUATES)|BIT(INFER_EXPORTS)|BIT(INFER_PLUMBS);
[be97baf]476   } else if (fmt == FMT_MAK) {
477      short *t;
478      int i;
479      settings *pcsNew;
480
481      pcsNew = osnew(settings);
482      *pcsNew = *pcs; /* copy contents */
483      pcsNew->begin_lineno = 0;
484      pcsNew->next = pcs;
485      pcs = pcsNew;
486
487      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
[44bb30d]488
[be97baf]489      t[EOF] = SPECIAL_EOL;
490      memset(t, 0, sizeof(short) * 33);
491      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
492      t[127] = 0;
493      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
494      t['['] = t[','] = t[';'] = 0;
495      t['\t'] |= SPECIAL_BLANK;
496      t[' '] |= SPECIAL_BLANK;
497      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
498      t['\n'] |= SPECIAL_EOL;
499      t['\r'] |= SPECIAL_EOL;
500      t['.'] |= SPECIAL_DECIMAL;
501      t['-'] |= SPECIAL_MINUS;
502      t['+'] |= SPECIAL_PLUS;
503      pcs->Translate = t;
504      pcs->Case = OFF;
505      pcs->Truncate = INT_MAX;
506   }
507
[7d5f3c0]508#ifdef HAVE_SETJMP_H
[a420b49]509   /* errors in nested functions can longjmp here */
510   if (setjmp(file.jbSkipLine)) {
[e363f8b]511      skipline();
[076d33d]512      process_eol();
[a420b49]513   }
[d1b1380]514#endif
[cb3d1e2]515
[be97baf]516   if (fmt == FMT_DAT) {
517      while (!feof(file.fh) && !ferror(file.fh)) {
518         static reading compass_order[] = {
[44bb30d]519            Fr, To, Tape, CompassDATComp, CompassDATClino,
[07442af]520            CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
[be97baf]521            CompassDATFlags, IgnoreAll
522         };
523         static reading compass_order_backsights[] = {
[44bb30d]524            Fr, To, Tape, CompassDATComp, CompassDATClino,
[07442af]525            CompassDATLeft, CompassDATRight, CompassDATUp, CompassDATDown,
[44bb30d]526            CompassDATBackComp, CompassDATBackClino,
[be97baf]527            CompassDATFlags, IgnoreAll
528         };
529         /* <Cave name> */
530         process_bol();
531         skipline();
532         process_eol();
533         /* SURVEY NAME: <Short name> */
534         get_token();
535         get_token();
536         /* if (ch != ':') ... */
537         nextch();
538         get_token();
539         skipline();
540         process_eol();
541         /* SURVEY DATE: 7 10 79  COMMENT:<Long name> */
542         get_token();
543         get_token();
[5dc0378]544         copy_on_write_meta(pcs);
[b5a3219]545         if (ch == ':') {
[1ee204e]546             int year, month, day;
[b5a3219]547
548             nextch();
[1ee204e]549
[2c39b49]550             /* NB order is *month* *day* year */
[1ee204e]551             month = read_uint();
552             day = read_uint();
553             year = read_uint();
[b5a3219]554             /* Note: Larry says a 2 digit year is always 19XX */
[1ee204e]555             if (year < 100) year += 1900;
[b5a3219]556
[1ee204e]557             pcs->meta->days1 = pcs->meta->days2 = days_since_1900(year, month, day);
[a6301a1]558         } else {
[1ee204e]559             pcs->meta->days1 = pcs->meta->days2 = -1;
[b5a3219]560         }
[be97baf]561         skipline();
562         process_eol();
563         /* SURVEY TEAM: */
564         get_token();
565         get_token();
566         skipline();
567         process_eol();
568         /* <Survey team> */
569         nextch();
570         skipline();
571         process_eol();
572         /* DECLINATION: 1.00  FORMAT: DDDDLUDRADLN  CORRECTIONS: 2.00 3.00 4.00 */
573         get_token();
574         nextch(); /* : */
575         skipblanks();
576         pcs->z[Q_DECLINATION] = -read_numeric(fFalse, NULL);
577         pcs->z[Q_DECLINATION] *= pcs->units[Q_DECLINATION];
578         get_token();
579         pcs->ordering = compass_order;
580         if (strcmp(buffer, "FORMAT") == 0) {
581            nextch(); /* : */
582            get_token();
583            if (strlen(buffer) >= 12 && buffer[11] == 'B') {
584               /* We have backsights for compass and clino */
585               pcs->ordering = compass_order_backsights;
586            }
587            get_token();
[107b8bd]588         }
[be97baf]589         if (strcmp(buffer, "CORRECTIONS") == 0) {
590            nextch(); /* : */
591            pcs->z[Q_BEARING] = -rad(read_numeric(fFalse, NULL));
592            pcs->z[Q_GRADIENT] = -rad(read_numeric(fFalse, NULL));
593            pcs->z[Q_LENGTH] = -read_numeric(fFalse, NULL);
594         } else {
595            pcs->z[Q_BEARING] = 0;
596            pcs->z[Q_GRADIENT] = 0;
597            pcs->z[Q_LENGTH] = 0;
[a420b49]598         }
[be97baf]599         skipline();
600         process_eol();
601         /* BLANK LINE */
602         process_bol();
603         skipline();
604         process_eol();
605         /* heading line */
606         process_bol();
607         skipline();
608         process_eol();
609         /* BLANK LINE */
610         process_bol();
611         skipline();
612         process_eol();
613         while (!feof(file.fh)) {
614            process_bol();
615            if (ch == '\x0c') {
616               nextch();
617               process_eol();
618               break;
619            }
[e2d48d8]620            data_normal();
[be97baf]621         }
[dda0ca7]622         clear_last_leg();
[be97baf]623      }
624      {
625         settings *pcsParent = pcs->next;
626         SVX_ASSERT(pcsParent);
627         pcs->ordering = NULL;
628         free_settings(pcs);
629         pcs = pcsParent;
[44bb30d]630      }
[be97baf]631   } else if (fmt == FMT_MAK) {
632      nextch_handling_eol();
633      while (!feof(file.fh) && !ferror(file.fh)) {
634         if (ch == '#') {
635            /* include a file */
636            int ch_store;
[b4fe9fb]637            char *dat_pth = path_from_fnm(file.filename);
638            char *dat_fnm = NULL;
639            int dat_fnm_len;
[be97baf]640            nextch_handling_eol();
641            while (ch != ',' && ch != ';' && ch != EOF) {
642               while (isEol(ch)) process_eol();
[63dc4eb]643               s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
[be97baf]644               nextch_handling_eol();
645            }
646            while (ch != ';' && ch != EOF) {
647               prefix *name;
648               nextch_handling_eol();
[c458cf7]649               name = read_prefix(PFX_STATION|PFX_OPT);
[be97baf]650               if (name) {
651                  skipblanks();
652                  if (ch == '[') {
653                     /* fixed pt */
654                     node *stn;
655                     real x, y, z;
656                     name->sflags |= BIT(SFLAGS_FIXED);
657                     nextch_handling_eol();
658                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
659                            ch != '.' && ch != ']' && ch != EOF) {
660                        nextch_handling_eol();
661                     }
662                     x = read_numeric(fFalse, NULL);
663                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
664                            ch != '.' && ch != ']' && ch != EOF) {
665                        nextch_handling_eol();
666                     }
667                     y = read_numeric(fFalse, NULL);
668                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
669                            ch != '.' && ch != ']' && ch != EOF) {
670                        nextch_handling_eol();
671                     }
672                     z = read_numeric(fFalse, NULL);
673                     stn = StnFromPfx(name);
674                     if (!fixed(stn)) {
675                        POS(stn, 0) = x;
676                        POS(stn, 1) = y;
677                        POS(stn, 2) = z;
678                        fix(stn);
679                     } else {
680                        if (x != POS(stn, 0) || y != POS(stn, 1) ||
681                            z != POS(stn, 2)) {
682                           compile_error(/*Station already fixed or equated to a fixed point*/46);
683                        } else {
684                           compile_warning(/*Station already fixed at the same coordinates*/55);
685                        }
686                     }
687                     while (ch != ']' && ch != EOF) nextch_handling_eol();
688                     if (ch == ']') {
689                        nextch_handling_eol();
690                        skipblanks();
691                     }
692                  } else {
693                     /* FIXME: link station - ignore for now */
694                     /* FIXME: perhaps issue warning? */
695                  }
696                  while (ch != ',' && ch != ';' && ch != EOF)
697                     nextch_handling_eol();
698               }
699            }
[b4fe9fb]700            if (dat_fnm) {
[be97baf]701               ch_store = ch;
[b4fe9fb]702               data_file(dat_pth, dat_fnm);
[be97baf]703               ch = ch_store;
[b4fe9fb]704               osfree(dat_fnm);
[be97baf]705            }
706         } else {
707            /* FIXME: also check for % and $ later */
708            nextch_handling_eol();
709         }
710      }
711      {
712         settings *pcsParent = pcs->next;
713         SVX_ASSERT(pcsParent);
714         free_settings(pcs);
715         pcs = pcsParent;
[44bb30d]716      }
[be97baf]717   } else {
718      while (!feof(file.fh) && !ferror(file.fh)) {
719         if (!process_non_data_line()) {
720            f_export_ok = fFalse;
721            switch (pcs->style) {
722             case STYLE_NORMAL:
723             case STYLE_DIVING:
724             case STYLE_CYLPOLAR:
[e2d48d8]725               data_normal();
[be97baf]726               break;
727             case STYLE_CARTESIAN:
[e2d48d8]728               data_cartesian();
[be97baf]729               break;
[ee05463]730             case STYLE_PASSAGE:
[e2d48d8]731               data_passage();
[ee05463]732               break;
[be97baf]733             case STYLE_NOSURVEY:
[e2d48d8]734               data_nosurvey();
[be97baf]735               break;
736             case STYLE_IGNORE:
[e2d48d8]737               data_ignore();
[be97baf]738               break;
739             default:
740               BUG("bad style");
741            }
742         }
[647407d]743      }
[dda0ca7]744      clear_last_leg();
[a420b49]745   }
746
[932f7e9]747   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
748    * including file */
749   f_export_ok = fFalse;
750
[a420b49]751#ifndef NO_PERCENTAGE
752   if (fPercent) putnl();
[4f8285d]753#endif
[d1b1380]754
[47c7a94]755   if (pcs->begin_lineno) {
[0e867ba6]756      error_in_file(file.filename, pcs->begin_lineno,
757                    /*BEGIN with no matching END in this file*/23);
[47c7a94]758      /* Implicitly close any unclosed BEGINs from this file */
759      do {
760         settings *pcsParent = pcs->next;
[4c07c51]761         SVX_ASSERT(pcsParent);
[647407d]762         free_settings(pcs);
[47c7a94]763         pcs = pcsParent;
764      } while (pcs->begin_lineno);
765   }
766
767   pcs->begin_lineno = begin_lineno_store;
[cb3d1e2]768
[22c9877]769   if (ferror(file.fh))
[bfe1242]770      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
[cb3d1e2]771
[22c9877]772   (void)fclose(file.fh);
[7f08c83]773
[22c9877]774   file = file_store;
[4d9eecd]775
776   /* don't free this - it may be pointed to by prefix.file */
777   /* osfree(file.filename); */
[a420b49]778}
[d1b1380]779
[b6de07d]780static real
781mod2pi(real a)
782{
783   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
784}
785
[dcec245]786static real
[5b7c1b7]787handle_plumb(clino_type *p_ctype)
[dcec245]788{
789   typedef enum {
790      CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
791   } clino_tok;
792   static sztok clino_tab[] = {
793      {"D",     CLINO_DOWN},
794      {"DOWN",  CLINO_DOWN},
795      {"H",     CLINO_LEVEL},
796      {"LEVEL", CLINO_LEVEL},
797      {"U",     CLINO_UP},
798      {"UP",    CLINO_UP},
799      {NULL,    CLINO_NULL}
800   };
[5b7c1b7]801   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
[dcec245]802   clino_tok tok;
803
804   skipblanks();
805   if (isalpha(ch)) {
[c80bd34]806      filepos fp;
807      get_pos(&fp);
[dcec245]808      get_token();
809      tok = match_tok(clino_tab, TABSIZE(clino_tab));
810      if (tok != CLINO_NULL) {
[be97baf]811         *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
[dcec245]812         return clinos[tok];
813      }
[c80bd34]814      set_pos(&fp);
[dcec245]815   } else if (isSign(ch)) {
816      int chOld = ch;
817      nextch();
818      if (toupper(ch) == 'V') {
819         nextch();
[fa42426]820         *p_ctype = CTYPE_PLUMB;
[bceebf4]821         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
[dcec245]822      }
823
[bd1913f]824      if (isOmit(chOld)) {
[fa42426]825         *p_ctype = CTYPE_OMIT;
[dcec245]826         /* no clino reading, so assume 0 with large sd */
827         return (real)0.0;
828      }
[5b7c1b7]829   } else if (isOmit(ch)) {
830      /* OMIT char may not be a SIGN char too so we need to check here as
831       * well as above... */
832      nextch();
[fa42426]833      *p_ctype = CTYPE_OMIT;
[5b7c1b7]834      /* no clino reading, so assume 0 with large sd */
835      return (real)0.0;
[dcec245]836   }
837   return HUGE_REAL;
838}
839
[b6de07d]840static void
[85c0078]841warn_readings_differ(int msgno, real diff, int units)
[b6de07d]842{
843   char buf[64];
844   char *p;
[85c0078]845   diff /= get_units_factor(units);
846   sprintf(buf, "%.2f", fabs(diff));
847   for (p = buf; *p; ++p) {
848      if (*p == '.') {
849         char *z = p;
850         while (*++p) {
851            if (*p != '0') z = p + 1;
852         }
853         p = z;
854         break;
[b6de07d]855      }
856   }
[85c0078]857   strcpy(p, get_units_string(units));
[b4fe9fb]858   compile_warning(msgno, buf);
[b6de07d]859}
860
861static bool
[21c226e]862handle_comp_units(void)
[b6de07d]863{
864   bool fNoComp = fTrue;
[21c226e]865   if (VAL(Comp) != HUGE_REAL) {
[b6de07d]866      fNoComp = fFalse;
[21c226e]867      VAL(Comp) *= pcs->units[Q_BEARING];
868      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
[736f7df]869         /* TRANSLATORS: Suspicious means something like 410 degrees or -20
870          * degrees */
[b6de07d]871         compile_warning(/*Suspicious compass reading*/59);
[21c226e]872         VAL(Comp) = mod2pi(VAL(Comp));
[b6de07d]873      }
874   }
[21c226e]875   if (VAL(BackComp) != HUGE_REAL) {
[b6de07d]876      fNoComp = fFalse;
[21c226e]877      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
878      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
[b6de07d]879         /* FIXME: different message for BackComp? */
880         compile_warning(/*Suspicious compass reading*/59);
[21c226e]881         VAL(BackComp) = mod2pi(VAL(BackComp));
[b6de07d]882      }
883   }
884   return fNoComp;
885}
886
887static real
[21c226e]888handle_compass(real *p_var)
[b6de07d]889{
[b4fe9fb]890   real compvar = VAR(Comp);
[21c226e]891   real comp = VAL(Comp);
892   real backcomp = VAL(BackComp);
[b6de07d]893   if (comp != HUGE_REAL) {
894      comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
895      comp -= pcs->z[Q_DECLINATION];
896   }
897   if (backcomp != HUGE_REAL) {
898      backcomp = (backcomp - pcs->z[Q_BACKBEARING])
899              * pcs->sc[Q_BACKBEARING];
900      backcomp -= pcs->z[Q_DECLINATION];
901      backcomp -= M_PI;
902      if (comp != HUGE_REAL) {
903         real diff = comp - backcomp;
904         real adj = fabs(diff) > M_PI ? M_PI : 0;
905         diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
[4dc9899]906         if (sqrd(diff / 3.0) > compvar + VAR(Q_BACKBEARING)) {
907            /* fore and back readings differ by more than 3 sds */
[85c0078]908            /* TRANSLATORS: %s is replaced by the amount the readings disagree
909             * by, e.g. "2.5°" or "3ᵍ". */
910            warn_readings_differ(/*COMPASS reading and BACKCOMPASS reading disagree by %s*/98,
911                                 diff, get_angle_units(Q_BEARING));
[b6de07d]912         }
[b4fe9fb]913         comp = (comp / compvar + backcomp / VAR(BackComp));
914         compvar = (compvar + VAR(BackComp)) / 4;
915         comp *= compvar;
[b6de07d]916         comp += adj;
917      } else {
918         comp = backcomp;
[b4fe9fb]919         compvar = VAR(BackComp);
[b6de07d]920      }
921   }
[b4fe9fb]922   *p_var = compvar;
[b6de07d]923   return comp;
924}
925
[90bb053]926static int
[21c226e]927process_normal(prefix *fr, prefix *to, bool fToFirst,
928               clino_type ctype, clino_type backctype)
[a420b49]929{
[21c226e]930   real tape = VAL(Tape);
931   real clin = VAL(Clino);
932   real backclin = VAL(BackClino);
933
[a420b49]934   real dx, dy, dz;
935   real vx, vy, vz;
936#ifndef NO_COVARIANCES
[cb3d1e2]937   real cxy, cyz, czx;
[a420b49]938#endif
[d1b1380]939
[90bb053]940   bool fNoComp;
[a420b49]941
[107b8bd]942   /* adjusted tape is negative -- probably the calibration is wrong */
943   if (tape < (real)0.0) {
944      /* TRANSLATE different message for topofil? */
945      compile_warning(/*Negative adjusted tape reading*/79);
[647407d]946   }
[d1b1380]947
[21c226e]948   fNoComp = handle_comp_units();
[d1b1380]949
[fa42426]950   if (ctype == CTYPE_READING) {
[93935c7]951      bool range_0_180;
952      real z;
[0b71cfc]953      real diff_from_abs90;
[a420b49]954      clin *= pcs->units[Q_GRADIENT];
[fa42426]955      /* percentage scale */
956      if (pcs->f_clino_percent) clin = atan(clin);
[93935c7]957      /* We want to warn if there's a reading which it would be impossible
958       * to have read from the instrument (e.g. on a -90 to 90 degree scale
959       * you can't read "96" (it's probably a typo for "69").  However, the
960       * gradient reading from a topofil is typically in the range 0 to 180,
961       * with 90 being horizontal.
962       *
963       * Really we should allow the valid range to be specified, but for now
964       * we infer it from the zero error - if this is within 45 degrees of
965       * 90 then we assume the range is 0 to 180.
966       */
967      z = pcs->z[Q_GRADIENT];
968      range_0_180 = (z > M_PI_4 && z < 3*M_PI_4);
[0b71cfc]969      diff_from_abs90 = fabs(clin) - M_PI_2;
970      if (diff_from_abs90 > EPSILON) {
[85c0078]971         if (!range_0_180) {
972            int clino_units = get_angle_units(Q_GRADIENT);
973            const char * units = get_units_string(clino_units);
974            real right_angle = M_PI_2 / get_units_factor(clino_units);
975            /* TRANSLATORS: %.f%s will be replaced with a right angle in the
976             * units currently in use, e.g. "90°" or "100ᵍ".  And "absolute
977             * value" means the reading ignoring the sign (so it might be
978             * < -90° or > 90°. */
979            compile_warning(/*Clino reading over %.f%s (absolute value)*/51,
980                            right_angle, units);
981         }
[27b8b59]982      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
[be97baf]983                 diff_from_abs90 >= -EPSILON) {
984         ctype = CTYPE_INFERPLUMB;
[a420b49]985      }
[93935c7]986      if (range_0_180 && ctype != CTYPE_INFERPLUMB) {
987         /* FIXME: Warning message not ideal... */
[85c0078]988         if (clin < 0.0 || clin - M_PI > EPSILON) {
989            int clino_units = get_angle_units(Q_GRADIENT);
990            const char * units = get_units_string(clino_units);
991            real right_angle = M_PI_2 / get_units_factor(clino_units);
992            compile_warning(/*Clino reading over %.f%s (absolute value)*/51,
993                            right_angle, units);
994         }
[93935c7]995      }
[a420b49]996   }
[cb3d1e2]997
[fa42426]998   if (backctype == CTYPE_READING) {
[b14f44f]999      backclin *= pcs->units[Q_BACKGRADIENT];
[fa42426]1000      /* percentage scale */
1001      if (pcs->f_backclino_percent) backclin = atan(backclin);
[be97baf]1002      if (ctype != CTYPE_READING) {
1003         real diff_from_abs90 = fabs(backclin) - M_PI_2;
1004         if (diff_from_abs90 > EPSILON) {
1005            /* FIXME: different message for BackClino? */
[85c0078]1006            int clino_units = get_angle_units(Q_BACKGRADIENT);
1007            const char * units = get_units_string(clino_units);
1008            real right_angle = M_PI_2 / get_units_factor(clino_units);
1009            compile_warning(/*Clino reading over %.f%s degrees (absolute value)*/51,
1010                            right_angle, units);
[be97baf]1011         } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
1012                    diff_from_abs90 >= -EPSILON) {
1013            backctype = CTYPE_INFERPLUMB;
1014         }
[0b71cfc]1015      }
1016   }
1017
[be97baf]1018   /* un-infer the plumb if the backsight was just a reading */
1019   if (ctype == CTYPE_INFERPLUMB && backctype == CTYPE_READING) {
1020       ctype = CTYPE_READING;
1021   }
1022
[fa42426]1023   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
[736f7df]1024      /* TRANSLATORS: In data with backsights, the user has tried to give a
1025       * plumb for the foresight and a clino reading for the backsight, or
1026       * something similar. */
[ee7511a]1027      compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
[b14f44f]1028      return 0;
[0b71cfc]1029   }
1030
[be97baf]1031   if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB ||
1032       backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) {
[a420b49]1033      /* plumbed */
1034      if (!fNoComp) {
[be97baf]1035         if (ctype == CTYPE_PLUMB ||
1036             (ctype == CTYPE_INFERPLUMB && VAL(Comp) != 0.0) ||
1037             backctype == CTYPE_PLUMB ||
1038             (backctype == CTYPE_INFERPLUMB && VAL(BackComp) != 0.0)) {
1039            /* FIXME: Different message for BackComp? */
[736f7df]1040            /* TRANSLATORS: A "plumbed leg" is one measured using a plumbline
1041             * (a weight on a string).  So the problem here is that the leg is
1042             * vertical, so a compass reading has no meaning! */
[be97baf]1043            compile_warning(/*Compass reading given on plumbed leg*/21);
1044         }
[a420b49]1045      }
1046
1047      dx = dy = (real)0.0;
[fa42426]1048      if (ctype != CTYPE_OMIT) {
1049         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
[b14f44f]1050            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
[ee7511a]1051            compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
[b14f44f]1052            return 0;
[0b71cfc]1053         }
1054         dz = (clin > (real)0.0) ? tape : -tape;
1055      } else {
1056         dz = (backclin < (real)0.0) ? tape : -tape;
1057      }
[a420b49]1058      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1059      vz = var(Q_POS) / 3.0 + VAR(Tape);
[4f8285d]1060#ifndef NO_COVARIANCES
[26a805f]1061      /* Correct values - no covariances in this case! */
1062      cxy = cyz = czx = (real)0.0;
[4f8285d]1063#endif
[a420b49]1064   } else {
[fa42426]1065      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
1066       * or CTYPE_OMIT */
[a420b49]1067      /* clino */
1068      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
[421b7d2]1069      if (fNoComp) {
[736f7df]1070         /* TRANSLATORS: Here "legs" are survey legs, i.e. measurements between
1071          * survey stations. */
[fa42426]1072         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
[a420b49]1073         return 0;
1074      }
1075      if (tape == (real)0.0) {
1076         dx = dy = dz = (real)0.0;
1077         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[4f8285d]1078#ifndef NO_COVARIANCES
[a420b49]1079         cxy = cyz = czx = (real)0.0;
[4f8285d]1080#endif
[d1b1380]1081#if DEBUG_DATAIN_1
[a420b49]1082         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]1083#endif
[a420b49]1084      } else {
[dbb4e19]1085         real sinGcosG;
[0b71cfc]1086         /* take into account variance in LEVEL case */
1087         real var_clin = var(Q_LEVEL);
[b6de07d]1088         real var_comp;
[21c226e]1089         real comp = handle_compass(&var_comp);
[fa42426]1090         /* ctype != CTYPE_READING is LEVEL case */
1091         if (ctype == CTYPE_READING) {
[a420b49]1092            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
[21c226e]1093            var_clin = VAR(Clino);
[0b71cfc]1094         }
[fa42426]1095         if (backctype == CTYPE_READING) {
[b14f44f]1096            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
1097               * pcs->sc[Q_BACKGRADIENT];
[fa42426]1098            if (ctype == CTYPE_READING) {
[be97baf]1099               if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) {
[b14f44f]1100                  /* fore and back readings differ by more than 3 sds */
[85c0078]1101                  /* TRANSLATORS: %s is replaced by the amount the readings disagree
1102                   * by, e.g. "2.5°" or "3ᵍ". */
1103                  warn_readings_differ(/*CLINO reading and BACKCLINO reading disagree by %s*/99,
1104                                       clin + backclin, get_angle_units(Q_GRADIENT));
[b14f44f]1105               }
[21c226e]1106               clin = (clin / var_clin - backclin / VAR(BackClino));
1107               var_clin = (var_clin + VAR(BackClino)) / 4;
[b14f44f]1108               clin *= var_clin;
[0b71cfc]1109            } else {
1110               clin = -backclin;
[21c226e]1111               var_clin = VAR(BackClino);
[0b71cfc]1112            }
1113         }
[d1b1380]1114
1115#if DEBUG_DATAIN
[a420b49]1116         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
[d1b1380]1117#endif
[a420b49]1118         cosG = cos(clin);
1119         LcosG = tape * cosG;
1120         sinB = sin(comp);
1121         cosB = cos(comp);
[d1b1380]1122#if DEBUG_DATAIN_1
[a420b49]1123         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
[d1b1380]1124#endif
[a420b49]1125         dx = LcosG * sinB;
1126         dy = LcosG * cosB;
1127         dz = tape * sin(clin);
[d1b1380]1128/*      printf("%.2f\n",clin); */
1129#if DEBUG_DATAIN_1
[a420b49]1130         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
[d1b1380]1131#endif
[a420b49]1132         dx2 = dx * dx;
1133         L2 = tape * tape;
[21c226e]1134         V = VAR(Tape) / L2;
[a420b49]1135         dy2 = dy * dy;
1136         cosG2 = cosG * cosG;
[dbb4e19]1137         sinGcosG = sin(clin) * cosG;
[a420b49]1138         dz2 = dz * dz;
[0b71cfc]1139         v = dz2 * var_clin;
[5b7c1b7]1140#ifdef NO_COVARIANCES
[0b71cfc]1141         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[a420b49]1142               (.5 + sinB * sinB * cosG2) * v);
[0b71cfc]1143         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[a420b49]1144               (.5 + cosB * cosB * cosG2) * v);
[fa42426]1145         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1146            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1147            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1148         } else {
[0b71cfc]1149            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1150         }
[a420b49]1151         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
[dbb4e19]1152#else
[0b71cfc]1153         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[dbb4e19]1154            (sinB * sinB * v);
[0b71cfc]1155         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[dbb4e19]1156            (cosB * cosB * v);
[fa42426]1157         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1158            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1159            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1160         } else {
[0b71cfc]1161            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1162         }
[647407d]1163         /* usual covariance formulae are fine in no clino case since
[0b71cfc]1164          * dz = 0 so value of var_clin is ignored */
[21c226e]1165         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
[0b71cfc]1166               - var_comp * dx * dy;
[21c226e]1167         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
1168         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
[dbb4e19]1169#if 0
1170         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
1171         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
1172#endif
[4f8285d]1173#endif
[d1b1380]1174#if DEBUG_DATAIN_1
[a420b49]1175         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]1176#endif
[a420b49]1177      }
1178   }
[d1b1380]1179#if DEBUG_DATAIN_1
[a420b49]1180   printf("Just before addleg, vx = %f\n", vx);
[d1b1380]1181#endif
[a420b49]1182   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
[90bb053]1183   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[4f8285d]1184#ifndef NO_COVARIANCES
[2140502]1185                , cyz, czx, cxy
[4f8285d]1186#endif
[2140502]1187                );
[a420b49]1188   return 1;
[d1b1380]1189}
1190
[5a38209]1191static int
[21c226e]1192process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[a420b49]1193{
[21c226e]1194   real tape = VAL(Tape);
1195
[a420b49]1196   real dx, dy, dz;
1197   real vx, vy, vz;
[f795df0]1198#ifndef NO_COVARIANCES
1199   real cxy = 0, cyz = 0, czx = 0;
1200#endif
[a420b49]1201
[21c226e]1202   handle_comp_units();
[a420b49]1203
[a6d094f]1204   /* depth gauge readings increase upwards with default calibration */
[6114207]1205   if (fDepthChange) {
[21c226e]1206      SVX_ASSERT(VAL(FrDepth) == 0.0);
1207      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1208      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1209   } else {
[21c226e]1210      dz = VAL(ToDepth) - VAL(FrDepth);
1211      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1212   }
[a420b49]1213
1214   /* adjusted tape is negative -- probably the calibration is wrong */
1215   if (tape < (real)0.0) {
1216      compile_warning(/*Negative adjusted tape reading*/79);
1217   }
1218
1219   /* check if tape is less than depth change */
1220   if (tape < fabs(dz)) {
[bd1913f]1221      /* FIXME: allow margin of error based on variances? */
[736f7df]1222      /* TRANSLATORS: This means that the data fed in said this.
1223       *
1224       * It could be a gross error (e.g. the decimal point is missing from the
1225       * depth gauge reading) or it could just be due to random error on a near
1226       * vertical leg */
[a420b49]1227      compile_warning(/*Tape reading is less than change in depth*/62);
1228   }
1229
[0a208f9]1230   if (tape == (real)0.0 && dz == 0.0) {
[a420b49]1231      dx = dy = dz = (real)0.0;
1232      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[21c226e]1233   } else if (VAL(Comp) == HUGE_REAL &&
1234              VAL(BackComp) == HUGE_REAL) {
[385b703]1235      /* plumb */
1236      dx = dy = (real)0.0;
1237      if (dz < 0) tape = -tape;
[21c226e]1238      /* FIXME: Should use FrDepth sometimes... */
1239      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1240         / (VAR(Tape) * 2 * VAR(ToDepth));
[385b703]1241      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1242      /* FIXME: Should use FrDepth sometimes... */
1243      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1244                              / (VAR(Tape) + VAR(ToDepth));
[a420b49]1245   } else {
[f795df0]1246      real L2, sinB, cosB, dz2, D2;
[b6de07d]1247      real var_comp;
[21c226e]1248      real comp = handle_compass(&var_comp);
[a420b49]1249      sinB = sin(comp);
1250      cosB = cos(comp);
1251      L2 = tape * tape;
1252      dz2 = dz * dz;
1253      D2 = L2 - dz2;
1254      if (D2 <= (real)0.0) {
[21c226e]1255         /* FIXME: Should use FrDepth sometimes... */
1256         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
[a420b49]1257         dx = dy = (real)0.0;
[f795df0]1258         vx = vy = var(Q_POS) / 3.0;
[21c226e]1259         /* FIXME: Should use FrDepth sometimes... */
1260         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
[f795df0]1261         if (dz > 0) {
[21c226e]1262            /* FIXME: Should use FrDepth sometimes... */
1263            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1264         } else {
[21c226e]1265            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1266         }
[a420b49]1267      } else {
1268         real D = sqrt(D2);
[21c226e]1269         /* FIXME: Should use FrDepth sometimes... */
1270         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
[a420b49]1271         dx = D * sinB;
1272         dy = D * cosB;
[f795df0]1273
1274         vx = var(Q_POS) / 3.0 +
[72095d3]1275            sinB * sinB * F / D2 + var_comp * dy * dy;
[f795df0]1276         vy = var(Q_POS) / 3.0 +
[72095d3]1277            cosB * cosB * F / D2 + var_comp * dx * dx;
[21c226e]1278         /* FIXME: Should use FrDepth sometimes... */
1279         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[f795df0]1280
1281#ifndef NO_COVARIANCES
[72095d3]1282         cxy = sinB * cosB * (F / D2 + var_comp * D2);
[21c226e]1283         /* FIXME: Should use FrDepth sometimes... */
1284         cyz = -2 * VAR(ToDepth) * dy / D;
1285         czx = -2 * VAR(ToDepth) * dx / D;
[f795df0]1286#endif
[a420b49]1287      }
1288   }
[5a38209]1289   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[2140502]1290#ifndef NO_COVARIANCES
[f795df0]1291                , cxy, cyz, czx
[647407d]1292#endif
[2140502]1293                );
[647407d]1294   return 1;
1295}
1296
[5a38209]1297static int
[21c226e]1298process_cartesian(prefix *fr, prefix *to, bool fToFirst)
[5a38209]1299{
[21c226e]1300   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
1301   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
1302   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
[647407d]1303
[21c226e]1304   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
[647407d]1305#ifndef NO_COVARIANCES
[c80bd34]1306                , 0, 0, 0
[647407d]1307#endif
[2140502]1308                );
[647407d]1309   return 1;
1310}
1311
[e2d48d8]1312static void
[5a38209]1313data_cartesian(void)
[647407d]1314{
[90bb053]1315   prefix *fr = NULL, *to = NULL;
[5a38209]1316
1317   bool fMulti = fFalse;
[647407d]1318
[0395657]1319   reading first_stn = End;
1320
1321   reading *ordering;
[647407d]1322
[5a38209]1323   again:
1324
[647407d]1325   for (ordering = pcs->ordering ; ; ordering++) {
1326      skipblanks();
1327      switch (*ordering) {
[4a6a094]1328       case Fr:
[c458cf7]1329         fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[21c226e]1330         if (first_stn == End) first_stn = Fr;
1331         break;
[4a6a094]1332       case To:
[c458cf7]1333         to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[21c226e]1334         if (first_stn == End) first_stn = To;
1335         break;
[5a38209]1336       case Station:
[21c226e]1337         fr = to;
[c458cf7]1338         to = read_prefix(PFX_STATION);
[21c226e]1339         first_stn = To;
1340         break;
1341       case Dx: case Dy: case Dz:
1342         read_reading(*ordering, fFalse);
1343         break;
[647407d]1344       case Ignore:
1345         skipword(); break;
[5a38209]1346       case IgnoreAllAndNewLine:
1347         skipline();
1348         /* fall through */
1349       case Newline:
1350         if (fr != NULL) {
[e2d48d8]1351            if (!process_cartesian(fr, to, first_stn == To))
1352               skipline();
[5a38209]1353         }
1354         fMulti = fTrue;
1355         while (1) {
1356            process_eol();
1357            process_bol();
1358            if (isData(ch)) break;
[ee6a621]1359            if (!isComm(ch)) {
[993454b]1360               push_back(ch);
[e2d48d8]1361               return;
[ee6a621]1362            }
[5a38209]1363         }
1364         break;
[647407d]1365       case IgnoreAll:
1366         skipline();
1367         /* fall through */
1368       case End:
[5a38209]1369         if (!fMulti) {
[e2d48d8]1370            process_cartesian(fr, to, first_stn == To);
[5a38209]1371            process_eol();
[e2d48d8]1372            return;
[5a38209]1373         }
[eef4d8c]1374         do {
[5a38209]1375            process_eol();
1376            process_bol();
[eef4d8c]1377         } while (isComm(ch));
[5a38209]1378         goto again;
[0395657]1379       default: BUG("Unknown reading in ordering");
[647407d]1380      }
1381   }
[5a38209]1382}
[cb3d1e2]1383
[98a2eec]1384static int
[21c226e]1385process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[98a2eec]1386{
[21c226e]1387   real tape = VAL(Tape);
1388
[98a2eec]1389   real dx, dy, dz;
1390   real vx, vy, vz;
1391#ifndef NO_COVARIANCES
1392   real cxy = 0;
1393#endif
1394
[21c226e]1395   handle_comp_units();
[98a2eec]1396
[a6d094f]1397   /* depth gauge readings increase upwards with default calibration */
[6114207]1398   if (fDepthChange) {
[21c226e]1399      SVX_ASSERT(VAL(FrDepth) == 0.0);
1400      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1401      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1402   } else {
[21c226e]1403      dz = VAL(ToDepth) - VAL(FrDepth);
1404      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1405   }
[98a2eec]1406
1407   /* adjusted tape is negative -- probably the calibration is wrong */
1408   if (tape < (real)0.0) {
1409      compile_warning(/*Negative adjusted tape reading*/79);
1410   }
1411
[21c226e]1412   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
[98a2eec]1413      /* plumb */
1414      dx = dy = (real)0.0;
1415      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1416      /* FIXME: Should use FrDepth sometimes... */
1417      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1418   } else {
1419      real sinB, cosB;
[b6de07d]1420      real var_comp;
[21c226e]1421      real comp = handle_compass(&var_comp);
[98a2eec]1422      sinB = sin(comp);
1423      cosB = cos(comp);
1424
1425      dx = tape * sinB;
1426      dy = tape * cosB;
1427
1428      vx = var(Q_POS) / 3.0 +
[21c226e]1429         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
[98a2eec]1430      vy = var(Q_POS) / 3.0 +
[21c226e]1431         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1432      /* FIXME: Should use FrDepth sometimes... */
1433      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1434
1435#ifndef NO_COVARIANCES
[21c226e]1436      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
[98a2eec]1437#endif
1438   }
1439   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1440#ifndef NO_COVARIANCES
1441                , cxy, 0, 0
1442#endif
1443                );
1444   return 1;
1445}
1446
[107b8bd]1447/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1448 * Also handles topofil (fromcount/tocount or count) in place of tape */
[e2d48d8]1449static void
[107b8bd]1450data_normal(void)
[54c4612]1451{
1452   prefix *fr = NULL, *to = NULL;
[107b8bd]1453   reading first_stn = End;
[54c4612]1454
[107b8bd]1455   bool fTopofil = fFalse, fMulti = fFalse;
1456   bool fRev;
[e217d67]1457   clino_type ctype, backctype;
[107b8bd]1458   bool fDepthChange;
[be97baf]1459   unsigned long compass_dat_flags = 0;
[54c4612]1460
1461   reading *ordering;
1462
[21c226e]1463   VAL(Tape) = 0;
[07442af]1464   VAL(Comp) = VAL(BackComp) = HUGE_REAL;
[21c226e]1465   VAL(FrCount) = VAL(ToCount) = 0;
1466   VAL(FrDepth) = VAL(ToDepth) = 0;
[07442af]1467   VAL(Left) = VAL(Right) = VAL(Up) = VAL(Down) = HUGE_REAL;
[54c4612]1468
1469   fRev = fFalse;
[fa42426]1470   ctype = backctype = CTYPE_OMIT;
[6114207]1471   fDepthChange = fFalse;
[54c4612]1472
[107b8bd]1473   /* ordering may omit clino reading, so set up default here */
1474   /* this is also used if clino reading is the omit character */
[21c226e]1475   VAL(Clino) = VAL(BackClino) = 0;
1476
1477   again:
[107b8bd]1478
[dcbcae0]1479   /* We clear these flags in the normal course of events, but if there's an
1480    * error in a reading, we might not, so make sure it has been cleared here.
[710ecc1]1481    */
[dcbcae0]1482   pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
[54c4612]1483   for (ordering = pcs->ordering; ; ordering++) {
1484      skipblanks();
1485      switch (*ordering) {
1486       case Fr:
[710ecc1]1487          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
[107b8bd]1488          if (first_stn == End) first_stn = Fr;
1489          break;
[54c4612]1490       case To:
[710ecc1]1491          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
[107b8bd]1492          if (first_stn == End) first_stn = To;
1493          break;
[54c4612]1494       case Station:
[107b8bd]1495          fr = to;
[c458cf7]1496          to = read_prefix(PFX_STATION);
[107b8bd]1497          first_stn = To;
1498          break;
[ee1ec59]1499       case Dir: {
1500          typedef enum {
1501             DIR_NULL=-1, DIR_FORE, DIR_BACK
1502          } dir_tok;
1503          static sztok dir_tab[] = {
1504             {"B",     DIR_BACK},
1505             {"F",     DIR_FORE},
1506          };
1507          dir_tok tok;
1508          get_token();
1509          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1510          switch (tok) {
1511           case DIR_FORE:
[54c4612]1512             break;
[ee1ec59]1513           case DIR_BACK:
[54c4612]1514             fRev = fTrue;
1515             break;
1516           default:
[da96015]1517             file.lpos += strlen(buffer);
1518             compile_error_skip(-/*Found “%s”, expecting “F” or “B”*/131,
[44bb30d]1519                                buffer);
[54c4612]1520             process_eol();
[e2d48d8]1521             return;
[54c4612]1522          }
1523          break;
[ee1ec59]1524       }
[107b8bd]1525       case Tape:
[21c226e]1526          read_reading(Tape, fFalse);
1527          if (VAL(Tape) < (real)0.0)
[da96015]1528             compile_warning(-/*Negative tape reading*/60);
[107b8bd]1529          break;
1530       case Count:
[21c226e]1531          VAL(FrCount) = VAL(ToCount);
1532          read_reading(ToCount, fFalse);
[b4fe9fb]1533          fTopofil = fTrue;
[107b8bd]1534          break;
1535       case FrCount:
[21c226e]1536          read_reading(FrCount, fFalse);
[107b8bd]1537          break;
1538       case ToCount:
[21c226e]1539          read_reading(ToCount, fFalse);
[107b8bd]1540          fTopofil = fTrue;
1541          break;
[07442af]1542       case Comp: case BackComp:
1543          read_bearing_or_omit(*ordering);
[5b7c1b7]1544          break;
[0a158bc]1545       case Clino: case BackClino: {
1546          reading r = *ordering;
1547          clino_type * p_ctype = (r == Clino ? &ctype : &backctype);
1548          read_reading(r, fTrue);
1549          if (VAL(r) == HUGE_REAL) {
1550             VAL(r) = handle_plumb(p_ctype);
1551             if (VAL(r) != HUGE_REAL) break;
[da96015]1552             compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[4bc4d58]1553             skipline();
[107b8bd]1554             process_eol();
[e2d48d8]1555             return;
[107b8bd]1556          }
[0a158bc]1557          *p_ctype = CTYPE_READING;
[107b8bd]1558          break;
[0a158bc]1559       }
[07442af]1560       case FrDepth: case ToDepth:
1561          read_reading(*ordering, fFalse);
[107b8bd]1562          break;
[54c4612]1563       case Depth:
[21c226e]1564          VAL(FrDepth) = VAL(ToDepth);
1565          read_reading(ToDepth, fFalse);
[54c4612]1566          break;
[6114207]1567       case DepthChange:
1568          fDepthChange = fTrue;
[21c226e]1569          VAL(FrDepth) = 0;
1570          read_reading(ToDepth, fFalse);
[a186573]1571          break;
[44bb30d]1572       case CompassDATComp:
1573          read_bearing_or_omit(Comp);
1574          if (is_compass_NaN(VAL(Comp))) VAL(Comp) = HUGE_REAL;
1575          break;
1576       case CompassDATBackComp:
1577          read_bearing_or_omit(BackComp);
1578          if (is_compass_NaN(VAL(BackComp))) VAL(BackComp) = HUGE_REAL;
1579          break;
[0a158bc]1580       case CompassDATClino: case CompassDATBackClino: {
1581          reading r;
1582          clino_type * p_ctype;
1583          if (*ordering == CompassDATClino) {
1584             r = Clino;
1585             p_ctype = &ctype;
[07442af]1586          } else {
[0a158bc]1587             r = BackClino;
1588             p_ctype = &backctype;
[07442af]1589          }
[0a158bc]1590          read_reading(r, fFalse);
1591          if (is_compass_NaN(VAL(r))) {
1592             VAL(r) = HUGE_REAL;
1593             *p_ctype = CTYPE_OMIT;
[07442af]1594          } else {
[0a158bc]1595             *p_ctype = CTYPE_READING;
[07442af]1596          }
1597          break;
[0a158bc]1598       }
[07442af]1599       case CompassDATLeft: case CompassDATRight:
1600       case CompassDATUp: case CompassDATDown: {
[ee05463]1601          /* FIXME: need to actually make use of these entries! */
[07442af]1602          reading actual = Left + (*ordering - CompassDATLeft);
1603          read_reading(actual, fFalse);
1604          if (VAL(actual) < 0) VAL(actual) = HUGE_REAL;
[44bb30d]1605          break;
[07442af]1606       }
[be97baf]1607       case CompassDATFlags:
1608          if (ch == '#') {
1609             nextch();
1610             if (ch == '|') {
1611                filepos fp;
1612                get_pos(&fp);
1613                nextch();
1614                while (ch >= 'A' && ch <= 'Z') {
1615                   compass_dat_flags |= BIT(ch - 'A');
[dfbd846]1616                   /* We currently understand:
[850fdc4]1617                    *   L (exclude from length)
1618                    *   X (exclude data)
[dfbd846]1619                    * FIXME: but should also handle at least some of:
1620                    *   C (no adjustment) (set all (co)variances to 0?)
1621                    *   P (no plot) (new flag in 3d for "hidden by default"?)
[0580c6a]1622                    */
[be97baf]1623                   nextch();
1624                }
1625                if (ch == '#') {
1626                   nextch();
1627                } else {
1628                   compass_dat_flags = 0;
1629                   set_pos(&fp);
1630                   push_back('|');
1631                   ch = '#';
1632                }
1633             } else {
1634                push_back(ch);
1635                ch = '#';
1636             }
1637          }
[107b8bd]1638          break;
[be97baf]1639       case Ignore:
1640          skipword(); break;
[54c4612]1641       case IgnoreAllAndNewLine:
[107b8bd]1642          skipline();
1643          /* fall through */
[54c4612]1644       case Newline:
[107b8bd]1645          if (fr != NULL) {
1646             int r;
[710ecc1]1647             int save_flags;
[dcbcae0]1648             int implicit_splay;
[21c226e]1649             if (fTopofil)
1650                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1651             /* Note: frdepth == todepth test works regardless of fDepthChange
1652              * (frdepth always zero, todepth is change of depth) and also
1653              * works for STYLE_NORMAL (both remain 0) */
[44bb30d]1654             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
[27b8b59]1655                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1656                process_equate(fr, to);
1657                goto inferred_equate;
1658             }
[e7576f6]1659             if (fRev) {
1660                prefix *t = fr;
[44bb30d]1661                fr = to;
1662                to = t;
[e7576f6]1663             }
[107b8bd]1664             if (fTopofil) {
[21c226e]1665                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1666             } else {
[21c226e]1667                VAL(Tape) *= pcs->units[Q_LENGTH];
1668                VAL(Tape) -= pcs->z[Q_LENGTH];
1669                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1670             }
[dcbcae0]1671             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1672             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1673             save_flags = pcs->flags;
1674             if (implicit_splay) {
[710ecc1]1675                pcs->flags |= BIT(FLAGS_SPLAY);
1676             }
[107b8bd]1677             switch (pcs->style) {
1678              case STYLE_NORMAL:
[21c226e]1679                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1680                                   ctype, backctype);
[107b8bd]1681                break;
1682              case STYLE_DIVING:
[21c226e]1683                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1684                                   fDepthChange);
[107b8bd]1685                break;
1686              case STYLE_CYLPOLAR:
[21c226e]1687                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1688                                     fDepthChange);
[107b8bd]1689                break;
1690              default:
[74a0681]1691                r = 0; /* avoid warning */
[107b8bd]1692                BUG("bad style");
1693             }
[710ecc1]1694             pcs->flags = save_flags;
[107b8bd]1695             if (!r) skipline();
[44bb30d]1696
[e7576f6]1697             /* Swap fr and to back to how they were for next line */
1698             if (fRev) {
1699                prefix *t = fr;
1700                fr = to;
1701                to = t;
1702             }
[107b8bd]1703          }
[21c226e]1704
1705          fRev = fFalse;
1706          ctype = backctype = CTYPE_OMIT;
1707          fDepthChange = fFalse;
1708
1709          /* ordering may omit clino reading, so set up default here */
1710          /* this is also used if clino reading is the omit character */
1711          VAL(Clino) = VAL(BackClino) = 0;
1712
[44bb30d]1713          inferred_equate:
[e7576f6]1714
[107b8bd]1715          fMulti = fTrue;
1716          while (1) {
1717              process_eol();
1718              process_bol();
1719              if (isData(ch)) break;
1720              if (!isComm(ch)) {
1721                 push_back(ch);
[e2d48d8]1722                 return;
[107b8bd]1723              }
1724          }
1725          break;
[54c4612]1726       case IgnoreAll:
[107b8bd]1727          skipline();
1728          /* fall through */
[54c4612]1729       case End:
[107b8bd]1730          if (!fMulti) {
[710ecc1]1731             int save_flags;
[dcbcae0]1732             int implicit_splay;
[be97baf]1733             /* Compass ignore flag is 'X' */
1734             if ((compass_dat_flags & BIT('X' - 'A'))) {
1735                process_eol();
[e2d48d8]1736                return;
[be97baf]1737             }
[107b8bd]1738             if (fRev) {
1739                prefix *t = fr;
1740                fr = to;
1741                to = t;
1742             }
[21c226e]1743             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1744             /* Note: frdepth == todepth test works regardless of fDepthChange
1745              * (frdepth always zero, todepth is change of depth) and also
1746              * works for STYLE_NORMAL (both remain 0) */
[07442af]1747             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
[27b8b59]1748                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1749                process_equate(fr, to);
1750                process_eol();
[e2d48d8]1751                return;
[107b8bd]1752             }
1753             if (fTopofil) {
[21c226e]1754                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1755             } else {
[21c226e]1756                VAL(Tape) *= pcs->units[Q_LENGTH];
1757                VAL(Tape) -= pcs->z[Q_LENGTH];
1758                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1759             }
[dcbcae0]1760             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1761             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
[710ecc1]1762             save_flags = pcs->flags;
[dcbcae0]1763             if (implicit_splay) {
[710ecc1]1764                pcs->flags |= BIT(FLAGS_SPLAY);
1765             }
[850fdc4]1766             if ((compass_dat_flags & BIT('L' - 'A'))) {
1767                /* 'L' means "exclude from length" - map this to Survex's
1768                 * FLAGS_DUPLICATE. */
1769                pcs->flags |= BIT(FLAGS_DUPLICATE);
1770             }
[107b8bd]1771             switch (pcs->style) {
1772              case STYLE_NORMAL:
[e2d48d8]1773                process_normal(fr, to, (first_stn == To) ^ fRev,
1774                               ctype, backctype);
[107b8bd]1775                break;
1776              case STYLE_DIVING:
[e2d48d8]1777                process_diving(fr, to, (first_stn == To) ^ fRev,
1778                               fDepthChange);
[107b8bd]1779                break;
1780              case STYLE_CYLPOLAR:
[e2d48d8]1781                process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1782                                 fDepthChange);
[107b8bd]1783                break;
1784              default:
1785                BUG("bad style");
1786             }
[710ecc1]1787             pcs->flags = save_flags;
[ee05463]1788
[107b8bd]1789             process_eol();
[e2d48d8]1790             return;
[107b8bd]1791          }
1792          do {
1793             process_eol();
1794             process_bol();
1795          } while (isComm(ch));
1796          goto again;
1797       default:
1798          BUG("Unknown reading in ordering");
[54c4612]1799      }
1800   }
1801}
1802
[ee05463]1803static int
1804process_lrud(prefix *stn)
1805{
1806   SVX_ASSERT(next_lrud);
1807   lrud * xsect = osnew(lrud);
1808   xsect->stn = stn;
1809   xsect->l = (VAL(Left) * pcs->units[Q_LEFT] - pcs->z[Q_LEFT]) * pcs->sc[Q_LEFT];
1810   xsect->r = (VAL(Right) * pcs->units[Q_RIGHT] - pcs->z[Q_RIGHT]) * pcs->sc[Q_RIGHT];
1811   xsect->u = (VAL(Up) * pcs->units[Q_UP] - pcs->z[Q_UP]) * pcs->sc[Q_UP];
1812   xsect->d = (VAL(Down) * pcs->units[Q_DOWN] - pcs->z[Q_DOWN]) * pcs->sc[Q_DOWN];
[a6301a1]1813   xsect->meta = pcs->meta;
1814   if (pcs->meta) ++pcs->meta->ref_count;
[ee05463]1815   xsect->next = NULL;
1816   *next_lrud = xsect;
1817   next_lrud = &(xsect->next);
1818
1819   return 1;
1820}
1821
[e2d48d8]1822static void
[ee05463]1823data_passage(void)
1824{
1825   prefix *stn = NULL;
1826   reading *ordering;
1827
1828   for (ordering = pcs->ordering ; ; ordering++) {
1829      skipblanks();
1830      switch (*ordering) {
1831       case Station:
[c458cf7]1832         stn = read_prefix(PFX_STATION);
[ee05463]1833         break;
1834       case Left: case Right: case Up: case Down:
[89d7d29]1835         read_reading(*ordering, fTrue);
1836         if (VAL(*ordering) == HUGE_REAL) {
1837            if (!isOmit(ch)) {
[da96015]1838               compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[4bc4d58]1839            } else {
1840               nextch();
[89d7d29]1841            }
1842            VAL(*ordering) = -1;
1843         }
[ee05463]1844         break;
1845       case Ignore:
1846         skipword(); break;
1847       case IgnoreAll:
1848         skipline();
1849         /* fall through */
1850       case End: {
[e2d48d8]1851         process_lrud(stn);
[ee05463]1852         process_eol();
[e2d48d8]1853         return;
[ee05463]1854       }
1855       default: BUG("Unknown reading in ordering");
1856      }
1857   }
1858}
1859
[5a38209]1860static int
1861process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1862{
1863   nosurveylink *link;
[647407d]1864
[f15cde77]1865   /* Suppress "unused fixed point" warnings for these stations */
1866   fr->sflags |= BIT(SFLAGS_USED);
1867   to->sflags |= BIT(SFLAGS_USED);
[838a602a]1868
[647407d]1869   /* add to linked list which is dealt with after network is solved */
1870   link = osnew(nosurveylink);
[5a38209]1871   if (fToFirst) {
[90bb053]1872      link->to = StnFromPfx(to);
1873      link->fr = StnFromPfx(fr);
[0395657]1874   } else {
[90bb053]1875      link->fr = StnFromPfx(fr);
1876      link->to = StnFromPfx(to);
[0395657]1877   }
[eb5aea0]1878   link->flags = pcs->flags | (STYLE_NOSURVEY << FLAGS_STYLE_BIT0);
[ee05463]1879   link->meta = pcs->meta;
1880   if (pcs->meta) ++pcs->meta->ref_count;
[647407d]1881   link->next = nosurveyhead;
1882   nosurveyhead = link;
[a420b49]1883   return 1;
[d1b1380]1884}
[50f6901]1885
[e2d48d8]1886static void
[5a38209]1887data_nosurvey(void)
1888{
1889   prefix *fr = NULL, *to = NULL;
1890
1891   bool fMulti = fFalse;
1892
1893   reading first_stn = End;
1894
1895   reading *ordering;
1896
1897   again:
1898
1899   for (ordering = pcs->ordering ; ; ordering++) {
1900      skipblanks();
1901      switch (*ordering) {
1902       case Fr:
[c458cf7]1903          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[5a38209]1904          if (first_stn == End) first_stn = Fr;
1905          break;
1906       case To:
[c458cf7]1907          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[5a38209]1908          if (first_stn == End) first_stn = To;
1909          break;
1910       case Station:
1911          fr = to;
[c458cf7]1912          to = read_prefix(PFX_STATION);
[5a38209]1913          first_stn = To;
1914          break;
1915       case Ignore:
1916         skipword(); break;
1917       case IgnoreAllAndNewLine:
1918         skipline();
1919         /* fall through */
1920       case Newline:
1921         if (fr != NULL) {
[e2d48d8]1922            if (!process_nosurvey(fr, to, first_stn == To))
1923               skipline();
[5a38209]1924         }
[838a602a]1925         if (ordering[1] == End) {
1926            do {
1927               process_eol();
1928               process_bol();
1929            } while (isComm(ch));
[ee6a621]1930            if (!isData(ch)) {
[993454b]1931               push_back(ch);
[e2d48d8]1932               return;
[ee6a621]1933            }
[838a602a]1934            goto again;
1935         }
[5a38209]1936         fMulti = fTrue;
1937         while (1) {
1938            process_eol();
1939            process_bol();
1940            if (isData(ch)) break;
[ee6a621]1941            if (!isComm(ch)) {
[993454b]1942               push_back(ch);
[e2d48d8]1943               return;
[ee6a621]1944            }
[5a38209]1945         }
1946         break;
1947       case IgnoreAll:
1948         skipline();
1949         /* fall through */
1950       case End:
1951         if (!fMulti) {
[e2d48d8]1952            (void)process_nosurvey(fr, to, first_stn == To);
[5a38209]1953            process_eol();
[e2d48d8]1954            return;
[5a38209]1955         }
[eef4d8c]1956         do {
[5a38209]1957            process_eol();
1958            process_bol();
[eef4d8c]1959         } while (isComm(ch));
[5a38209]1960         goto again;
1961       default: BUG("Unknown reading in ordering");
1962      }
1963   }
1964}
1965
[50f6901]1966/* totally ignore a line of survey data */
[e2d48d8]1967static void
[50f6901]1968data_ignore(void)
1969{
1970   skipline();
[ee6a621]1971   process_eol();
[50f6901]1972}
Note: See TracBrowser for help on using the repository browser.