source: git/src/cavern.c @ 570f4bf

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 570f4bf was 5f662da, checked in by Olly Betts <olly@…>, 24 years ago

cavern.c: Removed assumption of maximum station name length.

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

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