source: git/src/cavern.c @ 54404774

RELEASE/1.0
Last change on this file since 54404774 was 54404774, checked in by Olly Betts <olly@…>, 13 years ago

lib/messages.txt,src/cavern.c: Fix up backport for new cavern -v
option by backporting the message it needs too.

git-svn-id: file:///home/survex-svn/survex/branches/1.0@3694 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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