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