source: git/src/cavern.c @ 1fb581f

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

Merged changes with 1.0 branch.

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

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