source: git/src/cavern.c @ 838a602a

RELEASE/1.0RELEASE/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 838a602a was 977a053, checked in by Olly Betts <olly@…>, 24 years ago

Tiny fettle.

git-svn-id: file:///home/survex-svn/survex/trunk@1137 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 12.3 KB
Line 
1/* > cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
3 * Copyright (C) 1991-2001 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 <time.h>
25
26#include "cavern.h"
27#include "cmdline.h"
28#include "commands.h"
29#include "datain.h"
30#include "debug.h"
31#include "message.h"
32#include "filename.h"
33#include "filelist.h"
34#include "img.h"
35#include "listpos.h"
36#include "netbits.h"
37#include "netskel.h"
38#include "osdepend.h"
39#include "out.h"
40#include "str.h"
41#include "validate.h"
42
43#ifdef NEW3DFORMAT
44# if OS != RISCOS
45/* include header for getcwd() */
46#  if OS == MSDOS && defined(__TURBOC__)
47#   include <dir.h>
48#  else
49#   include <unistd.h>
50#  endif
51#endif
52
53#ifndef MAXPATHLEN
54#define MAXPATHLEN 1024
55#endif
56#endif
57
58/* For funcs which want to be immune from messing around with different
59 * calling conventions */
60#ifndef CDECL
61# define CDECL
62#endif
63
64/* Globals */
65node *stnlist = NULL;
66settings *pcs;
67prefix *root;
68long cLegs, cStns;
69long cComponents;
70bool fExportUsed = fFalse;
71
72FILE *fhErrStat = NULL;
73img *pimg = NULL;
74#ifndef NO_PERCENTAGE
75bool fPercent = fFalse;
76#endif
77bool fQuiet = fFalse; /* just show brief summary + errors */
78static bool fMute = fFalse; /* just show errors */
79bool fSuppress = fFalse; /* only output 3d(3dx) file */
80static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */   
81
82nosurveylink *nosurveyhead;
83
84real totadj, total, totplan, totvert;
85real min[3], max[3];
86prefix *pfxHi[3], *pfxLo[3];
87
88char *survey_title = NULL;
89int survey_title_len;
90
91bool fExplicitTitle = fFalse;
92
93char *fnm_output_base = NULL;
94int fnm_output_base_is_dir = 0;
95
96static void do_stats(void);
97
98static const struct option long_opts[] = {
99   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
100#ifdef NO_PERCENTAGE
101   {"percentage", no_argument, 0, 0},
102   {"no-percentage", no_argument, 0, 0},
103#else
104   {"percentage", no_argument, 0, 'p'},
105   {"no-percentage", no_argument, (int*)&fPercent, 0},
106#endif
107   {"output", required_argument, 0, 'o'},
108   {"quiet", no_argument, 0, 'q'},
109   {"no-auxiliary-files", no_argument, 0, 's'},
110   {"warnings-are-errors", no_argument, 0, 'w'},
111#ifdef NEW3DFORMAT
112   {"new-format", no_argument, 0, 'x'},
113#endif
114#ifdef NEW3DFORMAT
115   {"pause", no_argument, 0, 256},
116#endif
117   {"help", no_argument, 0, HLP_HELP},
118   {"version", no_argument, 0, HLP_VERSION},
119   {0, 0, 0, 0}
120};
121
122#ifdef NEW3DFORMAT
123#define short_opts "pxao:qswz:"
124#else
125#define short_opts "pao:qswz:"
126#endif
127
128/* TRANSLATE extract help messages to message file */
129static struct help_msg help[] = {
130/*                              <-- */
131   {HLP_ENCODELONG(0),          "display percentage progress"},
132   {HLP_ENCODELONG(2),          "set location for output files"},
133   {HLP_ENCODELONG(3),          "only show brief summary (-qq for errors only)"},
134   {HLP_ENCODELONG(4),          "do not create .inf or .err files"},
135   {HLP_ENCODELONG(5),          "turn warnings into errors"},
136#ifdef NEW3DFORMAT
137   {HLP_ENCODELONG(6),          "output data in chasm's 3dx format"},
138#endif
139 /*{'z',                        "set optimizations for network reduction"},*/
140   {0, 0}
141};
142
143/* atexit functions */
144static void
145delete_output_on_error(void)
146{
147   if (msg_errors || (f_warnings_are_errors && msg_warnings))
148      filename_delete_output();
149}
150
151#if (OS==WIN32)
152static void
153pause_on_exit(void)
154{
155   while (_kbhit()) _getch();
156   _getch();
157}
158#endif
159
160extern CDECL int
161main(int argc, char **argv)
162{
163   int d;
164   static clock_t tmCPUStart;
165   static time_t tmUserStart;
166   static double tmCPU, tmUser;
167
168   tmUserStart = time(NULL);
169   tmCPUStart = clock();
170   init_screen();
171
172   msg_init(argv[0]);
173
174   pcs = osnew(settings);
175   pcs->next = NULL;
176   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
177
178   /* Set up root of prefix hierarchy */
179   root = osnew(prefix);
180   root->up = root->right = root->down = NULL;
181   root->stn = NULL;
182   root->pos = NULL;   
183   /* FIXME: shouldn't need to set this really, but need to check that nothing assumes the value of root->ident isn't "" */
184   root->ident = "\\";
185   root->min_export = root->max_export = 0;
186   root->sflags = BIT(SFLAGS_SURVEY);
187   root->filename = NULL;
188
189   nosurveyhead = NULL;
190
191   stnlist = NULL;
192   cLegs = cStns = cComponents = 0;
193   totadj = total = totplan = totvert = 0.0;
194
195   for (d = 0; d <= 2; d++) {
196      min[d] = REAL_BIG;
197      max[d] = -REAL_BIG;
198      pfxHi[d] = pfxLo[d] = NULL;
199   }
200
201   /* at least one argument must be given */
202   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
203   while (1) {
204      int opt = cmdline_getopt();
205      if (opt == EOF) break;
206      switch (opt) {
207       case 'p':
208#ifndef NO_PERCENTAGE
209         fPercent = 1;
210#endif
211         break;
212       case 'o': {
213         /* can be a directory (in which case use basename of leaf input)
214          * or a file (in which case just trim the extension off) */
215         if (fDirectory(optarg)) {
216            /* this is a little tricky - we need to note the path here,
217             * and then add the leaf later on (in datain.c) */
218            fnm_output_base = base_from_fnm(optarg);
219            fnm_output_base_is_dir = 1;
220         } else {
221            osfree(fnm_output_base); /* in case of multiple -o options */
222            fnm_output_base = base_from_fnm(optarg);
223         }
224         break;
225       }
226#ifdef NEW3DFORMAT
227       case 'x': {
228         fUseNewFormat = 1;
229         break;
230       }
231#endif
232       case 'q':
233         if (fQuiet) fMute = 1;
234         fQuiet = 1;
235         break;
236       case 's':
237         fSuppress = 1;
238         break;
239       case 'w':
240         f_warnings_are_errors = 1;
241         break;
242       case 'z': {
243         /* Control which network optimisations are used (development tool) */
244         static int first_opt_z = 1;
245         char c;
246         if (first_opt_z) {
247            optimize = 0;
248            first_opt_z = 0;
249         }
250         /* Lollipops, Parallel legs, Iterate mx, Delta* */
251         while ((c = *optarg++) != '\0')
252             if (islower(c)) optimize |= BITA(c);
253         break;
254#if (OS==WIN32)
255       case 256:
256         atexit(pause_on_exit);
257         break;
258#endif
259       }
260      }
261   }
262
263   if (!fMute) puts(PACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n");
264
265   atexit(delete_output_on_error);
266
267   /* end of options, now process data files */
268   while (argv[optind]) {
269      const char *fnm = argv[optind];
270
271      if (!fExplicitTitle) {
272         char *lf;
273         lf = baseleaf_from_fnm(fnm);
274         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
275         s_cat(&survey_title, &survey_title_len, lf);
276         osfree(lf);
277      }
278
279      /* Select defaults settings */
280      default_all(pcs);
281#ifdef NEW3DFORMAT
282      /* we need to get the filename of the first one for our base_source */
283      /* and also run_file */
284      if (fUseNewFormat) {
285         create_twig(root, fnm);
286         rhizome = root->twig_link;
287         limb = get_twig(root);
288         firstfilename = osstrdup(fnm);
289         startingdir = osmalloc(MAXPATHLEN);
290#if (OS==RISCOS)
291         strcpy(startingdir, "@");
292#else
293         getcwd(startingdir, MAXPATHLEN);
294#endif
295      }
296#endif
297      data_file("", fnm); /* first argument is current path */
298
299      optind++;
300   }
301
302   validate();
303
304   solve_network(/*stnlist*/); /* Find coordinates of all points */
305   validate();
306
307#ifdef NEW3DFORMAT
308   if (fUseNewFormat) {
309     cave_close(pimg); /* this actually does all the writing */
310   } else {
311#endif
312     img_close(pimg); /* close .3d file */
313#ifdef NEW3DFORMAT
314   }
315#endif
316   if (fhErrStat) fclose(fhErrStat);
317
318   out_current_action(msg(/*Calculating statistics*/120));
319   do_stats();
320   if (!fQuiet) {
321      /* clock() typically wraps after 72 minutes, but there doesn't seem
322       * to be a better way.  Still 72 minutes means some cave!
323       */
324      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
325      tmUser = difftime(time(NULL), tmUserStart);
326
327      /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
328      if (tmCPU + 1 > tmUser) {
329         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
330      } else if (tmCPU == 0) {
331         if (tmUser == 0.0) {
332            printf(msg(/*Time used %5.2fs*/141), tmUser);
333         } else {
334            fputs(msg(/*Time used unavailable*/142), stdout);
335         }
336      } else {
337         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
338      }
339      putnl();
340
341      puts(msg(/*Done.*/144));
342   }
343   if (msg_warnings || msg_errors) {
344      printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
345             msg_warnings, msg_errors);
346      putnl();
347      /* FIXME: if (msg_errors || (f_warnings_are_errors && msg_warnings)) print "output not produced..."? */
348   }
349   if (msg_errors || (f_warnings_are_errors && msg_warnings))
350      return EXIT_FAILURE;
351   return EXIT_SUCCESS;
352}
353
354static void
355do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
356{
357   if (!fMute) {
358      printf(msg(msg1), max[d] - min[d]);
359      fprint_prefix(stdout, pfxHi[d]);
360      printf(msg(msg2), max[d]);
361      fprint_prefix(stdout, pfxLo[d]);
362      printf(msg(msg3), min[d]);
363      putnl();
364   }
365   if (fh) {
366      fprintf(fh, msg(msg1), max[d] - min[d]);
367      fprint_prefix(fh, pfxHi[d]);
368      fprintf(fh, msg(msg2), max[d]);
369      fprint_prefix(fh, pfxLo[d]);
370      fprintf(fh, msg(msg3), min[d]);
371      fputnl(fh);
372   }
373}
374
375static void
376do_stats(void)
377{
378   FILE *fh = NULL;
379   long cLoops = cComponents + cLegs - cStns;
380
381   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
382      fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
383   else
384      if (fMute) return;
385
386   if (!fMute) putnl();
387
388   if (cStns == 1) {
389      if (!fMute)
390         fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
391      if (fh)
392         fputs(msg(/*Survey contains 1 survey station,*/172), fh);
393   } else {
394      if (!fMute)
395         printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
396      if (fh)
397         fprintf(fh, msg(/*Survey contains %ld survey stations,*/173), cStns);
398   }
399
400   if (cLegs == 1) {
401      if (!fMute)
402         fputs(msg(/* joined by 1 leg.*/174), stdout);
403      if (fh)
404         fputs(msg(/* joined by 1 leg.*/174), fh);
405   } else {
406      if (!fMute)
407         printf(msg(/* joined by %ld legs.*/175), cLegs);
408      if (fh)
409         fprintf(fh, msg(/* joined by %ld legs.*/175), cLegs);
410   }
411
412   if (!fMute) putnl();
413   if (fh) fputnl(fh);
414
415   if (cLoops == 1) {
416      if (!fMute)
417         fputs(msg(/*There is 1 loop.*/138), stdout);
418      if (fh)
419         fputs(msg(/*There is 1 loop.*/138), fh);
420   } else {
421      if (!fMute)
422         printf(msg(/*There are %ld loops.*/139), cLoops);
423      if (fh)
424         fprintf(fh, msg(/*There are %ld loops.*/139), cLoops);
425   }
426
427   if (!fMute) putnl();
428   if (fh) fputnl(fh);
429
430   if (cComponents != 1) {
431      if (!fMute) {
432         printf(msg(/*Survey has %ld connected components.*/178), cComponents);
433         putnl();
434      }
435      if (fh) {
436         fprintf(fh, msg(/*Survey has %ld connected components.*/178), cComponents);
437         fputnl(fh);
438      }
439   }
440
441   if (!fMute) {
442      printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
443             total, totadj);
444      putnl();
445      printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
446             totplan);
447      putnl();
448      printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
449             totvert);
450      putnl();
451   }
452   if (fh) {
453      fprintf(fh, msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
454              total, totadj);
455      fputnl(fh);
456      fprintf(fh, msg(/*Total plan length of survey legs = %7.2fm*/133),
457              totplan);
458      fputnl(fh);
459      fprintf(fh, msg(/*Total vertical length of survey legs = %7.2fm*/134),
460              totvert);
461      fputnl(fh);
462   }
463
464   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
465            /* at %4.2fm to */136, /* at %4.2fm)*/137);
466   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
467            /* at %4.2fm to */196, /* at %4.2fm)*/197);
468   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
469            /* at %4.2fm to */196, /* at %4.2fm)*/197);
470
471   print_node_stats(fh);
472   /* Also, could give:
473    *  # nodes stations (ie have other than two references or are fixed)
474    *  # fixed stations (list of?)
475    */
476
477   if (fh) fclose(fh);
478}
Note: See TracBrowser for help on using the repository browser.