source: git/src/datain.c @ eb5aea0

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

src/: Track the data style of each leg, and pass it in to img - it's
not yet stored in the .3d file, but now it is easily available so it
can be.

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