source: git/src/cavern.c @ 533c45a

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

Added --warnings-are-errors.

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

  • Property mode set to 100644
File size: 11.2 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   root->ident = "\\";
169   root->min_export = root->max_export = 0;
170   root->fSuspectTypo = fFalse;
171   root->fSurvey = fTrue;
172
173   nosurveyhead = NULL;
174
175   stnlist = NULL;
176   cLegs = cStns = cComponents = 0;
177   totadj = total = totplan = totvert = 0.0;
178
179   for (d = 0; d <= 2; d++) {
180      min[d] = REAL_BIG;
181      max[d] = -REAL_BIG;
182      pfxHi[d] = pfxLo[d] = NULL;
183   }
184
185   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
186   while (1) {
187      /* at least one argument must be given */
188      int opt = cmdline_getopt();
189      if (opt == EOF) break;
190      switch (opt) {
191       case 'a':
192         fAscii = fTrue;
193         break;
194       case 'p':
195#ifndef NO_PERCENTAGE
196         fPercent = 1;
197#endif
198         break;
199       case 'o': {
200         /* can be a directory (in which case use basename of leaf input)
201          * or a file (in which case just trim the extension off) */
202         if (fDirectory(optarg)) {
203            /* this is a little tricky - we need to note the path here,
204             * and then add the leaf later on (in datain.c) */
205            fnm_output_base = base_from_fnm(optarg);
206            fnm_output_base_is_dir = 1;
207         } else {
208            osfree(fnm_output_base); /* in case of multiple -o options */
209            fnm_output_base = base_from_fnm(optarg);
210         }
211         break;
212       }
213#ifdef NEW3DFORMAT
214       case 'x': {
215         fUseNewFormat = 1;
216         break;
217       }
218#endif
219       case 'q':
220         if (fQuiet) fMute = 1;
221         fQuiet = 1;
222         break;
223       case 's':
224         fSuppress = 1;
225         break;
226       case 'w':
227         f_warnings_are_errors = 1;
228         break;
229       case 'z': {
230         /* Control which network optimisations are used (development tool) */
231         static int first_opt_z = 1;
232         char c;
233         if (first_opt_z) {
234            optimize = 0;
235            first_opt_z = 0;
236         }
237         /* Lollipops, Parallel legs, Iterate mx, Delta* */
238         while ((c = *optarg++) != '\0')
239             if (islower(c)) optimize |= BITA(ch);
240         break;
241       }
242      }
243   }
244
245   out_puts(PACKAGE" "VERSION);
246   out_puts(COPYRIGHT_MSG);
247   putnl();
248
249   atexit(delete_output_on_error);
250
251   /* end of options, now process data files */
252   while (argv[optind]) {
253      const char *fnm = argv[optind];
254
255      if (!fExplicitTitle) {
256         char *lf;
257         lf = baseleaf_from_fnm(fnm);
258         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
259         s_cat(&survey_title, &survey_title_len, lf);
260         osfree(lf);
261      }
262
263      /* Select defaults settings */
264      default_all(pcs);
265#ifdef NEW3DFORMAT
266      /* we need to get the filename of the first one for our base_source */
267      /* and also run_file */
268      if (fUseNewFormat) {
269         create_twig(root, fnm);
270         rhizome = root->twig_link;
271         limb = get_twig(root);
272         firstfilename = osstrdup(fnm);
273         startingdir = osmalloc(MAXPATHLEN);
274#if (OS==RISCOS)
275         strcpy(startingdir, "@");
276#else
277         getcwd(startingdir, MAXPATHLEN);
278#endif
279      }
280#endif
281      data_file("", fnm); /* first argument is current path */
282
283      optind++;
284   }
285
286   validate();
287
288   solve_network(/*stnlist*/); /* Find coordinates of all points */
289   validate();
290
291#ifdef NEW3DFORMAT
292   if (fUseNewFormat) {
293     cave_close(pimgOut); /* this actually does all the writing */
294   } else {
295#endif
296     img_close(pimgOut); /* close .3d file */
297#ifdef NEW3DFORMAT
298   }
299#endif
300   if (fhErrStat) fclose(fhErrStat);
301
302   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
303      list_pos(root); /* produce .pos file */
304
305   out_current_action(msg(/*Calculating statistics*/120));
306   do_stats();
307   if (!fQuiet) {
308      /* clock() typically wraps after 72 minutes, but there doesn't seem
309       * to be a better way.  Still 72 minutes means some cave!
310       */
311      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
312      tmUser = difftime(time(NULL), tmUserStart);
313
314      /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
315      if (tmCPU + 1 > tmUser) {
316         out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU));
317      } else if (tmCPU == 0) {
318         if (tmUser == 0.0) {
319            out_printf((msg(/*Time used %5.2fs*/141), tmUser));
320         } else {
321            out_puts(msg(/*Time used unavailable*/142));
322         }
323      } else {
324         out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143),
325                    tmUser, tmCPU));
326      }
327
328      out_puts(msg(/*Done.*/144));
329   }
330   if (msg_warnings || msg_errors) {
331      printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
332             msg_warnings, msg_errors);
333      putnl();
334      /* FIXME: if (msg_errors || (f_warnings_are_errors && msg_warnings)) print "output not produced..."? */
335   }
336   if (msg_errors || (f_warnings_are_errors && msg_warnings))
337      return EXIT_FAILURE;
338   return EXIT_SUCCESS;
339}
340
341static void
342do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
343{
344   char buf[1024];
345   sprintf(buf, msg(msg1), max[d] - min[d]);
346   strcat(buf, sprint_prefix(pfxHi[d]));
347   sprintf(buf + strlen(buf), msg(msg2), max[d]);
348   strcat(buf, sprint_prefix(pfxLo[d]));
349   sprintf(buf + strlen(buf), msg(msg3), min[d]);
350   if (!fMute) out_puts(buf);
351   if (fh) fputsnl(buf, fh);
352}
353
354static void
355do_stats(void)
356{
357   FILE *fh = NULL;
358   long cLoops = cComponents + cLegs - cStns;
359   char buf[1024];
360
361   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
362      fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
363
364   out_puts("");
365
366   if (cStns == 1)
367      sprintf(buf, msg(/*Survey contains 1 survey station,*/172));
368   else
369      sprintf(buf,
370              msg(/*Survey contains %ld survey stations,*/173), cStns);
371
372   if (cLegs == 1)
373      sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174));
374   else
375      sprintf(buf + strlen(buf),
376              msg(/* joined by %ld legs.*/175), cLegs);
377
378   if (!fMute) out_puts(buf);
379   if (fh) fputsnl(buf, fh);
380
381   if (cLoops == 1)
382      sprintf(buf, msg(/*There is 1 loop.*/138));
383   else
384      sprintf(buf, msg(/*There are %ld loops.*/139), cLoops);
385
386   if (!fMute) out_puts(buf);
387   if (fh) fputsnl(buf, fh);
388
389   if (cComponents != 1) {
390      sprintf(buf,
391              msg(/*Survey has %ld connected components.*/178), cComponents);
392      if (!fMute) out_puts(buf);
393      if (fh) fputsnl(buf, fh);
394   }
395
396   sprintf(buf,
397           msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
398           total, totadj);
399   if (!fMute) out_puts(buf);
400   if (fh) fputsnl(buf, fh);
401
402   sprintf(buf,
403           msg(/*Total plan length of survey legs = %7.2fm*/133), totplan);
404   if (!fMute) out_puts(buf);
405   if (fh) fputsnl(buf, fh);
406
407   sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134),
408           totvert);
409   if (!fMute) out_puts(buf);
410   if (fh) fputsnl(buf, fh);
411
412   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
413            /* at %4.2fm to */136, /* at %4.2fm)*/137);
414   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
415            /* at %4.2fm to */196, /* at %4.2fm)*/197);
416   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
417            /* at %4.2fm to */196, /* at %4.2fm)*/197);
418
419   print_node_stats(fh);
420   /* Also, could give:
421    *  # nodes stations (ie have other than two references or are fixed)
422    *  # fixed stations (list of?)
423    */
424
425   if (fh) fclose(fh);
426}
Note: See TracBrowser for help on using the repository browser.