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

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 8c15864 was 06a871f, checked in by Olly Betts <olly@…>, 24 years ago

Fixed code to remove .inf file after error(s).

git-svn-id: file:///home/survex-svn/survex/trunk@1129 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#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(void);
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
169   tmUserStart = time(NULL);
170   tmCPUStart = clock();
171   init_screen();
172
173   msg_init(argv[0]);
174
175   pcs = osnew(settings);
176   pcs->next = NULL;
177   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
178
179   /* Set up root of prefix hierarchy */
180   root = osnew(prefix);
181   root->up = root->right = root->down = NULL;
182   root->stn = NULL;
183   root->pos = NULL;   
184   /* FIXME: shouldn't need to set this really, but need to check that nothing assumes the value of root->ident isn't "" */
185   root->ident = "\\";
186   root->min_export = root->max_export = 0;
187   root->sflags = BIT(SFLAGS_SURVEY);
188   root->filename = NULL;
189
190   nosurveyhead = NULL;
191
192   stnlist = NULL;
193   cLegs = cStns = cComponents = 0;
194   totadj = total = totplan = totvert = 0.0;
195
196   for (d = 0; d <= 2; d++) {
197      min[d] = REAL_BIG;
198      max[d] = -REAL_BIG;
199      pfxHi[d] = pfxLo[d] = NULL;
200   }
201
202   /* at least one argument must be given */
203   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
204   while (1) {
205      int opt = cmdline_getopt();
206      if (opt == EOF) break;
207      switch (opt) {
208       case 'p':
209#ifndef NO_PERCENTAGE
210         fPercent = 1;
211#endif
212         break;
213       case 'o': {
214         /* can be a directory (in which case use basename of leaf input)
215          * or a file (in which case just trim the extension off) */
216         if (fDirectory(optarg)) {
217            /* this is a little tricky - we need to note the path here,
218             * and then add the leaf later on (in datain.c) */
219            fnm_output_base = base_from_fnm(optarg);
220            fnm_output_base_is_dir = 1;
221         } else {
222            osfree(fnm_output_base); /* in case of multiple -o options */
223            fnm_output_base = base_from_fnm(optarg);
224         }
225         break;
226       }
227#ifdef NEW3DFORMAT
228       case 'x': {
229         fUseNewFormat = 1;
230         break;
231       }
232#endif
233       case 'q':
234         if (fQuiet) fMute = 1;
235         fQuiet = 1;
236         break;
237       case 's':
238         fSuppress = 1;
239         break;
240       case 'w':
241         f_warnings_are_errors = 1;
242         break;
243       case 'z': {
244         /* Control which network optimisations are used (development tool) */
245         static int first_opt_z = 1;
246         char c;
247         if (first_opt_z) {
248            optimize = 0;
249            first_opt_z = 0;
250         }
251         /* Lollipops, Parallel legs, Iterate mx, Delta* */
252         while ((c = *optarg++) != '\0')
253             if (islower(c)) optimize |= BITA(c);
254         break;
255#if (OS==WIN32)
256       case 256:
257         atexit(pause_on_exit);
258         break;
259#endif
260       }
261      }
262   }
263
264   if (!fMute) puts(PACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n");
265
266   atexit(delete_output_on_error);
267
268   /* end of options, now process data files */
269   while (argv[optind]) {
270      const char *fnm = argv[optind];
271
272      if (!fExplicitTitle) {
273         char *lf;
274         lf = baseleaf_from_fnm(fnm);
275         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
276         s_cat(&survey_title, &survey_title_len, lf);
277         osfree(lf);
278      }
279
280      /* Select defaults settings */
281      default_all(pcs);
282#ifdef NEW3DFORMAT
283      /* we need to get the filename of the first one for our base_source */
284      /* and also run_file */
285      if (fUseNewFormat) {
286         create_twig(root, fnm);
287         rhizome = root->twig_link;
288         limb = get_twig(root);
289         firstfilename = osstrdup(fnm);
290         startingdir = osmalloc(MAXPATHLEN);
291#if (OS==RISCOS)
292         strcpy(startingdir, "@");
293#else
294         getcwd(startingdir, MAXPATHLEN);
295#endif
296      }
297#endif
298      data_file("", fnm); /* first argument is current path */
299
300      optind++;
301   }
302
303   validate();
304
305   solve_network(/*stnlist*/); /* Find coordinates of all points */
306   validate();
307
308#ifdef NEW3DFORMAT
309   if (fUseNewFormat) {
310     cave_close(pimg); /* this actually does all the writing */
311   } else {
312#endif
313     img_close(pimg); /* close .3d file */
314#ifdef NEW3DFORMAT
315   }
316#endif
317   if (fhErrStat) fclose(fhErrStat);
318
319   out_current_action(msg(/*Calculating statistics*/120));
320   do_stats();
321   if (!fQuiet) {
322      /* clock() typically wraps after 72 minutes, but there doesn't seem
323       * to be a better way.  Still 72 minutes means some cave!
324       */
325      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
326      tmUser = difftime(time(NULL), tmUserStart);
327
328      /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
329      if (tmCPU + 1 > tmUser) {
330         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
331      } else if (tmCPU == 0) {
332         if (tmUser == 0.0) {
333            printf(msg(/*Time used %5.2fs*/141), tmUser);
334         } else {
335            fputs(msg(/*Time used unavailable*/142), stdout);
336         }
337      } else {
338         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
339      }
340      putnl();
341
342      puts(msg(/*Done.*/144));
343   }
344   if (msg_warnings || msg_errors) {
345      printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
346             msg_warnings, msg_errors);
347      putnl();
348      /* FIXME: if (msg_errors || (f_warnings_are_errors && msg_warnings)) print "output not produced..."? */
349   }
350   if (msg_errors || (f_warnings_are_errors && msg_warnings))
351      return EXIT_FAILURE;
352   return EXIT_SUCCESS;
353}
354
355static void
356do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
357{
358   if (!fMute) {
359      printf(msg(msg1), max[d] - min[d]);
360      fprint_prefix(stdout, pfxHi[d]);
361      printf(msg(msg2), max[d]);
362      fprint_prefix(stdout, pfxLo[d]);
363      printf(msg(msg3), min[d]);
364      putnl();
365   }
366   if (fh) {
367      fprintf(fh, msg(msg1), max[d] - min[d]);
368      fprint_prefix(fh, pfxHi[d]);
369      fprintf(fh, msg(msg2), max[d]);
370      fprint_prefix(fh, pfxLo[d]);
371      fprintf(fh, msg(msg3), min[d]);
372      fputnl(fh);
373   }
374}
375
376static void
377do_stats(void)
378{
379   FILE *fh = NULL;
380   long cLoops = cComponents + cLegs - cStns;
381
382   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
383      fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
384
385   if (!fh && fMute) return;
386
387   if (!fMute) putnl();
388
389   if (cStns == 1) {
390      if (!fMute)
391         fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
392      if (fh)
393         fputs(msg(/*Survey contains 1 survey station,*/172), fh);
394   } else {
395      if (!fMute)
396         printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
397      if (fh)
398         fprintf(fh, msg(/*Survey contains %ld survey stations,*/173), cStns);
399   }
400
401   if (cLegs == 1) {
402      if (!fMute)
403         fputs(msg(/* joined by 1 leg.*/174), stdout);
404      if (fh)
405         fputs(msg(/* joined by 1 leg.*/174), fh);
406   } else {
407      if (!fMute)
408         printf(msg(/* joined by %ld legs.*/175), cLegs);
409      if (fh)
410         fprintf(fh, msg(/* joined by %ld legs.*/175), cLegs);
411   }
412
413   if (!fMute) putnl();
414   if (fh) fputnl(fh);
415
416   if (cLoops == 1) {
417      if (!fMute)
418         fputs(msg(/*There is 1 loop.*/138), stdout);
419      if (fh)
420         fputs(msg(/*There is 1 loop.*/138), fh);
421   } else {
422      if (!fMute)
423         printf(msg(/*There are %ld loops.*/139), cLoops);
424      if (fh)
425         fprintf(fh, msg(/*There are %ld loops.*/139), cLoops);
426   }
427
428   if (!fMute) putnl();
429   if (fh) fputnl(fh);
430
431   if (cComponents != 1) {
432      if (!fMute) {
433         printf(msg(/*Survey has %ld connected components.*/178), cComponents);
434         putnl();
435      }
436      if (fh) {
437         fprintf(fh, msg(/*Survey has %ld connected components.*/178), cComponents);
438         fputnl(fh);
439      }
440   }
441
442   if (!fMute) {
443      printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
444             total, totadj);
445      putnl();
446      printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
447             totplan);
448      putnl();
449      printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
450             totvert);
451      putnl();
452   }
453   if (fh) {
454      fprintf(fh, msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
455              total, totadj);
456      fputnl(fh);
457      fprintf(fh, msg(/*Total plan length of survey legs = %7.2fm*/133),
458              totplan);
459      fputnl(fh);
460      fprintf(fh, msg(/*Total vertical length of survey legs = %7.2fm*/134),
461              totvert);
462      fputnl(fh);
463   }
464
465   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
466            /* at %4.2fm to */136, /* at %4.2fm)*/137);
467   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
468            /* at %4.2fm to */196, /* at %4.2fm)*/197);
469   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
470            /* at %4.2fm to */196, /* at %4.2fm)*/197);
471
472   print_node_stats(fh);
473   /* Also, could give:
474    *  # nodes stations (ie have other than two references or are fixed)
475    *  # fixed stations (list of?)
476    */
477
478   if (fh) fclose(fh);
479}
Note: See TracBrowser for help on using the repository browser.