source: git/src/datain.c @ 525adb2

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

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

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