source: git/src/cavern.c @ e68fccf

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

Added missing newlines to range output.

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

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[bb90203]1/* > cavern.c
2 * SURVEX Cave surveying software: data reduction main and related functions
[932f7e9]3 * Copyright (C) 1991-2001 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[bb90203]18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <time.h>
25
[5853657]26#include "cavern.h"
27#include "cmdline.h"
28#include "commands.h"
[bb90203]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"
[5853657]36#include "netbits.h"
37#include "netskel.h"
38#include "osdepend.h"
[bb90203]39#include "out.h"
[8e8057c]40#include "str.h"
[5853657]41#include "validate.h"
[bb90203]42
[647407d]43#ifdef NEW3DFORMAT
[ee7bafa]44#if OS != RISCOS && OS != MSDOS
[407084d]45#include <unistd.h> /* for getcwd() */
[ee7bafa]46#endif
[407084d]47
[647407d]48#include "new3dout.h"
49#ifndef MAXPATHLEN
50#define MAXPATHLEN 1024
51#endif
52#endif
53
[bb90203]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;
[d1878c51]66bool fExportUsed = fFalse;
[bb90203]67
68FILE *fhErrStat = NULL;
69img *pimgOut = NULL;
70#ifndef NO_PERCENTAGE
71bool fPercent = fFalse;
72#endif
73bool fAscii = fFalse;
[647407d]74bool fQuiet = fFalse; /* just show brief summary + errors */
[25ab06b]75static bool fMute = fFalse; /* just show errors */
[647407d]76bool fSuppress = fFalse; /* only output 3d(3dx) file */
[bb9d869]77static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */   
[647407d]78
79nosurveylink *nosurveyhead;
[bb90203]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
[8e8057c]90char *fnm_output_base = NULL;
91int fnm_output_base_is_dir = 0;
92
[bb90203]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
[8e8057c]104   {"output", required_argument, 0, 'o'},
[647407d]105   {"quiet", no_argument, 0, 'q'},
106   {"no-auxiliary-files", no_argument, 0, 's'},
[bb9d869]107   {"warnings-are-errors", no_argument, 0, 'w'},
[647407d]108#ifdef NEW3DFORMAT
109   {"new-format", no_argument, 0, 'x'},
110#endif
[bb90203]111   {"help", no_argument, 0, HLP_HELP},
112   {"version", no_argument, 0, HLP_VERSION},
113   {0, 0, 0, 0}
114};
115
[647407d]116#ifdef NEW3DFORMAT
[bb9d869]117#define short_opts "pxao:qswz:"
[647407d]118#else
[bb9d869]119#define short_opts "pao:qswz:"
[647407d]120#endif
[bb90203]121
[647407d]122/* TRANSLATE extract help messages to message file */
[bb90203]123static struct help_msg help[] = {
124/*                              <-- */
125   {HLP_ENCODELONG(0),          "display percentage progress"},
126   {'a',                        "output ascii variant of .3d file"},
[8e8057c]127   {HLP_ENCODELONG(2),          "set location for output files"},
[647407d]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
[8e8057c]133 /*{'z',                        "set optimizations for network reduction"},*/
[bb90203]134   {0, 0}
135};
136
[25ab06b]137/* atexit function */
138static void
139delete_output_on_error(void)
140{
[bb9d869]141   if (msg_errors || (f_warnings_are_errors && msg_warnings))
142      filename_delete_output();
[25ab06b]143}
144
[bb90203]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
[b85e20f]157   msg_init(argv[0]);
[bb90203]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;
[85506af]168   /* FIXME: shouldn't need to set this really, but need to check that nothing assumes the value of root->ident isn't "" */
[bb90203]169   root->ident = "\\";
[932f7e9]170   root->min_export = root->max_export = 0;
[95c3272]171   root->sflags = BIT(SFLAGS_SURVEY);
[016068a]172   root->filename = NULL;
[647407d]173
174   nosurveyhead = NULL;
[bb90203]175
176   stnlist = NULL;
177   cLegs = cStns = cComponents = 0;
178   totadj = total = totplan = totvert = 0.0;
179
180   for (d = 0; d <= 2; d++) {
181      min[d] = REAL_BIG;
182      max[d] = -REAL_BIG;
183      pfxHi[d] = pfxLo[d] = NULL;
184   }
185
[b85e20f]186   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
[bb90203]187   while (1) {
[b85e20f]188      /* at least one argument must be given */
189      int opt = cmdline_getopt();
[bb90203]190      if (opt == EOF) break;
191      switch (opt) {
192       case 'a':
193         fAscii = fTrue;
194         break;
195       case 'p':
196#ifndef NO_PERCENTAGE
197         fPercent = 1;
198#endif
199         break;
[8e8057c]200       case 'o': {
201         /* can be a directory (in which case use basename of leaf input)
202          * or a file (in which case just trim the extension off) */
203         if (fDirectory(optarg)) {
204            /* this is a little tricky - we need to note the path here,
205             * and then add the leaf later on (in datain.c) */
206            fnm_output_base = base_from_fnm(optarg);
207            fnm_output_base_is_dir = 1;
208         } else {
209            osfree(fnm_output_base); /* in case of multiple -o options */
210            fnm_output_base = base_from_fnm(optarg);
211         }
212         break;
213       }
[647407d]214#ifdef NEW3DFORMAT
215       case 'x': {
216         fUseNewFormat = 1;
217         break;
218       }
219#endif
220       case 'q':
221         if (fQuiet) fMute = 1;
222         fQuiet = 1;
223         break;
224       case 's':
225         fSuppress = 1;
226         break;
[bb9d869]227       case 'w':
228         f_warnings_are_errors = 1;
229         break;
[8e8057c]230       case 'z': {
231         /* Control which network optimisations are used (development tool) */
232         static int first_opt_z = 1;
[eb18f4d]233         char c;
[8e8057c]234         if (first_opt_z) {
[bb90203]235            optimize = 0;
[8e8057c]236            first_opt_z = 0;
[bb90203]237         }
[c50391b8]238         /* Lollipops, Parallel legs, Iterate mx, Delta* */
[eb18f4d]239         while ((c = *optarg++) != '\0')
240             if (islower(c)) optimize |= BITA(ch);
[bb90203]241         break;
242       }
243      }
244   }
245
246   out_puts(PACKAGE" "VERSION);
247   out_puts(COPYRIGHT_MSG);
248   putnl();
249
[25ab06b]250   atexit(delete_output_on_error);
251
[bb90203]252   /* end of options, now process data files */
253   while (argv[optind]) {
254      const char *fnm = argv[optind];
255
256      if (!fExplicitTitle) {
257         char *lf;
[8e8057c]258         lf = baseleaf_from_fnm(fnm);
[bb90203]259         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
260         s_cat(&survey_title, &survey_title_len, lf);
261         osfree(lf);
262      }
263
264      /* Select defaults settings */
265      default_all(pcs);
[647407d]266#ifdef NEW3DFORMAT
267      /* we need to get the filename of the first one for our base_source */
268      /* and also run_file */
269      if (fUseNewFormat) {
270         create_twig(root, fnm);
271         rhizome = root->twig_link;
272         limb = get_twig(root);
273         firstfilename = osstrdup(fnm);
274         startingdir = osmalloc(MAXPATHLEN);
[49090c02]275#if (OS==RISCOS)
276         strcpy(startingdir, "@");
277#else
[647407d]278         getcwd(startingdir, MAXPATHLEN);
[49090c02]279#endif
[647407d]280      }
281#endif
[bb90203]282      data_file("", fnm); /* first argument is current path */
[cb3d1e2]283
[bb90203]284      optind++;
285   }
[cb3d1e2]286
[bb90203]287   validate();
288
289   solve_network(/*stnlist*/); /* Find coordinates of all points */
290   validate();
[647407d]291
[bb90203]292#ifdef NEW3DFORMAT
[647407d]293   if (fUseNewFormat) {
294     cave_close(pimgOut); /* this actually does all the writing */
295   } else {
[bb90203]296#endif
[647407d]297     img_close(pimgOut); /* close .3d file */
298#ifdef NEW3DFORMAT
299   }
300#endif
301   if (fhErrStat) fclose(fhErrStat);
[bb90203]302
[bb9d869]303   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
304      list_pos(root); /* produce .pos file */
[bb90203]305
306   out_current_action(msg(/*Calculating statistics*/120));
307   do_stats();
[647407d]308   if (!fQuiet) {
[f03053a7]309      /* clock() typically wraps after 72 minutes, but there doesn't seem
310       * to be a better way.  Still 72 minutes means some cave!
311       */
[647407d]312      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
313      tmUser = difftime(time(NULL), tmUserStart);
314
315      /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
316      if (tmCPU + 1 > tmUser) {
317         out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU));
318      } else if (tmCPU == 0) {
319         if (tmUser == 0.0) {
320            out_printf((msg(/*Time used %5.2fs*/141), tmUser));
321         } else {
322            out_puts(msg(/*Time used unavailable*/142));
323         }
[bb90203]324      } else {
[647407d]325         out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143),
326                    tmUser, tmCPU));
[bb90203]327      }
328
[647407d]329      out_puts(msg(/*Done.*/144));
330   }
[25ab06b]331   if (msg_warnings || msg_errors) {
332      printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
333             msg_warnings, msg_errors);
334      putnl();
[bb9d869]335      /* FIXME: if (msg_errors || (f_warnings_are_errors && msg_warnings)) print "output not produced..."? */
[25ab06b]336   }
[bb9d869]337   if (msg_errors || (f_warnings_are_errors && msg_warnings))
338      return EXIT_FAILURE;
339   return EXIT_SUCCESS;
[bb90203]340}
341
342static void
343do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
344{
[5f662da]345   if (!fMute) {
346      printf(msg(msg1), max[d] - min[d]);
347      fprint_prefix(stdout, pfxHi[d]);
348      printf(msg(msg2), max[d]);
349      fprint_prefix(stdout, pfxLo[d]);
350      printf(msg(msg3), min[d]);
[0ec1606]351      putnl();
[5f662da]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]);
[0ec1606]359      fputnl(fh);
[5f662da]360   }
[bb90203]361}
362
363static void
364do_stats(void)
365{
[407084d]366   FILE *fh = NULL;
[bb90203]367   long cLoops = cComponents + cLegs - cStns;
[5f662da]368   char buf[1024]; /* FIXME: try to remove static buffer */
[bb90203]369
[bb9d869]370   if (!fSuppress && !(msg_errors || (f_warnings_are_errors && msg_warnings)))
[647407d]371      fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
[bb90203]372
373   out_puts("");
374
375   if (cStns == 1)
376      sprintf(buf, msg(/*Survey contains 1 survey station,*/172));
377   else
378      sprintf(buf,
379              msg(/*Survey contains %ld survey stations,*/173), cStns);
380
381   if (cLegs == 1)
382      sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174));
383   else
384      sprintf(buf + strlen(buf),
385              msg(/* joined by %ld legs.*/175), cLegs);
386
[647407d]387   if (!fMute) out_puts(buf);
[407084d]388   if (fh) fputsnl(buf, fh);
[bb90203]389
390   if (cLoops == 1)
391      sprintf(buf, msg(/*There is 1 loop.*/138));
392   else
393      sprintf(buf, msg(/*There are %ld loops.*/139), cLoops);
394
[647407d]395   if (!fMute) out_puts(buf);
[407084d]396   if (fh) fputsnl(buf, fh);
[bb90203]397
398   if (cComponents != 1) {
399      sprintf(buf,
400              msg(/*Survey has %ld connected components.*/178), cComponents);
[647407d]401      if (!fMute) out_puts(buf);
[407084d]402      if (fh) fputsnl(buf, fh);
[bb90203]403   }
404
405   sprintf(buf,
406           msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
407           total, totadj);
[647407d]408   if (!fMute) out_puts(buf);
[407084d]409   if (fh) fputsnl(buf, fh);
[bb90203]410
411   sprintf(buf,
412           msg(/*Total plan length of survey legs = %7.2fm*/133), totplan);
[647407d]413   if (!fMute) out_puts(buf);
[407084d]414   if (fh) fputsnl(buf, fh);
[bb90203]415
416   sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134),
417           totvert);
[647407d]418   if (!fMute) out_puts(buf);
[407084d]419   if (fh) fputsnl(buf, fh);
[bb90203]420
421   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
422            /* at %4.2fm to */136, /* at %4.2fm)*/137);
423   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
424            /* at %4.2fm to */196, /* at %4.2fm)*/197);
425   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
426            /* at %4.2fm to */196, /* at %4.2fm)*/197);
427
428   print_node_stats(fh);
429   /* Also, could give:
430    *  # nodes stations (ie have other than two references or are fixed)
431    *  # fixed stations (list of?)
432    */
433
[407084d]434   if (fh) fclose(fh);
[bb90203]435}
Note: See TracBrowser for help on using the repository browser.