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
RevLine 
[0156ccfc]1/* cavern.c
[bb90203]2 * SURVEX Cave surveying software: data reduction main and related functions
[a4ae909]3 * Copyright (C) 1991-2003,2004,2005 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[bb90203]18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
[be97baf]24#include <limits.h>
[bb90203]25#include <time.h>
26
[5853657]27#include "cavern.h"
28#include "cmdline.h"
29#include "commands.h"
[bb90203]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"
[5853657]37#include "netbits.h"
38#include "netskel.h"
39#include "osdepend.h"
[bb90203]40#include "out.h"
[8e8057c]41#include "str.h"
[5853657]42#include "validate.h"
[bb90203]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;
[d1878c51]56bool fExportUsed = fFalse;
[bb90203]57
58FILE *fhErrStat = NULL;
[693388e]59img *pimg = NULL;
[bb90203]60#ifndef NO_PERCENTAGE
61bool fPercent = fFalse;
62#endif
[647407d]63bool fQuiet = fFalse; /* just show brief summary + errors */
[ed0f5b6]64bool fMute = fFalse; /* just show errors */
[647407d]65bool fSuppress = fFalse; /* only output 3d(3dx) file */
[2b6eca8]66static bool fLog = fFalse; /* stdout to .log file */
[421b7d2]67static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */
[647407d]68
69nosurveylink *nosurveyhead;
[bb90203]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
[8e8057c]80char *fnm_output_base = NULL;
81int fnm_output_base_is_dir = 0;
82
[ee05463]83lrudlist * model = NULL;
84lrud ** next_lrud = NULL;
85
[06a871f]86static void do_stats(void);
[bb90203]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'},
[b4fe9fb]95   {"no-percentage", no_argument, 0, 3},
[bb90203]96#endif
[8e8057c]97   {"output", required_argument, 0, 'o'},
[647407d]98   {"quiet", no_argument, 0, 'q'},
99   {"no-auxiliary-files", no_argument, 0, 's'},
[bb9d869]100   {"warnings-are-errors", no_argument, 0, 'w'},
[0dab87a]101   {"log", no_argument, 0, 1},
[ce6ab688]102#if (OS==WIN32)
[0dab87a]103   {"pause", no_argument, 0, 2},
[647407d]104#endif
[bb90203]105   {"help", no_argument, 0, HLP_HELP},
106   {"version", no_argument, 0, HLP_VERSION},
107   {0, 0, 0, 0}
108};
109
[bb9d869]110#define short_opts "pao:qswz:"
[bb90203]111
[647407d]112/* TRANSLATE extract help messages to message file */
[bb90203]113static struct help_msg help[] = {
114/*                              <-- */
[a4ae909]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"},*/
[bb90203]122   {0, 0}
123};
124
[5b68ae1]125/* atexit functions */
[25ab06b]126static void
127delete_output_on_error(void)
128{
[bb9d869]129   if (msg_errors || (f_warnings_are_errors && msg_warnings))
130      filename_delete_output();
[25ab06b]131}
132
[5b68ae1]133#if (OS==WIN32)
134static void
135pause_on_exit(void)
136{
137   while (_kbhit()) _getch();
138   _getch();
139}
140#endif
141
[bb90203]142extern CDECL int
143main(int argc, char **argv)
144{
145   int d;
[be97baf]146   time_t tmUserStart = time(NULL);
147   clock_t tmCPUStart = clock();
[b88b171]148
149   /* Always buffer by line for aven's benefit. */
150   setvbuf(stdout, NULL, _IOLBF, 0);
151
[bb90203]152   init_screen();
153
[bdfe97f]154   msg_init(argv);
[bb90203]155
156   pcs = osnew(settings);
157   pcs->next = NULL;
158   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
[b5a3219]159   pcs->meta = NULL;
[bb90203]160
161   /* Set up root of prefix hierarchy */
162   root = osnew(prefix);
163   root->up = root->right = root->down = NULL;
164   root->stn = NULL;
[421b7d2]165   root->pos = NULL;
[ff6cfe1]166   root->ident = NULL;
[932f7e9]167   root->min_export = root->max_export = 0;
[95c3272]168   root->sflags = BIT(SFLAGS_SURVEY);
[016068a]169   root->filename = NULL;
[647407d]170
171   nosurveyhead = NULL;
[bb90203]172
173   stnlist = NULL;
174   cLegs = cStns = cComponents = 0;
175   totadj = total = totplan = totvert = 0.0;
176
177   for (d = 0; d <= 2; d++) {
[fa42426]178      min[d] = HUGE_REAL;
179      max[d] = -HUGE_REAL;
[bb90203]180      pfxHi[d] = pfxLo[d] = NULL;
181   }
182
[d06141c]183   /* at least one argument must be given */
[b85e20f]184   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
[bb90203]185   while (1) {
[b85e20f]186      int opt = cmdline_getopt();
[bb90203]187      if (opt == EOF) break;
188      switch (opt) {
189       case 'p':
190#ifndef NO_PERCENTAGE
191         fPercent = 1;
192#endif
193         break;
[b4fe9fb]194#ifndef NO_PERCENTAGE
195       case 3:
196         fPercent = 0;
197         break;
198#endif
[8e8057c]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       }
[647407d]213       case 'q':
214         if (fQuiet) fMute = 1;
215         fQuiet = 1;
216         break;
217       case 's':
218         fSuppress = 1;
219         break;
[bb9d869]220       case 'w':
221         f_warnings_are_errors = 1;
222         break;
[8e8057c]223       case 'z': {
224         /* Control which network optimisations are used (development tool) */
225         static int first_opt_z = 1;
[eb18f4d]226         char c;
[8e8057c]227         if (first_opt_z) {
[bb90203]228            optimize = 0;
[8e8057c]229            first_opt_z = 0;
[bb90203]230         }
[c50391b8]231         /* Lollipops, Parallel legs, Iterate mx, Delta* */
[eb18f4d]232         while ((c = *optarg++) != '\0')
[0580c6a]233            if (islower((unsigned char)c)) optimize |= BITA(c);
[0dab87a]234         break;
235       case 1:
236         fLog = fTrue;
[bb90203]237         break;
[5b68ae1]238#if (OS==WIN32)
[0dab87a]239       case 2:
[5b68ae1]240         atexit(pause_on_exit);
241         break;
242#endif
[bb90203]243       }
244      }
245   }
246
[0dab87a]247   if (fLog) {
248      char *fnm;
[9887ea01]249      if (!fnm_output_base) {
250         char *p;
251         p = baseleaf_from_fnm(argv[optind]);
[0156ccfc]252         fnm = add_ext(p, EXT_LOG);
[421b7d2]253         osfree(p);
[9887ea01]254      } else if (fnm_output_base_is_dir) {
[09e8f4c]255         char *p;
256         fnm = baseleaf_from_fnm(argv[optind]);
257         p = use_path(fnm_output_base, fnm);
258         osfree(fnm);
[0156ccfc]259         fnm = add_ext(p, EXT_LOG);
[09e8f4c]260         osfree(p);
261      } else {
[0156ccfc]262         fnm = add_ext(fnm_output_base, EXT_LOG);
[09e8f4c]263      }
[421b7d2]264
[0dab87a]265      if (!freopen(fnm, "w", stdout))
266         fatalerror(/*Failed to open output file `%s'*/47, fnm);
267
268      osfree(fnm);
269   }
270
[90123e8]271   if (!fMute)
272      printf(PRETTYPACKAGE" "VERSION"\n"COPYRIGHT_MSG"\n", msg(/*&copy;*/0));
[bb90203]273
[25ab06b]274   atexit(delete_output_on_error);
275
[bb90203]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;
[8e8057c]282         lf = baseleaf_from_fnm(fnm);
[bb90203]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);
[f4b609d]290      data_file(NULL, fnm); /* first argument is current path */
[cb3d1e2]291
[bb90203]292      optind++;
293   }
[cb3d1e2]294
[bb90203]295   validate();
296
297   solve_network(/*stnlist*/); /* Find coordinates of all points */
298   validate();
[421b7d2]299
[a4ae909]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);
[647407d]304   }
[7104f16]305   if (fhErrStat) safe_fclose(fhErrStat);
[bb90203]306
307   out_current_action(msg(/*Calculating statistics*/120));
[2b6eca8]308   if (!fMute) do_stats();
[647407d]309   if (!fQuiet) {
[f03053a7]310      /* clock() typically wraps after 72 minutes, but there doesn't seem
311       * to be a better way.  Still 72 minutes means some cave!
[be97baf]312       * We detect if clock() could have wrapped and suppress CPU time
313       * printing in this case.
[f03053a7]314       */
[be97baf]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;
[647407d]326
[27b8b59]327      /* tmUser is integer, tmCPU not - equivalent to (ceil(tmCPU) >= tmUser) */
[647407d]328      if (tmCPU + 1 > tmUser) {
[421b7d2]329         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
[647407d]330      } else if (tmCPU == 0) {
[27b8b59]331         if (tmUser != 0.0) {
[421b7d2]332            printf(msg(/*Time used %5.2fs*/141), tmUser);
[647407d]333         } else {
[421b7d2]334            fputs(msg(/*Time used unavailable*/142), stdout);
[647407d]335         }
[bb90203]336      } else {
[5b68ae1]337         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
[bb90203]338      }
[5b68ae1]339      putnl();
[bb90203]340
[5b68ae1]341      puts(msg(/*Done.*/144));
[647407d]342   }
[25ab06b]343   if (msg_warnings || msg_errors) {
[7ebee5b]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);
[25ab06b]351      putnl();
352   }
[bb9d869]353   return EXIT_SUCCESS;
[bb90203]354}
355
356static void
[2b6eca8]357do_range(int d, int msg1, int msg2, int msg3)
[bb90203]358{
[2b6eca8]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();
[bb90203]365}
366
367static void
[06a871f]368do_stats(void)
[bb90203]369{
370   long cLoops = cComponents + cLegs - cStns;
371
[2b6eca8]372   putnl();
[bb90203]373
[a63fdd2a]374   if (cStns == 1) {
[2b6eca8]375      fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
[a63fdd2a]376   } else {
[2b6eca8]377      printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
[a63fdd2a]378   }
[bb90203]379
[a63fdd2a]380   if (cLegs == 1) {
[2b6eca8]381      fputs(msg(/* joined by 1 leg.*/174), stdout);
[a63fdd2a]382   } else {
[2b6eca8]383      printf(msg(/* joined by %ld legs.*/175), cLegs);
[a63fdd2a]384   }
[bb90203]385
[2b6eca8]386   putnl();
[bb90203]387
[a63fdd2a]388   if (cLoops == 1) {
[2b6eca8]389      fputs(msg(/*There is 1 loop.*/138), stdout);
[a63fdd2a]390   } else {
[2b6eca8]391      printf(msg(/*There are %ld loops.*/139), cLoops);
[a63fdd2a]392   }
[bb90203]393
[2b6eca8]394   putnl();
[bb90203]395
396   if (cComponents != 1) {
[2b6eca8]397      printf(msg(/*Survey has %ld connected components.*/178), cComponents);
[a63fdd2a]398      putnl();
399   }
[bb90203]400
[2b6eca8]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
[4ae2ea4]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   }
[bb90203]420
[2b6eca8]421   print_node_stats();
[bb90203]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.