source: git/src/datain.c @ 7f4a756

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

Removed support for writing Chasm's 3dx format.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2893 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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