source: git/src/cavern.c @ 570f4bf

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 570f4bf was 5f662da, checked in by Olly Betts <olly@…>, 24 years ago

cavern.c: Removed assumption of maximum station name length.

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