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

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

src/datain.c: Move comments next to the code that they relate to.

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