| 1 | /* dump3d.c */ |
|---|
| 2 | /* Show raw contents of .3d file in text form */ |
|---|
| 3 | /* Copyright (C) 2001-2022 Olly Betts |
|---|
| 4 | * |
|---|
| 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. |
|---|
| 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 |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifdef HAVE_CONFIG_H |
|---|
| 21 | # include <config.h> |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <math.h> |
|---|
| 28 | |
|---|
| 29 | #include "cmdline.h" |
|---|
| 30 | #include "date.h" |
|---|
| 31 | #include "debug.h" |
|---|
| 32 | #include "filelist.h" |
|---|
| 33 | #include "img_hosted.h" |
|---|
| 34 | |
|---|
| 35 | static const struct option long_opts[] = { |
|---|
| 36 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
|---|
| 37 | {"survey", required_argument, 0, 's'}, |
|---|
| 38 | {"rewind", no_argument, 0, 'r'}, |
|---|
| 39 | {"show-dates", no_argument, 0, 'd'}, |
|---|
| 40 | {"legs", no_argument, 0, 'l'}, |
|---|
| 41 | {"help", no_argument, 0, HLP_HELP}, |
|---|
| 42 | {"version", no_argument, 0, HLP_VERSION}, |
|---|
| 43 | {0, 0, 0, 0} |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | #define short_opts "rdls:" |
|---|
| 47 | |
|---|
| 48 | static struct help_msg help[] = { |
|---|
| 49 | /* <-- */ |
|---|
| 50 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
|---|
| 51 | /* TRANSLATORS: --help output for dump3d --rewind option */ |
|---|
| 52 | {HLP_ENCODELONG(1), /*rewind file and read it a second time*/204, 0}, |
|---|
| 53 | {HLP_ENCODELONG(2), /*show survey date information (if present)*/396, 0}, |
|---|
| 54 | {HLP_ENCODELONG(3), /*convert MOVE and LINE into LEG*/486, 0}, |
|---|
| 55 | {0, 0, 0} |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | int |
|---|
| 59 | main(int argc, char **argv) |
|---|
| 60 | { |
|---|
| 61 | char *fnm; |
|---|
| 62 | img *pimg; |
|---|
| 63 | img_point pt; |
|---|
| 64 | img_point from = { 0.0, 0.0, 0.0 }; |
|---|
| 65 | int code; |
|---|
| 66 | const char *survey = NULL; |
|---|
| 67 | bool fRewind = false; |
|---|
| 68 | bool show_dates = false; |
|---|
| 69 | bool make_legs = false; |
|---|
| 70 | |
|---|
| 71 | msg_init(argv); |
|---|
| 72 | |
|---|
| 73 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 1); |
|---|
| 74 | while (1) { |
|---|
| 75 | int opt = cmdline_getopt(); |
|---|
| 76 | if (opt == EOF) break; |
|---|
| 77 | if (opt == 's') survey = optarg; |
|---|
| 78 | if (opt == 'r') fRewind = true; |
|---|
| 79 | if (opt == 'd') show_dates = true; |
|---|
| 80 | if (opt == 'l') make_legs = true; |
|---|
| 81 | } |
|---|
| 82 | fnm = argv[optind]; |
|---|
| 83 | |
|---|
| 84 | pimg = img_open_survey(fnm, survey); |
|---|
| 85 | if (!pimg) fatalerror(img_error2msg(img_error()), fnm); |
|---|
| 86 | |
|---|
| 87 | printf("TITLE \"%s\"\n", pimg->title); |
|---|
| 88 | printf("DATE \"%s\"\n", pimg->datestamp); |
|---|
| 89 | printf("DATE_NUMERIC %ld\n", pimg->datestamp_numeric); |
|---|
| 90 | if (pimg->cs) |
|---|
| 91 | printf("CS %s\n", pimg->cs); |
|---|
| 92 | printf("VERSION %d\n", pimg->version); |
|---|
| 93 | if (pimg->is_extended_elevation) |
|---|
| 94 | printf("EXTENDED ELEVATION\n"); |
|---|
| 95 | printf("SEPARATOR '%c'\n", pimg->separator); |
|---|
| 96 | printf("--\n"); |
|---|
| 97 | |
|---|
| 98 | code = img_BAD; |
|---|
| 99 | do { |
|---|
| 100 | if (code == img_STOP) { |
|---|
| 101 | printf("<<< REWIND <<<\n"); |
|---|
| 102 | fRewind = false; |
|---|
| 103 | if (!img_rewind(pimg)) fatalerror(img_error2msg(img_error()), fnm); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | do { |
|---|
| 107 | code = img_read_item(pimg, &pt); |
|---|
| 108 | switch (code) { |
|---|
| 109 | case img_MOVE: |
|---|
| 110 | if (make_legs) { |
|---|
| 111 | from = pt; |
|---|
| 112 | } else { |
|---|
| 113 | printf("MOVE %.2f %.2f %.2f\n", pt.x, pt.y, pt.z); |
|---|
| 114 | } |
|---|
| 115 | break; |
|---|
| 116 | case img_LINE: |
|---|
| 117 | if (make_legs) { |
|---|
| 118 | printf("LEG %.2f %.2f %.2f %.2f %.2f %.2f [%s]", |
|---|
| 119 | from.x, from.y, from.z, |
|---|
| 120 | pt.x, pt.y, pt.z, pimg->label); |
|---|
| 121 | from = pt; |
|---|
| 122 | } else { |
|---|
| 123 | printf("LINE %.2f %.2f %.2f [%s]", |
|---|
| 124 | pt.x, pt.y, pt.z, pimg->label); |
|---|
| 125 | } |
|---|
| 126 | switch (pimg->style) { |
|---|
| 127 | case img_STYLE_UNKNOWN: |
|---|
| 128 | break; |
|---|
| 129 | case img_STYLE_NORMAL: |
|---|
| 130 | printf(" STYLE=NORMAL"); |
|---|
| 131 | break; |
|---|
| 132 | case img_STYLE_DIVING: |
|---|
| 133 | printf(" STYLE=DIVING"); |
|---|
| 134 | break; |
|---|
| 135 | case img_STYLE_CARTESIAN: |
|---|
| 136 | printf(" STYLE=CARTESIAN"); |
|---|
| 137 | break; |
|---|
| 138 | case img_STYLE_CYLPOLAR: |
|---|
| 139 | printf(" STYLE=CYLPOLAR"); |
|---|
| 140 | break; |
|---|
| 141 | case img_STYLE_NOSURVEY: |
|---|
| 142 | printf(" STYLE=NOSURVEY"); |
|---|
| 143 | break; |
|---|
| 144 | } |
|---|
| 145 | if (pimg->flags & img_FLAG_SURFACE) printf(" SURFACE"); |
|---|
| 146 | if (pimg->flags & img_FLAG_DUPLICATE) printf(" DUPLICATE"); |
|---|
| 147 | if (pimg->flags & img_FLAG_SPLAY) printf(" SPLAY"); |
|---|
| 148 | if (show_dates && pimg->days1 != -1) { |
|---|
| 149 | int y, m, d; |
|---|
| 150 | ymd_from_days_since_1900(pimg->days1, &y, &m, &d); |
|---|
| 151 | printf(" %04d.%02d.%02d", y, m, d); |
|---|
| 152 | if (pimg->days1 != pimg->days2) { |
|---|
| 153 | ymd_from_days_since_1900(pimg->days2, &y, &m, &d); |
|---|
| 154 | printf("-%04d.%02d.%02d", y, m, d); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | printf("\n"); |
|---|
| 158 | break; |
|---|
| 159 | case img_LABEL: |
|---|
| 160 | printf("NODE %.2f %.2f %.2f [%s]", pt.x, pt.y, pt.z, pimg->label); |
|---|
| 161 | if (pimg->flags & img_SFLAG_SURFACE) printf(" SURFACE"); |
|---|
| 162 | if (pimg->flags & img_SFLAG_UNDERGROUND) printf(" UNDERGROUND"); |
|---|
| 163 | if (pimg->flags & img_SFLAG_ENTRANCE) printf(" ENTRANCE"); |
|---|
| 164 | if (pimg->flags & img_SFLAG_EXPORTED) printf(" EXPORTED"); |
|---|
| 165 | if (pimg->flags & img_SFLAG_FIXED) printf(" FIXED"); |
|---|
| 166 | if (pimg->flags & img_SFLAG_ANON) printf(" ANON"); |
|---|
| 167 | if (pimg->flags & img_SFLAG_WALL) printf(" WALL"); |
|---|
| 168 | printf("\n"); |
|---|
| 169 | break; |
|---|
| 170 | case img_XSECT: |
|---|
| 171 | printf("XSECT %.2f %.2f %.2f %.2f [%s]", |
|---|
| 172 | pimg->l, pimg->r, pimg->u, pimg->d, pimg->label); |
|---|
| 173 | if (show_dates && pimg->days1 != -1) { |
|---|
| 174 | int y, m, d; |
|---|
| 175 | ymd_from_days_since_1900(pimg->days1, &y, &m, &d); |
|---|
| 176 | printf(" %04d.%02d.%02d", y, m, d); |
|---|
| 177 | if (pimg->days1 != pimg->days2) { |
|---|
| 178 | ymd_from_days_since_1900(pimg->days2, &y, &m, &d); |
|---|
| 179 | printf("-%04d.%02d.%02d", y, m, d); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | printf("\n"); |
|---|
| 183 | break; |
|---|
| 184 | case img_XSECT_END: |
|---|
| 185 | printf("XSECT_END\n"); |
|---|
| 186 | break; |
|---|
| 187 | case img_ERROR_INFO: |
|---|
| 188 | printf("ERROR_INFO #legs %d, len %.2fm, E %.2f H %.2f V %.2f\n", |
|---|
| 189 | pimg->n_legs, pimg->length, pimg->E, pimg->H, pimg->V); |
|---|
| 190 | break; |
|---|
| 191 | case img_BAD: |
|---|
| 192 | img_close(pimg); |
|---|
| 193 | fatalerror(img_error2msg(img_error()), fnm); |
|---|
| 194 | /* fatalerror() won't return, but the compiler can't tell that and |
|---|
| 195 | * may warn about dropping through into the next case without a |
|---|
| 196 | * "break;" here. |
|---|
| 197 | */ |
|---|
| 198 | break; |
|---|
| 199 | case img_STOP: |
|---|
| 200 | printf("STOP\n"); |
|---|
| 201 | break; |
|---|
| 202 | default: |
|---|
| 203 | printf("CODE_0x%02x\n", code); |
|---|
| 204 | } |
|---|
| 205 | } while (code != img_STOP); |
|---|
| 206 | } while (fRewind); |
|---|
| 207 | |
|---|
| 208 | img_close(pimg); |
|---|
| 209 | |
|---|
| 210 | return 0; |
|---|
| 211 | } |
|---|