source: git/src/datain.c @ 0c323ec

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 0c323ec was 0040469d, checked in by Olly Betts <olly@…>, 11 years ago

src/datain.c: Tiny code simplification.

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