source: git/src/cavern.c @ c0b279c

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since c0b279c was c0b279c, checked in by Olly Betts <olly@…>, 18 years ago

Remove lingering traces of .3dx support

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

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