source: git/src/datain.c @ 21df04b

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

src/datain.c: Remove superfluous call to skipblanks() before
get_token().

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