source: git/src/cavern.c @ 63befb6

RELEASE/1.0RELEASE/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 63befb6 was 6adb88c, checked in by Olly Betts <olly@…>, 24 years ago

shape now a property of a prefix, not a pos.

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

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