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