source: git/src/cavern.c @ 8e75607

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 8e75607 was 40587f3, checked in by Olly Betts <olly@…>, 24 years ago

cavern: on error, remove any existing .inf file for consistency with
.3d and .err files.

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

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