source: git/src/cavern.c @ 91c9bea

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 91c9bea was b88b171, checked in by Olly Betts <olly@…>, 20 years ago
  • aven: If aven is asked to load a .svx, .dat, or .mak file, run cavern on it, showing cavern's output in a window (with errors and warnings clickable to load the offending file into an editor), and then loading the resulting 3d file.
  • (Unix version): Check environmental variable LC_MESSAGES when deciding what language to use for messages.

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

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/* cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
3 * Copyright (C) 1991-2003,2004,2005 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/* For funcs which want to be immune from messing around with different
45 * calling conventions */
46#ifndef CDECL
47# define CDECL
48#endif
49
50/* Globals */
51node *stnlist = NULL;
52settings *pcs;
53prefix *root;
54long cLegs, cStns;
55long cComponents;
56bool fExportUsed = fFalse;
57
58FILE *fhErrStat = NULL;
59img *pimg = NULL;
60#ifndef NO_PERCENTAGE
61bool fPercent = fFalse;
62#endif
63bool fQuiet = fFalse; /* just show brief summary + errors */
64bool fMute = fFalse; /* just show errors */
65bool fSuppress = fFalse; /* only output 3d(3dx) file */
66static bool fLog = fFalse; /* stdout to .log file */
67static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */
68
69nosurveylink *nosurveyhead;
70
71real totadj, total, totplan, totvert;
72real min[3], max[3];
73prefix *pfxHi[3], *pfxLo[3];
74
75char *survey_title = NULL;
76int survey_title_len;
77
78bool fExplicitTitle = fFalse;
79
80char *fnm_output_base = NULL;
81int fnm_output_base_is_dir = 0;
82
83lrudlist * model = NULL;
84lrud ** next_lrud = NULL;
85
86static void do_stats(void);
87
88static const struct option long_opts[] = {
89   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
90#ifdef NO_PERCENTAGE
91   {"percentage", no_argument, 0, 0},
92   {"no-percentage", no_argument, 0, 0},
93#else
94   {"percentage", no_argument, 0, 'p'},
95   {"no-percentage", no_argument, 0, 3},
96#endif
97   {"output", required_argument, 0, 'o'},
98   {"quiet", no_argument, 0, 'q'},
99   {"no-auxiliary-files", no_argument, 0, 's'},
100   {"warnings-are-errors", no_argument, 0, 'w'},
101   {"log", no_argument, 0, 1},
102#if (OS==WIN32)
103   {"pause", no_argument, 0, 2},
104#endif
105   {"help", no_argument, 0, HLP_HELP},
106   {"version", no_argument, 0, HLP_VERSION},
107   {0, 0, 0, 0}
108};
109
110#define short_opts "pao:qswz:"
111
112/* TRANSLATE extract help messages to message file */
113static struct help_msg help[] = {
114/*                              <-- */
115   {HLP_ENCODELONG(0),          "display percentage progress"},
116   {HLP_ENCODELONG(2),          "set location for output files"},
117   {HLP_ENCODELONG(3),          "only show brief summary (-qq for errors only)"},
118   {HLP_ENCODELONG(4),          "do not create .err file"},
119   {HLP_ENCODELONG(5),          "turn warnings into errors"},
120   {HLP_ENCODELONG(6),          "log output to .log file"},
121 /*{'z',                        "set optimizations for network reduction"},*/
122   {0, 0}
123};
124
125/* atexit functions */
126static void
127delete_output_on_error(void)
128{
129   if (msg_errors || (f_warnings_are_errors && msg_warnings))
130      filename_delete_output();
131}
132
133#if (OS==WIN32)
134static void
135pause_on_exit(void)
136{
137   while (_kbhit()) _getch();
138   _getch();
139}
140#endif
141
142extern CDECL int
143main(int argc, char **argv)
144{
145   int d;
146   time_t tmUserStart = time(NULL);
147   clock_t tmCPUStart = clock();
148
149   /* Always buffer by line for aven's benefit. */
150   setvbuf(stdout, NULL, _IOLBF, 0);
151
152   init_screen();
153
154   msg_init(argv);
155
156   pcs = osnew(settings);
157   pcs->next = NULL;
158   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
159   pcs->meta = NULL;
160
161   /* Set up root of prefix hierarchy */
162   root = osnew(prefix);
163   root->up = root->right = root->down = NULL;
164   root->stn = NULL;
165   root->pos = NULL;
166   root->ident = NULL;
167   root->min_export = root->max_export = 0;
168   root->sflags = BIT(SFLAGS_SURVEY);
169   root->filename = NULL;
170
171   nosurveyhead = NULL;
172
173   stnlist = NULL;
174   cLegs = cStns = cComponents = 0;
175   totadj = total = totplan = totvert = 0.0;
176
177   for (d = 0; d <= 2; d++) {
178      min[d] = HUGE_REAL;
179      max[d] = -HUGE_REAL;
180      pfxHi[d] = pfxLo[d] = NULL;
181   }
182
183   /* at least one argument must be given */
184   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
185   while (1) {
186      int opt = cmdline_getopt();
187      if (opt == EOF) break;
188      switch (opt) {
189       case 'p':
190#ifndef NO_PERCENTAGE
191         fPercent = 1;
192#endif
193         break;
194#ifndef NO_PERCENTAGE
195       case 3:
196         fPercent = 0;
197         break;
198#endif
199       case 'o': {
200         /* can be a directory (in which case use basename of leaf input)
201          * or a file (in which case just trim the extension off) */
202         if (fDirectory(optarg)) {
203            /* this is a little tricky - we need to note the path here,
204             * and then add the leaf later on (in datain.c) */
205            fnm_output_base = base_from_fnm(optarg);
206            fnm_output_base_is_dir = 1;
207         } else {
208            osfree(fnm_output_base); /* in case of multiple -o options */
209            fnm_output_base = base_from_fnm(optarg);
210         }
211         break;
212       }
213       case 'q':
214         if (fQuiet) fMute = 1;
215         fQuiet = 1;
216         break;
217       case 's':
218         fSuppress = 1;
219         break;
220       case 'w':
221         f_warnings_are_errors = 1;
222         break;
223       case 'z': {
224         /* Control which network optimisations are used (development tool) */
225         static int first_opt_z = 1;
226         char c;
227         if (first_opt_z) {
228            optimize = 0;
229            first_opt_z = 0;
230         }
231         /* Lollipops, Parallel legs, Iterate mx, Delta* */
232         while ((c = *optarg++) != '\0')
233            if (islower((unsigned char)c)) optimize |= BITA(c);
234         break;
235       case 1:
236         fLog = fTrue;
237         break;
238#if (OS==WIN32)
239       case 2:
240         atexit(pause_on_exit);
241         break;
242#endif
243       }
244      }
245   }
246
247   if (fLog) {
248      char *fnm;
249      if (!fnm_output_base) {
250         char *p;
251         p = baseleaf_from_fnm(argv[optind]);
252         fnm = add_ext(p, EXT_LOG);
253         osfree(p);
254      } else if (fnm_output_base_is_dir) {
255         char *p;
256         fnm = baseleaf_from_fnm(argv[optind]);
257         p = use_path(fnm_output_base, fnm);
258         osfree(fnm);
259         fnm = add_ext(p, EXT_LOG);
260         osfree(p);
261      } else {
262         fnm = add_ext(fnm_output_base, EXT_LOG);
263      }
264
265      if (!freopen(fnm, "w", stdout))
266         fatalerror(/*Failed to open output file `%s'*/47, fnm);
267
268      osfree(fnm);
269   }
270
271   if (!fMute)
272      printf(PRETTYPACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n", msg(/*&copy;*/0));
273
274   atexit(delete_output_on_error);
275
276   /* end of options, now process data files */
277   while (argv[optind]) {
278      const char *fnm = argv[optind];
279
280      if (!fExplicitTitle) {
281         char *lf;
282         lf = baseleaf_from_fnm(fnm);
283         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
284         s_cat(&survey_title, &survey_title_len, lf);
285         osfree(lf);
286      }
287
288      /* Select defaults settings */
289      default_all(pcs);
290      data_file(NULL, fnm); /* first argument is current path */
291
292      optind++;
293   }
294
295   validate();
296
297   solve_network(/*stnlist*/); /* Find coordinates of all points */
298   validate();
299
300   /* close .3d file */
301   if (!img_close(pimg)) {
302      char *fnm = add_ext(fnm_output_base, EXT_SVX_3D);
303      fatalerror(img_error(), fnm);
304   }
305   if (fhErrStat) safe_fclose(fhErrStat);
306
307   out_current_action(msg(/*Calculating statistics*/120));
308   if (!fMute) do_stats();
309   if (!fQuiet) {
310      /* clock() typically wraps after 72 minutes, but there doesn't seem
311       * to be a better way.  Still 72 minutes means some cave!
312       * We detect if clock() could have wrapped and suppress CPU time
313       * printing in this case.
314       */
315      double tmUser = difftime(time(NULL), tmUserStart);
316      double tmCPU;
317      clock_t now = clock();
318#define CLOCK_T_WRAP \
319        (sizeof(clock_t)<sizeof(long)?(1ul << (CHAR_BIT * sizeof(clock_t))):0)
320      tmCPU = (now - (unsigned long)tmCPUStart)
321         / (double)CLOCKS_PER_SEC;
322      if (now < tmCPUStart)
323         tmCPU += CLOCK_T_WRAP / (double)CLOCKS_PER_SEC;
324      if (tmUser >= tmCPU + CLOCK_T_WRAP / (double)CLOCKS_PER_SEC)
325         tmCPU = 0;
326
327      /* tmUser is integer, tmCPU 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      if (msg_errors || (f_warnings_are_errors && msg_warnings)) {
345         printf(msg(/*There were %d warning(s) and %d non-fatal error(s) - no output files produced.*/113),
346                msg_warnings, msg_errors);
347         putnl();
348         return EXIT_FAILURE;
349      }
350      printf(msg(/*There were %d warning(s).*/16), msg_warnings);
351      putnl();
352   }
353   return EXIT_SUCCESS;
354}
355
356static void
357do_range(int d, int msg1, int msg2, int msg3)
358{
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
367static void
368do_stats(void)
369{
370   long cLoops = cComponents + cLegs - cStns;
371
372   putnl();
373
374   if (cStns == 1) {
375      fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
376   } else {
377      printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
378   }
379
380   if (cLegs == 1) {
381      fputs(msg(/* joined by 1 leg.*/174), stdout);
382   } else {
383      printf(msg(/* joined by %ld legs.*/175), cLegs);
384   }
385
386   putnl();
387
388   if (cLoops == 1) {
389      fputs(msg(/*There is 1 loop.*/138), stdout);
390   } else {
391      printf(msg(/*There are %ld loops.*/139), cLoops);
392   }
393
394   putnl();
395
396   if (cComponents != 1) {
397      printf(msg(/*Survey has %ld connected components.*/178), cComponents);
398      putnl();
399   }
400
401   printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
402          total, totadj);
403   putnl();
404   printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
405          totplan);
406   putnl();
407   printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
408          totvert);
409   putnl();
410
411   /* If there's no underground survey, we've no ranges */
412   if (pfxHi[0]) {
413      do_range(2, /*Vertical range = %4.2fm (from */135,
414               /* at %4.2fm to */136, /* at %4.2fm)*/137);
415      do_range(1, /*North-South range = %4.2fm (from */148,
416               /* at %4.2fm to */196, /* at %4.2fm)*/197);
417      do_range(0, /*East-West range = %4.2fm (from */149,
418               /* at %4.2fm to */196, /* at %4.2fm)*/197);
419   }
420
421   print_node_stats();
422   /* Also, could give:
423    *  # nodes stations (ie have other than two references or are fixed)
424    *  # fixed stations (list of?)
425    */
426}
Note: See TracBrowser for help on using the repository browser.