source: git/src/datain.c @ a786810

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since a786810 was dda0ca7, checked in by Olly Betts <olly@…>, 9 years ago

src/datain.c,src/netbits.c,src/netbits.h,tests/: If the same leg is
repeated consecutively, average the readings and treat as a single
leg.

  • 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
[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
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         }
[dda0ca7]620         clear_last_leg();
[be97baf]621      }
622      {
623         settings *pcsParent = pcs->next;
624         SVX_ASSERT(pcsParent);
625         pcs->ordering = NULL;
626         free_settings(pcs);
627         pcs = pcsParent;
[44bb30d]628      }
[be97baf]629   } else if (fmt == FMT_MAK) {
630      nextch_handling_eol();
631      while (!feof(file.fh) && !ferror(file.fh)) {
632         if (ch == '#') {
633            /* include a file */
634            int ch_store;
[b4fe9fb]635            char *dat_pth = path_from_fnm(file.filename);
636            char *dat_fnm = NULL;
637            int dat_fnm_len;
[be97baf]638            nextch_handling_eol();
639            while (ch != ',' && ch != ';' && ch != EOF) {
640               while (isEol(ch)) process_eol();
[63dc4eb]641               s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
[be97baf]642               nextch_handling_eol();
643            }
644            while (ch != ';' && ch != EOF) {
645               prefix *name;
646               nextch_handling_eol();
[c458cf7]647               name = read_prefix(PFX_STATION|PFX_OPT);
[be97baf]648               if (name) {
649                  skipblanks();
650                  if (ch == '[') {
651                     /* fixed pt */
652                     node *stn;
653                     real x, y, z;
654                     name->sflags |= BIT(SFLAGS_FIXED);
655                     nextch_handling_eol();
656                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
657                            ch != '.' && ch != ']' && ch != EOF) {
658                        nextch_handling_eol();
659                     }
660                     x = read_numeric(fFalse, NULL);
661                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
662                            ch != '.' && ch != ']' && ch != EOF) {
663                        nextch_handling_eol();
664                     }
665                     y = read_numeric(fFalse, NULL);
666                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
667                            ch != '.' && ch != ']' && ch != EOF) {
668                        nextch_handling_eol();
669                     }
670                     z = read_numeric(fFalse, NULL);
671                     stn = StnFromPfx(name);
672                     if (!fixed(stn)) {
673                        POS(stn, 0) = x;
674                        POS(stn, 1) = y;
675                        POS(stn, 2) = z;
676                        fix(stn);
677                     } else {
678                        if (x != POS(stn, 0) || y != POS(stn, 1) ||
679                            z != POS(stn, 2)) {
680                           compile_error(/*Station already fixed or equated to a fixed point*/46);
681                        } else {
682                           compile_warning(/*Station already fixed at the same coordinates*/55);
683                        }
684                     }
685                     while (ch != ']' && ch != EOF) nextch_handling_eol();
686                     if (ch == ']') {
687                        nextch_handling_eol();
688                        skipblanks();
689                     }
690                  } else {
691                     /* FIXME: link station - ignore for now */
692                     /* FIXME: perhaps issue warning? */
693                  }
694                  while (ch != ',' && ch != ';' && ch != EOF)
695                     nextch_handling_eol();
696               }
697            }
[b4fe9fb]698            if (dat_fnm) {
[be97baf]699               ch_store = ch;
[b4fe9fb]700               data_file(dat_pth, dat_fnm);
[be97baf]701               ch = ch_store;
[b4fe9fb]702               osfree(dat_fnm);
[be97baf]703            }
704         } else {
705            /* FIXME: also check for % and $ later */
706            nextch_handling_eol();
707         }
708      }
709      {
710         settings *pcsParent = pcs->next;
711         SVX_ASSERT(pcsParent);
712         free_settings(pcs);
713         pcs = pcsParent;
[44bb30d]714      }
[be97baf]715   } else {
716      while (!feof(file.fh) && !ferror(file.fh)) {
717         if (!process_non_data_line()) {
718            f_export_ok = fFalse;
719            switch (pcs->style) {
720             case STYLE_NORMAL:
721             case STYLE_DIVING:
722             case STYLE_CYLPOLAR:
[e2d48d8]723               data_normal();
[be97baf]724               break;
725             case STYLE_CARTESIAN:
[e2d48d8]726               data_cartesian();
[be97baf]727               break;
[ee05463]728             case STYLE_PASSAGE:
[e2d48d8]729               data_passage();
[ee05463]730               break;
[be97baf]731             case STYLE_NOSURVEY:
[e2d48d8]732               data_nosurvey();
[be97baf]733               break;
734             case STYLE_IGNORE:
[e2d48d8]735               data_ignore();
[be97baf]736               break;
737             default:
738               BUG("bad style");
739            }
740         }
[647407d]741      }
[dda0ca7]742      clear_last_leg();
[a420b49]743   }
744
[932f7e9]745   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
746    * including file */
747   f_export_ok = fFalse;
748
[a420b49]749#ifndef NO_PERCENTAGE
750   if (fPercent) putnl();
[4f8285d]751#endif
[d1b1380]752
[47c7a94]753   if (pcs->begin_lineno) {
[0e867ba6]754      error_in_file(file.filename, pcs->begin_lineno,
755                    /*BEGIN with no matching END in this file*/23);
[47c7a94]756      /* Implicitly close any unclosed BEGINs from this file */
757      do {
758         settings *pcsParent = pcs->next;
[4c07c51]759         SVX_ASSERT(pcsParent);
[647407d]760         free_settings(pcs);
[47c7a94]761         pcs = pcsParent;
762      } while (pcs->begin_lineno);
763   }
764
765   pcs->begin_lineno = begin_lineno_store;
[cb3d1e2]766
[22c9877]767   if (ferror(file.fh))
[bfe1242]768      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
[cb3d1e2]769
[22c9877]770   (void)fclose(file.fh);
[7f08c83]771
[22c9877]772   file = file_store;
[4d9eecd]773
774   /* don't free this - it may be pointed to by prefix.file */
775   /* osfree(file.filename); */
[a420b49]776}
[d1b1380]777
[b6de07d]778static real
779mod2pi(real a)
780{
781   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
782}
783
[dcec245]784static real
[5b7c1b7]785handle_plumb(clino_type *p_ctype)
[dcec245]786{
787   typedef enum {
788      CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
789   } clino_tok;
790   static sztok clino_tab[] = {
791      {"D",     CLINO_DOWN},
792      {"DOWN",  CLINO_DOWN},
793      {"H",     CLINO_LEVEL},
794      {"LEVEL", CLINO_LEVEL},
795      {"U",     CLINO_UP},
796      {"UP",    CLINO_UP},
797      {NULL,    CLINO_NULL}
798   };
[5b7c1b7]799   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
[dcec245]800   clino_tok tok;
801
802   skipblanks();
803   if (isalpha(ch)) {
[c80bd34]804      filepos fp;
805      get_pos(&fp);
[dcec245]806      get_token();
807      tok = match_tok(clino_tab, TABSIZE(clino_tab));
808      if (tok != CLINO_NULL) {
[be97baf]809         *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
[dcec245]810         return clinos[tok];
811      }
[c80bd34]812      set_pos(&fp);
[dcec245]813   } else if (isSign(ch)) {
814      int chOld = ch;
815      nextch();
816      if (toupper(ch) == 'V') {
817         nextch();
[fa42426]818         *p_ctype = CTYPE_PLUMB;
[bceebf4]819         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
[dcec245]820      }
821
[bd1913f]822      if (isOmit(chOld)) {
[fa42426]823         *p_ctype = CTYPE_OMIT;
[dcec245]824         /* no clino reading, so assume 0 with large sd */
825         return (real)0.0;
826      }
[5b7c1b7]827   } else if (isOmit(ch)) {
828      /* OMIT char may not be a SIGN char too so we need to check here as
829       * well as above... */
830      nextch();
[fa42426]831      *p_ctype = CTYPE_OMIT;
[5b7c1b7]832      /* no clino reading, so assume 0 with large sd */
833      return (real)0.0;
[dcec245]834   }
835   return HUGE_REAL;
836}
837
[b6de07d]838static void
[b4fe9fb]839warn_readings_differ(int msgno, real diff)
[b6de07d]840{
841   char buf[64];
842   char *p;
843   sprintf(buf, "%.2f", deg(fabs(diff)));
844   p = strchr(buf, '.');
845   if (p) {
846      char *z = p;
847      while (*++p) {
848         if (*p != '0') z = p + 1;
849      }
[0040469d]850      *z = '\0';
[b6de07d]851   }
[b4fe9fb]852   compile_warning(msgno, buf);
[b6de07d]853}
854
855static bool
[21c226e]856handle_comp_units(void)
[b6de07d]857{
858   bool fNoComp = fTrue;
[21c226e]859   if (VAL(Comp) != HUGE_REAL) {
[b6de07d]860      fNoComp = fFalse;
[21c226e]861      VAL(Comp) *= pcs->units[Q_BEARING];
862      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
[736f7df]863         /* TRANSLATORS: Suspicious means something like 410 degrees or -20
864          * degrees */
[b6de07d]865         compile_warning(/*Suspicious compass reading*/59);
[21c226e]866         VAL(Comp) = mod2pi(VAL(Comp));
[b6de07d]867      }
868   }
[21c226e]869   if (VAL(BackComp) != HUGE_REAL) {
[b6de07d]870      fNoComp = fFalse;
[21c226e]871      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
872      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
[b6de07d]873         /* FIXME: different message for BackComp? */
874         compile_warning(/*Suspicious compass reading*/59);
[21c226e]875         VAL(BackComp) = mod2pi(VAL(BackComp));
[b6de07d]876      }
877   }
878   return fNoComp;
879}
880
881static real
[21c226e]882handle_compass(real *p_var)
[b6de07d]883{
[b4fe9fb]884   real compvar = VAR(Comp);
[21c226e]885   real comp = VAL(Comp);
886   real backcomp = VAL(BackComp);
[b6de07d]887   if (comp != HUGE_REAL) {
888      comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
889      comp -= pcs->z[Q_DECLINATION];
890   }
891   if (backcomp != HUGE_REAL) {
892      backcomp = (backcomp - pcs->z[Q_BACKBEARING])
893              * pcs->sc[Q_BACKBEARING];
894      backcomp -= pcs->z[Q_DECLINATION];
895      backcomp -= M_PI;
896      if (comp != HUGE_REAL) {
897         real diff = comp - backcomp;
898         real adj = fabs(diff) > M_PI ? M_PI : 0;
899         diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
[b4fe9fb]900         if (sqrd(diff / 2.0) > compvar + VAR(Q_BACKBEARING)) {
[770157e]901            /* fore and back readings differ by more than 2 sds */
[736f7df]902            /* TRANSLATORS: Degrees are the angular measurement where there are
903             * 360 in a full circle. */
[ee7511a]904            warn_readings_differ(/*COMPASS reading and BACKCOMPASS reading disagree by %s degrees*/98, diff);
[b6de07d]905         }
[b4fe9fb]906         comp = (comp / compvar + backcomp / VAR(BackComp));
907         compvar = (compvar + VAR(BackComp)) / 4;
908         comp *= compvar;
[b6de07d]909         comp += adj;
910      } else {
911         comp = backcomp;
[b4fe9fb]912         compvar = VAR(BackComp);
[b6de07d]913      }
914   }
[b4fe9fb]915   *p_var = compvar;
[b6de07d]916   return comp;
917}
918
[90bb053f]919static int
[21c226e]920process_normal(prefix *fr, prefix *to, bool fToFirst,
921               clino_type ctype, clino_type backctype)
[a420b49]922{
[21c226e]923   real tape = VAL(Tape);
924   real clin = VAL(Clino);
925   real backclin = VAL(BackClino);
926
[a420b49]927   real dx, dy, dz;
928   real vx, vy, vz;
929#ifndef NO_COVARIANCES
[cb3d1e2]930   real cxy, cyz, czx;
[a420b49]931#endif
[d1b1380]932
[90bb053f]933   bool fNoComp;
[a420b49]934
[107b8bd]935   /* adjusted tape is negative -- probably the calibration is wrong */
936   if (tape < (real)0.0) {
937      /* TRANSLATE different message for topofil? */
938      compile_warning(/*Negative adjusted tape reading*/79);
[647407d]939   }
[d1b1380]940
[21c226e]941   fNoComp = handle_comp_units();
[d1b1380]942
[fa42426]943   if (ctype == CTYPE_READING) {
[93935c7]944      bool range_0_180;
945      real z;
[0b71cfc]946      real diff_from_abs90;
[a420b49]947      clin *= pcs->units[Q_GRADIENT];
[fa42426]948      /* percentage scale */
949      if (pcs->f_clino_percent) clin = atan(clin);
[93935c7]950      /* We want to warn if there's a reading which it would be impossible
951       * to have read from the instrument (e.g. on a -90 to 90 degree scale
952       * you can't read "96" (it's probably a typo for "69").  However, the
953       * gradient reading from a topofil is typically in the range 0 to 180,
954       * with 90 being horizontal.
955       *
956       * Really we should allow the valid range to be specified, but for now
957       * we infer it from the zero error - if this is within 45 degrees of
958       * 90 then we assume the range is 0 to 180.
959       */
960      z = pcs->z[Q_GRADIENT];
961      range_0_180 = (z > M_PI_4 && z < 3*M_PI_4);
[0b71cfc]962      diff_from_abs90 = fabs(clin) - M_PI_2;
963      if (diff_from_abs90 > EPSILON) {
[93935c7]964         if (!range_0_180)
[736f7df]965            /* TRANSLATORS: Degrees are the angular measurement where there are
966             * 360 in a full circle. */
[93935c7]967            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
[27b8b59]968      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
[be97baf]969                 diff_from_abs90 >= -EPSILON) {
970         ctype = CTYPE_INFERPLUMB;
[a420b49]971      }
[93935c7]972      if (range_0_180 && ctype != CTYPE_INFERPLUMB) {
973         /* FIXME: Warning message not ideal... */
974         if (clin < 0.0 || clin - M_PI > EPSILON)
975            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
976      }
[a420b49]977   }
[cb3d1e2]978
[fa42426]979   if (backctype == CTYPE_READING) {
[b14f44f]980      backclin *= pcs->units[Q_BACKGRADIENT];
[fa42426]981      /* percentage scale */
982      if (pcs->f_backclino_percent) backclin = atan(backclin);
[be97baf]983      if (ctype != CTYPE_READING) {
984         real diff_from_abs90 = fabs(backclin) - M_PI_2;
985         if (diff_from_abs90 > EPSILON) {
986            /* FIXME: different message for BackClino? */
987            compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
988         } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
989                    diff_from_abs90 >= -EPSILON) {
990            backctype = CTYPE_INFERPLUMB;
991         }
[0b71cfc]992      }
993   }
994
[be97baf]995   /* un-infer the plumb if the backsight was just a reading */
996   if (ctype == CTYPE_INFERPLUMB && backctype == CTYPE_READING) {
997       ctype = CTYPE_READING;
998   }
999
[fa42426]1000   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
[736f7df]1001      /* TRANSLATORS: In data with backsights, the user has tried to give a
1002       * plumb for the foresight and a clino reading for the backsight, or
1003       * something similar. */
[ee7511a]1004      compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
[b14f44f]1005      return 0;
[0b71cfc]1006   }
1007
[be97baf]1008   if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB ||
1009       backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) {
[a420b49]1010      /* plumbed */
1011      if (!fNoComp) {
[be97baf]1012         if (ctype == CTYPE_PLUMB ||
1013             (ctype == CTYPE_INFERPLUMB && VAL(Comp) != 0.0) ||
1014             backctype == CTYPE_PLUMB ||
1015             (backctype == CTYPE_INFERPLUMB && VAL(BackComp) != 0.0)) {
1016            /* FIXME: Different message for BackComp? */
[736f7df]1017            /* TRANSLATORS: A "plumbed leg" is one measured using a plumbline
1018             * (a weight on a string).  So the problem here is that the leg is
1019             * vertical, so a compass reading has no meaning! */
[be97baf]1020            compile_warning(/*Compass reading given on plumbed leg*/21);
1021         }
[a420b49]1022      }
1023
1024      dx = dy = (real)0.0;
[fa42426]1025      if (ctype != CTYPE_OMIT) {
1026         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
[b14f44f]1027            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
[ee7511a]1028            compile_error_skip(/*CLINO and BACKCLINO readings must be of the same type*/84);
[b14f44f]1029            return 0;
[0b71cfc]1030         }
1031         dz = (clin > (real)0.0) ? tape : -tape;
1032      } else {
1033         dz = (backclin < (real)0.0) ? tape : -tape;
1034      }
[a420b49]1035      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1036      vz = var(Q_POS) / 3.0 + VAR(Tape);
[4f8285d]1037#ifndef NO_COVARIANCES
[26a805f]1038      /* Correct values - no covariances in this case! */
1039      cxy = cyz = czx = (real)0.0;
[4f8285d]1040#endif
[a420b49]1041   } else {
[fa42426]1042      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
1043       * or CTYPE_OMIT */
[a420b49]1044      /* clino */
1045      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
[421b7d2]1046      if (fNoComp) {
[736f7df]1047         /* TRANSLATORS: Here "legs" are survey legs, i.e. measurements between
1048          * survey stations. */
[fa42426]1049         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
[a420b49]1050         return 0;
1051      }
1052      if (tape == (real)0.0) {
1053         dx = dy = dz = (real)0.0;
1054         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[4f8285d]1055#ifndef NO_COVARIANCES
[a420b49]1056         cxy = cyz = czx = (real)0.0;
[4f8285d]1057#endif
[d1b1380]1058#if DEBUG_DATAIN_1
[a420b49]1059         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]1060#endif
[a420b49]1061      } else {
[dbb4e19]1062         real sinGcosG;
[0b71cfc]1063         /* take into account variance in LEVEL case */
1064         real var_clin = var(Q_LEVEL);
[b6de07d]1065         real var_comp;
[21c226e]1066         real comp = handle_compass(&var_comp);
[fa42426]1067         /* ctype != CTYPE_READING is LEVEL case */
1068         if (ctype == CTYPE_READING) {
[a420b49]1069            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
[21c226e]1070            var_clin = VAR(Clino);
[0b71cfc]1071         }
[fa42426]1072         if (backctype == CTYPE_READING) {
[b14f44f]1073            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
1074               * pcs->sc[Q_BACKGRADIENT];
[fa42426]1075            if (ctype == CTYPE_READING) {
[be97baf]1076               if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) {
[b14f44f]1077                  /* fore and back readings differ by more than 3 sds */
[736f7df]1078                  /* TRANSLATORS: Degrees are the angular measurement where
1079                   * there are 360 in a full circle. */
[ee7511a]1080                  warn_readings_differ(/*CLINO reading and BACKCLINO reading disagree by %s degrees*/99, clin + backclin);
[b14f44f]1081               }
[21c226e]1082               clin = (clin / var_clin - backclin / VAR(BackClino));
1083               var_clin = (var_clin + VAR(BackClino)) / 4;
[b14f44f]1084               clin *= var_clin;
[0b71cfc]1085            } else {
1086               clin = -backclin;
[21c226e]1087               var_clin = VAR(BackClino);
[0b71cfc]1088            }
1089         }
[d1b1380]1090
1091#if DEBUG_DATAIN
[a420b49]1092         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
[d1b1380]1093#endif
[a420b49]1094         cosG = cos(clin);
1095         LcosG = tape * cosG;
1096         sinB = sin(comp);
1097         cosB = cos(comp);
[d1b1380]1098#if DEBUG_DATAIN_1
[a420b49]1099         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
[d1b1380]1100#endif
[a420b49]1101         dx = LcosG * sinB;
1102         dy = LcosG * cosB;
1103         dz = tape * sin(clin);
[d1b1380]1104/*      printf("%.2f\n",clin); */
1105#if DEBUG_DATAIN_1
[a420b49]1106         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
[d1b1380]1107#endif
[a420b49]1108         dx2 = dx * dx;
1109         L2 = tape * tape;
[21c226e]1110         V = VAR(Tape) / L2;
[a420b49]1111         dy2 = dy * dy;
1112         cosG2 = cosG * cosG;
[dbb4e19]1113         sinGcosG = sin(clin) * cosG;
[a420b49]1114         dz2 = dz * dz;
[0b71cfc]1115         v = dz2 * var_clin;
[5b7c1b7]1116#ifdef NO_COVARIANCES
[0b71cfc]1117         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[a420b49]1118               (.5 + sinB * sinB * cosG2) * v);
[0b71cfc]1119         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[a420b49]1120               (.5 + cosB * cosB * cosG2) * v);
[fa42426]1121         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1122            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1123            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1124         } else {
[0b71cfc]1125            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1126         }
[a420b49]1127         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
[dbb4e19]1128#else
[0b71cfc]1129         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[dbb4e19]1130            (sinB * sinB * v);
[0b71cfc]1131         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[dbb4e19]1132            (cosB * cosB * v);
[fa42426]1133         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1134            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1135            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1136         } else {
[0b71cfc]1137            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1138         }
[647407d]1139         /* usual covariance formulae are fine in no clino case since
[0b71cfc]1140          * dz = 0 so value of var_clin is ignored */
[21c226e]1141         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
[0b71cfc]1142               - var_comp * dx * dy;
[21c226e]1143         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
1144         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
[dbb4e19]1145#if 0
1146         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
1147         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
1148#endif
[4f8285d]1149#endif
[d1b1380]1150#if DEBUG_DATAIN_1
[a420b49]1151         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]1152#endif
[a420b49]1153      }
1154   }
[d1b1380]1155#if DEBUG_DATAIN_1
[a420b49]1156   printf("Just before addleg, vx = %f\n", vx);
[d1b1380]1157#endif
[a420b49]1158   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
[90bb053f]1159   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[4f8285d]1160#ifndef NO_COVARIANCES
[2140502]1161                , cyz, czx, cxy
[4f8285d]1162#endif
[2140502]1163                );
[a420b49]1164   return 1;
[d1b1380]1165}
1166
[5a38209]1167static int
[21c226e]1168process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[a420b49]1169{
[21c226e]1170   real tape = VAL(Tape);
1171
[a420b49]1172   real dx, dy, dz;
1173   real vx, vy, vz;
[f795df0]1174#ifndef NO_COVARIANCES
1175   real cxy = 0, cyz = 0, czx = 0;
1176#endif
[a420b49]1177
[21c226e]1178   handle_comp_units();
[a420b49]1179
[a6d094f]1180   /* depth gauge readings increase upwards with default calibration */
[6114207]1181   if (fDepthChange) {
[21c226e]1182      SVX_ASSERT(VAL(FrDepth) == 0.0);
1183      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1184      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1185   } else {
[21c226e]1186      dz = VAL(ToDepth) - VAL(FrDepth);
1187      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1188   }
[a420b49]1189
1190   /* adjusted tape is negative -- probably the calibration is wrong */
1191   if (tape < (real)0.0) {
1192      compile_warning(/*Negative adjusted tape reading*/79);
1193   }
1194
1195   /* check if tape is less than depth change */
1196   if (tape < fabs(dz)) {
[bd1913f]1197      /* FIXME: allow margin of error based on variances? */
[736f7df]1198      /* TRANSLATORS: This means that the data fed in said this.
1199       *
1200       * It could be a gross error (e.g. the decimal point is missing from the
1201       * depth gauge reading) or it could just be due to random error on a near
1202       * vertical leg */
[a420b49]1203      compile_warning(/*Tape reading is less than change in depth*/62);
1204   }
1205
[0a208f9]1206   if (tape == (real)0.0 && dz == 0.0) {
[a420b49]1207      dx = dy = dz = (real)0.0;
1208      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[21c226e]1209   } else if (VAL(Comp) == HUGE_REAL &&
1210              VAL(BackComp) == HUGE_REAL) {
[385b703]1211      /* plumb */
1212      dx = dy = (real)0.0;
1213      if (dz < 0) tape = -tape;
[21c226e]1214      /* FIXME: Should use FrDepth sometimes... */
1215      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1216         / (VAR(Tape) * 2 * VAR(ToDepth));
[385b703]1217      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1218      /* FIXME: Should use FrDepth sometimes... */
1219      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1220                              / (VAR(Tape) + VAR(ToDepth));
[a420b49]1221   } else {
[f795df0]1222      real L2, sinB, cosB, dz2, D2;
[b6de07d]1223      real var_comp;
[21c226e]1224      real comp = handle_compass(&var_comp);
[a420b49]1225      sinB = sin(comp);
1226      cosB = cos(comp);
1227      L2 = tape * tape;
1228      dz2 = dz * dz;
1229      D2 = L2 - dz2;
1230      if (D2 <= (real)0.0) {
[21c226e]1231         /* FIXME: Should use FrDepth sometimes... */
1232         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
[a420b49]1233         dx = dy = (real)0.0;
[f795df0]1234         vx = vy = var(Q_POS) / 3.0;
[21c226e]1235         /* FIXME: Should use FrDepth sometimes... */
1236         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
[f795df0]1237         if (dz > 0) {
[21c226e]1238            /* FIXME: Should use FrDepth sometimes... */
1239            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1240         } else {
[21c226e]1241            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1242         }
[a420b49]1243      } else {
1244         real D = sqrt(D2);
[21c226e]1245         /* FIXME: Should use FrDepth sometimes... */
1246         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
[a420b49]1247         dx = D * sinB;
1248         dy = D * cosB;
[f795df0]1249
1250         vx = var(Q_POS) / 3.0 +
[72095d3]1251            sinB * sinB * F / D2 + var_comp * dy * dy;
[f795df0]1252         vy = var(Q_POS) / 3.0 +
[72095d3]1253            cosB * cosB * F / D2 + var_comp * dx * dx;
[21c226e]1254         /* FIXME: Should use FrDepth sometimes... */
1255         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[f795df0]1256
1257#ifndef NO_COVARIANCES
[72095d3]1258         cxy = sinB * cosB * (F / D2 + var_comp * D2);
[21c226e]1259         /* FIXME: Should use FrDepth sometimes... */
1260         cyz = -2 * VAR(ToDepth) * dy / D;
1261         czx = -2 * VAR(ToDepth) * dx / D;
[f795df0]1262#endif
[a420b49]1263      }
1264   }
[5a38209]1265   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[2140502]1266#ifndef NO_COVARIANCES
[f795df0]1267                , cxy, cyz, czx
[647407d]1268#endif
[2140502]1269                );
[647407d]1270   return 1;
1271}
1272
[5a38209]1273static int
[21c226e]1274process_cartesian(prefix *fr, prefix *to, bool fToFirst)
[5a38209]1275{
[21c226e]1276   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
1277   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
1278   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
[647407d]1279
[21c226e]1280   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
[647407d]1281#ifndef NO_COVARIANCES
[c80bd34]1282                , 0, 0, 0
[647407d]1283#endif
[2140502]1284                );
[647407d]1285   return 1;
1286}
1287
[e2d48d8]1288static void
[5a38209]1289data_cartesian(void)
[647407d]1290{
[90bb053f]1291   prefix *fr = NULL, *to = NULL;
[5a38209]1292
1293   bool fMulti = fFalse;
[647407d]1294
[0395657]1295   reading first_stn = End;
1296
1297   reading *ordering;
[647407d]1298
[5a38209]1299   again:
1300
[647407d]1301   for (ordering = pcs->ordering ; ; ordering++) {
1302      skipblanks();
1303      switch (*ordering) {
[4a6a094]1304       case Fr:
[c458cf7]1305         fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[21c226e]1306         if (first_stn == End) first_stn = Fr;
1307         break;
[4a6a094]1308       case To:
[c458cf7]1309         to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[21c226e]1310         if (first_stn == End) first_stn = To;
1311         break;
[5a38209]1312       case Station:
[21c226e]1313         fr = to;
[c458cf7]1314         to = read_prefix(PFX_STATION);
[21c226e]1315         first_stn = To;
1316         break;
1317       case Dx: case Dy: case Dz:
1318         read_reading(*ordering, fFalse);
1319         break;
[647407d]1320       case Ignore:
1321         skipword(); break;
[5a38209]1322       case IgnoreAllAndNewLine:
1323         skipline();
1324         /* fall through */
1325       case Newline:
1326         if (fr != NULL) {
[e2d48d8]1327            if (!process_cartesian(fr, to, first_stn == To))
1328               skipline();
[5a38209]1329         }
1330         fMulti = fTrue;
1331         while (1) {
1332            process_eol();
1333            process_bol();
1334            if (isData(ch)) break;
[ee6a621]1335            if (!isComm(ch)) {
[993454b]1336               push_back(ch);
[e2d48d8]1337               return;
[ee6a621]1338            }
[5a38209]1339         }
1340         break;
[647407d]1341       case IgnoreAll:
1342         skipline();
1343         /* fall through */
1344       case End:
[5a38209]1345         if (!fMulti) {
[e2d48d8]1346            process_cartesian(fr, to, first_stn == To);
[5a38209]1347            process_eol();
[e2d48d8]1348            return;
[5a38209]1349         }
[eef4d8c]1350         do {
[5a38209]1351            process_eol();
1352            process_bol();
[eef4d8c]1353         } while (isComm(ch));
[5a38209]1354         goto again;
[0395657]1355       default: BUG("Unknown reading in ordering");
[647407d]1356      }
1357   }
[5a38209]1358}
[cb3d1e2]1359
[98a2eec]1360static int
[21c226e]1361process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[98a2eec]1362{
[21c226e]1363   real tape = VAL(Tape);
1364
[98a2eec]1365   real dx, dy, dz;
1366   real vx, vy, vz;
1367#ifndef NO_COVARIANCES
1368   real cxy = 0;
1369#endif
1370
[21c226e]1371   handle_comp_units();
[98a2eec]1372
[a6d094f]1373   /* depth gauge readings increase upwards with default calibration */
[6114207]1374   if (fDepthChange) {
[21c226e]1375      SVX_ASSERT(VAL(FrDepth) == 0.0);
1376      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1377      dz *= pcs->sc[Q_DEPTH];
[a6d094f]1378   } else {
[21c226e]1379      dz = VAL(ToDepth) - VAL(FrDepth);
1380      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1381   }
[98a2eec]1382
1383   /* adjusted tape is negative -- probably the calibration is wrong */
1384   if (tape < (real)0.0) {
1385      compile_warning(/*Negative adjusted tape reading*/79);
1386   }
1387
[21c226e]1388   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
[98a2eec]1389      /* plumb */
1390      dx = dy = (real)0.0;
1391      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1392      /* FIXME: Should use FrDepth sometimes... */
1393      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1394   } else {
1395      real sinB, cosB;
[b6de07d]1396      real var_comp;
[21c226e]1397      real comp = handle_compass(&var_comp);
[98a2eec]1398      sinB = sin(comp);
1399      cosB = cos(comp);
1400
1401      dx = tape * sinB;
1402      dy = tape * cosB;
1403
1404      vx = var(Q_POS) / 3.0 +
[21c226e]1405         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
[98a2eec]1406      vy = var(Q_POS) / 3.0 +
[21c226e]1407         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1408      /* FIXME: Should use FrDepth sometimes... */
1409      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1410
1411#ifndef NO_COVARIANCES
[21c226e]1412      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
[98a2eec]1413#endif
1414   }
1415   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1416#ifndef NO_COVARIANCES
1417                , cxy, 0, 0
1418#endif
1419                );
1420   return 1;
1421}
1422
[107b8bd]1423/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1424 * Also handles topofil (fromcount/tocount or count) in place of tape */
[e2d48d8]1425static void
[107b8bd]1426data_normal(void)
[54c4612]1427{
1428   prefix *fr = NULL, *to = NULL;
[107b8bd]1429   reading first_stn = End;
[54c4612]1430
[107b8bd]1431   bool fTopofil = fFalse, fMulti = fFalse;
1432   bool fRev;
[e217d67]1433   clino_type ctype, backctype;
[107b8bd]1434   bool fDepthChange;
[be97baf]1435   unsigned long compass_dat_flags = 0;
[54c4612]1436
1437   reading *ordering;
1438
[21c226e]1439   VAL(Tape) = 0;
[07442af]1440   VAL(Comp) = VAL(BackComp) = HUGE_REAL;
[21c226e]1441   VAL(FrCount) = VAL(ToCount) = 0;
1442   VAL(FrDepth) = VAL(ToDepth) = 0;
[07442af]1443   VAL(Left) = VAL(Right) = VAL(Up) = VAL(Down) = HUGE_REAL;
[54c4612]1444
1445   fRev = fFalse;
[fa42426]1446   ctype = backctype = CTYPE_OMIT;
[6114207]1447   fDepthChange = fFalse;
[54c4612]1448
[107b8bd]1449   /* ordering may omit clino reading, so set up default here */
1450   /* this is also used if clino reading is the omit character */
[21c226e]1451   VAL(Clino) = VAL(BackClino) = 0;
1452
1453   again:
[107b8bd]1454
[dcbcae0]1455   /* We clear these flags in the normal course of events, but if there's an
1456    * error in a reading, we might not, so make sure it has been cleared here.
[710ecc1]1457    */
[dcbcae0]1458   pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
[54c4612]1459   for (ordering = pcs->ordering; ; ordering++) {
1460      skipblanks();
1461      switch (*ordering) {
1462       case Fr:
[710ecc1]1463          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
[107b8bd]1464          if (first_stn == End) first_stn = Fr;
1465          break;
[54c4612]1466       case To:
[710ecc1]1467          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_ANON);
[107b8bd]1468          if (first_stn == End) first_stn = To;
1469          break;
[54c4612]1470       case Station:
[107b8bd]1471          fr = to;
[c458cf7]1472          to = read_prefix(PFX_STATION);
[107b8bd]1473          first_stn = To;
1474          break;
[ee1ec59]1475       case Dir: {
1476          typedef enum {
1477             DIR_NULL=-1, DIR_FORE, DIR_BACK
1478          } dir_tok;
1479          static sztok dir_tab[] = {
1480             {"B",     DIR_BACK},
1481             {"F",     DIR_FORE},
1482          };
1483          dir_tok tok;
1484          get_token();
1485          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1486          switch (tok) {
1487           case DIR_FORE:
[54c4612]1488             break;
[ee1ec59]1489           case DIR_BACK:
[54c4612]1490             fRev = fTrue;
1491             break;
1492           default:
[da96015]1493             file.lpos += strlen(buffer);
1494             compile_error_skip(-/*Found “%s”, expecting “F” or “B”*/131,
[44bb30d]1495                                buffer);
[54c4612]1496             process_eol();
[e2d48d8]1497             return;
[54c4612]1498          }
1499          break;
[ee1ec59]1500       }
[107b8bd]1501       case Tape:
[21c226e]1502          read_reading(Tape, fFalse);
1503          if (VAL(Tape) < (real)0.0)
[da96015]1504             compile_warning(-/*Negative tape reading*/60);
[107b8bd]1505          break;
1506       case Count:
[21c226e]1507          VAL(FrCount) = VAL(ToCount);
1508          read_reading(ToCount, fFalse);
[b4fe9fb]1509          fTopofil = fTrue;
[107b8bd]1510          break;
1511       case FrCount:
[21c226e]1512          read_reading(FrCount, fFalse);
[107b8bd]1513          break;
1514       case ToCount:
[21c226e]1515          read_reading(ToCount, fFalse);
[107b8bd]1516          fTopofil = fTrue;
1517          break;
[07442af]1518       case Comp: case BackComp:
1519          read_bearing_or_omit(*ordering);
[5b7c1b7]1520          break;
[0a158bc]1521       case Clino: case BackClino: {
1522          reading r = *ordering;
1523          clino_type * p_ctype = (r == Clino ? &ctype : &backctype);
1524          read_reading(r, fTrue);
1525          if (VAL(r) == HUGE_REAL) {
1526             VAL(r) = handle_plumb(p_ctype);
1527             if (VAL(r) != HUGE_REAL) break;
[da96015]1528             compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[4bc4d58]1529             skipline();
[107b8bd]1530             process_eol();
[e2d48d8]1531             return;
[107b8bd]1532          }
[0a158bc]1533          *p_ctype = CTYPE_READING;
[107b8bd]1534          break;
[0a158bc]1535       }
[07442af]1536       case FrDepth: case ToDepth:
1537          read_reading(*ordering, fFalse);
[107b8bd]1538          break;
[54c4612]1539       case Depth:
[21c226e]1540          VAL(FrDepth) = VAL(ToDepth);
1541          read_reading(ToDepth, fFalse);
[54c4612]1542          break;
[6114207]1543       case DepthChange:
1544          fDepthChange = fTrue;
[21c226e]1545          VAL(FrDepth) = 0;
1546          read_reading(ToDepth, fFalse);
[a186573]1547          break;
[44bb30d]1548       case CompassDATComp:
1549          read_bearing_or_omit(Comp);
1550          if (is_compass_NaN(VAL(Comp))) VAL(Comp) = HUGE_REAL;
1551          break;
1552       case CompassDATBackComp:
1553          read_bearing_or_omit(BackComp);
1554          if (is_compass_NaN(VAL(BackComp))) VAL(BackComp) = HUGE_REAL;
1555          break;
[0a158bc]1556       case CompassDATClino: case CompassDATBackClino: {
1557          reading r;
1558          clino_type * p_ctype;
1559          if (*ordering == CompassDATClino) {
1560             r = Clino;
1561             p_ctype = &ctype;
[07442af]1562          } else {
[0a158bc]1563             r = BackClino;
1564             p_ctype = &backctype;
[07442af]1565          }
[0a158bc]1566          read_reading(r, fFalse);
1567          if (is_compass_NaN(VAL(r))) {
1568             VAL(r) = HUGE_REAL;
1569             *p_ctype = CTYPE_OMIT;
[07442af]1570          } else {
[0a158bc]1571             *p_ctype = CTYPE_READING;
[07442af]1572          }
1573          break;
[0a158bc]1574       }
[07442af]1575       case CompassDATLeft: case CompassDATRight:
1576       case CompassDATUp: case CompassDATDown: {
[ee05463]1577          /* FIXME: need to actually make use of these entries! */
[07442af]1578          reading actual = Left + (*ordering - CompassDATLeft);
1579          read_reading(actual, fFalse);
1580          if (VAL(actual) < 0) VAL(actual) = HUGE_REAL;
[44bb30d]1581          break;
[07442af]1582       }
[be97baf]1583       case CompassDATFlags:
1584          if (ch == '#') {
1585             nextch();
1586             if (ch == '|') {
1587                filepos fp;
1588                get_pos(&fp);
1589                nextch();
1590                while (ch >= 'A' && ch <= 'Z') {
1591                   compass_dat_flags |= BIT(ch - 'A');
[850fdc4]1592                   /* FIXME: we currently understand:
1593                    *   L (exclude from length)
1594                    *   X (exclude data)
[0580c6a]1595                    * but should also handle at least some of:
[850fdc4]1596                    *   C (no adjustment)
1597                    *   P (no plot)
[0580c6a]1598                    */
[be97baf]1599                   nextch();
1600                }
1601                if (ch == '#') {
1602                   nextch();
1603                } else {
1604                   compass_dat_flags = 0;
1605                   set_pos(&fp);
1606                   push_back('|');
1607                   ch = '#';
1608                }
1609             } else {
1610                push_back(ch);
1611                ch = '#';
1612             }
1613          }
[107b8bd]1614          break;
[be97baf]1615       case Ignore:
1616          skipword(); break;
[54c4612]1617       case IgnoreAllAndNewLine:
[107b8bd]1618          skipline();
1619          /* fall through */
[54c4612]1620       case Newline:
[107b8bd]1621          if (fr != NULL) {
1622             int r;
[710ecc1]1623             int save_flags;
[dcbcae0]1624             int implicit_splay;
[21c226e]1625             if (fTopofil)
1626                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1627             /* Note: frdepth == todepth test works regardless of fDepthChange
1628              * (frdepth always zero, todepth is change of depth) and also
1629              * works for STYLE_NORMAL (both remain 0) */
[44bb30d]1630             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
[27b8b59]1631                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1632                process_equate(fr, to);
1633                goto inferred_equate;
1634             }
[e7576f6]1635             if (fRev) {
1636                prefix *t = fr;
[44bb30d]1637                fr = to;
1638                to = t;
[e7576f6]1639             }
[107b8bd]1640             if (fTopofil) {
[21c226e]1641                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1642             } else {
[21c226e]1643                VAL(Tape) *= pcs->units[Q_LENGTH];
1644                VAL(Tape) -= pcs->z[Q_LENGTH];
1645                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1646             }
[dcbcae0]1647             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1648             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
1649             save_flags = pcs->flags;
1650             if (implicit_splay) {
[710ecc1]1651                pcs->flags |= BIT(FLAGS_SPLAY);
1652             }
[107b8bd]1653             switch (pcs->style) {
1654              case STYLE_NORMAL:
[21c226e]1655                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1656                                   ctype, backctype);
[107b8bd]1657                break;
1658              case STYLE_DIVING:
[21c226e]1659                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1660                                   fDepthChange);
[107b8bd]1661                break;
1662              case STYLE_CYLPOLAR:
[21c226e]1663                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1664                                     fDepthChange);
[107b8bd]1665                break;
1666              default:
[74a0681]1667                r = 0; /* avoid warning */
[107b8bd]1668                BUG("bad style");
1669             }
[710ecc1]1670             pcs->flags = save_flags;
[107b8bd]1671             if (!r) skipline();
[44bb30d]1672
[e7576f6]1673             /* Swap fr and to back to how they were for next line */
1674             if (fRev) {
1675                prefix *t = fr;
1676                fr = to;
1677                to = t;
1678             }
[107b8bd]1679          }
[21c226e]1680
1681          fRev = fFalse;
1682          ctype = backctype = CTYPE_OMIT;
1683          fDepthChange = fFalse;
1684
1685          /* ordering may omit clino reading, so set up default here */
1686          /* this is also used if clino reading is the omit character */
1687          VAL(Clino) = VAL(BackClino) = 0;
1688
[44bb30d]1689          inferred_equate:
[e7576f6]1690
[107b8bd]1691          fMulti = fTrue;
1692          while (1) {
1693              process_eol();
1694              process_bol();
1695              if (isData(ch)) break;
1696              if (!isComm(ch)) {
1697                 push_back(ch);
[e2d48d8]1698                 return;
[107b8bd]1699              }
1700          }
1701          break;
[54c4612]1702       case IgnoreAll:
[107b8bd]1703          skipline();
1704          /* fall through */
[54c4612]1705       case End:
[107b8bd]1706          if (!fMulti) {
[710ecc1]1707             int save_flags;
[dcbcae0]1708             int implicit_splay;
[be97baf]1709             /* Compass ignore flag is 'X' */
1710             if ((compass_dat_flags & BIT('X' - 'A'))) {
1711                process_eol();
[e2d48d8]1712                return;
[be97baf]1713             }
[107b8bd]1714             if (fRev) {
1715                prefix *t = fr;
1716                fr = to;
1717                to = t;
1718             }
[21c226e]1719             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1720             /* Note: frdepth == todepth test works regardless of fDepthChange
1721              * (frdepth always zero, todepth is change of depth) and also
1722              * works for STYLE_NORMAL (both remain 0) */
[07442af]1723             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
[27b8b59]1724                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1725                process_equate(fr, to);
1726                process_eol();
[e2d48d8]1727                return;
[107b8bd]1728             }
1729             if (fTopofil) {
[21c226e]1730                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1731             } else {
[21c226e]1732                VAL(Tape) *= pcs->units[Q_LENGTH];
1733                VAL(Tape) -= pcs->z[Q_LENGTH];
1734                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1735             }
[dcbcae0]1736             implicit_splay = TSTBIT(pcs->flags, FLAGS_IMPLICIT_SPLAY);
1737             pcs->flags &= ~(BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY));
[710ecc1]1738             save_flags = pcs->flags;
[dcbcae0]1739             if (implicit_splay) {
[710ecc1]1740                pcs->flags |= BIT(FLAGS_SPLAY);
1741             }
[850fdc4]1742             if ((compass_dat_flags & BIT('L' - 'A'))) {
1743                /* 'L' means "exclude from length" - map this to Survex's
1744                 * FLAGS_DUPLICATE. */
1745                pcs->flags |= BIT(FLAGS_DUPLICATE);
1746             }
[107b8bd]1747             switch (pcs->style) {
1748              case STYLE_NORMAL:
[e2d48d8]1749                process_normal(fr, to, (first_stn == To) ^ fRev,
1750                               ctype, backctype);
[107b8bd]1751                break;
1752              case STYLE_DIVING:
[e2d48d8]1753                process_diving(fr, to, (first_stn == To) ^ fRev,
1754                               fDepthChange);
[107b8bd]1755                break;
1756              case STYLE_CYLPOLAR:
[e2d48d8]1757                process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1758                                 fDepthChange);
[107b8bd]1759                break;
1760              default:
1761                BUG("bad style");
1762             }
[710ecc1]1763             pcs->flags = save_flags;
[ee05463]1764
[107b8bd]1765             process_eol();
[e2d48d8]1766             return;
[107b8bd]1767          }
1768          do {
1769             process_eol();
1770             process_bol();
1771          } while (isComm(ch));
1772          goto again;
1773       default:
1774          BUG("Unknown reading in ordering");
[54c4612]1775      }
1776   }
1777}
1778
[ee05463]1779static int
1780process_lrud(prefix *stn)
1781{
1782   SVX_ASSERT(next_lrud);
1783   lrud * xsect = osnew(lrud);
1784   xsect->stn = stn;
1785   xsect->l = (VAL(Left) * pcs->units[Q_LEFT] - pcs->z[Q_LEFT]) * pcs->sc[Q_LEFT];
1786   xsect->r = (VAL(Right) * pcs->units[Q_RIGHT] - pcs->z[Q_RIGHT]) * pcs->sc[Q_RIGHT];
1787   xsect->u = (VAL(Up) * pcs->units[Q_UP] - pcs->z[Q_UP]) * pcs->sc[Q_UP];
1788   xsect->d = (VAL(Down) * pcs->units[Q_DOWN] - pcs->z[Q_DOWN]) * pcs->sc[Q_DOWN];
[a6301a1]1789   xsect->meta = pcs->meta;
1790   if (pcs->meta) ++pcs->meta->ref_count;
[ee05463]1791   xsect->next = NULL;
1792   *next_lrud = xsect;
1793   next_lrud = &(xsect->next);
1794
1795   return 1;
1796}
1797
[e2d48d8]1798static void
[ee05463]1799data_passage(void)
1800{
1801   prefix *stn = NULL;
1802   reading *ordering;
1803
1804   for (ordering = pcs->ordering ; ; ordering++) {
1805      skipblanks();
1806      switch (*ordering) {
1807       case Station:
[c458cf7]1808         stn = read_prefix(PFX_STATION);
[ee05463]1809         break;
1810       case Left: case Right: case Up: case Down:
[89d7d29]1811         read_reading(*ordering, fTrue);
1812         if (VAL(*ordering) == HUGE_REAL) {
1813            if (!isOmit(ch)) {
[da96015]1814               compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[4bc4d58]1815            } else {
1816               nextch();
[89d7d29]1817            }
1818            VAL(*ordering) = -1;
1819         }
[ee05463]1820         break;
1821       case Ignore:
1822         skipword(); break;
1823       case IgnoreAll:
1824         skipline();
1825         /* fall through */
1826       case End: {
[e2d48d8]1827         process_lrud(stn);
[ee05463]1828         process_eol();
[e2d48d8]1829         return;
[ee05463]1830       }
1831       default: BUG("Unknown reading in ordering");
1832      }
1833   }
1834}
1835
[5a38209]1836static int
1837process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1838{
1839   nosurveylink *link;
[647407d]1840
[f15cde77]1841   /* Suppress "unused fixed point" warnings for these stations */
1842   fr->sflags |= BIT(SFLAGS_USED);
1843   to->sflags |= BIT(SFLAGS_USED);
[838a602a]1844
[647407d]1845   /* add to linked list which is dealt with after network is solved */
1846   link = osnew(nosurveylink);
[5a38209]1847   if (fToFirst) {
[90bb053f]1848      link->to = StnFromPfx(to);
1849      link->fr = StnFromPfx(fr);
[0395657]1850   } else {
[90bb053f]1851      link->fr = StnFromPfx(fr);
1852      link->to = StnFromPfx(to);
[0395657]1853   }
[eb5aea0]1854   link->flags = pcs->flags | (STYLE_NOSURVEY << FLAGS_STYLE_BIT0);
[ee05463]1855   link->meta = pcs->meta;
1856   if (pcs->meta) ++pcs->meta->ref_count;
[647407d]1857   link->next = nosurveyhead;
1858   nosurveyhead = link;
[a420b49]1859   return 1;
[d1b1380]1860}
[50f6901]1861
[e2d48d8]1862static void
[5a38209]1863data_nosurvey(void)
1864{
1865   prefix *fr = NULL, *to = NULL;
1866
1867   bool fMulti = fFalse;
1868
1869   reading first_stn = End;
1870
1871   reading *ordering;
1872
1873   again:
1874
1875   for (ordering = pcs->ordering ; ; ordering++) {
1876      skipblanks();
1877      switch (*ordering) {
1878       case Fr:
[c458cf7]1879          fr = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[5a38209]1880          if (first_stn == End) first_stn = Fr;
1881          break;
1882       case To:
[c458cf7]1883          to = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
[5a38209]1884          if (first_stn == End) first_stn = To;
1885          break;
1886       case Station:
1887          fr = to;
[c458cf7]1888          to = read_prefix(PFX_STATION);
[5a38209]1889          first_stn = To;
1890          break;
1891       case Ignore:
1892         skipword(); break;
1893       case IgnoreAllAndNewLine:
1894         skipline();
1895         /* fall through */
1896       case Newline:
1897         if (fr != NULL) {
[e2d48d8]1898            if (!process_nosurvey(fr, to, first_stn == To))
1899               skipline();
[5a38209]1900         }
[838a602a]1901         if (ordering[1] == End) {
1902            do {
1903               process_eol();
1904               process_bol();
1905            } while (isComm(ch));
[ee6a621]1906            if (!isData(ch)) {
[993454b]1907               push_back(ch);
[e2d48d8]1908               return;
[ee6a621]1909            }
[838a602a]1910            goto again;
1911         }
[5a38209]1912         fMulti = fTrue;
1913         while (1) {
1914            process_eol();
1915            process_bol();
1916            if (isData(ch)) break;
[ee6a621]1917            if (!isComm(ch)) {
[993454b]1918               push_back(ch);
[e2d48d8]1919               return;
[ee6a621]1920            }
[5a38209]1921         }
1922         break;
1923       case IgnoreAll:
1924         skipline();
1925         /* fall through */
1926       case End:
1927         if (!fMulti) {
[e2d48d8]1928            (void)process_nosurvey(fr, to, first_stn == To);
[5a38209]1929            process_eol();
[e2d48d8]1930            return;
[5a38209]1931         }
[eef4d8c]1932         do {
[5a38209]1933            process_eol();
1934            process_bol();
[eef4d8c]1935         } while (isComm(ch));
[5a38209]1936         goto again;
1937       default: BUG("Unknown reading in ordering");
1938      }
1939   }
1940}
1941
[50f6901]1942/* totally ignore a line of survey data */
[e2d48d8]1943static void
[50f6901]1944data_ignore(void)
1945{
1946   skipline();
[ee6a621]1947   process_eol();
[50f6901]1948}
Note: See TracBrowser for help on using the repository browser.