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