source: git/src/cavern.c @ b5f661c

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

Turned gcc's warnings up real loud and fixed all the resulting warnings
(except ones about y1); removed now unused cvrotfns.h.

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

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