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