source: git/src/cavern.c @ 1d2ff41

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 1d2ff41 was 7104f16, checked in by Olly Betts <olly@…>, 24 years ago

another use of safe_fclose()

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

  • Property mode set to 100644
File size: 13.1 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
[8318645]44# if OS != RISCOS
45/* include header for getcwd() */
46#  if OS == MSDOS && defined(__TURBOC__)
47#   include <dir.h>
48#  else
49#   include <unistd.h>
50#  endif
[2d3f65a]51#endif
[407084d]52
[647407d]53#ifndef MAXPATHLEN
54#define MAXPATHLEN 1024
55#endif
56#endif
57
[bb90203]58/* For funcs which want to be immune from messing around with different
59 * calling conventions */
60#ifndef CDECL
61# define CDECL
62#endif
63
64/* Globals */
65node *stnlist = NULL;
66settings *pcs;
67prefix *root;
68long cLegs, cStns;
69long cComponents;
[d1878c51]70bool fExportUsed = fFalse;
[bb90203]71
72FILE *fhErrStat = NULL;
[693388e]73img *pimg = NULL;
[bb90203]74#ifndef NO_PERCENTAGE
75bool fPercent = fFalse;
76#endif
[647407d]77bool fQuiet = fFalse; /* just show brief summary + errors */
[ed0f5b6]78bool fMute = fFalse; /* just show errors */
[647407d]79bool fSuppress = fFalse; /* only output 3d(3dx) file */
[0dab87a]80static bool fLog = fFalse; /* stdout to .log file, suppress .inf file */
[bb9d869]81static bool f_warnings_are_errors = fFalse; /* turn warnings into errors */   
[647407d]82
83nosurveylink *nosurveyhead;
[bb90203]84
85real totadj, total, totplan, totvert;
86real min[3], max[3];
87prefix *pfxHi[3], *pfxLo[3];
88
89char *survey_title = NULL;
90int survey_title_len;
91
92bool fExplicitTitle = fFalse;
93
[8e8057c]94char *fnm_output_base = NULL;
95int fnm_output_base_is_dir = 0;
96
[06a871f]97static void do_stats(void);
[bb90203]98
99static const struct option long_opts[] = {
100   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
101#ifdef NO_PERCENTAGE
102   {"percentage", no_argument, 0, 0},
103   {"no-percentage", no_argument, 0, 0},
104#else
105   {"percentage", no_argument, 0, 'p'},
106   {"no-percentage", no_argument, (int*)&fPercent, 0},
107#endif
[8e8057c]108   {"output", required_argument, 0, 'o'},
[647407d]109   {"quiet", no_argument, 0, 'q'},
110   {"no-auxiliary-files", no_argument, 0, 's'},
[bb9d869]111   {"warnings-are-errors", no_argument, 0, 'w'},
[0dab87a]112   {"log", no_argument, 0, 1},
[647407d]113#ifdef NEW3DFORMAT
114   {"new-format", no_argument, 0, 'x'},
[5b68ae1]115#endif
[ce6ab688]116#if (OS==WIN32)
[0dab87a]117   {"pause", no_argument, 0, 2},
[647407d]118#endif
[bb90203]119   {"help", no_argument, 0, HLP_HELP},
120   {"version", no_argument, 0, HLP_VERSION},
121   {0, 0, 0, 0}
122};
123
[647407d]124#ifdef NEW3DFORMAT
[bb9d869]125#define short_opts "pxao:qswz:"
[647407d]126#else
[bb9d869]127#define short_opts "pao:qswz:"
[647407d]128#endif
[bb90203]129
[647407d]130/* TRANSLATE extract help messages to message file */
[bb90203]131static struct help_msg help[] = {
132/*                              <-- */
133   {HLP_ENCODELONG(0),          "display percentage progress"},
[8e8057c]134   {HLP_ENCODELONG(2),          "set location for output files"},
[647407d]135   {HLP_ENCODELONG(3),          "only show brief summary (-qq for errors only)"},
[c7c70fe]136   {HLP_ENCODELONG(4),          "do not create .inf or .err files"},
[9085f95]137   {HLP_ENCODELONG(5),          "turn warnings into errors"},
[0dab87a]138   {HLP_ENCODELONG(6),          "log output to .log file; don't create .inf file"},
[647407d]139#ifdef NEW3DFORMAT
[0dab87a]140   {HLP_ENCODELONG(7),          "output data in chasm's 3dx format"},
[647407d]141#endif
[8e8057c]142 /*{'z',                        "set optimizations for network reduction"},*/
[bb90203]143   {0, 0}
144};
145
[5b68ae1]146/* atexit functions */
[25ab06b]147static void
148delete_output_on_error(void)
149{
[bb9d869]150   if (msg_errors || (f_warnings_are_errors && msg_warnings))
151      filename_delete_output();
[25ab06b]152}
153
[5b68ae1]154#if (OS==WIN32)
155static void
156pause_on_exit(void)
157{
158   while (_kbhit()) _getch();
159   _getch();
160}
161#endif
162
[bb90203]163extern CDECL int
164main(int argc, char **argv)
165{
166   int d;
167   static clock_t tmCPUStart;
168   static time_t tmUserStart;
169   static double tmCPU, tmUser;
170
171   tmUserStart = time(NULL);
172   tmCPUStart = clock();
173   init_screen();
174
[b85e20f]175   msg_init(argv[0]);
[bb90203]176
177   pcs = osnew(settings);
178   pcs->next = NULL;
179   pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1;
180
181   /* Set up root of prefix hierarchy */
182   root = osnew(prefix);
183   root->up = root->right = root->down = NULL;
184   root->stn = NULL;
[6adb88c]185   root->pos = NULL;   
[85506af]186   /* FIXME: shouldn't need to set this really, but need to check that nothing assumes the value of root->ident isn't "" */
[bb90203]187   root->ident = "\\";
[932f7e9]188   root->min_export = root->max_export = 0;
[95c3272]189   root->sflags = BIT(SFLAGS_SURVEY);
[016068a]190   root->filename = NULL;
[647407d]191
192   nosurveyhead = NULL;
[bb90203]193
194   stnlist = NULL;
195   cLegs = cStns = cComponents = 0;
196   totadj = total = totplan = totvert = 0.0;
197
198   for (d = 0; d <= 2; d++) {
199      min[d] = REAL_BIG;
200      max[d] = -REAL_BIG;
201      pfxHi[d] = pfxLo[d] = NULL;
202   }
203
[d06141c]204   /* at least one argument must be given */
[b85e20f]205   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1);
[bb90203]206   while (1) {
[b85e20f]207      int opt = cmdline_getopt();
[bb90203]208      if (opt == EOF) break;
209      switch (opt) {
210       case 'p':
211#ifndef NO_PERCENTAGE
212         fPercent = 1;
213#endif
214         break;
[8e8057c]215       case 'o': {
216         /* can be a directory (in which case use basename of leaf input)
217          * or a file (in which case just trim the extension off) */
218         if (fDirectory(optarg)) {
219            /* this is a little tricky - we need to note the path here,
220             * and then add the leaf later on (in datain.c) */
221            fnm_output_base = base_from_fnm(optarg);
222            fnm_output_base_is_dir = 1;
223         } else {
224            osfree(fnm_output_base); /* in case of multiple -o options */
225            fnm_output_base = base_from_fnm(optarg);
226         }
227         break;
228       }
[647407d]229#ifdef NEW3DFORMAT
230       case 'x': {
231         fUseNewFormat = 1;
232         break;
233       }
234#endif
235       case 'q':
236         if (fQuiet) fMute = 1;
237         fQuiet = 1;
238         break;
239       case 's':
240         fSuppress = 1;
241         break;
[bb9d869]242       case 'w':
243         f_warnings_are_errors = 1;
244         break;
[8e8057c]245       case 'z': {
246         /* Control which network optimisations are used (development tool) */
247         static int first_opt_z = 1;
[eb18f4d]248         char c;
[8e8057c]249         if (first_opt_z) {
[bb90203]250            optimize = 0;
[8e8057c]251            first_opt_z = 0;
[bb90203]252         }
[c50391b8]253         /* Lollipops, Parallel legs, Iterate mx, Delta* */
[eb18f4d]254         while ((c = *optarg++) != '\0')
[0dab87a]255            if (islower(c)) optimize |= BITA(c);
256         break;
257       case 1:
258         fLog = fTrue;
[bb90203]259         break;
[5b68ae1]260#if (OS==WIN32)
[0dab87a]261       case 2:
[5b68ae1]262         atexit(pause_on_exit);
263         break;
264#endif
[bb90203]265       }
266      }
267   }
268
[0dab87a]269   if (fLog) {
270      char *fnm;
[9887ea01]271      if (!fnm_output_base) {
272         char *p;
273         p = baseleaf_from_fnm(argv[optind]);
274         fnm = add_ext(p, "log");
275         osfree(p);     
276      } else if (fnm_output_base_is_dir) {
[09e8f4c]277         char *p;
278         fnm = baseleaf_from_fnm(argv[optind]);
279         p = use_path(fnm_output_base, fnm);
280         osfree(fnm);
281         fnm = add_ext(p, "log");
282         osfree(p);
283      } else {
284         fnm = add_ext(fnm_output_base, "log");
285      }
286     
[0dab87a]287      if (!freopen(fnm, "w", stdout))
288         fatalerror(/*Failed to open output file `%s'*/47, fnm);
289
290      osfree(fnm);
291   }
292
[ed0f5b6]293   if (!fMute) puts(PACKAGE" "VERSION"\n"COPYRIGHT_MSG);
[bb90203]294
[25ab06b]295   atexit(delete_output_on_error);
296
[bb90203]297   /* end of options, now process data files */
298   while (argv[optind]) {
299      const char *fnm = argv[optind];
300
301      if (!fExplicitTitle) {
302         char *lf;
[8e8057c]303         lf = baseleaf_from_fnm(fnm);
[bb90203]304         if (survey_title) s_catchar(&survey_title, &survey_title_len, ' ');
305         s_cat(&survey_title, &survey_title_len, lf);
306         osfree(lf);
307      }
308
309      /* Select defaults settings */
310      default_all(pcs);
[647407d]311#ifdef NEW3DFORMAT
312      /* we need to get the filename of the first one for our base_source */
313      /* and also run_file */
314      if (fUseNewFormat) {
315         create_twig(root, fnm);
316         rhizome = root->twig_link;
317         limb = get_twig(root);
318         firstfilename = osstrdup(fnm);
319         startingdir = osmalloc(MAXPATHLEN);
[49090c02]320#if (OS==RISCOS)
321         strcpy(startingdir, "@");
322#else
[647407d]323         getcwd(startingdir, MAXPATHLEN);
[49090c02]324#endif
[647407d]325      }
326#endif
[bb90203]327      data_file("", fnm); /* first argument is current path */
[cb3d1e2]328
[bb90203]329      optind++;
330   }
[cb3d1e2]331
[bb90203]332   validate();
333
334   solve_network(/*stnlist*/); /* Find coordinates of all points */
335   validate();
[7104f16]336   
[bb90203]337#ifdef NEW3DFORMAT
[647407d]338   if (fUseNewFormat) {
[ed0f5b6]339      cave_close(pimg); /* this actually does all the writing */
[647407d]340   } else {
[bb90203]341#endif
[ed0f5b6]342      img_close(pimg); /* close .3d file */
[647407d]343#ifdef NEW3DFORMAT
344   }
345#endif
[7104f16]346   if (fhErrStat) safe_fclose(fhErrStat);
[bb90203]347
348   out_current_action(msg(/*Calculating statistics*/120));
[06a871f]349   do_stats();
[647407d]350   if (!fQuiet) {
[f03053a7]351      /* clock() typically wraps after 72 minutes, but there doesn't seem
352       * to be a better way.  Still 72 minutes means some cave!
353       */
[647407d]354      tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC;
355      tmUser = difftime(time(NULL), tmUserStart);
356
357      /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */
358      if (tmCPU + 1 > tmUser) {
[5b68ae1]359         printf(msg(/*CPU time used %5.2fs*/140), tmCPU);
[647407d]360      } else if (tmCPU == 0) {
361         if (tmUser == 0.0) {
[5b68ae1]362            printf(msg(/*Time used %5.2fs*/141), tmUser);
[647407d]363         } else {
[5b68ae1]364            fputs(msg(/*Time used unavailable*/142), stdout);
[647407d]365         }
[bb90203]366      } else {
[5b68ae1]367         printf(msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), tmUser, tmCPU);
[bb90203]368      }
[5b68ae1]369      putnl();
[bb90203]370
[5b68ae1]371      puts(msg(/*Done.*/144));
[647407d]372   }
[25ab06b]373   if (msg_warnings || msg_errors) {
374      printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
375             msg_warnings, msg_errors);
376      putnl();
[bb9d869]377      /* FIXME: if (msg_errors || (f_warnings_are_errors && msg_warnings)) print "output not produced..."? */
[25ab06b]378   }
[bb9d869]379   if (msg_errors || (f_warnings_are_errors && msg_warnings))
380      return EXIT_FAILURE;
381   return EXIT_SUCCESS;
[bb90203]382}
383
384static void
385do_range(FILE *fh, int d, int msg1, int msg2, int msg3)
386{
[5f662da]387   if (!fMute) {
388      printf(msg(msg1), max[d] - min[d]);
389      fprint_prefix(stdout, pfxHi[d]);
390      printf(msg(msg2), max[d]);
391      fprint_prefix(stdout, pfxLo[d]);
392      printf(msg(msg3), min[d]);
[0ec1606]393      putnl();
[5f662da]394   }
395   if (fh) {
396      fprintf(fh, msg(msg1), max[d] - min[d]);
397      fprint_prefix(fh, pfxHi[d]);
398      fprintf(fh, msg(msg2), max[d]);
399      fprint_prefix(fh, pfxLo[d]);
400      fprintf(fh, msg(msg3), min[d]);
[0ec1606]401      fputnl(fh);
[5f662da]402   }
[bb90203]403}
404
405static void
[06a871f]406do_stats(void)
[bb90203]407{
[06a871f]408   FILE *fh = NULL;
[bb90203]409   long cLoops = cComponents + cLegs - cStns;
410
[0dab87a]411   if (!fLog && !fSuppress &&
412       !(msg_errors || (f_warnings_are_errors && msg_warnings)))
[06a871f]413      fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w");
[977a053]414   else
415      if (fMute) return;
[bb90203]416
[5b68ae1]417   if (!fMute) putnl();
[bb90203]418
[a63fdd2a]419   if (cStns == 1) {
420      if (!fMute)
421         fputs(msg(/*Survey contains 1 survey station,*/172), stdout);
422      if (fh)
423         fputs(msg(/*Survey contains 1 survey station,*/172), fh);
424   } else {
425      if (!fMute)
426         printf(msg(/*Survey contains %ld survey stations,*/173), cStns);
427      if (fh)
428         fprintf(fh, msg(/*Survey contains %ld survey stations,*/173), cStns);
429   }
[bb90203]430
[a63fdd2a]431   if (cLegs == 1) {
432      if (!fMute)
433         fputs(msg(/* joined by 1 leg.*/174), stdout);
434      if (fh)
435         fputs(msg(/* joined by 1 leg.*/174), fh);
436   } else {
437      if (!fMute)
438         printf(msg(/* joined by %ld legs.*/175), cLegs);
439      if (fh)
440         fprintf(fh, msg(/* joined by %ld legs.*/175), cLegs);
441   }
[bb90203]442
[a63fdd2a]443   if (!fMute) putnl();
444   if (fh) fputnl(fh);
[bb90203]445
[a63fdd2a]446   if (cLoops == 1) {
447      if (!fMute)
448         fputs(msg(/*There is 1 loop.*/138), stdout);
449      if (fh)
450         fputs(msg(/*There is 1 loop.*/138), fh);
451   } else {
452      if (!fMute)
453         printf(msg(/*There are %ld loops.*/139), cLoops);
454      if (fh)
455         fprintf(fh, msg(/*There are %ld loops.*/139), cLoops);
456   }
[bb90203]457
[a63fdd2a]458   if (!fMute) putnl();
459   if (fh) fputnl(fh);
[bb90203]460
461   if (cComponents != 1) {
[a63fdd2a]462      if (!fMute) {
463         printf(msg(/*Survey has %ld connected components.*/178), cComponents);
464         putnl();
465      }
466      if (fh) {
467         fprintf(fh, msg(/*Survey has %ld connected components.*/178), cComponents);
468         fputnl(fh);
469      }
[bb90203]470   }
471
[a63fdd2a]472   if (!fMute) {
473      printf(msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
474             total, totadj);
475      putnl();
476      printf(msg(/*Total plan length of survey legs = %7.2fm*/133),
477             totplan);
478      putnl();
479      printf(msg(/*Total vertical length of survey legs = %7.2fm*/134),
480             totvert);
481      putnl();
482   }
483   if (fh) {
484      fprintf(fh, msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132),
485              total, totadj);
486      fputnl(fh);
487      fprintf(fh, msg(/*Total plan length of survey legs = %7.2fm*/133),
488              totplan);
489      fputnl(fh);
490      fprintf(fh, msg(/*Total vertical length of survey legs = %7.2fm*/134),
491              totvert);
492      fputnl(fh);
493   }
[bb90203]494
495   do_range(fh, 2, /*Vertical range = %4.2fm (from */135,
496            /* at %4.2fm to */136, /* at %4.2fm)*/137);
497   do_range(fh, 1, /*North-South range = %4.2fm (from */148,
498            /* at %4.2fm to */196, /* at %4.2fm)*/197);
499   do_range(fh, 0, /*East-West range = %4.2fm (from */149,
500            /* at %4.2fm to */196, /* at %4.2fm)*/197);
501
502   print_node_stats(fh);
503   /* Also, could give:
504    *  # nodes stations (ie have other than two references or are fixed)
505    *  # fixed stations (list of?)
506    */
507
[f1067a2]508   if (fh) safe_fclose(fh);
[bb90203]509}
Note: See TracBrowser for help on using the repository browser.