source: git/src/cavern.c @ 3c82630

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 3c82630 was 8318645, checked in by Olly Betts <olly@…>, 24 years ago

Fix djgpp warning.

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