source: git/src/cavern.c @ 89a6a1e

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey svn-merged-from-1.0
Last change on this file since 89a6a1e was 27b8b59, checked in by Olly Betts <olly@…>, 23 years ago

Sync with 1.0 branch.

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

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/* cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
3 * Copyright (C) 1991-2002 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 */
78bool fMute = fFalse; /* just show errors */
79bool fSuppress = fFalse; /* only output 3d(3dx) file */
80static bool fLog = fFalse; /* stdout to .log 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   {"log", no_argument, 0, 1},
113#ifdef NEW3DFORMAT
114   {"chasm-format", no_argument, 0, 'x'},
115#endif
116#if (OS==WIN32)
117   {"pause", no_argument, 0, 2},
118#endif
119   {"help", no_argument, 0, HLP_HELP},
120   {"version", no_argument, 0, HLP_VERSION},
121   {0, 0, 0, 0}
122};
123
124#ifdef NEW3DFORMAT
125#define short_opts "pxao:qswz:"
126#else
127#define short_opts "pao:qswz:"
128#endif
129
130/* TRANSLATE extract help messages to message file */
131static struct help_msg help[] = {
132/*                              <-- */
133   {HLP_ENCODELONG(0),          "display percentage progress"},
134   {HLP_ENCODELONG(2),          "set location for output files"},
135   {HLP_ENCODELONG(3),          "only show brief summary (-qq for errors only)"},
136   {HLP_ENCODELONG(4),          "do not create .err file"},
137   {HLP_ENCODELONG(5),          "turn warnings into errors"},
138   {HLP_ENCODELONG(6),          "log output to .log file"},
139#ifdef NEW3DFORMAT
140   {HLP_ENCODELONG(7),          "output data in chasm's 3dx format"},
141#endif
142 /*{'z',                        "set optimizations for network reduction"},*/
143   {0, 0}
144};
145
146/* atexit functions */
147static void
148delete_output_on_error(void)
149{
150   if (msg_errors || (f_warnings_are_errors && msg_warnings))
151      filename_delete_output();
152}
153
154#if (OS==WIN32)
155static void
156pause_on_exit(void)
157{
158   while (_kbhit()) _getch();
159   _getch();
160}
161#endif
162
163extern CDECL int
164main(int argc, char **argv)
165{
166   int d;
167   static clock_t tmCPUStart;
168   static time_t tmUserStart;
169   static double tmCPU, tmUser;
170
171   tmUserStart = time(NULL);
172   tmCPUStart = clock();
173   init_screen();
174
175   msg_init(argv);
176
177   pcs = osnew(settings);
178   pcs->next = NULL;
179   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
180
181   /* Set up root of prefix hierarchy */
182   root = osnew(prefix);
183   root->up = root->right = root->down = NULL;
184   root->stn = NULL;
185   root->pos = NULL;
186   root->ident = NULL;
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] = HUGE_REAL;
199      max[d] = -HUGE_REAL;
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       case 1:
257         fLog = fTrue;
258         break;
259#if (OS==WIN32)
260       case 2:
261         atexit(pause_on_exit);
262         break;
263#endif
264       }
265      }
266   }
267
268   if (fLog) {
269      char *fnm;
270      if (!fnm_output_base) {
271         char *p;
272         p = baseleaf_from_fnm(argv[optind]);
273         fnm = add_ext(p, EXT_LOG);
274         osfree(p);
275      } else if (fnm_output_base_is_dir) {
276         char *p;
277         fnm = baseleaf_from_fnm(argv[optind]);
278         p = use_path(fnm_output_base, fnm);
279         osfree(fnm);
280         fnm = add_ext(p, EXT_LOG);
281         osfree(p);
282      } else {
283         fnm = add_ext(fnm_output_base, EXT_LOG);
284      }
285
286      if (!freopen(fnm, "w", stdout))
287         fatalerror(/*Failed to open output file `%s'*/47, fnm);
288
289      osfree(fnm);
290   }
291
292   if (!fMute)
293      printf(PRETTYPACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n", msg(/*&copy;*/0));
294
295   atexit(delete_output_on_error);
296
297   /* end of options, now process data files */
298   while (argv[optind]) {
299      const char *fnm = argv[optind];
300
301      if (!fExplicitTitle) {
302         char *lf;
303         lf = baseleaf_from_fnm(fnm);
304         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
305         s_cat(&survey_title, &survey_title_len, lf);
306         osfree(lf);
307      }
308
309      /* Select defaults settings */
310      default_all(pcs);
311#ifdef NEW3DFORMAT
312      /* we need to get the filename of the first one for our base_source */
313      /* and also run_file */
314      if (fUseNewFormat) {
315         create_twig(root, fnm);
316         rhizome = root->twig_link;
317         limb = get_twig(root);
318         firstfilename = osstrdup(fnm);
319         startingdir = osmalloc(MAXPATHLEN);
320#if (OS==RISCOS)
321         strcpy(startingdir, "@");
322#else
323         getcwd(startingdir, MAXPATHLEN);
324#endif
325      }
326#endif
327      data_file(NULL, fnm); /* first argument is current path */
328
329      optind++;
330   }
331
332   validate();
333
334   solve_network(/*stnlist*/); /* Find coordinates of all points */
335   validate();
336
337#ifdef NEW3DFORMAT
338   if (fUseNewFormat) {
339      /* this actually does all the writing */
340      if (!cave_close(pimg)) {
341         char *fnm = add_ext(fnm_output_base, EXT_SVX_3DX);
342         fatalerror_in_file(fnm, 0, /*Error writing to file*/111);
343      }
344   } else {
345#endif
346      /* close .3d file */
347      if (!img_close(pimg)) {
348         char *fnm = add_ext(fnm_output_base, EXT_SVX_3D);
349         fatalerror(img_error(), fnm);
350      }
351#ifdef NEW3DFORMAT
352   }
353#endif
354   if (fhErrStat) safe_fclose(fhErrStat);
355
356   out_current_action(msg(/*Calculating statistics*/120));
357   if (!fMute) do_stats();
358   if (!fQuiet) {
359      /* clock() typically wraps after 72 minutes, but there doesn't seem
360       * to be a better way.  Still 72 minutes means some cave!
361       */
362      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
363      tmUser = difftime(time(NULL), tmUserStart);
364
365      /* tmUser is integer, tmCPU not - equivalent to (ceil(tmCPU) >= tmUser) */
366      if (tmCPU + 1 > tmUser) {
367         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
368      } else if (tmCPU == 0) {
369         if (tmUser != 0.0) {
370            printf(msg(/*Time used %5.2fs*/141), tmUser);
371         } else {
372            fputs(msg(/*Time used unavailable*/142), stdout);
373         }
374      } else {
375         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
376      }
377      putnl();
378
379      puts(msg(/*Done.*/144));
380   }
381   if (msg_warnings || msg_errors) {
382      if (msg_errors || (f_warnings_are_errors && msg_warnings)) {
383         printf(msg(/*There were %d warning(s) and %d non-fatal error(s) - no output files produced.*/113),
384                msg_warnings, msg_errors);
385         putnl();
386         return EXIT_FAILURE;
387      }
388      printf(msg(/*There were %d warning(s).*/16), msg_warnings);
389      putnl();
390   }
391   return EXIT_SUCCESS;
392}
393
394static void
395do_range(int d, int msg1, int msg2, int msg3)
396{
397   printf(msg(msg1), max[d] - min[d]);
398   fprint_prefix(stdout, pfxHi[d]);
399   printf(msg(msg2), max[d]);
400   fprint_prefix(stdout, pfxLo[d]);
401   printf(msg(msg3), min[d]);
402   putnl();
403}
404
405static void
406do_stats(void)
407{
408   long cLoops = cComponents + cLegs - cStns;
409
410   putnl();
411
412   if (cStns == 1) {
413      fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
414   } else {
415      printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
416   }
417
418   if (cLegs == 1) {
419      fputs(msg(/* joined by 1 leg.*/174), stdout);
420   } else {
421      printf(msg(/* joined by %ld legs.*/175), cLegs);
422   }
423
424   putnl();
425
426   if (cLoops == 1) {
427      fputs(msg(/*There is 1 loop.*/138), stdout);
428   } else {
429      printf(msg(/*There are %ld loops.*/139), cLoops);
430   }
431
432   putnl();
433
434   if (cComponents != 1) {
435      printf(msg(/*Survey has %ld connected components.*/178), cComponents);
436      putnl();
437   }
438
439   printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
440          total, totadj);
441   putnl();
442   printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
443          totplan);
444   putnl();
445   printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
446          totvert);
447   putnl();
448
449   /* If there's no underground survey, we've no ranges */
450   if (pfxHi[0]) {
451      do_range(2, /*Vertical range = %4.2fm (from */135,
452               /* at %4.2fm to */136, /* at %4.2fm)*/137);
453      do_range(1, /*North-South range = %4.2fm (from */148,
454               /* at %4.2fm to */196, /* at %4.2fm)*/197);
455      do_range(0, /*East-West range = %4.2fm (from */149,
456               /* at %4.2fm to */196, /* at %4.2fm)*/197);
457   }
458
459   print_node_stats();
460   /* Also, could give:
461    *  # nodes stations (ie have other than two references or are fixed)
462    *  # fixed stations (list of?)
463    */
464}
Note: See TracBrowser for help on using the repository browser.