source: git/src/cavern.c @ fc4ee5b

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 fc4ee5b was b5a3219, checked in by Olly Betts <olly@…>, 22 years ago

Work towards "colour by date"...

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

  • Property mode set to 100644
File size: 12.0 KB
Line 
1/* cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
3 * Copyright (C) 1991-2003 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <limits.h>
25#include <time.h>
26
27#include "cavern.h"
28#include "cmdline.h"
29#include "commands.h"
30#include "datain.h"
31#include "debug.h"
32#include "message.h"
33#include "filename.h"
34#include "filelist.h"
35#include "img.h"
36#include "listpos.h"
37#include "netbits.h"
38#include "netskel.h"
39#include "osdepend.h"
40#include "out.h"
41#include "str.h"
42#include "validate.h"
43
44#ifdef CHASM3DX
45# if OS != RISCOS
46/* include header for getcwd() */
47#  if OS == MSDOS && defined(__TURBOC__)
48#   include <dir.h>
49#  else
50#   include <unistd.h>
51#  endif
52#endif
53
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 */
79bool fMute = fFalse; /* just show errors */
80bool fSuppress = fFalse; /* only output 3d(3dx) file */
81static bool fLog = fFalse; /* stdout to .log file */
82static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */
83
84nosurveylink *nosurveyhead;
85
86real totadj, total, totplan, totvert;
87real min[3], max[3];
88prefix *pfxHi[3], *pfxLo[3];
89
90char *survey_title = NULL;
91int survey_title_len;
92
93bool fExplicitTitle = fFalse;
94
95char *fnm_output_base = NULL;
96int fnm_output_base_is_dir = 0;
97
98static void do_stats(void);
99
100static const struct option long_opts[] = {
101   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
102#ifdef NO_PERCENTAGE
103   {"percentage", no_argument, 0, 0},
104   {"no-percentage", no_argument, 0, 0},
105#else
106   {"percentage", no_argument, 0, 'p'},
107   {"no-percentage", no_argument, (int*)&fPercent, 0},
108#endif
109   {"output", required_argument, 0, 'o'},
110   {"quiet", no_argument, 0, 'q'},
111   {"no-auxiliary-files", no_argument, 0, 's'},
112   {"warnings-are-errors", no_argument, 0, 'w'},
113   {"log", no_argument, 0, 1},
114#ifdef CHASM3DX
115   {"chasm-format", no_argument, 0, 'x'},
116#endif
117#if (OS==WIN32)
118   {"pause", no_argument, 0, 2},
119#endif
120   {"help", no_argument, 0, HLP_HELP},
121   {"version", no_argument, 0, HLP_VERSION},
122   {0, 0, 0, 0}
123};
124
125#ifdef CHASM3DX
126#define short_opts "pxao:qswz:"
127#else
128#define short_opts "pao:qswz:"
129#endif
130
131/* TRANSLATE extract help messages to message file */
132static struct help_msg help[] = {
133/*                              <-- */
134   {HLP_ENCODELONG(0),          "display percentage progress"},
135   {HLP_ENCODELONG(2),          "set location for output files"},
136   {HLP_ENCODELONG(3),          "only show brief summary (-qq for errors only)"},
137   {HLP_ENCODELONG(4),          "do not create .err file"},
138   {HLP_ENCODELONG(5),          "turn warnings into errors"},
139   {HLP_ENCODELONG(6),          "log output to .log file"},
140#ifdef CHASM3DX
141   {HLP_ENCODELONG(7),          "output data in chasm's 3dx format"},
142#endif
143 /*{'z',                        "set optimizations for network reduction"},*/
144   {0, 0}
145};
146
147/* atexit functions */
148static void
149delete_output_on_error(void)
150{
151   if (msg_errors || (f_warnings_are_errors && msg_warnings))
152      filename_delete_output();
153}
154
155#if (OS==WIN32)
156static void
157pause_on_exit(void)
158{
159   while (_kbhit()) _getch();
160   _getch();
161}
162#endif
163
164extern CDECL int
165main(int argc, char **argv)
166{
167   int d;
168   time_t tmUserStart = time(NULL);
169   clock_t tmCPUStart = clock();
170   init_screen();
171
172   msg_init(argv);
173
174   pcs = osnew(settings);
175   pcs->next = NULL;
176   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
177   pcs->meta = NULL;
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   root->ident = NULL;
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] = HUGE_REAL;
197      max[d] = -HUGE_REAL;
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 CHASM3DX
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       case 1:
255         fLog = fTrue;
256         break;
257#if (OS==WIN32)
258       case 2:
259         atexit(pause_on_exit);
260         break;
261#endif
262       }
263      }
264   }
265
266   if (fLog) {
267      char *fnm;
268      if (!fnm_output_base) {
269         char *p;
270         p = baseleaf_from_fnm(argv[optind]);
271         fnm = add_ext(p, EXT_LOG);
272         osfree(p);
273      } else if (fnm_output_base_is_dir) {
274         char *p;
275         fnm = baseleaf_from_fnm(argv[optind]);
276         p = use_path(fnm_output_base, fnm);
277         osfree(fnm);
278         fnm = add_ext(p, EXT_LOG);
279         osfree(p);
280      } else {
281         fnm = add_ext(fnm_output_base, EXT_LOG);
282      }
283
284      if (!freopen(fnm, "w", stdout))
285         fatalerror(/*Failed to open output file `%s'*/47, fnm);
286
287      osfree(fnm);
288   }
289
290   if (!fMute)
291      printf(PRETTYPACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n", msg(/*&copy;*/0));
292
293   atexit(delete_output_on_error);
294
295   /* end of options, now process data files */
296   while (argv[optind]) {
297      const char *fnm = argv[optind];
298
299      if (!fExplicitTitle) {
300         char *lf;
301         lf = baseleaf_from_fnm(fnm);
302         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
303         s_cat(&survey_title, &survey_title_len, lf);
304         osfree(lf);
305      }
306
307      /* Select defaults settings */
308      default_all(pcs);
309#ifdef CHASM3DX
310      /* we need to get the filename of the first one for our base_source */
311      /* and also run_file */
312      if (fUseNewFormat) {
313         create_twig(root, fnm);
314         rhizome = root->twig_link;
315         limb = get_twig(root);
316         firstfilename = osstrdup(fnm);
317         startingdir = osmalloc(MAXPATHLEN);
318#if (OS==RISCOS)
319         strcpy(startingdir, "@");
320#else
321         getcwd(startingdir, MAXPATHLEN);
322#endif
323      }
324#endif
325      data_file(NULL, fnm); /* first argument is current path */
326
327      optind++;
328   }
329
330   validate();
331
332   solve_network(/*stnlist*/); /* Find coordinates of all points */
333   validate();
334
335#ifdef CHASM3DX
336   if (fUseNewFormat) {
337      /* this actually does all the writing */
338      if (!cave_close(pimg)) {
339         char *fnm = add_ext(fnm_output_base, EXT_SVX_3DX);
340         fatalerror_in_file(fnm, 0, /*Error writing to file*/111);
341      }
342   } else {
343#endif
344      /* close .3d file */
345      if (!img_close(pimg)) {
346         char *fnm = add_ext(fnm_output_base, EXT_SVX_3D);
347         fatalerror(img_error(), fnm);
348      }
349#ifdef CHASM3DX
350   }
351#endif
352   if (fhErrStat) safe_fclose(fhErrStat);
353
354   out_current_action(msg(/*Calculating statistics*/120));
355   if (!fMute) do_stats();
356   if (!fQuiet) {
357      /* clock() typically wraps after 72 minutes, but there doesn't seem
358       * to be a better way.  Still 72 minutes means some cave!
359       * We detect if clock() could have wrapped and suppress CPU time
360       * printing in this case.
361       */
362      double tmUser = difftime(time(NULL), tmUserStart);
363      double tmCPU;
364      clock_t now = clock();
365#define CLOCK_T_WRAP \
366        (sizeof(clock_t)<sizeof(long)?(1ul << (CHAR_BIT * sizeof(clock_t))):0)
367      tmCPU = (now - (unsigned long)tmCPUStart)
368         / (double)CLOCKS_PER_SEC;
369      if (now < tmCPUStart)
370         tmCPU += CLOCK_T_WRAP / (double)CLOCKS_PER_SEC;
371      if (tmUser >= tmCPU + CLOCK_T_WRAP / (double)CLOCKS_PER_SEC)
372         tmCPU = 0;
373
374      /* tmUser is integer, tmCPU not - equivalent to (ceil(tmCPU) >= tmUser) */
375      if (tmCPU + 1 > tmUser) {
376         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
377      } else if (tmCPU == 0) {
378         if (tmUser != 0.0) {
379            printf(msg(/*Time used %5.2fs*/141), tmUser);
380         } else {
381            fputs(msg(/*Time used unavailable*/142), stdout);
382         }
383      } else {
384         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
385      }
386      putnl();
387
388      puts(msg(/*Done.*/144));
389   }
390   if (msg_warnings || msg_errors) {
391      if (msg_errors || (f_warnings_are_errors && msg_warnings)) {
392         printf(msg(/*There were %d warning(s) and %d non-fatal error(s) - no output files produced.*/113),
393                msg_warnings, msg_errors);
394         putnl();
395         return EXIT_FAILURE;
396      }
397      printf(msg(/*There were %d warning(s).*/16), msg_warnings);
398      putnl();
399   }
400   return EXIT_SUCCESS;
401}
402
403static void
404do_range(int d, int msg1, int msg2, int msg3)
405{
406   printf(msg(msg1), max[d] - min[d]);
407   fprint_prefix(stdout, pfxHi[d]);
408   printf(msg(msg2), max[d]);
409   fprint_prefix(stdout, pfxLo[d]);
410   printf(msg(msg3), min[d]);
411   putnl();
412}
413
414static void
415do_stats(void)
416{
417   long cLoops = cComponents + cLegs - cStns;
418
419   putnl();
420
421   if (cStns == 1) {
422      fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
423   } else {
424      printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
425   }
426
427   if (cLegs == 1) {
428      fputs(msg(/* joined by 1 leg.*/174), stdout);
429   } else {
430      printf(msg(/* joined by %ld legs.*/175), cLegs);
431   }
432
433   putnl();
434
435   if (cLoops == 1) {
436      fputs(msg(/*There is 1 loop.*/138), stdout);
437   } else {
438      printf(msg(/*There are %ld loops.*/139), cLoops);
439   }
440
441   putnl();
442
443   if (cComponents != 1) {
444      printf(msg(/*Survey has %ld connected components.*/178), cComponents);
445      putnl();
446   }
447
448   printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
449          total, totadj);
450   putnl();
451   printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
452          totplan);
453   putnl();
454   printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
455          totvert);
456   putnl();
457
458   /* If there's no underground survey, we've no ranges */
459   if (pfxHi[0]) {
460      do_range(2, /*Vertical range = %4.2fm (from */135,
461               /* at %4.2fm to */136, /* at %4.2fm)*/137);
462      do_range(1, /*North-South range = %4.2fm (from */148,
463               /* at %4.2fm to */196, /* at %4.2fm)*/197);
464      do_range(0, /*East-West range = %4.2fm (from */149,
465               /* at %4.2fm to */196, /* at %4.2fm)*/197);
466   }
467
468   print_node_stats();
469   /* Also, could give:
470    *  # nodes stations (ie have other than two references or are fixed)
471    *  # fixed stations (list of?)
472    */
473}
Note: See TracBrowser for help on using the repository browser.