source: git/src/datain.c @ 79ee183

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 79ee183 was e7576f6, checked in by Olly Betts <olly@…>, 23 years ago

Merged from survex 1.0 branch.

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

  • Property mode set to 100644
File size: 36.9 KB
Line 
1/* datain.c
2 * Reads in survey files, dealing with special characters, keywords & data
3 * Copyright (C) 1991-2002 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <limits.h>
25#include <stdarg.h>
26
27#include "debug.h"
28#include "cavern.h"
29#include "filename.h"
30#include "message.h"
31#include "filelist.h"
32#include "netbits.h"
33#include "netskel.h"
34#include "readval.h"
35#include "datain.h"
36#include "commands.h"
37#include "out.h"
38#include "str.h"
39
40#define EPSILON (REAL_EPSILON * 1000)
41
42#define var(I) (pcs->Var[(I)])
43
44int ch;
45
46typedef enum {CTYPE_OMIT, CTYPE_READING, CTYPE_PLUMB, CTYPE_HORIZ} clino_type;
47
48/* Don't explicitly initialise as we can't set the jmp_buf - this has
49 * static scope so will be initialised like this anyway */
50parse file /* = { NULL, NULL, 0, fFalse, NULL } */ ;
51
52bool f_export_ok;
53
54static real value[Fr - 1];
55#define VAL(N) value[(N)-1]
56static real variance[Fr - 1];
57#define VAR(N) variance[(N)-1]
58
59static filepos fpLineStart;
60
61void
62get_pos(filepos *fp)
63{
64   fp->ch = ch;
65   fp->offset = ftell(file.fh);
66   if (fp->offset == -1)
67      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
68}
69
70void
71set_pos(const filepos *fp)
72{
73   ch = fp->ch;
74   if (fseek(file.fh, fp->offset, SEEK_SET) == -1)
75      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
76}
77
78static void
79push_back(int c)
80{
81   if (c != EOF && ungetc(c, file.fh) == EOF)
82      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
83}
84
85static void
86error_list_parent_files(void)
87{
88   if (!file.reported_where && file.parent) {
89      parse *p = file.parent;
90      const char *m = msg(/*In file included from*/5);
91      size_t len = strlen(m);
92
93      fprintf(STDERR, m);
94      m = msg(/*from*/3);
95
96      /* Suppress reporting of full include tree for further errors
97       * in this file */
98      file.reported_where = fTrue;
99
100      while (p) {
101         /* Force re-report of include tree for further errors in
102          * parent files */
103         p->reported_where = fFalse;
104         fprintf(STDERR, " %s:%d", p->filename, p->line);
105         p = p->parent;
106         if (p) fprintf(STDERR, ",\n%*s", (int)len, m);
107      }
108      fprintf(STDERR, ":\n");
109   }
110}
111
112void
113compile_error(int en, ...)
114{
115   va_list ap;
116   va_start(ap, en);
117   error_list_parent_files();
118   v_report(1, file.filename, file.line, en, ap);
119   va_end(ap);
120}
121
122void
123compile_error_skip(int en, ...)
124{
125   va_list ap;
126   va_start(ap, en);
127   error_list_parent_files();
128   v_report(1, file.filename, file.line, en, ap);
129   va_end(ap);
130   skipline();
131}
132
133void
134compile_error_token(int en)
135{
136   char *p = NULL;
137   static int len;
138   s_zero(&p);
139   skipblanks();
140   while (!isBlank(ch) && !isEol(ch)) {
141      s_catchar(&p, &len, ch);
142      nextch();
143   }
144   compile_error_skip(en, p ? p : "");
145   osfree(p);
146}
147
148void
149compile_warning(int en, ...)
150{
151   va_list ap;
152   va_start(ap, en);
153   error_list_parent_files();
154   v_report(0, file.filename, file.line, en, ap);
155   va_end(ap);
156}
157
158/* This function makes a note where to put output files */
159static void
160using_data_file(const char *fnm)
161{
162   if (!fnm_output_base) {
163      /* was: fnm_output_base = base_from_fnm(fnm); */
164      fnm_output_base = baseleaf_from_fnm(fnm);
165   } else if (fnm_output_base_is_dir) {
166      /* --output pointed to directory so use the leaf basename in that dir */
167      char *lf, *p;
168      lf = baseleaf_from_fnm(fnm);
169      p = use_path(fnm_output_base, lf);
170      osfree(lf);
171      osfree(fnm_output_base);
172      fnm_output_base = p;
173      fnm_output_base_is_dir = 0;
174   }
175}
176
177static void
178skipword(void)
179{
180   while (!isBlank(ch) && !isEol(ch)) nextch();
181}
182
183extern void
184skipblanks(void)
185{
186   while (isBlank(ch)) nextch();
187}
188
189extern void
190skipline(void)
191{
192   while (!isEol(ch)) nextch();
193}
194
195#ifndef NO_PERCENTAGE
196static long int filelen;
197#endif
198
199static void
200process_bol(void)
201{
202   /* Note start of line for error reporting */
203   get_pos(&fpLineStart);
204
205#ifndef NO_PERCENTAGE
206   /* print %age of file done */
207   if (filelen > 0)
208      printf("%d%%\r", (int)(100 * fpLineStart.offset / filelen));
209#endif
210
211   nextch();
212   skipblanks();
213}
214
215static void
216process_eol(void)
217{
218   int eolchar;
219
220   skipblanks();
221
222   if (!isEol(ch)) {
223      if (!isComm(ch)) compile_error(/*End of line not blank*/15);
224      skipline();
225   }
226
227   eolchar = ch;
228   file.line++;
229   /* skip any different eol characters so we get line counts correct on
230    * DOS text files and similar, but don't count several adjacent blank
231    * lines as one */
232   while (ch != EOF) {
233      nextch();
234      if (ch == eolchar || !isEol(ch)) {
235         push_back(ch);
236         break;
237      }
238   }
239}
240
241static bool
242process_non_data_line(void)
243{
244   process_bol();
245
246   if (isData(ch)) return fFalse;
247
248   if (isKeywd(ch)) {
249      nextch();
250      handle_command();
251   }
252
253   process_eol();
254
255   return fTrue;
256}
257
258static void
259read_reading(reading r, bool f_optional)
260{
261   int n_readings;
262   q_quantity q;
263   VAL(r) = read_numeric(f_optional, &n_readings);
264   switch (r) {
265      case Tape: q = Q_LENGTH; break;
266      case Comp: q = Q_BEARING; break;
267      case BackComp: q = Q_BACKBEARING; break;
268      case Clino: q = Q_GRADIENT; break;
269      case BackClino: q = Q_BACKGRADIENT; break;
270      case FrDepth: case ToDepth: q = Q_DEPTH; break;
271      case Dx: q = Q_DX; break;
272      case Dy: q = Q_DY; break;
273      case Dz: q = Q_DZ; break;
274      case FrCount: case ToCount: q = Q_COUNT; break;
275      default: BUG("Unexpected case");
276   }
277   VAR(r) = var(q);
278   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
279}
280
281static void
282read_bearing_or_omit(reading r)
283{
284   int n_readings;
285   q_quantity q;
286   VAL(r) = read_numeric_or_omit(&n_readings);
287   switch (r) {
288      case Comp: q = Q_BEARING; break;
289      case BackComp: q = Q_BACKBEARING; break;
290      default: BUG("Unexpected case");
291   }
292   VAR(r) = var(q);
293   if (n_readings > 1) VAR(r) /= sqrt(n_readings);
294}
295
296extern void
297data_file(const char *pth, const char *fnm)
298{
299   int begin_lineno_store;
300   parse file_store;
301
302   {
303      char *filename;
304      FILE *fh;
305      if (!pth) {
306         /* file specified on command line - don't do special translation */
307         fh = fopenWithPthAndExt(pth, fnm, EXT_SVX_DATA, "rb", &filename);
308      } else {
309         fh = fopen_portable(pth, fnm, EXT_SVX_DATA, "rb", &filename);
310      }
311
312      if (fh == NULL) {
313         compile_error(/*Couldn't open data file `%s'*/24, fnm);
314         return;
315      }
316
317      file_store = file;
318      if (file.fh) file.parent = &file_store;
319      file.fh = fh;
320      file.filename = filename;
321      file.line = 1;
322      file.reported_where = fFalse;
323   }
324
325   if (fPercent) printf("%s:\n", fnm);
326
327   using_data_file(file.filename);
328
329   begin_lineno_store = pcs->begin_lineno;
330   pcs->begin_lineno = 0;
331
332#ifndef NO_PERCENTAGE
333   /* Try to find how long the file is...
334    * However, under ANSI fseek( ..., SEEK_END) may not be supported */
335   filelen = 0;
336   if (fPercent) {
337      if (fseek(file.fh, 0l, SEEK_END) == 0) {
338         filepos fp;
339         get_pos(&fp);
340         filelen = fp.offset;
341      }
342      rewind(file.fh); /* reset file ptr to start & clear any error state */
343   }
344#endif
345
346#ifdef HAVE_SETJMP_H
347   /* errors in nested functions can longjmp here */
348   if (setjmp(file.jbSkipLine)) {
349      process_eol();
350   }
351#endif
352
353   while (!feof(file.fh) && !ferror(file.fh)) {
354      if (!process_non_data_line()) {
355         int r;
356#ifdef NEW3DFORMAT
357         twig *temp = limb;
358#endif
359         f_export_ok = fFalse;
360         switch (pcs->style) {
361          case STYLE_NORMAL:
362          case STYLE_DIVING:
363          case STYLE_CYLPOLAR:
364            r = data_normal();
365            break;
366          case STYLE_CARTESIAN:
367            r = data_cartesian();
368            break;
369          case STYLE_NOSURVEY:
370            r = data_nosurvey();
371            break;
372          case STYLE_IGNORE:
373            r = data_ignore();
374            break;
375          default:
376            BUG("bad style");
377         }
378         /* style function returns 0 => error */
379#ifdef NEW3DFORMAT
380         if (!r) {
381            /* we have just created a very naughty twiglet, and it must be
382             * punished */
383            osfree(limb);
384            limb = temp;
385         }
386#endif
387      }
388   }
389
390   /* don't allow *BEGIN at the end of a file, then *EXPORT in the
391    * including file */
392   f_export_ok = fFalse;
393
394#ifndef NO_PERCENTAGE
395   if (fPercent) putnl();
396#endif
397
398   if (pcs->begin_lineno) {
399      error_in_file(file.filename, pcs->begin_lineno,
400                    /*BEGIN with no matching END in this file*/23);
401      /* Implicitly close any unclosed BEGINs from this file */
402      do {
403         settings *pcsParent = pcs->next;
404         SVX_ASSERT(pcsParent);
405         free_settings(pcs);
406         pcs = pcsParent;
407      } while (pcs->begin_lineno);
408   }
409
410   pcs->begin_lineno = begin_lineno_store;
411
412   if (ferror(file.fh))
413      fatalerror_in_file(file.filename, 0, /*Error reading file*/18);
414
415   (void)fclose(file.fh);
416
417   file = file_store;
418
419   /* don't free this - it may be pointed to by prefix.file */
420   /* osfree(file.filename); */
421}
422
423static real
424mod2pi(real a)
425{
426   return a - floor(a / (2 * M_PI)) * (2 * M_PI);
427}
428
429static real
430handle_plumb(clino_type *p_ctype)
431{
432   typedef enum {
433      CLINO_NULL=-1, CLINO_UP, CLINO_DOWN, CLINO_LEVEL
434   } clino_tok;
435   static sztok clino_tab[] = {
436      {"D",     CLINO_DOWN},
437      {"DOWN",  CLINO_DOWN},
438      {"H",     CLINO_LEVEL},
439      {"LEVEL", CLINO_LEVEL},
440      {"U",     CLINO_UP},
441      {"UP",    CLINO_UP},
442      {NULL,    CLINO_NULL}
443   };
444   static real clinos[] = {(real)M_PI_2, (real)(-M_PI_2), (real)0.0};
445   clino_tok tok;
446
447   skipblanks();
448   if (isalpha(ch)) {
449      filepos fp;
450      get_pos(&fp);
451      get_token();
452      tok = match_tok(clino_tab, TABSIZE(clino_tab));
453      if (tok != CLINO_NULL) {
454         *p_ctype = (clinos[tok] == CLINO_LEVEL ? CTYPE_HORIZ : CTYPE_PLUMB);
455         return clinos[tok];
456      }
457      set_pos(&fp);
458   } else if (isSign(ch)) {
459      int chOld = ch;
460      nextch();
461      if (toupper(ch) == 'V') {
462         nextch();
463         *p_ctype = CTYPE_PLUMB;
464         return (!isMinus(chOld) ? M_PI_2 : -M_PI_2);
465      }
466
467      if (isOmit(chOld)) {
468         *p_ctype = CTYPE_OMIT;
469         /* no clino reading, so assume 0 with large sd */
470         return (real)0.0;
471      }
472   } else if (isOmit(ch)) {
473      /* OMIT char may not be a SIGN char too so we need to check here as
474       * well as above... */
475      nextch();
476      *p_ctype = CTYPE_OMIT;
477      /* no clino reading, so assume 0 with large sd */
478      return (real)0.0;
479   }
480   return HUGE_REAL;
481}
482
483static void
484warn_readings_differ(int msg, real diff)
485{
486   char buf[64];
487   char *p;
488   sprintf(buf, "%.2f", deg(fabs(diff)));
489   p = strchr(buf, '.');
490   if (p) {
491      char *z = p;
492      while (*++p) {
493         if (*p != '0') z = p + 1;
494      }
495      if (*z) *z = '\0';
496   }
497   compile_warning(msg, buf);
498}
499
500static bool
501handle_comp_units(void)
502{
503   bool fNoComp = fTrue;
504   if (VAL(Comp) != HUGE_REAL) {
505      fNoComp = fFalse;
506      VAL(Comp) *= pcs->units[Q_BEARING];
507      if (VAL(Comp) < (real)0.0 || VAL(Comp) - M_PI * 2.0 > EPSILON) {
508         compile_warning(/*Suspicious compass reading*/59);
509         VAL(Comp) = mod2pi(VAL(Comp));
510      }
511   }
512   if (VAL(BackComp) != HUGE_REAL) {
513      fNoComp = fFalse;
514      VAL(BackComp) *= pcs->units[Q_BACKBEARING];
515      if (VAL(BackComp) < (real)0.0 || VAL(BackComp) - M_PI * 2.0 > EPSILON) {
516         /* FIXME: different message for BackComp? */
517         compile_warning(/*Suspicious compass reading*/59);
518         VAL(BackComp) = mod2pi(VAL(BackComp));
519      }
520   }
521   return fNoComp;
522}
523
524static real
525handle_compass(real *p_var)
526{
527   real var = VAR(Comp);
528   real comp = VAL(Comp);
529   real backcomp = VAL(BackComp);
530   if (comp != HUGE_REAL) {
531      comp = (comp - pcs->z[Q_BEARING]) * pcs->sc[Q_BEARING];
532      comp -= pcs->z[Q_DECLINATION];
533   }
534   if (backcomp != HUGE_REAL) {
535      backcomp = (backcomp - pcs->z[Q_BACKBEARING])
536              * pcs->sc[Q_BACKBEARING];
537      backcomp -= pcs->z[Q_DECLINATION];
538      backcomp -= M_PI;
539      if (comp != HUGE_REAL) {
540         real diff = comp - backcomp;
541         real adj = fabs(diff) > M_PI ? M_PI : 0;
542         diff -= floor((diff + M_PI) / (2 * M_PI)) * 2 * M_PI;
543         if (sqrd(diff / 2.0) > var + VAR(Q_BACKBEARING)) {
544            /* fore and back readings differ by more than 2 sds */
545            warn_readings_differ(/*Compass reading and back compass reading disagree by %s degrees*/98, diff);
546         }
547         comp = (comp / var + backcomp / VAR(BackComp));
548         var = (var + VAR(BackComp)) / 4;
549         comp *= var;
550         comp += adj;
551      } else {
552         comp = backcomp;
553         var = VAR(BackComp);
554      }
555   }
556   *p_var = var;
557   return comp;
558}
559
560static int
561process_normal(prefix *fr, prefix *to, bool fToFirst,
562               clino_type ctype, clino_type backctype)
563{
564   real tape = VAL(Tape);
565   real comp = VAL(Comp);
566   real clin = VAL(Clino);
567   real backclin = VAL(BackClino);
568
569   real dx, dy, dz;
570   real vx, vy, vz;
571#ifndef NO_COVARIANCES
572   real cxy, cyz, czx;
573#endif
574
575   bool fNoComp;
576
577   /* adjusted tape is negative -- probably the calibration is wrong */
578   if (tape < (real)0.0) {
579      /* TRANSLATE different message for topofil? */
580      compile_warning(/*Negative adjusted tape reading*/79);
581   }
582
583   fNoComp = handle_comp_units();
584
585   if (ctype == CTYPE_READING) {
586      real diff_from_abs90;
587      clin *= pcs->units[Q_GRADIENT];
588      /* percentage scale */
589      if (pcs->f_clino_percent) clin = atan(clin);
590      diff_from_abs90 = fabs(clin) - M_PI_2;
591      if (diff_from_abs90 > EPSILON) {
592         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
593      } else if (pcs->f90Up && diff_from_abs90 > -EPSILON) {
594         ctype = CTYPE_PLUMB;
595      }
596   }
597
598   if (backctype == CTYPE_READING) {
599      real diff_from_abs90;
600      backclin *= pcs->units[Q_BACKGRADIENT];
601      /* percentage scale */
602      if (pcs->f_backclino_percent) backclin = atan(backclin);
603      diff_from_abs90 = fabs(backclin) - M_PI_2;
604      if (diff_from_abs90 > EPSILON) {
605         /* FIXME: different message for BackClino? */
606         compile_warning(/*Clino reading over 90 degrees (absolute value)*/51);
607      } else if (pcs->f90Up && diff_from_abs90 > -EPSILON) {
608         backctype = CTYPE_PLUMB;
609      }
610   }
611
612   if (ctype != CTYPE_OMIT && backctype != CTYPE_OMIT && ctype != backctype) {
613      compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
614      return 0;
615   }
616
617   if (ctype == CTYPE_PLUMB || backctype == CTYPE_PLUMB) {
618      /* plumbed */
619      if (!fNoComp) {
620         /* FIXME: Different message for BackComp? */
621         compile_warning(/*Compass reading given on plumbed leg*/21);
622      }
623
624      dx = dy = (real)0.0;
625      if (ctype != CTYPE_OMIT) {
626         if (backctype != CTYPE_OMIT && (clin > 0) == (backclin > 0)) {
627            /* We've got two UPs or two DOWNs - FIXME: not ideal message */
628            compile_error_skip(/*Clino and BackClino readings must be of the same type*/84);
629            return 0;
630         }
631         dz = (clin > (real)0.0) ? tape : -tape;
632      } else {
633         dz = (backclin < (real)0.0) ? tape : -tape;
634      }
635      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
636      vz = var(Q_POS) / 3.0 + VAR(Tape);
637#ifndef NO_COVARIANCES
638      /* Correct values - no covariances in this case! */
639      cxy = cyz = czx = (real)0.0;
640#endif
641   } else {
642      /* Each of ctype and backctype are either CTYPE_READING/CTYPE_HORIZ
643       * or CTYPE_OMIT */
644      /* clino */
645      real L2, cosG, LcosG, cosG2, sinB, cosB, dx2, dy2, dz2, v, V;
646      if (fNoComp) {
647         compile_error_skip(/*Compass reading may not be omitted except on plumbed legs*/14);
648         return 0;
649      }
650      if (tape == (real)0.0) {
651         dx = dy = dz = (real)0.0;
652         vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
653#ifndef NO_COVARIANCES
654         cxy = cyz = czx = (real)0.0;
655#endif
656#if DEBUG_DATAIN_1
657         printf("Zero length leg: vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
658#endif
659      } else {
660         real sinGcosG;
661         /* take into account variance in LEVEL case */
662         real var_clin = var(Q_LEVEL);
663         real var_comp;
664         real comp = handle_compass(&var_comp);
665         /* ctype != CTYPE_READING is LEVEL case */
666         if (ctype == CTYPE_READING) {
667            clin = (clin - pcs->z[Q_GRADIENT]) * pcs->sc[Q_GRADIENT];
668            var_clin = VAR(Clino);
669         }
670         if (backctype == CTYPE_READING) {
671            backclin = (backclin - pcs->z[Q_BACKGRADIENT])
672               * pcs->sc[Q_BACKGRADIENT];
673            if (ctype == CTYPE_READING) {
674               if (sqrd((clin + backclin) / 3.0) >
675                     var_clin + VAR(BackClino)) {
676                  /* fore and back readings differ by more than 3 sds */
677                  warn_readings_differ(/*Clino reading and back clino reading disagree by %s degrees*/99, clin + backclin);
678               }
679               clin = (clin / var_clin - backclin / VAR(BackClino));
680               var_clin = (var_clin + VAR(BackClino)) / 4;
681               clin *= var_clin;
682            } else {
683               clin = -backclin;
684               var_clin = VAR(BackClino);
685            }
686         }
687
688#if DEBUG_DATAIN
689         printf("    %4.2f %4.2f %4.2f\n", tape, comp, clin);
690#endif
691         cosG = cos(clin);
692         LcosG = tape * cosG;
693         sinB = sin(comp);
694         cosB = cos(comp);
695#if DEBUG_DATAIN_1
696         printf("sinB = %f, cosG = %f, LcosG = %f\n", sinB, cosG, LcosG);
697#endif
698         dx = LcosG * sinB;
699         dy = LcosG * cosB;
700         dz = tape * sin(clin);
701/*      printf("%.2f\n",clin); */
702#if DEBUG_DATAIN_1
703         printf("dx = %f\ndy = %f\ndz = %f\n", dx, dy, dz);
704#endif
705         dx2 = dx * dx;
706         L2 = tape * tape;
707         V = VAR(Tape) / L2;
708         dy2 = dy * dy;
709         cosG2 = cosG * cosG;
710         sinGcosG = sin(clin) * cosG;
711         dz2 = dz * dz;
712         v = dz2 * var_clin;
713#ifdef NO_COVARIANCES
714         vx = (var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
715               (.5 + sinB * sinB * cosG2) * v);
716         vy = (var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
717               (.5 + cosB * cosB * cosG2) * v);
718         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
719            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
720            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
721         } else {
722            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
723         }
724         /* for Surveyor87 errors: vx=vy=vz=var(Q_POS)/3.0; */
725#else
726         vx = var(Q_POS) / 3.0 + dx2 * V + dy2 * var_comp +
727            (sinB * sinB * v);
728         vy = var(Q_POS) / 3.0 + dy2 * V + dx2 * var_comp +
729            (cosB * cosB * v);
730         if (ctype == CTYPE_OMIT && backctype == CTYPE_OMIT) {
731            /* if no clino, assume sd=tape/sqrt(10) so 3sds = .95*tape */
732            vz = var(Q_POS) / 3.0 + L2 * (real)0.1;
733         } else {
734            vz = var(Q_POS) / 3.0 + dz2 * V + L2 * cosG2 * var_clin;
735         }
736         /* usual covariance formulae are fine in no clino case since
737          * dz = 0 so value of var_clin is ignored */
738         cxy = sinB * cosB * (VAR(Tape) * cosG2 + var_clin * dz2)
739               - var_comp * dx * dy;
740         czx = VAR(Tape) * sinB * sinGcosG - var_clin * dx * dz;
741         cyz = VAR(Tape) * cosB * sinGcosG - var_clin * dy * dz;
742#if 0
743         printf("vx = %6.3f, vy = %6.3f, vz = %6.3f\n", vx, vy, vz);
744         printf("cxy = %6.3f, cyz = %6.3f, czx = %6.3f\n", cxy, cyz, czx);
745#endif
746#endif
747#if DEBUG_DATAIN_1
748         printf("In DATAIN.C, vx = %f, vy = %f, vz = %f\n", vx, vy, vz);
749#endif
750      }
751   }
752#if DEBUG_DATAIN_1
753   printf("Just before addleg, vx = %f\n", vx);
754#endif
755   /*printf("dx,dy,dz = %.2f %.2f %.2f\n\n", dx, dy, dz);*/
756   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
757#ifndef NO_COVARIANCES
758                , cyz, czx, cxy
759#endif
760                );
761
762#ifdef NEW3DFORMAT
763   if (fUseNewFormat) {
764      /* new twiglet and insert into twig tree */
765      twig *twiglet = osnew(twig);
766      twiglet->from = fr;
767      twiglet->to = to;
768      twiglet->down = twiglet->right = NULL;
769      twiglet->source = twiglet->drawings
770        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
771      twiglet->up = limb->up;
772      limb->right = twiglet;
773      limb = twiglet;
774
775      /* record pre-fettling deltas */
776      twiglet->delta[0] = dx;
777      twiglet->delta[1] = dy;
778      twiglet->delta[2] = dz;
779   }
780#endif
781   return 1;
782}
783
784static int
785process_diving(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
786{
787   real tape = VAL(Tape);
788
789   real dx, dy, dz;
790   real vx, vy, vz;
791#ifndef NO_COVARIANCES
792   real cxy = 0, cyz = 0, czx = 0;
793#endif
794
795   handle_comp_units();
796
797   /* depth gauge readings increase upwards with default calibration */
798   if (fDepthChange) {
799      SVX_ASSERT(VAL(FrDepth) == 0.0);
800      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
801      dz *= pcs->sc[Q_DEPTH];
802   } else {
803      dz = VAL(ToDepth) - VAL(FrDepth);
804      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
805   }
806
807   /* adjusted tape is negative -- probably the calibration is wrong */
808   if (tape < (real)0.0) {
809      compile_warning(/*Negative adjusted tape reading*/79);
810   }
811
812   /* check if tape is less than depth change */
813   if (tape < fabs(dz)) {
814      /* FIXME: allow margin of error based on variances? */
815      compile_warning(/*Tape reading is less than change in depth*/62);
816   }
817
818   if (tape == (real)0.0 && dz == 0.0) {
819      dx = dy = dz = (real)0.0;
820      vx = vy = vz = (real)(var(Q_POS) / 3.0); /* Position error only */
821   } else if (VAL(Comp) == HUGE_REAL &&
822              VAL(BackComp) == HUGE_REAL) {
823      /* plumb */
824      dx = dy = (real)0.0;
825      if (dz < 0) tape = -tape;
826      /* FIXME: Should use FrDepth sometimes... */
827      dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth))
828         / (VAR(Tape) * 2 * VAR(ToDepth));
829      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
830      /* FIXME: Should use FrDepth sometimes... */
831      vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth)
832                              / (VAR(Tape) + VAR(ToDepth));
833   } else {
834      real L2, sinB, cosB, dz2, D2;
835      real var_comp;
836      real comp = handle_compass(&var_comp);
837      sinB = sin(comp);
838      cosB = cos(comp);
839      L2 = tape * tape;
840      dz2 = dz * dz;
841      D2 = L2 - dz2;
842      if (D2 <= (real)0.0) {
843         /* FIXME: Should use FrDepth sometimes... */
844         real vsum = VAR(Tape) + 2 * VAR(ToDepth);
845         dx = dy = (real)0.0;
846         vx = vy = var(Q_POS) / 3.0;
847         /* FIXME: Should use FrDepth sometimes... */
848         vz = var(Q_POS) / 3.0 + VAR(Tape) * 2 * VAR(ToDepth) / vsum;
849         if (dz > 0) {
850            /* FIXME: Should use FrDepth sometimes... */
851            dz = (dz * VAR(Tape) + tape * 2 * VAR(ToDepth)) / vsum;
852         } else {
853            dz = (dz * VAR(Tape) - tape * 2 * VAR(ToDepth)) / vsum;
854         }
855      } else {
856         real D = sqrt(D2);
857         /* FIXME: Should use FrDepth sometimes... */
858         real F = VAR(Tape) * L2 + 2 * VAR(ToDepth) * D2;
859         dx = D * sinB;
860         dy = D * cosB;
861
862         vx = var(Q_POS) / 3.0 +
863            sinB * sinB * F / D2 + var_comp * dy * dy;
864         vy = var(Q_POS) / 3.0 +
865            cosB * cosB * F / D2 + var_comp * dx * dx;
866         /* FIXME: Should use FrDepth sometimes... */
867         vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
868
869#ifndef NO_COVARIANCES
870         cxy = sinB * cosB * (F / D2 + var_comp * D2);
871         /* FIXME: Should use FrDepth sometimes... */
872         cyz = -2 * VAR(ToDepth) * dy / D;
873         czx = -2 * VAR(ToDepth) * dx / D;
874#endif
875      }
876   }
877   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
878#ifndef NO_COVARIANCES
879                , cxy, cyz, czx
880#endif
881                );
882#ifdef NEW3DFORMAT
883   if (fUseNewFormat) {
884      /*new twiglet and insert into twig tree*/
885      twig *twiglet = osnew(twig);
886      twiglet->from = fr;
887      twiglet->to = to;
888      twiglet->down = twiglet->right = NULL;
889      twiglet->source = twiglet->drawings
890        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
891      twiglet->up = limb->up;
892      limb->right = twiglet;
893      limb = twiglet;
894
895      /* record pre-fettling deltas */
896      twiglet->delta[0] = dx;
897      twiglet->delta[1] = dy;
898      twiglet->delta[2] = dz;
899   }
900#endif
901
902   return 1;
903}
904
905static int
906process_cartesian(prefix *fr, prefix *to, bool fToFirst)
907{
908   real dx = (VAL(Dx) * pcs->units[Q_DX] - pcs->z[Q_DX]) * pcs->sc[Q_DX];
909   real dy = (VAL(Dy) * pcs->units[Q_DY] - pcs->z[Q_DY]) * pcs->sc[Q_DY];
910   real dz = (VAL(Dz) * pcs->units[Q_DZ] - pcs->z[Q_DZ]) * pcs->sc[Q_DZ];
911
912   addlegbyname(fr, to, fToFirst, dx, dy, dz, VAR(Dx), VAR(Dy), VAR(Dz)
913#ifndef NO_COVARIANCES
914                , 0, 0, 0
915#endif
916                );
917
918#ifdef NEW3DFORMAT
919   if (fUseNewFormat) {
920      /* new twiglet and insert into twig tree */
921      twig *twiglet = osnew(twig);
922      twiglet->from = fr;
923      twiglet->to = to;
924      twiglet->down = twiglet->right = NULL;
925      twiglet->source = twiglet->drawings
926        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
927      twiglet->up = limb->up;
928      limb->right = twiglet;
929      limb = twiglet;
930
931      /* record pre-fettling deltas */
932      twiglet->delta[0] = dx;
933      twiglet->delta[1] = dy;
934      twiglet->delta[2] = dz;
935   }
936#endif
937
938   return 1;
939}
940
941extern int
942data_cartesian(void)
943{
944   prefix *fr = NULL, *to = NULL;
945
946   bool fMulti = fFalse;
947
948   reading first_stn = End;
949
950   reading *ordering;
951
952   again:
953
954   for (ordering = pcs->ordering ; ; ordering++) {
955      skipblanks();
956      switch (*ordering) {
957       case Fr:
958         fr = read_prefix_stn(fFalse, fTrue);
959         if (first_stn == End) first_stn = Fr;
960         break;
961       case To:
962         to = read_prefix_stn(fFalse, fTrue);
963         if (first_stn == End) first_stn = To;
964         break;
965       case Station:
966         fr = to;
967         to = read_prefix_stn(fFalse, fFalse);
968         first_stn = To;
969         break;
970       case Dx: case Dy: case Dz:
971         read_reading(*ordering, fFalse);
972         break;
973       case Ignore:
974         skipword(); break;
975       case IgnoreAllAndNewLine:
976         skipline();
977         /* fall through */
978       case Newline:
979         if (fr != NULL) {
980            int r;
981            r = process_cartesian(fr, to, first_stn == To);
982            if (!r) skipline();
983         }
984         fMulti = fTrue;
985         while (1) {
986            process_eol();
987            process_bol();
988            if (isData(ch)) break;
989            if (!isComm(ch)) {
990               push_back(ch);
991               return 1;
992            }
993         }
994         break;
995       case IgnoreAll:
996         skipline();
997         /* fall through */
998       case End:
999         if (!fMulti) {
1000            int r = process_cartesian(fr, to, first_stn == To);
1001            process_eol();
1002            return r;
1003         }
1004         do {
1005            process_eol();
1006            process_bol();
1007         } while (isComm(ch));
1008         goto again;
1009       default: BUG("Unknown reading in ordering");
1010      }
1011   }
1012}
1013
1014static int
1015process_cylpolar(prefix *fr, prefix *to, bool fToFirst, bool fDepthChange)
1016{
1017   real tape = VAL(Tape);
1018   real frdepth = VAL(FrDepth);
1019   real todepth = VAL(ToDepth);
1020
1021   real dx, dy, dz;
1022   real vx, vy, vz;
1023#ifndef NO_COVARIANCES
1024   real cxy = 0;
1025#endif
1026
1027   handle_comp_units();
1028
1029   /* depth gauge readings increase upwards with default calibration */
1030   if (fDepthChange) {
1031      SVX_ASSERT(VAL(FrDepth) == 0.0);
1032      dz = VAL(ToDepth) * pcs->units[Q_DEPTH] - pcs->z[Q_DEPTH];
1033      dz *= pcs->sc[Q_DEPTH];
1034   } else {
1035      dz = VAL(ToDepth) - VAL(FrDepth);
1036      dz *= pcs->units[Q_DEPTH] * pcs->sc[Q_DEPTH];
1037   }
1038
1039   /* adjusted tape is negative -- probably the calibration is wrong */
1040   if (tape < (real)0.0) {
1041      compile_warning(/*Negative adjusted tape reading*/79);
1042   }
1043
1044   if (VAL(Comp) == HUGE_REAL && VAL(BackComp) == HUGE_REAL) {
1045      /* plumb */
1046      dx = dy = (real)0.0;
1047      vx = vy = var(Q_POS) / 3.0 + dz * dz * var(Q_PLUMB);
1048      /* FIXME: Should use FrDepth sometimes... */
1049      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1050   } else {
1051      real sinB, cosB;
1052      real var_comp;
1053      real comp = handle_compass(&var_comp);
1054      sinB = sin(comp);
1055      cosB = cos(comp);
1056
1057      dx = tape * sinB;
1058      dy = tape * cosB;
1059
1060      vx = var(Q_POS) / 3.0 +
1061         VAR(Tape) * sinB * sinB + var_comp * dy * dy;
1062      vy = var(Q_POS) / 3.0 +
1063         VAR(Tape) * cosB * cosB + var_comp * dx * dx;
1064      /* FIXME: Should use FrDepth sometimes... */
1065      vz = var(Q_POS) / 3.0 + 2 * VAR(ToDepth);
1066
1067#ifndef NO_COVARIANCES
1068      cxy = (VAR(Tape) - var_comp * tape * tape) * sinB * cosB;
1069#endif
1070   }
1071   addlegbyname(fr, to, fToFirst, dx, dy, dz, vx, vy, vz
1072#ifndef NO_COVARIANCES
1073                , cxy, 0, 0
1074#endif
1075                );
1076#ifdef NEW3DFORMAT
1077   if (fUseNewFormat) {
1078      /*new twiglet and insert into twig tree*/
1079      twig *twiglet = osnew(twig);
1080      twiglet->from = fr;
1081      twiglet->to = to;
1082      twiglet->down = twiglet->right = NULL;
1083      twiglet->source = twiglet->drawings
1084        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1085      twiglet->up = limb->up;
1086      limb->right = twiglet;
1087      limb = twiglet;
1088
1089      /* record pre-fettling deltas */
1090      twiglet->delta[0] = dx;
1091      twiglet->delta[1] = dy;
1092      twiglet->delta[2] = dz;
1093   }
1094#endif
1095
1096   return 1;
1097}
1098
1099/* Process tape/compass/clino, diving, and cylpolar styles of survey data
1100 * Also handles topofil (fromcount/tocount or count) in place of tape */
1101extern int
1102data_normal(void)
1103{
1104   prefix *fr = NULL, *to = NULL;
1105   reading first_stn = End;
1106
1107   bool fTopofil = fFalse, fMulti = fFalse;
1108   bool fRev;
1109   clino_type ctype, backctype;
1110   bool fDepthChange;
1111
1112   reading *ordering;
1113
1114   VAL(Tape) = 0;
1115   VAL(Comp) = VAL(BackComp) = HUGE_VAL;
1116   VAL(FrCount) = VAL(ToCount) = 0;
1117   VAL(FrDepth) = VAL(ToDepth) = 0;
1118
1119   fRev = fFalse;
1120   ctype = backctype = CTYPE_OMIT;
1121   fDepthChange = fFalse;
1122
1123   /* ordering may omit clino reading, so set up default here */
1124   /* this is also used if clino reading is the omit character */
1125   VAL(Clino) = VAL(BackClino) = 0;
1126
1127   again:
1128
1129   for (ordering = pcs->ordering; ; ordering++) {
1130      skipblanks();
1131      switch (*ordering) {
1132       case Fr:
1133          fr = read_prefix_stn(fFalse, fTrue);
1134          if (first_stn == End) first_stn = Fr;
1135          break;
1136       case To:
1137          to = read_prefix_stn(fFalse, fTrue);
1138          if (first_stn == End) first_stn = To;
1139          break;
1140       case Station:
1141          fr = to;
1142          to = read_prefix_stn(fFalse, fFalse);
1143          first_stn = To;
1144          break;
1145       case Dir: {
1146          typedef enum {
1147             DIR_NULL=-1, DIR_FORE, DIR_BACK
1148          } dir_tok;
1149          static sztok dir_tab[] = {
1150             {"B",     DIR_BACK},
1151             {"F",     DIR_FORE},
1152          };
1153          dir_tok tok;
1154          get_token();
1155          tok = match_tok(dir_tab, TABSIZE(dir_tab));
1156          switch (tok) {
1157           case DIR_FORE:
1158             break;
1159           case DIR_BACK:
1160             fRev = fTrue;
1161             break;
1162           default:
1163             compile_error_skip(/*Found `%s', expecting `F' or `B'*/131,
1164                                buffer);
1165             process_eol();
1166             return 0;
1167          }
1168          break;
1169       }
1170       case Tape:
1171          read_reading(Tape, fFalse);
1172          if (VAL(Tape) < (real)0.0)
1173             compile_warning(/*Negative tape reading*/60);
1174          break;
1175       case Count:
1176          VAL(FrCount) = VAL(ToCount);
1177          read_reading(ToCount, fFalse);
1178          break;
1179       case FrCount:
1180          read_reading(FrCount, fFalse);
1181          break;
1182       case ToCount:
1183          read_reading(ToCount, fFalse);
1184          fTopofil = fTrue;
1185          break;
1186       case Comp:
1187          read_bearing_or_omit(Comp);
1188          break;
1189       case BackComp:
1190          read_bearing_or_omit(BackComp);
1191          break;
1192       case Clino:
1193          read_reading(Clino, fTrue);
1194          if (VAL(Clino) == HUGE_REAL) {
1195             VAL(Clino) = handle_plumb(&ctype);
1196             if (VAL(Clino) != HUGE_REAL) break;
1197             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1198             process_eol();
1199             return 0;
1200          }
1201          ctype = CTYPE_READING;
1202          break;
1203       case BackClino:
1204          read_reading(BackClino, fTrue);
1205          if (VAL(BackClino) == HUGE_REAL) {
1206             VAL(BackClino) = handle_plumb(&backctype);
1207             if (VAL(BackClino) != HUGE_REAL) break;
1208             compile_error_token(/*Expecting numeric field, found `%s'*/9);
1209             process_eol();
1210             return 0;
1211          }
1212          backctype = CTYPE_READING;
1213          break;
1214       case FrDepth:
1215          read_reading(FrDepth, fFalse);
1216          break;
1217       case ToDepth:
1218          read_reading(ToDepth, fFalse);
1219          break;
1220       case Depth:
1221          VAL(FrDepth) = VAL(ToDepth);
1222          read_reading(ToDepth, fFalse);
1223          break;
1224       case DepthChange:
1225          fDepthChange = fTrue;
1226          VAL(FrDepth) = 0;
1227          read_reading(ToDepth, fFalse);
1228          break;
1229       case Ignore:
1230          skipword();
1231          break;
1232       case IgnoreAllAndNewLine:
1233          skipline();
1234          /* fall through */
1235       case Newline:
1236          if (fr != NULL) {
1237             int r;
1238             if (fTopofil)
1239                VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1240             /* Note: frdepth == todepth test works regardless of fDepthChange
1241              * (frdepth always zero, todepth is change of depth) and also
1242              * works for STYLE_NORMAL (both remain 0) */
1243             if (pcs->f0Eq && VAL(Tape) == (real)0.0 &&
1244                 VAL(FrDepth) == VAL(ToDepth)) {
1245                process_equate(fr, to);
1246                goto inferred_equate;
1247             }
1248             if (fRev) {
1249                prefix *t = fr;
1250                fr = to;
1251                to = t;
1252             }
1253             if (fTopofil) {
1254                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1255             } else {
1256                VAL(Tape) *= pcs->units[Q_LENGTH];
1257                VAL(Tape) -= pcs->z[Q_LENGTH];
1258                VAL(Tape) *= pcs->sc[Q_LENGTH];
1259             }
1260             switch (pcs->style) {
1261              case STYLE_NORMAL:
1262                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1263                                   ctype, backctype);
1264                break;
1265              case STYLE_DIVING:
1266                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1267                                   fDepthChange);
1268                break;
1269              case STYLE_CYLPOLAR:
1270                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1271                                     fDepthChange);
1272                break;
1273              default:
1274                BUG("bad style");
1275             }
1276             if (!r) skipline();
1277             
1278             /* Swap fr and to back to how they were for next line */
1279             if (fRev) {
1280                prefix *t = fr;
1281                fr = to;
1282                to = t;
1283             }
1284          }
1285
1286          fRev = fFalse;
1287          ctype = backctype = CTYPE_OMIT;
1288          fDepthChange = fFalse;
1289
1290          /* ordering may omit clino reading, so set up default here */
1291          /* this is also used if clino reading is the omit character */
1292          VAL(Clino) = VAL(BackClino) = 0;
1293
1294          inferred_equate:
1295
1296          fMulti = fTrue;
1297          while (1) {
1298              process_eol();
1299              process_bol();
1300              if (isData(ch)) break;
1301              if (!isComm(ch)) {
1302                 push_back(ch);
1303                 return 1;
1304              }
1305          }
1306          break;
1307       case IgnoreAll:
1308          skipline();
1309          /* fall through */
1310       case End:
1311          if (!fMulti) {
1312             int r;
1313             if (fRev) {
1314                prefix *t = fr;
1315                fr = to;
1316                to = t;
1317             }
1318             if (fTopofil) VAL(Tape) = VAL(ToCount) - VAL(FrCount);
1319             /* Note: frdepth == todepth test works regardless of fDepthChange
1320              * (frdepth always zero, todepth is change of depth) and also
1321              * works for STYLE_NORMAL (both remain 0) */
1322             if (pcs->f0Eq && VAL(Tape) == (real)0.0 &&
1323                 VAL(FrDepth) == VAL(ToDepth)) {
1324                process_equate(fr, to);
1325                process_eol();
1326                return 1;
1327             }
1328             if (fTopofil) {
1329                VAL(Tape) *= pcs->units[Q_COUNT] * pcs->sc[Q_COUNT];
1330             } else {
1331                VAL(Tape) *= pcs->units[Q_LENGTH];
1332                VAL(Tape) -= pcs->z[Q_LENGTH];
1333                VAL(Tape) *= pcs->sc[Q_LENGTH];
1334             }
1335             switch (pcs->style) {
1336              case STYLE_NORMAL:
1337                r = process_normal(fr, to, (first_stn == To) ^ fRev,
1338                                   ctype, backctype);
1339                break;
1340              case STYLE_DIVING:
1341                r = process_diving(fr, to, (first_stn == To) ^ fRev,
1342                                   fDepthChange);
1343                break;
1344              case STYLE_CYLPOLAR:
1345                r = process_cylpolar(fr, to, (first_stn == To) ^ fRev,
1346                                     fDepthChange);
1347                break;
1348              default:
1349                BUG("bad style");
1350             }
1351             process_eol();
1352             return r;
1353          }
1354          do {
1355             process_eol();
1356             process_bol();
1357          } while (isComm(ch));
1358          goto again;
1359       default:
1360          BUG("Unknown reading in ordering");
1361      }
1362   }
1363}
1364
1365static int
1366process_nosurvey(prefix *fr, prefix *to, bool fToFirst)
1367{
1368   nosurveylink *link;
1369   int shape;
1370
1371#ifdef NEW3DFORMAT
1372   if (fUseNewFormat) {
1373      /* new twiglet and insert into twig tree */
1374      twig *twiglet = osnew(twig);
1375      twiglet->from = fr;
1376      twiglet->to = to;
1377      twiglet->down = twiglet->right = NULL;
1378      twiglet->source = twiglet->drawings
1379        = twiglet->date = twiglet->instruments = twiglet->tape = NULL;
1380      twiglet->up = limb->up;
1381      limb->right = twiglet;
1382      limb = twiglet;
1383      /* delta is only used to calculate error - pass zero and cope
1384       * elsewhere */
1385      twiglet->delta[0] = twiglet->delta[1] = twiglet->delta[2] = 0;
1386   }
1387#endif
1388
1389   /* Suppress "unused fixed point" warnings for these stations
1390    * We do this if it's a 0 or 1 node - 1 node might be an sdfix
1391    */
1392   shape = fr->shape;
1393   if (shape == 0 || shape == 1) fr->shape = -1 - shape;
1394   shape = to->shape;
1395   if (shape == 0 || shape == 1) to->shape = -1 - shape;
1396
1397   /* add to linked list which is dealt with after network is solved */
1398   link = osnew(nosurveylink);
1399   if (fToFirst) {
1400      link->to = StnFromPfx(to);
1401      link->fr = StnFromPfx(fr);
1402   } else {
1403      link->fr = StnFromPfx(fr);
1404      link->to = StnFromPfx(to);
1405   }
1406   link->flags = pcs->flags;
1407   link->next = nosurveyhead;
1408   nosurveyhead = link;
1409   return 1;
1410}
1411
1412extern int
1413data_nosurvey(void)
1414{
1415   prefix *fr = NULL, *to = NULL;
1416
1417   bool fMulti = fFalse;
1418
1419   reading first_stn = End;
1420
1421   reading *ordering;
1422
1423   again:
1424
1425   for (ordering = pcs->ordering ; ; ordering++) {
1426      skipblanks();
1427      switch (*ordering) {
1428       case Fr:
1429          fr = read_prefix_stn(fFalse, fTrue);
1430          if (first_stn == End) first_stn = Fr;
1431          break;
1432       case To:
1433          to = read_prefix_stn(fFalse, fTrue);
1434          if (first_stn == End) first_stn = To;
1435          break;
1436       case Station:
1437          fr = to;
1438          to = read_prefix_stn(fFalse, fFalse);
1439          first_stn = To;
1440          break;
1441       case Ignore:
1442         skipword(); break;
1443       case IgnoreAllAndNewLine:
1444         skipline();
1445         /* fall through */
1446       case Newline:
1447         if (fr != NULL) {
1448            int r;
1449            r = process_nosurvey(fr, to, first_stn == To);
1450            if (!r) skipline();
1451         }
1452         if (ordering[1] == End) {
1453            do {
1454               process_eol();
1455               process_bol();
1456            } while (isComm(ch));
1457            if (!isData(ch)) {
1458               push_back(ch);
1459               return 1;
1460            }
1461            goto again;
1462         }
1463         fMulti = fTrue;
1464         while (1) {
1465            process_eol();
1466            process_bol();
1467            if (isData(ch)) break;
1468            if (!isComm(ch)) {
1469               push_back(ch);
1470               return 1;
1471            }
1472         }
1473         break;
1474       case IgnoreAll:
1475         skipline();
1476         /* fall through */
1477       case End:
1478         if (!fMulti) {
1479            int r = process_nosurvey(fr, to, first_stn == To);
1480            process_eol();
1481            return r;
1482         }
1483         do {
1484            process_eol();
1485            process_bol();
1486         } while (isComm(ch));
1487         goto again;
1488       default: BUG("Unknown reading in ordering");
1489      }
1490   }
1491}
1492
1493/* totally ignore a line of survey data */
1494extern int
1495data_ignore(void)
1496{
1497   skipline();
1498   process_eol();
1499   return 1;
1500}
Note: See TracBrowser for help on using the repository browser.