[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 |
---|
[407084d] | 44 | #include <unistd.h> /* for getcwd() */ |
---|
| 45 | |
---|
[647407d] | 46 | #include "new3dout.h" |
---|
| 47 | #ifndef MAXPATHLEN |
---|
| 48 | #define MAXPATHLEN 1024 |
---|
| 49 | #endif |
---|
| 50 | #endif |
---|
| 51 | |
---|
[bb90203] | 52 | /* For funcs which want to be immune from messing around with different |
---|
| 53 | * calling conventions */ |
---|
| 54 | #ifndef CDECL |
---|
| 55 | # define CDECL |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | /* Globals */ |
---|
| 59 | node *stnlist = NULL; |
---|
| 60 | settings *pcs; |
---|
| 61 | prefix *root; |
---|
| 62 | long cLegs, cStns; |
---|
| 63 | long cComponents; |
---|
[d1878c51] | 64 | bool fExportUsed = fFalse; |
---|
[bb90203] | 65 | |
---|
| 66 | FILE *fhErrStat = NULL; |
---|
| 67 | img *pimgOut = NULL; |
---|
| 68 | #ifndef NO_PERCENTAGE |
---|
| 69 | bool fPercent = fFalse; |
---|
| 70 | #endif |
---|
| 71 | bool fAscii = fFalse; |
---|
[647407d] | 72 | bool fQuiet = fFalse; /* just show brief summary + errors */ |
---|
[25ab06b] | 73 | static bool fMute = fFalse; /* just show errors */ |
---|
[647407d] | 74 | bool fSuppress = fFalse; /* only output 3d(3dx) file */ |
---|
| 75 | |
---|
| 76 | nosurveylink *nosurveyhead; |
---|
[bb90203] | 77 | |
---|
| 78 | real totadj, total, totplan, totvert; |
---|
| 79 | real min[3], max[3]; |
---|
| 80 | prefix *pfxHi[3], *pfxLo[3]; |
---|
| 81 | |
---|
| 82 | char *survey_title = NULL; |
---|
| 83 | int survey_title_len; |
---|
| 84 | |
---|
| 85 | bool fExplicitTitle = fFalse; |
---|
| 86 | |
---|
[8e8057c] | 87 | char *fnm_output_base = NULL; |
---|
| 88 | int fnm_output_base_is_dir = 0; |
---|
| 89 | |
---|
[bb90203] | 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, (int*)&fPercent, 0}, |
---|
| 100 | #endif |
---|
[8e8057c] | 101 | {"output", required_argument, 0, 'o'}, |
---|
[647407d] | 102 | {"quiet", no_argument, 0, 'q'}, |
---|
| 103 | {"no-auxiliary-files", no_argument, 0, 's'}, |
---|
| 104 | #ifdef NEW3DFORMAT |
---|
| 105 | {"new-format", no_argument, 0, 'x'}, |
---|
| 106 | #endif |
---|
[bb90203] | 107 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 108 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 109 | {0, 0, 0, 0} |
---|
| 110 | }; |
---|
| 111 | |
---|
[647407d] | 112 | #ifdef NEW3DFORMAT |
---|
| 113 | #define short_opts "pxao:qsz:" |
---|
| 114 | #else |
---|
| 115 | #define short_opts "pao:qsz:" |
---|
| 116 | #endif |
---|
[bb90203] | 117 | |
---|
[647407d] | 118 | /* TRANSLATE extract help messages to message file */ |
---|
[bb90203] | 119 | static struct help_msg help[] = { |
---|
| 120 | /* <-- */ |
---|
| 121 | {HLP_ENCODELONG(0), "display percentage progress"}, |
---|
| 122 | {'a', "output ascii variant of .3d file"}, |
---|
[8e8057c] | 123 | {HLP_ENCODELONG(2), "set location for output files"}, |
---|
[647407d] | 124 | {HLP_ENCODELONG(3), "only show brief summary (-qq for errors only)"}, |
---|
| 125 | {HLP_ENCODELONG(4), "do not create .pos, .inf, or .err files"}, |
---|
| 126 | #ifdef NEW3DFORMAT |
---|
| 127 | {HLP_ENCODELONG(5), "output data in 3dx format"}, |
---|
| 128 | #endif |
---|
[8e8057c] | 129 | /*{'z', "set optimizations for network reduction"},*/ |
---|
[bb90203] | 130 | {0, 0} |
---|
| 131 | }; |
---|
| 132 | |
---|
[25ab06b] | 133 | /* atexit function */ |
---|
| 134 | static void |
---|
| 135 | delete_output_on_error(void) |
---|
| 136 | { |
---|
| 137 | if (msg_errors) filename_delete_output(); |
---|
| 138 | } |
---|
| 139 | |
---|
[bb90203] | 140 | extern CDECL int |
---|
| 141 | main(int argc, char **argv) |
---|
| 142 | { |
---|
| 143 | int d; |
---|
| 144 | static clock_t tmCPUStart; |
---|
| 145 | static time_t tmUserStart; |
---|
| 146 | static double tmCPU, tmUser; |
---|
| 147 | |
---|
| 148 | tmUserStart = time(NULL); |
---|
| 149 | tmCPUStart = clock(); |
---|
| 150 | init_screen(); |
---|
| 151 | |
---|
[b85e20f] | 152 | msg_init(argv[0]); |
---|
[bb90203] | 153 | |
---|
| 154 | pcs = osnew(settings); |
---|
| 155 | pcs->next = NULL; |
---|
| 156 | pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1; |
---|
| 157 | |
---|
| 158 | /* Set up root of prefix hierarchy */ |
---|
| 159 | root = osnew(prefix); |
---|
| 160 | root->up = root->right = root->down = NULL; |
---|
| 161 | root->stn = NULL; |
---|
| 162 | root->pos = NULL; |
---|
| 163 | root->ident = "\\"; |
---|
[932f7e9] | 164 | root->min_export = root->max_export = 0; |
---|
[647407d] | 165 | root->fSuspectTypo = fFalse; |
---|
| 166 | |
---|
| 167 | nosurveyhead = NULL; |
---|
[bb90203] | 168 | |
---|
| 169 | stnlist = NULL; |
---|
| 170 | cLegs = cStns = cComponents = 0; |
---|
| 171 | totadj = total = totplan = totvert = 0.0; |
---|
| 172 | |
---|
| 173 | for (d = 0; d <= 2; d++) { |
---|
| 174 | min[d] = REAL_BIG; |
---|
| 175 | max[d] = -REAL_BIG; |
---|
| 176 | pfxHi[d] = pfxLo[d] = NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
[b85e20f] | 179 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1); |
---|
[bb90203] | 180 | while (1) { |
---|
[b85e20f] | 181 | /* at least one argument must be given */ |
---|
| 182 | int opt = cmdline_getopt(); |
---|
[bb90203] | 183 | if (opt == EOF) break; |
---|
| 184 | switch (opt) { |
---|
| 185 | case 'a': |
---|
| 186 | fAscii = fTrue; |
---|
| 187 | break; |
---|
| 188 | case 'p': |
---|
| 189 | #ifndef NO_PERCENTAGE |
---|
| 190 | fPercent = 1; |
---|
| 191 | #endif |
---|
| 192 | break; |
---|
[8e8057c] | 193 | case 'o': { |
---|
| 194 | /* can be a directory (in which case use basename of leaf input) |
---|
| 195 | * or a file (in which case just trim the extension off) */ |
---|
| 196 | if (fDirectory(optarg)) { |
---|
| 197 | /* this is a little tricky - we need to note the path here, |
---|
| 198 | * and then add the leaf later on (in datain.c) */ |
---|
| 199 | fnm_output_base = base_from_fnm(optarg); |
---|
| 200 | fnm_output_base_is_dir = 1; |
---|
| 201 | } else { |
---|
| 202 | osfree(fnm_output_base); /* in case of multiple -o options */ |
---|
| 203 | fnm_output_base = base_from_fnm(optarg); |
---|
| 204 | } |
---|
| 205 | break; |
---|
| 206 | } |
---|
[647407d] | 207 | #ifdef NEW3DFORMAT |
---|
| 208 | case 'x': { |
---|
| 209 | fUseNewFormat = 1; |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | #endif |
---|
| 213 | case 'q': |
---|
| 214 | if (fQuiet) fMute = 1; |
---|
| 215 | fQuiet = 1; |
---|
| 216 | break; |
---|
| 217 | case 's': |
---|
| 218 | fSuppress = 1; |
---|
| 219 | break; |
---|
[8e8057c] | 220 | case 'z': { |
---|
| 221 | /* Control which network optimisations are used (development tool) */ |
---|
| 222 | static int first_opt_z = 1; |
---|
[eb18f4d] | 223 | char c; |
---|
[8e8057c] | 224 | if (first_opt_z) { |
---|
[bb90203] | 225 | optimize = 0; |
---|
[8e8057c] | 226 | first_opt_z = 0; |
---|
[bb90203] | 227 | } |
---|
[c50391b8] | 228 | /* Lollipops, Parallel legs, Iterate mx, Delta* */ |
---|
[eb18f4d] | 229 | while ((c = *optarg++) != '\0') |
---|
| 230 | if (islower(c)) optimize |= BITA(ch); |
---|
[bb90203] | 231 | break; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | out_puts(PACKAGE" "VERSION); |
---|
| 237 | out_puts(COPYRIGHT_MSG); |
---|
| 238 | putnl(); |
---|
| 239 | |
---|
[25ab06b] | 240 | atexit(delete_output_on_error); |
---|
| 241 | |
---|
[bb90203] | 242 | /* end of options, now process data files */ |
---|
| 243 | while (argv[optind]) { |
---|
| 244 | const char *fnm = argv[optind]; |
---|
| 245 | |
---|
| 246 | if (!fExplicitTitle) { |
---|
| 247 | char *lf; |
---|
[8e8057c] | 248 | lf = baseleaf_from_fnm(fnm); |
---|
[bb90203] | 249 | if (survey_title) s_catchar(&survey_title, &survey_title_len, ' '); |
---|
| 250 | s_cat(&survey_title, &survey_title_len, lf); |
---|
| 251 | osfree(lf); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /* Select defaults settings */ |
---|
| 255 | default_all(pcs); |
---|
[647407d] | 256 | #ifdef NEW3DFORMAT |
---|
| 257 | /* we need to get the filename of the first one for our base_source */ |
---|
| 258 | /* and also run_file */ |
---|
| 259 | if (fUseNewFormat) { |
---|
| 260 | create_twig(root, fnm); |
---|
| 261 | rhizome = root->twig_link; |
---|
| 262 | limb = get_twig(root); |
---|
| 263 | firstfilename = osstrdup(fnm); |
---|
| 264 | startingdir = osmalloc(MAXPATHLEN); |
---|
[49090c02] | 265 | #if (OS==RISCOS) |
---|
| 266 | strcpy(startingdir, "@"); |
---|
| 267 | #else |
---|
[647407d] | 268 | getcwd(startingdir, MAXPATHLEN); |
---|
[49090c02] | 269 | #endif |
---|
[647407d] | 270 | } |
---|
| 271 | #endif |
---|
[bb90203] | 272 | data_file("", fnm); /* first argument is current path */ |
---|
[cb3d1e2] | 273 | |
---|
[bb90203] | 274 | optind++; |
---|
| 275 | } |
---|
[cb3d1e2] | 276 | |
---|
[bb90203] | 277 | validate(); |
---|
| 278 | |
---|
| 279 | solve_network(/*stnlist*/); /* Find coordinates of all points */ |
---|
| 280 | validate(); |
---|
[647407d] | 281 | |
---|
[bb90203] | 282 | #ifdef NEW3DFORMAT |
---|
[647407d] | 283 | if (fUseNewFormat) { |
---|
| 284 | cave_close(pimgOut); /* this actually does all the writing */ |
---|
| 285 | } else { |
---|
[bb90203] | 286 | #endif |
---|
[647407d] | 287 | img_close(pimgOut); /* close .3d file */ |
---|
| 288 | #ifdef NEW3DFORMAT |
---|
| 289 | } |
---|
| 290 | #endif |
---|
| 291 | if (fhErrStat) fclose(fhErrStat); |
---|
[bb90203] | 292 | |
---|
[25ab06b] | 293 | if (!fSuppress && !msg_errors) list_pos(root); /* produce .pos file */ |
---|
[bb90203] | 294 | |
---|
| 295 | out_current_action(msg(/*Calculating statistics*/120)); |
---|
| 296 | do_stats(); |
---|
[647407d] | 297 | if (!fQuiet) { |
---|
| 298 | tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC; |
---|
| 299 | tmUser = difftime(time(NULL), tmUserStart); |
---|
| 300 | |
---|
| 301 | /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */ |
---|
| 302 | if (tmCPU + 1 > tmUser) { |
---|
| 303 | out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU)); |
---|
| 304 | } else if (tmCPU == 0) { |
---|
| 305 | if (tmUser == 0.0) { |
---|
| 306 | out_printf((msg(/*Time used %5.2fs*/141), tmUser)); |
---|
| 307 | } else { |
---|
| 308 | out_puts(msg(/*Time used unavailable*/142)); |
---|
| 309 | } |
---|
[bb90203] | 310 | } else { |
---|
[647407d] | 311 | out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), |
---|
| 312 | tmUser, tmCPU)); |
---|
[bb90203] | 313 | } |
---|
| 314 | |
---|
[647407d] | 315 | out_puts(msg(/*Done.*/144)); |
---|
| 316 | } |
---|
[25ab06b] | 317 | if (msg_warnings || msg_errors) { |
---|
| 318 | printf(msg(/*There were %d warning(s) and %d non-fatal error(s).*/16), |
---|
| 319 | msg_warnings, msg_errors); |
---|
| 320 | putnl(); |
---|
| 321 | /* FIXME: if (msg_errors) print "output not produced..."? */ |
---|
| 322 | } |
---|
| 323 | return (msg_errors ? EXIT_FAILURE : EXIT_SUCCESS); |
---|
[bb90203] | 324 | } |
---|
| 325 | |
---|
| 326 | static void |
---|
| 327 | do_range(FILE *fh, int d, int msg1, int msg2, int msg3) |
---|
| 328 | { |
---|
[647407d] | 329 | char buf[1024]; |
---|
[bb90203] | 330 | sprintf(buf, msg(msg1), max[d] - min[d]); |
---|
| 331 | strcat(buf, sprint_prefix(pfxHi[d])); |
---|
| 332 | sprintf(buf + strlen(buf), msg(msg2), max[d]); |
---|
| 333 | strcat(buf, sprint_prefix(pfxLo[d])); |
---|
| 334 | sprintf(buf + strlen(buf), msg(msg3), min[d]); |
---|
[647407d] | 335 | if (!fMute) out_puts(buf); |
---|
[407084d] | 336 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 337 | } |
---|
| 338 | |
---|
| 339 | static void |
---|
| 340 | do_stats(void) |
---|
| 341 | { |
---|
[407084d] | 342 | FILE *fh = NULL; |
---|
[bb90203] | 343 | long cLoops = cComponents + cLegs - cStns; |
---|
| 344 | char buf[1024]; |
---|
| 345 | |
---|
[25ab06b] | 346 | if (!fSuppress && !msg_errors) |
---|
[647407d] | 347 | fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w"); |
---|
[bb90203] | 348 | |
---|
| 349 | out_puts(""); |
---|
| 350 | |
---|
| 351 | if (cStns == 1) |
---|
| 352 | sprintf(buf, msg(/*Survey contains 1 survey station,*/172)); |
---|
| 353 | else |
---|
| 354 | sprintf(buf, |
---|
| 355 | msg(/*Survey contains %ld survey stations,*/173), cStns); |
---|
| 356 | |
---|
| 357 | if (cLegs == 1) |
---|
| 358 | sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174)); |
---|
| 359 | else |
---|
| 360 | sprintf(buf + strlen(buf), |
---|
| 361 | msg(/* joined by %ld legs.*/175), cLegs); |
---|
| 362 | |
---|
[647407d] | 363 | if (!fMute) out_puts(buf); |
---|
[407084d] | 364 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 365 | |
---|
| 366 | if (cLoops == 1) |
---|
| 367 | sprintf(buf, msg(/*There is 1 loop.*/138)); |
---|
| 368 | else |
---|
| 369 | sprintf(buf, msg(/*There are %ld loops.*/139), cLoops); |
---|
| 370 | |
---|
[647407d] | 371 | if (!fMute) out_puts(buf); |
---|
[407084d] | 372 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 373 | |
---|
| 374 | if (cComponents != 1) { |
---|
| 375 | sprintf(buf, |
---|
| 376 | msg(/*Survey has %ld connected components.*/178), cComponents); |
---|
[647407d] | 377 | if (!fMute) out_puts(buf); |
---|
[407084d] | 378 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 379 | } |
---|
| 380 | |
---|
| 381 | sprintf(buf, |
---|
| 382 | msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132), |
---|
| 383 | total, totadj); |
---|
[647407d] | 384 | if (!fMute) out_puts(buf); |
---|
[407084d] | 385 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 386 | |
---|
| 387 | sprintf(buf, |
---|
| 388 | msg(/*Total plan length of survey legs = %7.2fm*/133), totplan); |
---|
[647407d] | 389 | if (!fMute) out_puts(buf); |
---|
[407084d] | 390 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 391 | |
---|
| 392 | sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134), |
---|
| 393 | totvert); |
---|
[647407d] | 394 | if (!fMute) out_puts(buf); |
---|
[407084d] | 395 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 396 | |
---|
| 397 | do_range(fh, 2, /*Vertical range = %4.2fm (from */135, |
---|
| 398 | /* at %4.2fm to */136, /* at %4.2fm)*/137); |
---|
| 399 | do_range(fh, 1, /*North-South range = %4.2fm (from */148, |
---|
| 400 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 401 | do_range(fh, 0, /*East-West range = %4.2fm (from */149, |
---|
| 402 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 403 | |
---|
| 404 | print_node_stats(fh); |
---|
| 405 | /* Also, could give: |
---|
| 406 | * # nodes stations (ie have other than two references or are fixed) |
---|
| 407 | * # fixed stations (list of?) |
---|
| 408 | */ |
---|
| 409 | |
---|
[407084d] | 410 | if (fh) fclose(fh); |
---|
[bb90203] | 411 | } |
---|