source: git/src/cavern.c @ 60dbe70

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 60dbe70 was affaeee, checked in by Olly Betts <olly@…>, 20 years ago

Rework the OS== mechanism as defining WIN32 and UNIX causes problems with
third party headers which test for these defines and assume the wrong OS!

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

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