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