source: git/src/cavern.c @ 7913cbac

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 7913cbac was 6430f0e, checked in by Olly Betts <olly@…>, 24 years ago

Now catch a station with the same name as a survey with no stations in.

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