source: git/src/cavern.c @ ca7769e

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 ca7769e was 0580c6a, checked in by Olly Betts <olly@…>, 21 years ago

Copy changes over from 1.0 branch - everything except Printing from Aven, and
Exporting CAD formats from Aven.

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

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