source: git/src/datain.c @ fc4b814

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 fc4b814 was 74a0681, checked in by Olly Betts <olly@…>, 22 years ago

Fixed a few warnings.

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

  • Property mode set to 100644
File size: 46.0 KB
Line 
1/* datain.c
2 * Reads in survey files, dealing with special characters, keywords & data
3 * Copyright (C) 1991-2003 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <limits.h>
25#include <stdarg.h>
26#include <time.h>
27
28#include "debug.h"
29#include "cavern.h"
30#include "filename.h"
31#include "message.h"
32#include "filelist.h"
33#include "netbits.h"
34#include "netskel.h"
35#include "readval.h"
36#include "datain.h"
37#include "commands.h"
38#include "out.h"
39#include "str.h"
40
41#define EPSILON (REAL_EPSILON * 1000)
42
43#define var(I) (pcs->Var[(I)])
44
45int ch;
46
47typedef enum {
48    CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_INFERPLUMB, CTYPE_HORIZ
49} clino_type;
50
51/* Don't explicitly initialise as we can't set the jmp_buf - this has
52 * static scope so will be initialised like this anyway */
53parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
54
55bool f_export_ok;
56
57static real value[Fr - 1];
58#define VAL(N) value[(N)-1]
59static real variance[Fr - 1];
60#define VAR(N) variance[(N)-1]
61
62void
63get_pos(filepos *fp)
64{
65   fp->ch = ch;
66   fp->offset = ftell(file.fh);
67   if (fp->offset == -1)
68      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
69}
70
71void
72set_pos(const filepos *fp)
73{
74   ch = fp->ch;
75   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
76      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
77}
78
79static void
80push_back(int c)
81{
82   if (c != EOF && ungetc(c, file.fh) == EOF)
83      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
84}
85
86static void
87error_list_parent_files(void)
88{
89   if (!file.reported_where && file.parent) {
90      parse *p = file.parent;
91      const char *m = msg(/*In file included from*/5);
92      size_t len = strlen(m);
93
94      fprintf(STDERR, m);
95      m = msg(/*from*/3);
96
97      /* Suppress reporting of full include tree for further errors
98       * in this file */
99      file.reported_where = fTrue;
100
101      while (p) {
102         /* Force re-report of include tree for further errors in
103          * parent files */
104         p->reported_where = fFalse;
105         fprintf(STDERR, " %s:%d", p->filename, p->line);
106         p = p->parent;
107         if (p) fprintf(STDERR, ",\n%*s", (int)len, m);
108      }
109      fprintf(STDERR, ":\n");
110   }
111}
112
113void
114compile_error(int en, ...)
115{
116   va_list ap;
117   va_start(ap, en);
118   error_list_parent_files();
119   v_report(1, file.filename, file.line, en, ap);
120   va_end(ap);
121}
122
123void
124compile_error_skip(int en, ...)
125{
126   va_list ap;
127   va_start(ap, en);
128   error_list_parent_files();
129   v_report(1, file.filename, file.line, en, ap);
130   va_end(ap);
131   skipline();
132}
133
134void
135compile_error_token(int en)
136{
137   char *p = NULL;
138   static int len;
139   s_zero(&p);
140   skipblanks();
141   while (!isBlank(ch) && !isEol(ch)) {
142      s_catchar(&p, &len, (char)ch);
143      nextch();
144   }
145   compile_error_skip(en, p ? p : "");
146   osfree(p);
147}
148
149void
150compile_warning(int en, ...)
151{
152   va_list ap;
153   va_start(ap, en);
154   error_list_parent_files();
155   v_report(0, file.filename, file.line, en, ap);
156   va_end(ap);
157}
158
159/* This function makes a note where to put output files */
160static void
161using_data_file(const char *fnm)
162{
163   if (!fnm_output_base) {
164      /* was: fnm_output_base = base_from_fnm(fnm); */
165      fnm_output_base = baseleaf_from_fnm(fnm);
166   } else if (fnm_output_base_is_dir) {
167      /* --output pointed to directory so use the leaf basename in that dir */
168      char *lf, *p;
169      lf = baseleaf_from_fnm(fnm);
170      p = use_path(fnm_output_base, lf);
171      osfree(lf);
172      osfree(fnm_output_base);
173      fnm_output_base = p;
174      fnm_output_base_is_dir = 0;
175   }
176}
177
178static void
179skipword(void)
180{
181   while (!isBlank(ch) && !isEol(ch)) nextch();
182}
183
184extern void
185skipblanks(void)
186{
187   while (isBlank(ch)) nextch();
188}
189
190extern void
191skipline(void)
192{
193   while (!isEol(ch)) nextch();
194}
195
196#ifndef NO_PERCENTAGE
197static long int filelen;
198#endif
199
200static void
201process_bol(void)
202{
203#ifndef NO_PERCENTAGE
204   /* print %age of file done */
205   if (filelen > 0) {
206      filepos fp;
207      get_pos(&fp);
208      printf("%d%%\r", (int)(100 * fp.offset / filelen));
209   }
210#endif
211
212   nextch();
213   skipblanks();
214}
215
216static void
217process_eol(void)
218{
219   int eolchar;
220
221   skipblanks();
222
223   if (!isEol(ch)) {
224      if (!isComm(ch)) compile_error(/*End of line not blank*/15);
225      skipline();
226   }
227
228   eolchar = ch;
229   file.line++;
230   /* skip any different eol characters so we get line counts correct on
231    * DOS text files and similar, but don't count several adjacent blank
232    * lines as one */
233   while (ch != EOF) {
234      nextch();
235      if (ch == eolchar || !isEol(ch)) {
236         push_back(ch);
237         break;
238      }
239   }
240}
241
242static bool
243process_non_data_line(void)
244{
245   process_bol();
246
247   if (isData(ch)) return fFalse;
248
249   if (isKeywd(ch)) {
250      nextch();
251      handle_command();
252   }
253
254   process_eol();
255
256   return fTrue;
257}
258
259static void
260read_reading(reading r, bool f_optional)
261{
262   int n_readings;
263   q_quantity q;
264   VAL(r) = read_numeric(f_optional, &n_readings);
265   switch (r) {
266      case Tape: q = Q_LENGTH; break;
267      case Comp: q = Q_BEARING; break;
268      case BackComp: q = Q_BACKBEARING; break;
269      case Clino: q = Q_GRADIENT; break;
270      case BackClino: q = Q_BACKGRADIENT; break;
271      case FrDepth: case ToDepth: q = Q_DEPTH; break;
272      case Dx: q = Q_DX; break;
273      case Dy: q = Q_DY; break;
274      case Dz: q = Q_DZ; break;
275      case FrCount: case ToCount: q = Q_COUNT; break;
276      default:
277        q = Q_NULL; /* Suppress compiler warning */;
278        BUG("Unexpected case");
279   }
280   VAR(r) = var(q);
281   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
282}
283
284static void
285read_bearing_or_omit(reading r)
286{
287   int n_readings;
288   q_quantity q = Q_NULL;
289   VAL(r) = read_numeric_or_omit(&n_readings);
290   switch (r) {
291      case Comp: q = Q_BEARING; break;
292      case BackComp: q = Q_BACKBEARING; break;
293      default:
294        q = Q_NULL; /* Suppress compiler warning */;
295        BUG("Unexpected case");
296   }
297   VAR(r) = var(q);
298   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
299}
300
301/* For reading Compass MAK files which have a freeform syntax */
302static void
303nextch_handling_eol(void)
304{
305   nextch();
306   while (ch != EOF && isEol(ch)) {
307      process_eol();
308      nextch();
309   }
310}
311
312#define LITLEN(S) (sizeof(S"") - 1)
313#define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
314                        (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
315                        strcasecmp((F) + (L) - LITLEN(E), E) == 0)
316extern void
317data_file(const char *pth, const char *fnm)
318{
319   int begin_lineno_store;
320   parse file_store;
321   volatile enum {FMT_SVX, FMT_DAT, FMT_MAK} fmt = FMT_SVX;
322
323   {
324      char *filename;
325      FILE *fh;
326      size_t len;
327
328      if (!pth) {
329         /* file specified on command line - don't do special translation */
330         fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
331      } else {
332         fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
333      }
334
335      if (fh == NULL) {
336         compile_error(/*Couldn't open data file `%s'*/24, fnm);
337         return;
338      }
339
340      len = strlen(filename);
341      if (has_ext(filename, len, "dat")) {
342         fmt = FMT_DAT;
343      } else if (has_ext(filename, len, "mak")) {
344         fmt = FMT_MAK;
345      }
346
347      file_store = file;
348      if (file.fh) file.parent = &file_store;
349      file.fh = fh;
350      file.filename = filename;
351      file.line = 1;
352      file.reported_where = fFalse;
353   }
354
355   if (fPercent) printf("%s:\n", fnm);
356
357   using_data_file(file.filename);
358
359   begin_lineno_store = pcs->begin_lineno;
360   pcs->begin_lineno = 0;
361
362#ifndef NO_PERCENTAGE
363   /* Try to find how long the file is...
364    * However, under ANSI fseek( ..., SEEK_END) may not be supported */
365   filelen = 0;
366   if (fPercent) {
367      if (fseek(file.fh, 0l, SEEK_END) == 0) {
368         filepos fp;
369         get_pos(&fp);
370         filelen = fp.offset;
371      }
372      rewind(file.fh); /* reset file ptr to start & clear any error state */
373   }
374#endif
375
376   if (fmt == FMT_DAT) {
377      short *t;
378      int i;
379      settings *pcsNew;
380
381      pcsNew = osnew(settings);
382      *pcsNew = *pcs; /* copy contents */
383      pcsNew->begin_lineno = 0;
384      pcsNew->next = pcs;
385      pcs = pcsNew;
386      default_units(pcs);
387      default_calib(pcs);
388
389      pcs->style = STYLE_NORMAL;
390      pcs->units[Q_LENGTH] = METRES_PER_FOOT;
391      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
392
393      t[EOF] = SPECIAL_EOL;
394      memset(t, 0, sizeof(short) * 33);
395      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
396      t[127] = 0;
397      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
398      t['\t'] |= SPECIAL_BLANK;
399      t[' '] |= SPECIAL_BLANK;
400      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
401      t['\n'] |= SPECIAL_EOL;
402      t['\r'] |= SPECIAL_EOL;
403      t['.'] |= SPECIAL_DECIMAL;
404      t['-'] |= SPECIAL_MINUS;
405      t['+'] |= SPECIAL_PLUS;
406      pcs->Translate = t;
407      pcs->Case = OFF;
408      pcs->Truncate = INT_MAX;
409      pcs->infer = 7; /* FIXME: BIT(EQUATES)|BIT(EXPORTS)|BIT(PLUMBS); */
410   } else if (fmt == FMT_MAK) {
411      short *t;
412      int i;
413      settings *pcsNew;
414
415      pcsNew = osnew(settings);
416      *pcsNew = *pcs; /* copy contents */
417      pcsNew->begin_lineno = 0;
418      pcsNew->next = pcs;
419      pcs = pcsNew;
420
421      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
422     
423      t[EOF] = SPECIAL_EOL;
424      memset(t, 0, sizeof(short) * 33);
425      for (i = 33; i < 127; i++) t[i] = SPECIAL_NAMES;
426      t[127] = 0;
427      for (i = 128; i < 256; i++) t[i] = SPECIAL_NAMES;
428      t['['] = t[','] = t[';'] = 0;
429      t['\t'] |= SPECIAL_BLANK;
430      t[' '] |= SPECIAL_BLANK;
431      t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
432      t['\n'] |= SPECIAL_EOL;
433      t['\r'] |= SPECIAL_EOL;
434      t['.'] |= SPECIAL_DECIMAL;
435      t['-'] |= SPECIAL_MINUS;
436      t['+'] |= SPECIAL_PLUS;
437      pcs->Translate = t;
438      pcs->Case = OFF;
439      pcs->Truncate = INT_MAX;
440   }
441
442#ifdef HAVE_SETJMP_H
443   /* errors in nested functions can longjmp here */
444   if (setjmp(file.jbSkipLine)) {
445      process_eol();
446   }
447#endif
448
449   if (fmt == FMT_DAT) {
450      while (!feof(file.fh) && !ferror(file.fh)) {
451         static reading compass_order[] = {
452            Fr, To, Tape, Comp, Clino, Ignore, Ignore, Ignore, Ignore,
453            CompassDATFlags, IgnoreAll
454         };
455         static reading compass_order_backsights[] = {
456            Fr, To, Tape, Comp, Clino, Ignore, Ignore, Ignore, Ignore,
457            BackComp, BackClino,
458            CompassDATFlags, IgnoreAll
459         };
460         /* <Cave name> */
461         process_bol();
462         skipline();
463         process_eol();
464         /* SURVEY NAME: <Short name> */
465         get_token();
466         get_token();
467         /* if (ch != ':') ... */
468         nextch();
469         skipblanks();
470         get_token();
471         skipline();
472         process_eol();
473         /* SURVEY DATE: 7 10 79  COMMENT:<Long name> */
474         /* NB order is *month* *day* year */
475         get_token();
476         get_token();
477         if (ch == ':') {
478             struct tm t;
479
480             copy_on_write_meta(pcs);
481
482             nextch();
483             /* struct tm month uses 0 for Jan */
484             t.tm_mon = read_uint() - 1;
485             t.tm_mday = read_uint();
486             /* struct tm uses year - 1900 */
487             t.tm_year = read_uint();
488             /* Note: Larry says a 2 digit year is always 19XX */
489             if (t.tm_year >= 100) t.tm_year -= 1900;
490
491             pcs->meta->date1 = mktime(&t);
492             pcs->meta->date2 = pcs->meta->date1;
493         }
494         skipline();
495         process_eol();
496         /* SURVEY TEAM: */
497         get_token();
498         get_token();
499         skipline();
500         process_eol();
501         /* <Survey team> */
502         nextch();
503         skipline();
504         process_eol();
505         /* DECLINATION: 1.00  FORMAT: DDDDLUDRADLN  CORRECTIONS: 2.00 3.00 4.00 */
506         get_token();
507         nextch(); /* : */
508         skipblanks();
509         pcs->z[Q_DECLINATION] = -read_numeric(fFalse, NULL);
510         pcs->z[Q_DECLINATION] *= pcs->units[Q_DECLINATION];
511         get_token();
512         pcs->ordering = compass_order;
513         if (strcmp(buffer, "FORMAT") == 0) {
514            nextch(); /* : */
515            get_token();
516            if (strlen(buffer) >= 12 && buffer[11] == 'B') {
517               /* We have backsights for compass and clino */
518               pcs->ordering = compass_order_backsights;
519            }
520            get_token();
521         }
522         if (strcmp(buffer, "CORRECTIONS") == 0) {
523            nextch(); /* : */
524            pcs->z[Q_BEARING] = -rad(read_numeric(fFalse, NULL));
525            pcs->z[Q_GRADIENT] = -rad(read_numeric(fFalse, NULL));
526            pcs->z[Q_LENGTH] = -read_numeric(fFalse, NULL);
527         } else {
528            pcs->z[Q_BEARING] = 0;
529            pcs->z[Q_GRADIENT] = 0;
530            pcs->z[Q_LENGTH] = 0;
531         }
532         skipline();
533         process_eol();
534         /* BLANK LINE */
535         process_bol();
536         skipline();
537         process_eol();
538         /* heading line */
539         process_bol();
540         skipline();
541         process_eol();
542         /* BLANK LINE */
543         process_bol();
544         skipline();
545         process_eol();
546         while (!feof(file.fh)) {
547            process_bol();
548            if (ch == '\x0c') {
549               nextch();
550               process_eol();
551               break;
552            }
553            (void)data_normal();
554         }
555      }
556      {
557         settings *pcsParent = pcs->next;
558         SVX_ASSERT(pcsParent);
559         pcs->ordering = NULL;
560         free_settings(pcs);
561         pcs = pcsParent;
562      }                                   
563   } else if (fmt == FMT_MAK) {
564      nextch_handling_eol();
565      while (!feof(file.fh) && !ferror(file.fh)) {
566         if (ch == '#') {
567            /* include a file */
568            int ch_store;
569            char *dat_pth = path_from_fnm(file.filename);
570            char *dat_fnm = NULL;
571            int dat_fnm_len;
572            nextch_handling_eol();
573            while (ch != ',' && ch != ';' && ch != EOF) {
574               while (isEol(ch)) process_eol();
575               s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
576               nextch_handling_eol();
577            }
578            while (ch != ';' && ch != EOF) {
579               prefix *name;
580               nextch_handling_eol();
581               name = read_prefix_stn(fTrue, fFalse);
582               if (name) {
583                  skipblanks();
584                  if (ch == '[') {
585                     /* fixed pt */
586                     node *stn;
587                     real x, y, z;
588                     name->sflags |= BIT(SFLAGS_FIXED);
589                     nextch_handling_eol();
590                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
591                            ch != '.' && ch != ']' && ch != EOF) {
592                        nextch_handling_eol();
593                     }
594                     x = read_numeric(fFalse, NULL);
595                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
596                            ch != '.' && ch != ']' && ch != EOF) {
597                        nextch_handling_eol();
598                     }
599                     y = read_numeric(fFalse, NULL);
600                     while (!isdigit(ch) && ch != '+' && ch != '-' &&
601                            ch != '.' && ch != ']' && ch != EOF) {
602                        nextch_handling_eol();
603                     }
604                     z = read_numeric(fFalse, NULL);
605                     stn = StnFromPfx(name);
606                     if (!fixed(stn)) {
607                        POS(stn, 0) = x;
608                        POS(stn, 1) = y;
609                        POS(stn, 2) = z;
610                        fix(stn);
611                     } else {
612                        if (x != POS(stn, 0) || y != POS(stn, 1) ||
613                            z != POS(stn, 2)) {
614                           compile_error(/*Station already fixed or equated to a fixed point*/46);
615                        } else {
616                           compile_warning(/*Station already fixed at the same coordinates*/55);
617                        }
618                     }
619                     while (ch != ']' && ch != EOF) nextch_handling_eol();
620                     if (ch == ']') {
621                        nextch_handling_eol();
622                        skipblanks();
623                     }
624                  } else {
625                     /* FIXME: link station - ignore for now */
626                     /* FIXME: perhaps issue warning? */
627                  }
628                  while (ch != ',' && ch != ';' && ch != EOF)
629                     nextch_handling_eol();
630               }
631            }
632            if (dat_fnm) {
633               ch_store = ch;
634               data_file(dat_pth, dat_fnm);
635               ch = ch_store;
636               osfree(dat_fnm);
637            }
638         } else {
639            /* FIXME: also check for % and $ later */
640            nextch_handling_eol();
641         }
642      }
643      {
644         settings *pcsParent = pcs->next;
645         SVX_ASSERT(pcsParent);
646         free_settings(pcs);
647         pcs = pcsParent;
648      }                                   
649   } else {
650      while (!feof(file.fh) && !ferror(file.fh)) {
651         if (!process_non_data_line()) {
652            volatile int r;
653#ifdef CHASM3DX
654            twig *temp = limb;
655#endif
656            f_export_ok = fFalse;
657            switch (pcs->style) {
658             case STYLE_NORMAL:
659             case STYLE_DIVING:
660             case STYLE_CYLPOLAR:
661               r = data_normal();
662               break;
663             case STYLE_CARTESIAN:
664               r = data_cartesian();
665               break;
666             case STYLE_NOSURVEY:
667               r = data_nosurvey();
668               break;
669             case STYLE_IGNORE:
670               r = data_ignore();
671               break;
672             default:
673               r = 0; /* avoid warning */
674               BUG("bad style");
675            }
676            /* style function returns 0 => error */
677#ifdef CHASM3DX
678            if (!r && fUseNewFormat) {
679               /* we have just created a very naughty twiglet, and it must be
680                * punished */
681               osfree(limb);
682               limb = temp;
683            }
684#endif
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
1073#ifdef CHASM3DX
1074   if (fUseNewFormat) {
1075      /* new twiglet and insert into twig tree */
1076      twig *twiglet = osnew(twig);
1077      twiglet->from = fr;
1078      twiglet->to = to;
1079      twiglet->down = twiglet->right = NULL;
1080      twiglet->source = twiglet->drawings
1081        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1082      twiglet->up = limb->up;
1083      limb->right = twiglet;
1084      limb = twiglet;
1085
1086      /* record pre-fettling deltas */
1087      twiglet->delta[0] = dx;
1088      twiglet->delta[1] = dy;
1089      twiglet->delta[2] = dz;
1090   }
1091#endif
1092   return 1;
1093}
1094
1095static int
1096process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1097{
1098   real tape = VAL(Tape);
1099
1100   real dx, dy, dz;
1101   real vx, vy, vz;
1102#ifndef NO_COVARIANCES
1103   real cxy = 0, cyz = 0, czx = 0;
1104#endif
1105
1106   handle_comp_units();
1107
1108   /* depth gauge readings increase upwards with default calibration */
1109   if (fDepthChange) {
1110      SVX_ASSERT(VAL(FrDepth) == 0.0);
1111      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1112      dz *= pcs->sc[Q_DEPTH];
1113   } else {
1114      dz = VAL(ToDepth) - VAL(FrDepth);
1115      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1116   }
1117
1118   /* adjusted tape is negative -- probably the calibration is wrong */
1119   if (tape < (real)0.0) {
1120      compile_warning(/*Negative adjusted tape reading*/79);
1121   }
1122
1123   /* check if tape is less than depth change */
1124   if (tape < fabs(dz)) {
1125      /* FIXME: allow margin of error based on variances? */
1126      compile_warning(/*Tape reading is less than change in depth*/62);
1127   }
1128
1129   if (tape == (real)0.0 && dz == 0.0) {
1130      dx = dy = dz = (real)0.0;
1131      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
1132   } else if (VAL(Comp) == HUGE_REAL &&
1133              VAL(BackComp) == HUGE_REAL) {
1134      /* plumb */
1135      dx = dy = (real)0.0;
1136      if (dz < 0) tape = -tape;
1137      /* FIXME: Should use FrDepth sometimes... */
1138      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1139         / (VAR(Tape) * 2 * VAR(ToDepth));
1140      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1141      /* FIXME: Should use FrDepth sometimes... */
1142      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1143                              / (VAR(Tape) + VAR(ToDepth));
1144   } else {
1145      real L2, sinB, cosB, dz2, D2;
1146      real var_comp;
1147      real comp = handle_compass(&var_comp);
1148      sinB = sin(comp);
1149      cosB = cos(comp);
1150      L2 = tape * tape;
1151      dz2 = dz * dz;
1152      D2 = L2 - dz2;
1153      if (D2 <= (real)0.0) {
1154         /* FIXME: Should use FrDepth sometimes... */
1155         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
1156         dx = dy = (real)0.0;
1157         vx = vy = var(Q_POS) / 3.0;
1158         /* FIXME: Should use FrDepth sometimes... */
1159         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
1160         if (dz > 0) {
1161            /* FIXME: Should use FrDepth sometimes... */
1162            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
1163         } else {
1164            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
1165         }
1166      } else {
1167         real D = sqrt(D2);
1168         /* FIXME: Should use FrDepth sometimes... */
1169         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
1170         dx = D * sinB;
1171         dy = D * cosB;
1172
1173         vx = var(Q_POS) / 3.0 +
1174            sinB * sinB * F / D2 + var_comp * dy * dy;
1175         vy = var(Q_POS) / 3.0 +
1176            cosB * cosB * F / D2 + var_comp * dx * dx;
1177         /* FIXME: Should use FrDepth sometimes... */
1178         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1179
1180#ifndef NO_COVARIANCES
1181         cxy = sinB * cosB * (F / D2 + var_comp * D2);
1182         /* FIXME: Should use FrDepth sometimes... */
1183         cyz = -2 * VAR(ToDepth) * dy / D;
1184         czx = -2 * VAR(ToDepth) * dx / D;
1185#endif
1186      }
1187   }
1188   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1189#ifndef NO_COVARIANCES
1190                , cxy, cyz, czx
1191#endif
1192                );
1193#ifdef CHASM3DX
1194   if (fUseNewFormat) {
1195      /* new twiglet and insert into twig tree */
1196      twig *twiglet = osnew(twig);
1197      twiglet->from = fr;
1198      twiglet->to = to;
1199      twiglet->down = twiglet->right = NULL;
1200      twiglet->source = twiglet->drawings
1201        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1202      twiglet->up = limb->up;
1203      limb->right = twiglet;
1204      limb = twiglet;
1205
1206      /* record pre-fettling deltas */
1207      twiglet->delta[0] = dx;
1208      twiglet->delta[1] = dy;
1209      twiglet->delta[2] = dz;
1210   }
1211#endif
1212
1213   return 1;
1214}
1215
1216static int
1217process_cartesian(prefix *fr, prefix *to, bool fToFirst)
1218{
1219   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
1220   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
1221   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
1222
1223   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
1224#ifndef NO_COVARIANCES
1225                , 0, 0, 0
1226#endif
1227                );
1228
1229#ifdef CHASM3DX
1230   if (fUseNewFormat) {
1231      /* new twiglet and insert into twig tree */
1232      twig *twiglet = osnew(twig);
1233      twiglet->from = fr;
1234      twiglet->to = to;
1235      twiglet->down = twiglet->right = NULL;
1236      twiglet->source = twiglet->drawings
1237        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1238      twiglet->up = limb->up;
1239      limb->right = twiglet;
1240      limb = twiglet;
1241
1242      /* record pre-fettling deltas */
1243      twiglet->delta[0] = dx;
1244      twiglet->delta[1] = dy;
1245      twiglet->delta[2] = dz;
1246   }
1247#endif
1248
1249   return 1;
1250}
1251
1252extern int
1253data_cartesian(void)
1254{
1255   prefix *fr = NULL, *to = NULL;
1256
1257   bool fMulti = fFalse;
1258
1259   reading first_stn = End;
1260
1261   reading *ordering;
1262
1263   again:
1264
1265   for (ordering = pcs->ordering ; ; ordering++) {
1266      skipblanks();
1267      switch (*ordering) {
1268       case Fr:
1269         fr = read_prefix_stn(fFalse, fTrue);
1270         if (first_stn == End) first_stn = Fr;
1271         break;
1272       case To:
1273         to = read_prefix_stn(fFalse, fTrue);
1274         if (first_stn == End) first_stn = To;
1275         break;
1276       case Station:
1277         fr = to;
1278         to = read_prefix_stn(fFalse, fFalse);
1279         first_stn = To;
1280         break;
1281       case Dx: case Dy: case Dz:
1282         read_reading(*ordering, fFalse);
1283         break;
1284       case Ignore:
1285         skipword(); break;
1286       case IgnoreAllAndNewLine:
1287         skipline();
1288         /* fall through */
1289       case Newline:
1290         if (fr != NULL) {
1291            int r;
1292            r = process_cartesian(fr, to, first_stn == To);
1293            if (!r) skipline();
1294         }
1295         fMulti = fTrue;
1296         while (1) {
1297            process_eol();
1298            process_bol();
1299            if (isData(ch)) break;
1300            if (!isComm(ch)) {
1301               push_back(ch);
1302               return 1;
1303            }
1304         }
1305         break;
1306       case IgnoreAll:
1307         skipline();
1308         /* fall through */
1309       case End:
1310         if (!fMulti) {
1311            int r = process_cartesian(fr, to, first_stn == To);
1312            process_eol();
1313            return r;
1314         }
1315         do {
1316            process_eol();
1317            process_bol();
1318         } while (isComm(ch));
1319         goto again;
1320       default: BUG("Unknown reading in ordering");
1321      }
1322   }
1323}
1324
1325static int
1326process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1327{
1328   real tape = VAL(Tape);
1329
1330   real dx, dy, dz;
1331   real vx, vy, vz;
1332#ifndef NO_COVARIANCES
1333   real cxy = 0;
1334#endif
1335
1336   handle_comp_units();
1337
1338   /* depth gauge readings increase upwards with default calibration */
1339   if (fDepthChange) {
1340      SVX_ASSERT(VAL(FrDepth) == 0.0);
1341      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1342      dz *= pcs->sc[Q_DEPTH];
1343   } else {
1344      dz = VAL(ToDepth) - VAL(FrDepth);
1345      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1346   }
1347
1348   /* adjusted tape is negative -- probably the calibration is wrong */
1349   if (tape < (real)0.0) {
1350      compile_warning(/*Negative adjusted tape reading*/79);
1351   }
1352
1353   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
1354      /* plumb */
1355      dx = dy = (real)0.0;
1356      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1357      /* FIXME: Should use FrDepth sometimes... */
1358      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1359   } else {
1360      real sinB, cosB;
1361      real var_comp;
1362      real comp = handle_compass(&var_comp);
1363      sinB = sin(comp);
1364      cosB = cos(comp);
1365
1366      dx = tape * sinB;
1367      dy = tape * cosB;
1368
1369      vx = var(Q_POS) / 3.0 +
1370         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
1371      vy = var(Q_POS) / 3.0 +
1372         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1373      /* FIXME: Should use FrDepth sometimes... */
1374      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1375
1376#ifndef NO_COVARIANCES
1377      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
1378#endif
1379   }
1380   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1381#ifndef NO_COVARIANCES
1382                , cxy, 0, 0
1383#endif
1384                );
1385#ifdef CHASM3DX
1386   if (fUseNewFormat) {
1387      /* new twiglet and insert into twig tree */
1388      twig *twiglet = osnew(twig);
1389      twiglet->from = fr;
1390      twiglet->to = to;
1391      twiglet->down = twiglet->right = NULL;
1392      twiglet->source = twiglet->drawings
1393        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1394      twiglet->up = limb->up;
1395      limb->right = twiglet;
1396      limb = twiglet;
1397
1398      /* record pre-fettling deltas */
1399      twiglet->delta[0] = dx;
1400      twiglet->delta[1] = dy;
1401      twiglet->delta[2] = dz;
1402   }
1403#endif
1404
1405   return 1;
1406}
1407
1408/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1409 * Also handles topofil (fromcount/tocount or count) in place of tape */
1410extern int
1411data_normal(void)
1412{
1413   prefix *fr = NULL, *to = NULL;
1414   reading first_stn = End;
1415
1416   bool fTopofil = fFalse, fMulti = fFalse;
1417   bool fRev;
1418   clino_type ctype, backctype;
1419   bool fDepthChange;
1420   unsigned long compass_dat_flags = 0;
1421
1422   reading *ordering;
1423
1424   VAL(Tape) = 0;
1425   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1426   VAL(FrCount) = VAL(ToCount) = 0;
1427   VAL(FrDepth) = VAL(ToDepth) = 0;
1428
1429   fRev = fFalse;
1430   ctype = backctype = CTYPE_OMIT;
1431   fDepthChange = fFalse;
1432
1433   /* ordering may omit clino reading, so set up default here */
1434   /* this is also used if clino reading is the omit character */
1435   VAL(Clino) = VAL(BackClino) = 0;
1436
1437   again:
1438
1439   for (ordering = pcs->ordering; ; ordering++) {
1440      skipblanks();
1441      switch (*ordering) {
1442       case Fr:
1443          fr = read_prefix_stn(fFalse, fTrue);
1444          if (first_stn == End) first_stn = Fr;
1445          break;
1446       case To:
1447          to = read_prefix_stn(fFalse, fTrue);
1448          if (first_stn == End) first_stn = To;
1449          break;
1450       case Station:
1451          fr = to;
1452          to = read_prefix_stn(fFalse, fFalse);
1453          first_stn = To;
1454          break;
1455       case Dir: {
1456          typedef enum {
1457             DIR_NULL=-1, DIR_FORE, DIR_BACK
1458          } dir_tok;
1459          static sztok dir_tab[] = {
1460             {"B",     DIR_BACK},
1461             {"F",     DIR_FORE},
1462          };
1463          dir_tok tok;
1464          get_token();
1465          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1466          switch (tok) {
1467           case DIR_FORE:
1468             break;
1469           case DIR_BACK:
1470             fRev = fTrue;
1471             break;
1472           default:
1473             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1474                                buffer);
1475             process_eol();
1476             return 0;
1477          }
1478          break;
1479       }
1480       case Tape:
1481          read_reading(Tape, fFalse);
1482          if (VAL(Tape) < (real)0.0)
1483             compile_warning(/*Negative tape reading*/60);
1484          break;
1485       case Count:
1486          VAL(FrCount) = VAL(ToCount);
1487          read_reading(ToCount, fFalse);
1488          fTopofil = fTrue;
1489          break;
1490       case FrCount:
1491          read_reading(FrCount, fFalse);
1492          break;
1493       case ToCount:
1494          read_reading(ToCount, fFalse);
1495          fTopofil = fTrue;
1496          break;
1497       case Comp:
1498          read_bearing_or_omit(Comp);
1499          break;
1500       case BackComp:
1501          read_bearing_or_omit(BackComp);
1502          break;
1503       case Clino:
1504          read_reading(Clino, fTrue);
1505          if (VAL(Clino) == HUGE_REAL) {
1506             VAL(Clino) = handle_plumb(&ctype);
1507             if (VAL(Clino) != HUGE_REAL) break;
1508             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1509             process_eol();
1510             return 0;
1511          }
1512          ctype = CTYPE_READING;
1513          break;
1514       case BackClino:
1515          read_reading(BackClino, fTrue);
1516          if (VAL(BackClino) == HUGE_REAL) {
1517             VAL(BackClino) = handle_plumb(&backctype);
1518             if (VAL(BackClino) != HUGE_REAL) break;
1519             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1520             process_eol();
1521             return 0;
1522          }
1523          backctype = CTYPE_READING;
1524          break;
1525       case FrDepth:
1526          read_reading(FrDepth, fFalse);
1527          break;
1528       case ToDepth:
1529          read_reading(ToDepth, fFalse);
1530          break;
1531       case Depth:
1532          VAL(FrDepth) = VAL(ToDepth);
1533          read_reading(ToDepth, fFalse);
1534          break;
1535       case DepthChange:
1536          fDepthChange = fTrue;
1537          VAL(FrDepth) = 0;
1538          read_reading(ToDepth, fFalse);
1539          break;
1540       case CompassDATFlags:
1541          if (ch == '#') {
1542             nextch();
1543             if (ch == '|') {
1544                filepos fp;
1545                get_pos(&fp);
1546                nextch();
1547                while (ch >= 'A' && ch <= 'Z') {
1548                   compass_dat_flags |= BIT(ch - 'A');
1549                   nextch();
1550                }
1551                if (ch == '#') {
1552                   nextch();
1553                } else {
1554                   compass_dat_flags = 0;
1555                   set_pos(&fp);
1556                   push_back('|');
1557                   ch = '#';
1558                }
1559             } else {
1560                push_back(ch);
1561                ch = '#';
1562             }
1563          }
1564          break;
1565       case Ignore:
1566          skipword(); break;
1567       case IgnoreAllAndNewLine:
1568          skipline();
1569          /* fall through */
1570       case Newline:
1571          if (fr != NULL) {
1572             int r;
1573             if (fTopofil)
1574                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1575             /* Note: frdepth == todepth test works regardless of fDepthChange
1576              * (frdepth always zero, todepth is change of depth) and also
1577              * works for STYLE_NORMAL (both remain 0) */
1578             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1579                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1580                process_equate(fr, to);
1581                goto inferred_equate;
1582             }
1583             if (fRev) {
1584                prefix *t = fr;
1585                fr = to;
1586                to = t;
1587             }
1588             if (fTopofil) {
1589                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1590             } else {
1591                VAL(Tape) *= pcs->units[Q_LENGTH];
1592                VAL(Tape) -= pcs->z[Q_LENGTH];
1593                VAL(Tape) *= pcs->sc[Q_LENGTH];
1594             }
1595             switch (pcs->style) {
1596              case STYLE_NORMAL:
1597                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1598                                   ctype, backctype);
1599                break;
1600              case STYLE_DIVING:
1601                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1602                                   fDepthChange);
1603                break;
1604              case STYLE_CYLPOLAR:
1605                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1606                                     fDepthChange);
1607                break;
1608              default:
1609                r = 0; /* avoid warning */
1610                BUG("bad style");
1611             }
1612             if (!r) skipline();
1613             
1614             /* Swap fr and to back to how they were for next line */
1615             if (fRev) {
1616                prefix *t = fr;
1617                fr = to;
1618                to = t;
1619             }
1620          }
1621
1622          fRev = fFalse;
1623          ctype = backctype = CTYPE_OMIT;
1624          fDepthChange = fFalse;
1625
1626          /* ordering may omit clino reading, so set up default here */
1627          /* this is also used if clino reading is the omit character */
1628          VAL(Clino) = VAL(BackClino) = 0;
1629
1630          inferred_equate:
1631
1632          fMulti = fTrue;
1633          while (1) {
1634              process_eol();
1635              process_bol();
1636              if (isData(ch)) break;
1637              if (!isComm(ch)) {
1638                 push_back(ch);
1639                 return 1;
1640              }
1641          }
1642          break;
1643       case IgnoreAll:
1644          skipline();
1645          /* fall through */
1646       case End:
1647          if (!fMulti) {
1648             int r;
1649             /* Compass ignore flag is 'X' */
1650             if ((compass_dat_flags & BIT('X' - 'A'))) {
1651                process_eol();
1652                return 1;
1653             }
1654             if (fRev) {
1655                prefix *t = fr;
1656                fr = to;
1657                to = t;
1658             }
1659             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1660             /* Note: frdepth == todepth test works regardless of fDepthChange
1661              * (frdepth always zero, todepth is change of depth) and also
1662              * works for STYLE_NORMAL (both remain 0) */
1663             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1664                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
1665                process_equate(fr, to);
1666                process_eol();
1667                return 1;
1668             }
1669             if (fTopofil) {
1670                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1671             } else {
1672                VAL(Tape) *= pcs->units[Q_LENGTH];
1673                VAL(Tape) -= pcs->z[Q_LENGTH];
1674                VAL(Tape) *= pcs->sc[Q_LENGTH];
1675             }
1676             switch (pcs->style) {
1677              case STYLE_NORMAL:
1678                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1679                                   ctype, backctype);
1680                break;
1681              case STYLE_DIVING:
1682                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1683                                   fDepthChange);
1684                break;
1685              case STYLE_CYLPOLAR:
1686                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1687                                     fDepthChange);
1688                break;
1689              default:
1690                r = 0; /* Suppress compiler warning */;
1691                BUG("bad style");
1692             }
1693             process_eol();
1694             return r;
1695          }
1696          do {
1697             process_eol();
1698             process_bol();
1699          } while (isComm(ch));
1700          goto again;
1701       default:
1702          BUG("Unknown reading in ordering");
1703      }
1704   }
1705}
1706
1707static int
1708process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1709{
1710   nosurveylink *link;
1711   int shape;
1712
1713#ifdef CHASM3DX
1714   if (fUseNewFormat) {
1715      /* new twiglet and insert into twig tree */
1716      twig *twiglet = osnew(twig);
1717      twiglet->from = fr;
1718      twiglet->to = to;
1719      twiglet->down = twiglet->right = NULL;
1720      twiglet->source = twiglet->drawings
1721        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1722      twiglet->up = limb->up;
1723      limb->right = twiglet;
1724      limb = twiglet;
1725      /* delta is only used to calculate error - pass zero and cope
1726       * elsewhere */
1727      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1728   }
1729#endif
1730
1731   /* Suppress "unused fixed point" warnings for these stations
1732    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1733    */
1734   shape = fr->shape;
1735   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1736   shape = to->shape;
1737   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1738
1739   /* add to linked list which is dealt with after network is solved */
1740   link = osnew(nosurveylink);
1741   if (fToFirst) {
1742      link->to = StnFromPfx(to);
1743      link->fr = StnFromPfx(fr);
1744   } else {
1745      link->fr = StnFromPfx(fr);
1746      link->to = StnFromPfx(to);
1747   }
1748   link->flags = pcs->flags;
1749   link->next = nosurveyhead;
1750   nosurveyhead = link;
1751   return 1;
1752}
1753
1754extern int
1755data_nosurvey(void)
1756{
1757   prefix *fr = NULL, *to = NULL;
1758
1759   bool fMulti = fFalse;
1760
1761   reading first_stn = End;
1762
1763   reading *ordering;
1764
1765   again:
1766
1767   for (ordering = pcs->ordering ; ; ordering++) {
1768      skipblanks();
1769      switch (*ordering) {
1770       case Fr:
1771          fr = read_prefix_stn(fFalse, fTrue);
1772          if (first_stn == End) first_stn = Fr;
1773          break;
1774       case To:
1775          to = read_prefix_stn(fFalse, fTrue);
1776          if (first_stn == End) first_stn = To;
1777          break;
1778       case Station:
1779          fr = to;
1780          to = read_prefix_stn(fFalse, fFalse);
1781          first_stn = To;
1782          break;
1783       case Ignore:
1784         skipword(); break;
1785       case IgnoreAllAndNewLine:
1786         skipline();
1787         /* fall through */
1788       case Newline:
1789         if (fr != NULL) {
1790            int r;
1791            r = process_nosurvey(fr, to, first_stn == To);
1792            if (!r) skipline();
1793         }
1794         if (ordering[1] == End) {
1795            do {
1796               process_eol();
1797               process_bol();
1798            } while (isComm(ch));
1799            if (!isData(ch)) {
1800               push_back(ch);
1801               return 1;
1802            }
1803            goto again;
1804         }
1805         fMulti = fTrue;
1806         while (1) {
1807            process_eol();
1808            process_bol();
1809            if (isData(ch)) break;
1810            if (!isComm(ch)) {
1811               push_back(ch);
1812               return 1;
1813            }
1814         }
1815         break;
1816       case IgnoreAll:
1817         skipline();
1818         /* fall through */
1819       case End:
1820         if (!fMulti) {
1821            int r = process_nosurvey(fr, to, first_stn == To);
1822            process_eol();
1823            return r;
1824         }
1825         do {
1826            process_eol();
1827            process_bol();
1828         } while (isComm(ch));
1829         goto again;
1830       default: BUG("Unknown reading in ordering");
1831      }
1832   }
1833}
1834
1835/* totally ignore a line of survey data */
1836extern int
1837data_ignore(void)
1838{
1839   skipline();
1840   process_eol();
1841   return 1;
1842}
Note: See TracBrowser for help on using the repository browser.