source: git/src/cavern.c @ 5311876

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 5311876 was 5311876, checked in by Olly Betts <olly@…>, 13 years ago

lib/messages.txt,src/cavern.c: Drop "non-fatal" from the report of
how many errors there were at the end of the run - it just confuses
users - we won't even get here if there's a fatal error!
lib/messages.txt: Add missing translations of two "Export" variants
for ca, sk, and ro.

git-svn-id: file:///home/survex-svn/survex/trunk@3592 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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