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