source: git/trunk/src/datain.c @ 7bb8184

Last change on this file since 7bb8184 was 7bb8184, checked in by Olly Betts <olly@…>, 13 years ago

Retagging 1.2.0

git-svn-id: file:///home/survex-svn/survex/tags/1.2.0@3664 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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