source: git/src/cavern.c @ 0a33e5c

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

Fixed borlandc compile warnings.

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

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