source: git/src/cavern.c@ 54404774

RELEASE/1.0
Last change on this file since 54404774 was 54404774, checked in by Olly Betts <olly@…>, 15 years ago

lib/messages.txt,src/cavern.c: Fix up backport for new cavern -v
option by backporting the message it needs too.

git-svn-id: file:///home/survex-svn/survex/branches/1.0@3694 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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