source: git/src/datain.c @ ca7769e

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 ca7769e was 07442af, checked in by Olly Betts <olly@…>, 20 years ago

Next step towards LRUD support.

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

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