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