source: git/src/datain.c @ d9e6d7c

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 d9e6d7c was 0580c6a, checked in by Olly Betts <olly@…>, 21 years ago

Copy changes over from 1.0 branch - everything except Printing from Aven, and
Exporting CAD formats from Aven.

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

  • Property mode set to 100644
File size: 46.2 KB
RevLine 
[b0d908e]1/* datain.c
[d1b1380]2 * Reads in survey files, dealing with special characters, keywords & data
[b5a3219]3 * Copyright (C) 1991-2003 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[d1b1380]18 */
19
[a420b49]20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
[d1b1380]23
24#include <limits.h>
[a420b49]25#include <stdarg.h>
[b5a3219]26#include <time.h>
[d1b1380]27
28#include "debug.h"
[a420b49]29#include "cavern.h"
[07025e3]30#include "filename.h"
31#include "message.h"
[d1b1380]32#include "filelist.h"
33#include "netbits.h"
[5853657]34#include "netskel.h"
[d1b1380]35#include "readval.h"
36#include "datain.h"
37#include "commands.h"
38#include "out.h"
[39fba51]39#include "str.h"
[d1b1380]40
[2b078c4]41#define EPSILON (REAL_EPSILON * 1000)
[d1b1380]42
[58cc1fb]43#define var(I) (pcs->Var[(I)])
[d1b1380]44
[58cc1fb]45int ch;
[d1b1380]46
[be97baf]47typedef enum {
48    CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_INFERPLUMB, CTYPE_HORIZ
49} clino_type;
[5b7c1b7]50
[eb18f4d]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 */
[b0d908e]53parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
[a420b49]54
[932f7e9]55bool f_export_ok;
56
[21c226e]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
[c7451a9]62void
[c80bd34]63get_pos(filepos *fp)
[c7451a9]64{
[c80bd34]65   fp->ch = ch;
66   fp->offset = ftell(file.fh);
67   if (fp->offset == -1)
[c7451a9]68      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
69}
70
[c80bd34]71void
72set_pos(const filepos *fp)
[c7451a9]73{
[c80bd34]74   ch = fp->ch;
75   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
[c7451a9]76      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
77}
78
[44bf1d9]79static void
[993454b]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
[a420b49]86static void
87error_list_parent_files(void)
88{
[b0d908e]89   if (!file.reported_where && file.parent) {
[421b7d2]90      parse *p = file.parent;
[46cb98f]91      const char *m = msg(/*In file included from*/5);
[84c60fc]92      size_t len = strlen(m);
[b0d908e]93
[84c60fc]94      fprintf(STDERR, m);
[46cb98f]95      m = msg(/*from*/3);
[b0d908e]96
97      /* Suppress reporting of full include tree for further errors
98       * in this file */
99      file.reported_where = fTrue;
100
[a420b49]101      while (p) {
[b0d908e]102         /* Force re-report of include tree for further errors in
103          * parent files */
104         p->reported_where = fFalse;
[46cb98f]105         fprintf(STDERR, " %s:%d", p->filename, p->line);
[a420b49]106         p = p->parent;
[84f3ed6]107         if (p) fprintf(STDERR, ",\n%*s", (int)len, m);
[a420b49]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
[fa42426]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
[39fba51]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)) {
[63dc4eb]142      s_catchar(&p, &len, (char)ch);
[39fba51]143      nextch();
144   }
[fa42426]145   compile_error_skip(en, p ? p : "");
[39fba51]146   osfree(p);
147}
148
[a420b49]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) {
[4bab027]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;
[58cc1fb]175   }
176}
177
[a420b49]178static void
179skipword(void)
180{
[58cc1fb]181   while (!isBlank(ch) && !isEol(ch)) nextch();
[d1b1380]182}
183
[a420b49]184extern void
185skipblanks(void)
186{
[58cc1fb]187   while (isBlank(ch)) nextch();
[d1b1380]188}
189
[a420b49]190extern void
191skipline(void)
192{
[58cc1fb]193   while (!isEol(ch)) nextch();
[d1b1380]194}
195
[90bb053f]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 */
[be97baf]205   if (filelen > 0) {
206      filepos fp;
207      get_pos(&fp);
208      printf("%d%%\r", (int)(100 * fp.offset / filelen));
209   }
[90bb053f]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)) {
[44bf1d9]224      if (!isComm(ch)) compile_error(/*End of line not blank*/15);
[90bb053f]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)) {
[993454b]236         push_back(ch);
[90bb053f]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
[21c226e]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;
[74a0681]276      default:
277        q = Q_NULL; /* Suppress compiler warning */;
278        BUG("Unexpected case");
[21c226e]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;
[74a0681]288   q_quantity q = Q_NULL;
[21c226e]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;
[74a0681]293      default:
294        q = Q_NULL; /* Suppress compiler warning */;
295        BUG("Unexpected case");
[21c226e]296   }
297   VAR(r) = var(q);
298   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
299}
300
[be97baf]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)
[a420b49]316extern void
317data_file(const char *pth, const char *fnm)
318{
[47c7a94]319   int begin_lineno_store;
[7f08c83]320   parse file_store;
[b4fe9fb]321   volatile enum {FMT_SVX, FMT_DAT, FMT_MAK} fmt = FMT_SVX;
[d1b1380]322
[7f08c83]323   {
324      char *filename;
[f4b609d]325      FILE *fh;
[be97baf]326      size_t len;
327
[f4b609d]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      }
[bd1913f]334
[7f08c83]335      if (fh == NULL) {
[759fb47]336         compile_error(/*Couldn't open data file `%s'*/24, fnm);
[7f08c83]337         return;
338      }
339
[be97baf]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
[7f08c83]347      file_store = file;
348      if (file.fh) file.parent = &file_store;
349      file.fh = fh;
350      file.filename = filename;
351      file.line = 1;
[b0d908e]352      file.reported_where = fFalse;
[a420b49]353   }
[cb3d1e2]354
[5b68ae1]355   if (fPercent) printf("%s:\n", fnm);
[d1b1380]356
[a420b49]357   using_data_file(file.filename);
[d1b1380]358
[47c7a94]359   begin_lineno_store = pcs->begin_lineno;
360   pcs->begin_lineno = 0;
[cb3d1e2]361
[d1b1380]362#ifndef NO_PERCENTAGE
[58cc1fb]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) {
[c80bd34]367      if (fseek(file.fh, 0l, SEEK_END) == 0) {
368         filepos fp;
369         get_pos(&fp);
370         filelen = fp.offset;
371      }
[58cc1fb]372      rewind(file.fh); /* reset file ptr to start & clear any error state */
373   }
[d1b1380]374#endif
375
[be97baf]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
[7d5f3c0]442#ifdef HAVE_SETJMP_H
[a420b49]443   /* errors in nested functions can longjmp here */
444   if (setjmp(file.jbSkipLine)) {
[076d33d]445      process_eol();
[a420b49]446   }
[d1b1380]447#endif
[cb3d1e2]448
[be97baf]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> */
[b5a3219]474         /* NB order is *month* *day* year */
[be97baf]475         get_token();
476         get_token();
[b5a3219]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         }
[be97baf]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();
[107b8bd]521         }
[be97baf]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;
[a420b49]531         }
[be97baf]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;
[b4fe9fb]569            char *dat_pth = path_from_fnm(file.filename);
570            char *dat_fnm = NULL;
571            int dat_fnm_len;
[be97baf]572            nextch_handling_eol();
573            while (ch != ',' && ch != ';' && ch != EOF) {
574               while (isEol(ch)) process_eol();
[63dc4eb]575               s_catchar(&dat_fnm, &dat_fnm_len, (char)ch);
[be97baf]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            }
[b4fe9fb]632            if (dat_fnm) {
[be97baf]633               ch_store = ch;
[b4fe9fb]634               data_file(dat_pth, dat_fnm);
[be97baf]635               ch = ch_store;
[b4fe9fb]636               osfree(dat_fnm);
[be97baf]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()) {
[b4fe9fb]652            volatile int r;
[be97baf]653#ifdef CHASM3DX
654            twig *temp = limb;
[21c226e]655#endif
[be97baf]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:
[74a0681]673               r = 0; /* avoid warning */
[be97baf]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         }
[647407d]686      }
[a420b49]687   }
688
[932f7e9]689   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
690    * including file */
691   f_export_ok = fFalse;
692
[a420b49]693#ifndef NO_PERCENTAGE
694   if (fPercent) putnl();
[4f8285d]695#endif
[d1b1380]696
[47c7a94]697   if (pcs->begin_lineno) {
[0e867ba6]698      error_in_file(file.filename, pcs->begin_lineno,
699                    /*BEGIN with no matching END in this file*/23);
[47c7a94]700      /* Implicitly close any unclosed BEGINs from this file */
701      do {
702         settings *pcsParent = pcs->next;
[4c07c51]703         SVX_ASSERT(pcsParent);
[647407d]704         free_settings(pcs);
[47c7a94]705         pcs = pcsParent;
706      } while (pcs->begin_lineno);
707   }
708
709   pcs->begin_lineno = begin_lineno_store;
[cb3d1e2]710
[22c9877]711   if (ferror(file.fh))
[bfe1242]712      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
[cb3d1e2]713
[22c9877]714   (void)fclose(file.fh);
[7f08c83]715
[22c9877]716   file = file_store;
[4d9eecd]717
718   /* don't free this - it may be pointed to by prefix.file */
719   /* osfree(file.filename); */
[a420b49]720}
[d1b1380]721
[b6de07d]722static real
723mod2pi(real a)
724{
725   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
726}
727
[dcec245]728static real
[5b7c1b7]729handle_plumb(clino_type *p_ctype)
[dcec245]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   };
[5b7c1b7]743   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
[dcec245]744   clino_tok tok;
745
746   skipblanks();
747   if (isalpha(ch)) {
[c80bd34]748      filepos fp;
749      get_pos(&fp);
[dcec245]750      get_token();
751      tok = match_tok(clino_tab, TABSIZE(clino_tab));
752      if (tok != CLINO_NULL) {
[be97baf]753         *p_ctype = (tok == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
[dcec245]754         return clinos[tok];
755      }
[c80bd34]756      set_pos(&fp);
[dcec245]757   } else if (isSign(ch)) {
758      int chOld = ch;
759      nextch();
760      if (toupper(ch) == 'V') {
761         nextch();
[fa42426]762         *p_ctype = CTYPE_PLUMB;
[bceebf4]763         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
[dcec245]764      }
765
[bd1913f]766      if (isOmit(chOld)) {
[fa42426]767         *p_ctype = CTYPE_OMIT;
[dcec245]768         /* no clino reading, so assume 0 with large sd */
769         return (real)0.0;
770      }
[5b7c1b7]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();
[fa42426]775      *p_ctype = CTYPE_OMIT;
[5b7c1b7]776      /* no clino reading, so assume 0 with large sd */
777      return (real)0.0;
[dcec245]778   }
779   return HUGE_REAL;
780}
781
[b6de07d]782static void
[b4fe9fb]783warn_readings_differ(int msgno, real diff)
[b6de07d]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   }
[b4fe9fb]796   compile_warning(msgno, buf);
[b6de07d]797}
798
799static bool
[21c226e]800handle_comp_units(void)
[b6de07d]801{
802   bool fNoComp = fTrue;
[21c226e]803   if (VAL(Comp) != HUGE_REAL) {
[b6de07d]804      fNoComp = fFalse;
[21c226e]805      VAL(Comp) *= pcs->units[Q_BEARING];
806      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
[b6de07d]807         compile_warning(/*Suspicious compass reading*/59);
[21c226e]808         VAL(Comp) = mod2pi(VAL(Comp));
[b6de07d]809      }
810   }
[21c226e]811   if (VAL(BackComp) != HUGE_REAL) {
[b6de07d]812      fNoComp = fFalse;
[21c226e]813      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
814      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
[b6de07d]815         /* FIXME: different message for BackComp? */
816         compile_warning(/*Suspicious compass reading*/59);
[21c226e]817         VAL(BackComp) = mod2pi(VAL(BackComp));
[b6de07d]818      }
819   }
820   return fNoComp;
821}
822
823static real
[21c226e]824handle_compass(real *p_var)
[b6de07d]825{
[b4fe9fb]826   real compvar = VAR(Comp);
[21c226e]827   real comp = VAL(Comp);
828   real backcomp = VAL(BackComp);
[b6de07d]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;
[b4fe9fb]842         if (sqrd(diff / 2.0) > compvar + VAR(Q_BACKBEARING)) {
[770157e]843            /* fore and back readings differ by more than 2 sds */
[fa42426]844            warn_readings_differ(/*Compass reading and back compass reading disagree by %s degrees*/98, diff);
[b6de07d]845         }
[b4fe9fb]846         comp = (comp / compvar + backcomp / VAR(BackComp));
847         compvar = (compvar + VAR(BackComp)) / 4;
848         comp *= compvar;
[b6de07d]849         comp += adj;
850      } else {
851         comp = backcomp;
[b4fe9fb]852         compvar = VAR(BackComp);
[b6de07d]853      }
854   }
[b4fe9fb]855   *p_var = compvar;
[b6de07d]856   return comp;
857}
858
[90bb053f]859static int
[21c226e]860process_normal(prefix *fr, prefix *to, bool fToFirst,
861               clino_type ctype, clino_type backctype)
[a420b49]862{
[21c226e]863   real tape = VAL(Tape);
864   real clin = VAL(Clino);
865   real backclin = VAL(BackClino);
866
[a420b49]867   real dx, dy, dz;
868   real vx, vy, vz;
869#ifndef NO_COVARIANCES
[cb3d1e2]870   real cxy, cyz, czx;
[a420b49]871#endif
[d1b1380]872
[90bb053f]873   bool fNoComp;
[a420b49]874
[107b8bd]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);
[647407d]879   }
[d1b1380]880
[21c226e]881   fNoComp = handle_comp_units();
[d1b1380]882
[fa42426]883   if (ctype == CTYPE_READING) {
[0b71cfc]884      real diff_from_abs90;
[a420b49]885      clin *= pcs->units[Q_GRADIENT];
[fa42426]886      /* percentage scale */
887      if (pcs->f_clino_percent) clin = atan(clin);
[0b71cfc]888      diff_from_abs90 = fabs(clin) - M_PI_2;
889      if (diff_from_abs90 > EPSILON) {
[a420b49]890         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
[27b8b59]891      } else if (TSTBIT(pcs->infer, INFER_PLUMBS) &&
[be97baf]892                 diff_from_abs90 >= -EPSILON) {
893         ctype = CTYPE_INFERPLUMB;
[a420b49]894      }
895   }
[cb3d1e2]896
[fa42426]897   if (backctype == CTYPE_READING) {
[b14f44f]898      backclin *= pcs->units[Q_BACKGRADIENT];
[fa42426]899      /* percentage scale */
900      if (pcs->f_backclino_percent) backclin = atan(backclin);
[be97baf]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         }
[0b71cfc]910      }
911   }
912
[be97baf]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
[fa42426]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);
[b14f44f]920      return 0;
[0b71cfc]921   }
922
[be97baf]923   if (ctype == CTYPE_PLUMB || ctype == CTYPE_INFERPLUMB ||
924       backctype == CTYPE_PLUMB || backctype == CTYPE_INFERPLUMB) {
[a420b49]925      /* plumbed */
926      if (!fNoComp) {
[be97baf]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         }
[a420b49]934      }
935
936      dx = dy = (real)0.0;
[fa42426]937      if (ctype != CTYPE_OMIT) {
938         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
[b14f44f]939            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
[fa42426]940            compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
[b14f44f]941            return 0;
[0b71cfc]942         }
943         dz = (clin > (real)0.0) ? tape : -tape;
944      } else {
945         dz = (backclin < (real)0.0) ? tape : -tape;
946      }
[a420b49]947      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]948      vz = var(Q_POS) / 3.0 + VAR(Tape);
[4f8285d]949#ifndef NO_COVARIANCES
[26a805f]950      /* Correct values - no covariances in this case! */
951      cxy = cyz = czx = (real)0.0;
[4f8285d]952#endif
[a420b49]953   } else {
[fa42426]954      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
955       * or CTYPE_OMIT */
[a420b49]956      /* clino */
957      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
[421b7d2]958      if (fNoComp) {
[fa42426]959         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
[a420b49]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 */
[4f8285d]965#ifndef NO_COVARIANCES
[a420b49]966         cxy = cyz = czx = (real)0.0;
[4f8285d]967#endif
[d1b1380]968#if DEBUG_DATAIN_1
[a420b49]969         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]970#endif
[a420b49]971      } else {
[dbb4e19]972         real sinGcosG;
[0b71cfc]973         /* take into account variance in LEVEL case */
974         real var_clin = var(Q_LEVEL);
[b6de07d]975         real var_comp;
[21c226e]976         real comp = handle_compass(&var_comp);
[fa42426]977         /* ctype != CTYPE_READING is LEVEL case */
978         if (ctype == CTYPE_READING) {
[a420b49]979            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
[21c226e]980            var_clin = VAR(Clino);
[0b71cfc]981         }
[fa42426]982         if (backctype == CTYPE_READING) {
[b14f44f]983            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
984               * pcs->sc[Q_BACKGRADIENT];
[fa42426]985            if (ctype == CTYPE_READING) {
[be97baf]986               if (sqrd((clin + backclin) / 3.0) > var_clin + VAR(BackClino)) {
[b14f44f]987                  /* fore and back readings differ by more than 3 sds */
[fa42426]988                  warn_readings_differ(/*Clino reading and back clino reading disagree by %s degrees*/99, clin + backclin);
[b14f44f]989               }
[21c226e]990               clin = (clin / var_clin - backclin / VAR(BackClino));
991               var_clin = (var_clin + VAR(BackClino)) / 4;
[b14f44f]992               clin *= var_clin;
[0b71cfc]993            } else {
994               clin = -backclin;
[21c226e]995               var_clin = VAR(BackClino);
[0b71cfc]996            }
997         }
[d1b1380]998
999#if DEBUG_DATAIN
[a420b49]1000         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
[d1b1380]1001#endif
[a420b49]1002         cosG = cos(clin);
1003         LcosG = tape * cosG;
1004         sinB = sin(comp);
1005         cosB = cos(comp);
[d1b1380]1006#if DEBUG_DATAIN_1
[a420b49]1007         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
[d1b1380]1008#endif
[a420b49]1009         dx = LcosG * sinB;
1010         dy = LcosG * cosB;
1011         dz = tape * sin(clin);
[d1b1380]1012/*      printf("%.2f\n",clin); */
1013#if DEBUG_DATAIN_1
[a420b49]1014         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
[d1b1380]1015#endif
[a420b49]1016         dx2 = dx * dx;
1017         L2 = tape * tape;
[21c226e]1018         V = VAR(Tape) / L2;
[a420b49]1019         dy2 = dy * dy;
1020         cosG2 = cosG * cosG;
[dbb4e19]1021         sinGcosG = sin(clin) * cosG;
[a420b49]1022         dz2 = dz * dz;
[0b71cfc]1023         v = dz2 * var_clin;
[5b7c1b7]1024#ifdef NO_COVARIANCES
[0b71cfc]1025         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[a420b49]1026               (.5 + sinB * sinB * cosG2) * v);
[0b71cfc]1027         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[a420b49]1028               (.5 + cosB * cosB * cosG2) * v);
[fa42426]1029         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1030            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1031            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1032         } else {
[0b71cfc]1033            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1034         }
[a420b49]1035         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
[dbb4e19]1036#else
[0b71cfc]1037         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
[dbb4e19]1038            (sinB * sinB * v);
[0b71cfc]1039         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
[dbb4e19]1040            (cosB * cosB * v);
[fa42426]1041         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
[5b7c1b7]1042            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
[0b71cfc]1043            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
[5b7c1b7]1044         } else {
[0b71cfc]1045            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
[5b7c1b7]1046         }
[647407d]1047         /* usual covariance formulae are fine in no clino case since
[0b71cfc]1048          * dz = 0 so value of var_clin is ignored */
[21c226e]1049         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
[0b71cfc]1050               - var_comp * dx * dy;
[21c226e]1051         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
1052         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
[dbb4e19]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
[4f8285d]1057#endif
[d1b1380]1058#if DEBUG_DATAIN_1
[a420b49]1059         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
[d1b1380]1060#endif
[a420b49]1061      }
1062   }
[d1b1380]1063#if DEBUG_DATAIN_1
[a420b49]1064   printf("Just before addleg, vx = %f\n", vx);
[d1b1380]1065#endif
[a420b49]1066   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
[90bb053f]1067   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[4f8285d]1068#ifndef NO_COVARIANCES
[2140502]1069                , cyz, czx, cxy
[4f8285d]1070#endif
[2140502]1071                );
[647407d]1072
[be97baf]1073#ifdef CHASM3DX
[647407d]1074   if (fUseNewFormat) {
[407084d]1075      /* new twiglet and insert into twig tree */
1076      twig *twiglet = osnew(twig);
[90bb053f]1077      twiglet->from = fr;
1078      twiglet->to = to;
[407084d]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 */
[647407d]1087      twiglet->delta[0] = dx;
1088      twiglet->delta[1] = dy;
1089      twiglet->delta[2] = dz;
1090   }
1091#endif
[a420b49]1092   return 1;
[d1b1380]1093}
1094
[5a38209]1095static int
[21c226e]1096process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[a420b49]1097{
[21c226e]1098   real tape = VAL(Tape);
1099
[a420b49]1100   real dx, dy, dz;
1101   real vx, vy, vz;
[f795df0]1102#ifndef NO_COVARIANCES
1103   real cxy = 0, cyz = 0, czx = 0;
1104#endif
[a420b49]1105
[21c226e]1106   handle_comp_units();
[a420b49]1107
[a6d094f]1108   /* depth gauge readings increase upwards with default calibration */
[6114207]1109   if (fDepthChange) {
[21c226e]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];
[a6d094f]1113   } else {
[21c226e]1114      dz = VAL(ToDepth) - VAL(FrDepth);
1115      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1116   }
[a420b49]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)) {
[bd1913f]1125      /* FIXME: allow margin of error based on variances? */
[a420b49]1126      compile_warning(/*Tape reading is less than change in depth*/62);
1127   }
1128
[0a208f9]1129   if (tape == (real)0.0 && dz == 0.0) {
[a420b49]1130      dx = dy = dz = (real)0.0;
1131      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
[21c226e]1132   } else if (VAL(Comp) == HUGE_REAL &&
1133              VAL(BackComp) == HUGE_REAL) {
[385b703]1134      /* plumb */
1135      dx = dy = (real)0.0;
1136      if (dz < 0) tape = -tape;
[21c226e]1137      /* FIXME: Should use FrDepth sometimes... */
1138      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
1139         / (VAR(Tape) * 2 * VAR(ToDepth));
[385b703]1140      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1141      /* FIXME: Should use FrDepth sometimes... */
1142      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
1143                              / (VAR(Tape) + VAR(ToDepth));
[a420b49]1144   } else {
[f795df0]1145      real L2, sinB, cosB, dz2, D2;
[b6de07d]1146      real var_comp;
[21c226e]1147      real comp = handle_compass(&var_comp);
[a420b49]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) {
[21c226e]1154         /* FIXME: Should use FrDepth sometimes... */
1155         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
[a420b49]1156         dx = dy = (real)0.0;
[f795df0]1157         vx = vy = var(Q_POS) / 3.0;
[21c226e]1158         /* FIXME: Should use FrDepth sometimes... */
1159         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
[f795df0]1160         if (dz > 0) {
[21c226e]1161            /* FIXME: Should use FrDepth sometimes... */
1162            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1163         } else {
[21c226e]1164            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
[f795df0]1165         }
[a420b49]1166      } else {
1167         real D = sqrt(D2);
[21c226e]1168         /* FIXME: Should use FrDepth sometimes... */
1169         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
[a420b49]1170         dx = D * sinB;
1171         dy = D * cosB;
[f795df0]1172
1173         vx = var(Q_POS) / 3.0 +
[72095d3]1174            sinB * sinB * F / D2 + var_comp * dy * dy;
[f795df0]1175         vy = var(Q_POS) / 3.0 +
[72095d3]1176            cosB * cosB * F / D2 + var_comp * dx * dx;
[21c226e]1177         /* FIXME: Should use FrDepth sometimes... */
1178         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[f795df0]1179
1180#ifndef NO_COVARIANCES
[72095d3]1181         cxy = sinB * cosB * (F / D2 + var_comp * D2);
[21c226e]1182         /* FIXME: Should use FrDepth sometimes... */
1183         cyz = -2 * VAR(ToDepth) * dy / D;
1184         czx = -2 * VAR(ToDepth) * dx / D;
[f795df0]1185#endif
[a420b49]1186      }
1187   }
[5a38209]1188   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
[2140502]1189#ifndef NO_COVARIANCES
[f795df0]1190                , cxy, cyz, czx
[647407d]1191#endif
[2140502]1192                );
[be97baf]1193#ifdef CHASM3DX
[647407d]1194   if (fUseNewFormat) {
[be97baf]1195      /* new twiglet and insert into twig tree */
[407084d]1196      twig *twiglet = osnew(twig);
[90bb053f]1197      twiglet->from = fr;
1198      twiglet->to = to;
[407084d]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 */
[647407d]1207      twiglet->delta[0] = dx;
1208      twiglet->delta[1] = dy;
1209      twiglet->delta[2] = dz;
1210   }
1211#endif
1212
1213   return 1;
1214}
1215
[5a38209]1216static int
[21c226e]1217process_cartesian(prefix *fr, prefix *to, bool fToFirst)
[5a38209]1218{
[21c226e]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];
[647407d]1222
[21c226e]1223   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
[647407d]1224#ifndef NO_COVARIANCES
[c80bd34]1225                , 0, 0, 0
[647407d]1226#endif
[2140502]1227                );
[647407d]1228
[be97baf]1229#ifdef CHASM3DX
[647407d]1230   if (fUseNewFormat) {
[407084d]1231      /* new twiglet and insert into twig tree */
1232      twig *twiglet = osnew(twig);
[90bb053f]1233      twiglet->from = fr;
1234      twiglet->to = to;
[407084d]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 */
[647407d]1243      twiglet->delta[0] = dx;
1244      twiglet->delta[1] = dy;
1245      twiglet->delta[2] = dz;
1246   }
[4f8285d]1247#endif
[647407d]1248
1249   return 1;
1250}
1251
1252extern int
[5a38209]1253data_cartesian(void)
[647407d]1254{
[90bb053f]1255   prefix *fr = NULL, *to = NULL;
[5a38209]1256
1257   bool fMulti = fFalse;
[647407d]1258
[0395657]1259   reading first_stn = End;
1260
1261   reading *ordering;
[647407d]1262
[5a38209]1263   again:
1264
[647407d]1265   for (ordering = pcs->ordering ; ; ordering++) {
1266      skipblanks();
1267      switch (*ordering) {
[4a6a094]1268       case Fr:
[21c226e]1269         fr = read_prefix_stn(fFalse, fTrue);
1270         if (first_stn == End) first_stn = Fr;
1271         break;
[4a6a094]1272       case To:
[21c226e]1273         to = read_prefix_stn(fFalse, fTrue);
1274         if (first_stn == End) first_stn = To;
1275         break;
[5a38209]1276       case Station:
[21c226e]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;
[647407d]1284       case Ignore:
1285         skipword(); break;
[5a38209]1286       case IgnoreAllAndNewLine:
1287         skipline();
1288         /* fall through */
1289       case Newline:
1290         if (fr != NULL) {
1291            int r;
[21c226e]1292            r = process_cartesian(fr, to, first_stn == To);
[5a38209]1293            if (!r) skipline();
1294         }
1295         fMulti = fTrue;
1296         while (1) {
1297            process_eol();
1298            process_bol();
1299            if (isData(ch)) break;
[ee6a621]1300            if (!isComm(ch)) {
[993454b]1301               push_back(ch);
[ee6a621]1302               return 1;
1303            }
[5a38209]1304         }
1305         break;
[647407d]1306       case IgnoreAll:
1307         skipline();
1308         /* fall through */
1309       case End:
[5a38209]1310         if (!fMulti) {
[21c226e]1311            int r = process_cartesian(fr, to, first_stn == To);
[5a38209]1312            process_eol();
1313            return r;
1314         }
[eef4d8c]1315         do {
[5a38209]1316            process_eol();
1317            process_bol();
[eef4d8c]1318         } while (isComm(ch));
[5a38209]1319         goto again;
[0395657]1320       default: BUG("Unknown reading in ordering");
[647407d]1321      }
1322   }
[5a38209]1323}
[cb3d1e2]1324
[98a2eec]1325static int
[21c226e]1326process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
[98a2eec]1327{
[21c226e]1328   real tape = VAL(Tape);
1329
[98a2eec]1330   real dx, dy, dz;
1331   real vx, vy, vz;
1332#ifndef NO_COVARIANCES
1333   real cxy = 0;
1334#endif
1335
[21c226e]1336   handle_comp_units();
[98a2eec]1337
[a6d094f]1338   /* depth gauge readings increase upwards with default calibration */
[6114207]1339   if (fDepthChange) {
[21c226e]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];
[a6d094f]1343   } else {
[21c226e]1344      dz = VAL(ToDepth) - VAL(FrDepth);
1345      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
[a6d094f]1346   }
[98a2eec]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
[21c226e]1353   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
[98a2eec]1354      /* plumb */
1355      dx = dy = (real)0.0;
1356      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
[21c226e]1357      /* FIXME: Should use FrDepth sometimes... */
1358      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
[98a2eec]1359   } else {
1360      real sinB, cosB;
[b6de07d]1361      real var_comp;
[21c226e]1362      real comp = handle_compass(&var_comp);
[98a2eec]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 +
[21c226e]1370         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
[98a2eec]1371      vy = var(Q_POS) / 3.0 +
[21c226e]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);
[98a2eec]1375
1376#ifndef NO_COVARIANCES
[21c226e]1377      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
[98a2eec]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                );
[be97baf]1385#ifdef CHASM3DX
[98a2eec]1386   if (fUseNewFormat) {
[be97baf]1387      /* new twiglet and insert into twig tree */
[98a2eec]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
[107b8bd]1408/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1409 * Also handles topofil (fromcount/tocount or count) in place of tape */
[54c4612]1410extern int
[107b8bd]1411data_normal(void)
[54c4612]1412{
1413   prefix *fr = NULL, *to = NULL;
[107b8bd]1414   reading first_stn = End;
[54c4612]1415
[107b8bd]1416   bool fTopofil = fFalse, fMulti = fFalse;
1417   bool fRev;
[e217d67]1418   clino_type ctype, backctype;
[107b8bd]1419   bool fDepthChange;
[be97baf]1420   unsigned long compass_dat_flags = 0;
[54c4612]1421
1422   reading *ordering;
1423
[21c226e]1424   VAL(Tape) = 0;
1425   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1426   VAL(FrCount) = VAL(ToCount) = 0;
1427   VAL(FrDepth) = VAL(ToDepth) = 0;
[54c4612]1428
1429   fRev = fFalse;
[fa42426]1430   ctype = backctype = CTYPE_OMIT;
[6114207]1431   fDepthChange = fFalse;
[54c4612]1432
[107b8bd]1433   /* ordering may omit clino reading, so set up default here */
1434   /* this is also used if clino reading is the omit character */
[21c226e]1435   VAL(Clino) = VAL(BackClino) = 0;
1436
1437   again:
[107b8bd]1438
[54c4612]1439   for (ordering = pcs->ordering; ; ordering++) {
1440      skipblanks();
1441      switch (*ordering) {
1442       case Fr:
[107b8bd]1443          fr = read_prefix_stn(fFalse, fTrue);
1444          if (first_stn == End) first_stn = Fr;
1445          break;
[54c4612]1446       case To:
[107b8bd]1447          to = read_prefix_stn(fFalse, fTrue);
1448          if (first_stn == End) first_stn = To;
1449          break;
[54c4612]1450       case Station:
[107b8bd]1451          fr = to;
1452          to = read_prefix_stn(fFalse, fFalse);
1453          first_stn = To;
1454          break;
[ee1ec59]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:
[54c4612]1468             break;
[ee1ec59]1469           case DIR_BACK:
[54c4612]1470             fRev = fTrue;
1471             break;
1472           default:
[ee1ec59]1473             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1474                                buffer);
[54c4612]1475             process_eol();
1476             return 0;
1477          }
1478          break;
[ee1ec59]1479       }
[107b8bd]1480       case Tape:
[21c226e]1481          read_reading(Tape, fFalse);
1482          if (VAL(Tape) < (real)0.0)
1483             compile_warning(/*Negative tape reading*/60);
[107b8bd]1484          break;
1485       case Count:
[21c226e]1486          VAL(FrCount) = VAL(ToCount);
1487          read_reading(ToCount, fFalse);
[b4fe9fb]1488          fTopofil = fTrue;
[107b8bd]1489          break;
1490       case FrCount:
[21c226e]1491          read_reading(FrCount, fFalse);
[107b8bd]1492          break;
1493       case ToCount:
[21c226e]1494          read_reading(ToCount, fFalse);
[107b8bd]1495          fTopofil = fTrue;
1496          break;
1497       case Comp:
[21c226e]1498          read_bearing_or_omit(Comp);
[107b8bd]1499          break;
[5b7c1b7]1500       case BackComp:
[21c226e]1501          read_bearing_or_omit(BackComp);
[5b7c1b7]1502          break;
[5757725]1503       case Clino:
[21c226e]1504          read_reading(Clino, fTrue);
1505          if (VAL(Clino) == HUGE_REAL) {
1506             VAL(Clino) = handle_plumb(&ctype);
1507             if (VAL(Clino) != HUGE_REAL) break;
[107b8bd]1508             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1509             process_eol();
1510             return 0;
1511          }
[fa42426]1512          ctype = CTYPE_READING;
[5b7c1b7]1513          break;
1514       case BackClino:
[21c226e]1515          read_reading(BackClino, fTrue);
1516          if (VAL(BackClino) == HUGE_REAL) {
1517             VAL(BackClino) = handle_plumb(&backctype);
1518             if (VAL(BackClino) != HUGE_REAL) break;
[5b7c1b7]1519             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1520             process_eol();
1521             return 0;
1522          }
[fa42426]1523          backctype = CTYPE_READING;
[107b8bd]1524          break;
1525       case FrDepth:
[21c226e]1526          read_reading(FrDepth, fFalse);
[107b8bd]1527          break;
1528       case ToDepth:
[21c226e]1529          read_reading(ToDepth, fFalse);
[107b8bd]1530          break;
[54c4612]1531       case Depth:
[21c226e]1532          VAL(FrDepth) = VAL(ToDepth);
1533          read_reading(ToDepth, fFalse);
[54c4612]1534          break;
[6114207]1535       case DepthChange:
1536          fDepthChange = fTrue;
[21c226e]1537          VAL(FrDepth) = 0;
1538          read_reading(ToDepth, fFalse);
[a186573]1539          break;
[be97baf]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');
[0580c6a]1549                   /* FIXME: we currently understand X (exclude data)
1550                    * but should also handle at least some of:
1551                    * L exclude from length
1552                    * P no plot
1553                    * C no adjustment
1554                    */
[be97baf]1555                   nextch();
1556                }
1557                if (ch == '#') {
1558                   nextch();
1559                } else {
1560                   compass_dat_flags = 0;
1561                   set_pos(&fp);
1562                   push_back('|');
1563                   ch = '#';
1564                }
1565             } else {
1566                push_back(ch);
1567                ch = '#';
1568             }
1569          }
[107b8bd]1570          break;
[be97baf]1571       case Ignore:
1572          skipword(); break;
[54c4612]1573       case IgnoreAllAndNewLine:
[107b8bd]1574          skipline();
1575          /* fall through */
[54c4612]1576       case Newline:
[107b8bd]1577          if (fr != NULL) {
1578             int r;
[21c226e]1579             if (fTopofil)
1580                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1581             /* Note: frdepth == todepth test works regardless of fDepthChange
1582              * (frdepth always zero, todepth is change of depth) and also
1583              * works for STYLE_NORMAL (both remain 0) */
[27b8b59]1584             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1585                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1586                process_equate(fr, to);
1587                goto inferred_equate;
1588             }
[e7576f6]1589             if (fRev) {
1590                prefix *t = fr;
1591                fr = to;
1592                to = t;
1593             }
[107b8bd]1594             if (fTopofil) {
[21c226e]1595                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1596             } else {
[21c226e]1597                VAL(Tape) *= pcs->units[Q_LENGTH];
1598                VAL(Tape) -= pcs->z[Q_LENGTH];
1599                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1600             }
1601             switch (pcs->style) {
1602              case STYLE_NORMAL:
[21c226e]1603                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1604                                   ctype, backctype);
[107b8bd]1605                break;
1606              case STYLE_DIVING:
[21c226e]1607                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1608                                   fDepthChange);
[107b8bd]1609                break;
1610              case STYLE_CYLPOLAR:
[21c226e]1611                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1612                                     fDepthChange);
[107b8bd]1613                break;
1614              default:
[74a0681]1615                r = 0; /* avoid warning */
[107b8bd]1616                BUG("bad style");
1617             }
1618             if (!r) skipline();
[e7576f6]1619             
1620             /* Swap fr and to back to how they were for next line */
1621             if (fRev) {
1622                prefix *t = fr;
1623                fr = to;
1624                to = t;
1625             }
[107b8bd]1626          }
[21c226e]1627
1628          fRev = fFalse;
1629          ctype = backctype = CTYPE_OMIT;
1630          fDepthChange = fFalse;
1631
1632          /* ordering may omit clino reading, so set up default here */
1633          /* this is also used if clino reading is the omit character */
1634          VAL(Clino) = VAL(BackClino) = 0;
1635
[107b8bd]1636          inferred_equate:
[e7576f6]1637
[107b8bd]1638          fMulti = fTrue;
1639          while (1) {
1640              process_eol();
1641              process_bol();
1642              if (isData(ch)) break;
1643              if (!isComm(ch)) {
1644                 push_back(ch);
1645                 return 1;
1646              }
1647          }
1648          break;
[54c4612]1649       case IgnoreAll:
[107b8bd]1650          skipline();
1651          /* fall through */
[54c4612]1652       case End:
[107b8bd]1653          if (!fMulti) {
1654             int r;
[be97baf]1655             /* Compass ignore flag is 'X' */
1656             if ((compass_dat_flags & BIT('X' - 'A'))) {
1657                process_eol();
1658                return 1;
1659             }
[107b8bd]1660             if (fRev) {
1661                prefix *t = fr;
1662                fr = to;
1663                to = t;
1664             }
[21c226e]1665             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
[107b8bd]1666             /* Note: frdepth == todepth test works regardless of fDepthChange
1667              * (frdepth always zero, todepth is change of depth) and also
1668              * works for STYLE_NORMAL (both remain 0) */
[27b8b59]1669             if (TSTBIT(pcs->infer, INFER_EQUATES) &&
1670                 VAL(Tape) == (real)0.0 && VAL(FrDepth) == VAL(ToDepth)) {
[107b8bd]1671                process_equate(fr, to);
1672                process_eol();
1673                return 1;
1674             }
1675             if (fTopofil) {
[21c226e]1676                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
[107b8bd]1677             } else {
[21c226e]1678                VAL(Tape) *= pcs->units[Q_LENGTH];
1679                VAL(Tape) -= pcs->z[Q_LENGTH];
1680                VAL(Tape) *= pcs->sc[Q_LENGTH];
[107b8bd]1681             }
1682             switch (pcs->style) {
1683              case STYLE_NORMAL:
[21c226e]1684                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1685                                   ctype, backctype);
[107b8bd]1686                break;
1687              case STYLE_DIVING:
[21c226e]1688                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1689                                   fDepthChange);
[107b8bd]1690                break;
1691              case STYLE_CYLPOLAR:
[21c226e]1692                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1693                                     fDepthChange);
[107b8bd]1694                break;
1695              default:
[74a0681]1696                r = 0; /* Suppress compiler warning */;
[107b8bd]1697                BUG("bad style");
1698             }
1699             process_eol();
1700             return r;
1701          }
1702          do {
1703             process_eol();
1704             process_bol();
1705          } while (isComm(ch));
1706          goto again;
1707       default:
1708          BUG("Unknown reading in ordering");
[54c4612]1709      }
1710   }
1711}
1712
[5a38209]1713static int
1714process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1715{
1716   nosurveylink *link;
[838a602a]1717   int shape;
[647407d]1718
[be97baf]1719#ifdef CHASM3DX
[647407d]1720   if (fUseNewFormat) {
[407084d]1721      /* new twiglet and insert into twig tree */
1722      twig *twiglet = osnew(twig);
[90bb053f]1723      twiglet->from = fr;
1724      twiglet->to = to;
[647407d]1725      twiglet->down = twiglet->right = NULL;
1726      twiglet->source = twiglet->drawings
1727        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1728      twiglet->up = limb->up;
1729      limb->right = twiglet;
1730      limb = twiglet;
[3bcbcb2]1731      /* delta is only used to calculate error - pass zero and cope
1732       * elsewhere */
[647407d]1733      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1734   }
1735#endif
1736
[838a602a]1737   /* Suppress "unused fixed point" warnings for these stations
1738    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1739    */
1740   shape = fr->shape;
1741   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1742   shape = to->shape;
1743   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1744
[647407d]1745   /* add to linked list which is dealt with after network is solved */
1746   link = osnew(nosurveylink);
[5a38209]1747   if (fToFirst) {
[90bb053f]1748      link->to = StnFromPfx(to);
1749      link->fr = StnFromPfx(fr);
[0395657]1750   } else {
[90bb053f]1751      link->fr = StnFromPfx(fr);
1752      link->to = StnFromPfx(to);
[0395657]1753   }
[b3bef47]1754   link->flags = pcs->flags;
[647407d]1755   link->next = nosurveyhead;
1756   nosurveyhead = link;
[a420b49]1757   return 1;
[d1b1380]1758}
[50f6901]1759
[5a38209]1760extern int
1761data_nosurvey(void)
1762{
1763   prefix *fr = NULL, *to = NULL;
1764
1765   bool fMulti = fFalse;
1766
1767   reading first_stn = End;
1768
1769   reading *ordering;
1770
1771   again:
1772
1773   for (ordering = pcs->ordering ; ; ordering++) {
1774      skipblanks();
1775      switch (*ordering) {
1776       case Fr:
[84c60fc]1777          fr = read_prefix_stn(fFalse, fTrue);
[5a38209]1778          if (first_stn == End) first_stn = Fr;
1779          break;
1780       case To:
[84c60fc]1781          to = read_prefix_stn(fFalse, fTrue);
[5a38209]1782          if (first_stn == End) first_stn = To;
1783          break;
1784       case Station:
1785          fr = to;
[84c60fc]1786          to = read_prefix_stn(fFalse, fFalse);
[5a38209]1787          first_stn = To;
1788          break;
1789       case Ignore:
1790         skipword(); break;
1791       case IgnoreAllAndNewLine:
1792         skipline();
1793         /* fall through */
1794       case Newline:
1795         if (fr != NULL) {
1796            int r;
1797            r = process_nosurvey(fr, to, first_stn == To);
1798            if (!r) skipline();
1799         }
[838a602a]1800         if (ordering[1] == End) {
1801            do {
1802               process_eol();
1803               process_bol();
1804            } while (isComm(ch));
[ee6a621]1805            if (!isData(ch)) {
[993454b]1806               push_back(ch);
[ee6a621]1807               return 1;
1808            }
[838a602a]1809            goto again;
1810         }
[5a38209]1811         fMulti = fTrue;
1812         while (1) {
1813            process_eol();
1814            process_bol();
1815            if (isData(ch)) break;
[ee6a621]1816            if (!isComm(ch)) {
[993454b]1817               push_back(ch);
[ee6a621]1818               return 1;
1819            }
[5a38209]1820         }
1821         break;
1822       case IgnoreAll:
1823         skipline();
1824         /* fall through */
1825       case End:
1826         if (!fMulti) {
[ee6a621]1827            int r = process_nosurvey(fr, to, first_stn == To);
[5a38209]1828            process_eol();
1829            return r;
1830         }
[eef4d8c]1831         do {
[5a38209]1832            process_eol();
1833            process_bol();
[eef4d8c]1834         } while (isComm(ch));
[5a38209]1835         goto again;
1836       default: BUG("Unknown reading in ordering");
1837      }
1838   }
1839}
1840
[50f6901]1841/* totally ignore a line of survey data */
1842extern int
1843data_ignore(void)
1844{
1845   skipline();
[ee6a621]1846   process_eol();
[570f4bf]1847   return 1;
[50f6901]1848}
Note: See TracBrowser for help on using the repository browser.