source: git/src/cavern.c @ ef3d3cc

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since ef3d3cc was 736f7df, checked in by Olly Betts <olly@…>, 10 years ago

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

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