source: git/src/cavern.c @ 4588a499

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 4588a499 was f03053a7, checked in by Olly Betts <olly@…>, 24 years ago

Merged in old wishlist I found.

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

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