source: git/src/cavern.c @ daf88e1

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 daf88e1 was 84de6be, checked in by Olly Betts <olly@…>, 26 years ago

Check fhErrStat open before closing

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

  • Property mode set to 100644
File size: 7.7 KB
Line 
1/* > cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
3 * Copyright (C) 1991-1999 Olly Betts
4 */
5
6#ifdef HAVE_CONFIG_H
7#include <config.h>
8#endif
9
10#include <time.h>
11
12#include "cavern.h"
13#include "cmdline.h"
14#include "commands.h"
15#include "datain.h"
16#include "debug.h"
17#include "message.h"
18#include "filename.h"
19#include "filelist.h"
20#include "img.h"
21#include "listpos.h"
22#include "netbits.h"
23#include "netskel.h"
24#include "osdepend.h"
25#include "out.h"
26#include "str.h"
27#include "validate.h"
28
29/* For funcs which want to be immune from messing around with different
30 * calling conventions */
31#ifndef CDECL
32# define CDECL
33#endif
34
35/* Globals */
36node *stnlist = NULL;
37settings *pcs;
38prefix *root;
39long cLegs, cStns;
40long cComponents;
41
42FILE *fhErrStat = NULL;
43img *pimgOut = NULL;
44#ifndef NO_PERCENTAGE
45bool fPercent = fFalse;
46#endif
47bool fAscii = fFalse;
48
49real totadj, total, totplan, totvert;
50real min[3], max[3];
51prefix *pfxHi[3], *pfxLo[3];
52
53char *survey_title = NULL;
54int survey_title_len;
55
56bool fExplicitTitle = fFalse;
57
58char *fnm_output_base = NULL;
59int fnm_output_base_is_dir = 0;
60
61static void do_stats(void);
62
63static const struct option long_opts[] = {
64   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
65#ifdef NO_PERCENTAGE
66   {"percentage", no_argument, 0, 0},
67   {"no-percentage", no_argument, 0, 0},
68#else
69   {"percentage", no_argument, 0, 'p'},
70   {"no-percentage", no_argument, (int*)&fPercent, 0},
71#endif
72   {"output", required_argument, 0, 'o'},
73   {"help", no_argument, 0, HLP_HELP},
74   {"version", no_argument, 0, HLP_VERSION},
75   {0, 0, 0, 0}
76};
77
78#define short_opts "pao:z:"
79
80/* TRANSLATE: FIXME extract help messages to message file */
81static struct help_msg help[] = {
82/*                              <-- */
83   {HLP_ENCODELONG(0),          "display percentage progress"},
84   {'a',                        "output ascii variant of .3d file"},
85   {HLP_ENCODELONG(2),          "set location for output files"},
86 /*{'z',                        "set optimizations for network reduction"},*/
87   {0, 0}
88};
89
90extern CDECL int
91main(int argc, char **argv)
92{
93   int d;
94   static clock_t tmCPUStart;
95   static time_t tmUserStart;
96   static double tmCPU, tmUser;
97
98   check_fp_ok(); /* check very early on */
99
100   tmUserStart = time(NULL);
101   tmCPUStart = clock();
102   init_screen();
103
104   msg_init(argv[0]);
105
106   pcs = osnew(settings);
107   pcs->next = NULL;
108   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
109
110   /* Set up root of prefix hierarchy */
111   root = osnew(prefix);
112   root->up = root->right = root->down = NULL;
113   root->stn = NULL;
114   root->pos = NULL;
115   root->ident = "\\";
116
117   stnlist = NULL;
118   cLegs = cStns = cComponents = 0;
119   totadj = total = totplan = totvert = 0.0;
120
121   for (d = 0; d <= 2; d++) {
122      min[d] = REAL_BIG;
123      max[d] = -REAL_BIG;
124      pfxHi[d] = pfxLo[d] = NULL;
125   }
126
127   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
128   while (1) {
129      /* at least one argument must be given */
130      int opt = cmdline_getopt();
131      if (opt == EOF) break;
132      switch (opt) {
133       case 'a':
134         fAscii = fTrue;
135         break;
136       case 'p':
137#ifndef NO_PERCENTAGE
138         fPercent = 1;
139#endif
140         break;
141       case 'o': {
142         /* can be a directory (in which case use basename of leaf input)
143          * or a file (in which case just trim the extension off) */
144         if (fDirectory(optarg)) {
145            /* this is a little tricky - we need to note the path here,
146             * and then add the leaf later on (in datain.c) */
147            fnm_output_base = base_from_fnm(optarg);
148            fnm_output_base_is_dir = 1;
149         } else {
150            osfree(fnm_output_base); /* in case of multiple -o options */
151            fnm_output_base = base_from_fnm(optarg);
152         }
153         break;
154       }
155       case 'z': {
156         /* Control which network optimisations are used (development tool) */
157         static int first_opt_z = 1;
158         int ch;
159         if (first_opt_z) {
160            optimize = 0;
161            first_opt_z = 0;
162         }
163         /* Lollipops, Parallel legs, Iterate mx, Delta* */
164         while ((ch = *optarg++)) if (islower(ch)) optimize |= BITA(ch);
165         break;
166       }
167      }
168   }
169
170   out_puts(PACKAGE" "VERSION);
171   out_puts(COPYRIGHT_MSG);
172   putnl();
173
174   /* end of options, now process data files */
175   while (argv[optind]) {
176      const char *fnm = argv[optind];
177
178      if (!fExplicitTitle) {
179         char *lf;
180         lf = baseleaf_from_fnm(fnm);
181         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
182         s_cat(&survey_title, &survey_title_len, lf);
183         osfree(lf);
184      }
185
186      /* Select defaults settings */
187      default_all(pcs);
188
189      data_file("", fnm); /* first argument is current path */
190     
191      optind++;
192   }
193   
194   validate();
195
196   solve_network(/*stnlist*/); /* Find coordinates of all points */
197   validate();
198#ifdef NEW3DFORMAT
199   cave_close(pimgOut); /* close .3d file */
200#else
201   img_close(pimgOut); /* close .3d file */
202#endif
203   if (fhErrStat) fclose(fhErrStat); /* FIXME: only NULL if we're meddling? */
204
205   list_pos(root); /* produce .pos file */
206
207   out_current_action(msg(/*Calculating statistics*/120));
208   do_stats();
209
210   tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
211   tmUser = difftime(time(NULL), tmUserStart);
212
213   /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
214   if (tmCPU + 1 > tmUser) {
215      out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU));
216   } else if (tmCPU == 0) {
217      if (tmUser == 0.0) {
218         out_printf((msg(/*Time used %5.2fs*/141), tmUser));
219      } else {
220         out_puts(msg(/*Time used unavailable*/142));
221      }
222   } else {
223      out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143),
224                  tmUser, tmCPU));
225   }
226
227   out_puts(msg(/*Done.*/144));
228   return error_summary();
229}
230
231static void
232do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
233{
234   char buf [1024];
235   sprintf(buf, msg(msg1), max[d] - min[d]);
236   strcat(buf, sprint_prefix(pfxHi[d]));
237   sprintf(buf + strlen(buf), msg(msg2), max[d]);
238   strcat(buf, sprint_prefix(pfxLo[d]));
239   sprintf(buf + strlen(buf), msg(msg3), min[d]);
240   out_puts(buf);
241   fputsnl(buf, fh);
242}
243
244static void
245do_stats(void)
246{
247   FILE *fh;
248   long cLoops = cComponents + cLegs - cStns;
249   char buf[1024];
250
251   fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
252
253   out_puts("");
254
255   if (cStns == 1)
256      sprintf(buf, msg(/*Survey contains 1 survey station,*/172));
257   else
258      sprintf(buf,
259              msg(/*Survey contains %ld survey stations,*/173), cStns);
260
261   if (cLegs == 1)
262      sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174));
263   else
264      sprintf(buf + strlen(buf),
265              msg(/* joined by %ld legs.*/175), cLegs);
266
267   out_puts(buf);
268   fputsnl(buf, fh);
269
270   if (cLoops == 1)
271      sprintf(buf, msg(/*There is 1 loop.*/138));
272   else
273      sprintf(buf, msg(/*There are %ld loops.*/139), cLoops);
274
275   out_puts(buf);
276   fputsnl(buf, fh);
277
278   if (cComponents != 1) {
279      sprintf(buf,
280              msg(/*Survey has %ld connected components.*/178), cComponents);
281      out_puts(buf);
282      fputsnl(buf, fh);
283   }
284
285   sprintf(buf,
286           msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
287           total, totadj);
288   out_puts(buf);
289   fputsnl(buf, fh);
290
291   sprintf(buf,
292           msg(/*Total plan length of survey legs = %7.2fm*/133), totplan);
293   out_puts(buf);
294   fputsnl(buf, fh);
295
296   sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134),
297           totvert);
298   out_puts(buf);
299   fputsnl(buf, fh);
300
301   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
302            /* at %4.2fm to */136, /* at %4.2fm)*/137);
303   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
304            /* at %4.2fm to */196, /* at %4.2fm)*/197);
305   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
306            /* at %4.2fm to */196, /* at %4.2fm)*/197);
307
308   print_node_stats(fh);
309   /* Also, could give:
310    *  # nodes stations (ie have other than two references or are fixed)
311    *  # fixed stations (list of?)
312    */
313
314   fclose(fh);
315}
Note: See TracBrowser for help on using the repository browser.