source: git/src/cavern.c @ 7f4a756

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 7f4a756 was a4ae909, checked in by Olly Betts <olly@…>, 20 years ago

Removed support for writing Chasm's 3dx format.

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

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