source: git/src/cavern.c @ b091b69

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 b091b69 was 2d3f65a, checked in by Olly Betts <olly@…>, 24 years ago

Fixed borlandc compile warnings.

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