source: git/src/datain.c @ b65c13f

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 b65c13f was 2431dea, checked in by Olly Betts <olly@…>, 20 years ago

Fix compiler warning.

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

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