1 | /* dump3d.c */ |
---|
2 | /* Show raw contents of .3d file in text form */ |
---|
3 | /* Copyright (C) 2001,2002,2006,2011,2012,2013,2014,2015,2018 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 | {"help", no_argument, 0, HLP_HELP}, |
---|
41 | {"version", no_argument, 0, HLP_VERSION}, |
---|
42 | {0, 0, 0, 0} |
---|
43 | }; |
---|
44 | |
---|
45 | #define short_opts "rds:" |
---|
46 | |
---|
47 | static struct help_msg help[] = { |
---|
48 | /* <-- */ |
---|
49 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
---|
50 | /* TRANSLATORS: --help output for dump3d --rewind option */ |
---|
51 | {HLP_ENCODELONG(1), /*rewind file and read it a second time*/204, 0}, |
---|
52 | {HLP_ENCODELONG(2), /*show survey date information (if present)*/396, 0}, |
---|
53 | {0, 0, 0} |
---|
54 | }; |
---|
55 | |
---|
56 | int |
---|
57 | main(int argc, char **argv) |
---|
58 | { |
---|
59 | char *fnm; |
---|
60 | img *pimg; |
---|
61 | img_point pt; |
---|
62 | int code; |
---|
63 | const char *survey = NULL; |
---|
64 | bool fRewind = fFalse; |
---|
65 | bool show_dates = fFalse; |
---|
66 | |
---|
67 | msg_init(argv); |
---|
68 | |
---|
69 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 1); |
---|
70 | while (1) { |
---|
71 | int opt = cmdline_getopt(); |
---|
72 | if (opt == EOF) break; |
---|
73 | if (opt == 's') survey = optarg; |
---|
74 | if (opt == 'r') fRewind = fTrue; |
---|
75 | if (opt == 'd') show_dates = fTrue; |
---|
76 | } |
---|
77 | fnm = argv[optind]; |
---|
78 | |
---|
79 | pimg = img_open_survey(fnm, survey); |
---|
80 | if (!pimg) fatalerror(img_error2msg(img_error()), fnm); |
---|
81 | |
---|
82 | printf("TITLE \"%s\"\n", pimg->title); |
---|
83 | printf("DATE \"%s\"\n", pimg->datestamp); |
---|
84 | printf("DATE_NUMERIC %ld\n", pimg->datestamp_numeric); |
---|
85 | if (pimg->cs) |
---|
86 | printf("CS %s\n", pimg->cs); |
---|
87 | printf("VERSION %d\n", pimg->version); |
---|
88 | if (pimg->is_extended_elevation) |
---|
89 | printf("EXTENDED ELEVATION\n"); |
---|
90 | printf("SEPARATOR '%c'\n", pimg->separator); |
---|
91 | printf("--\n"); |
---|
92 | |
---|
93 | code = img_BAD; |
---|
94 | do { |
---|
95 | if (code == img_STOP) { |
---|
96 | printf("<<< REWIND <<<\n"); |
---|
97 | fRewind = fFalse; |
---|
98 | if (!img_rewind(pimg)) fatalerror(img_error2msg(img_error()), fnm); |
---|
99 | } |
---|
100 | |
---|
101 | do { |
---|
102 | code = img_read_item(pimg, &pt); |
---|
103 | switch (code) { |
---|
104 | case img_MOVE: |
---|
105 | printf("MOVE %.2f %.2f %.2f\n", pt.x, pt.y, pt.z); |
---|
106 | break; |
---|
107 | case img_LINE: |
---|
108 | printf("LINE %.2f %.2f %.2f [%s]", pt.x, pt.y, pt.z, pimg->label); |
---|
109 | switch (pimg->style) { |
---|
110 | case img_STYLE_UNKNOWN: |
---|
111 | break; |
---|
112 | case img_STYLE_NORMAL: |
---|
113 | printf(" STYLE=NORMAL"); |
---|
114 | break; |
---|
115 | case img_STYLE_DIVING: |
---|
116 | printf(" STYLE=DIVING"); |
---|
117 | break; |
---|
118 | case img_STYLE_CARTESIAN: |
---|
119 | printf(" STYLE=CARTESIAN"); |
---|
120 | break; |
---|
121 | case img_STYLE_CYLPOLAR: |
---|
122 | printf(" STYLE=CYLPOLAR"); |
---|
123 | break; |
---|
124 | case img_STYLE_NOSURVEY: |
---|
125 | printf(" STYLE=NOSURVEY"); |
---|
126 | break; |
---|
127 | } |
---|
128 | if (pimg->flags & img_FLAG_SURFACE) printf(" SURFACE"); |
---|
129 | if (pimg->flags & img_FLAG_DUPLICATE) printf(" DUPLICATE"); |
---|
130 | if (pimg->flags & img_FLAG_SPLAY) printf(" SPLAY"); |
---|
131 | if (show_dates && pimg->days1 != -1) { |
---|
132 | int y, m, d; |
---|
133 | ymd_from_days_since_1900(pimg->days1, &y, &m, &d); |
---|
134 | printf(" %04d.%02d.%02d", y, m, d); |
---|
135 | if (pimg->days1 != pimg->days2) { |
---|
136 | ymd_from_days_since_1900(pimg->days2, &y, &m, &d); |
---|
137 | printf("-%04d.%02d.%02d", y, m, d); |
---|
138 | } |
---|
139 | } |
---|
140 | printf("\n"); |
---|
141 | break; |
---|
142 | case img_LABEL: |
---|
143 | printf("NODE %.2f %.2f %.2f [%s]", pt.x, pt.y, pt.z, pimg->label); |
---|
144 | if (pimg->flags & img_SFLAG_SURFACE) printf(" SURFACE"); |
---|
145 | if (pimg->flags & img_SFLAG_UNDERGROUND) printf(" UNDERGROUND"); |
---|
146 | if (pimg->flags & img_SFLAG_ENTRANCE) printf(" ENTRANCE"); |
---|
147 | if (pimg->flags & img_SFLAG_EXPORTED) printf(" EXPORTED"); |
---|
148 | if (pimg->flags & img_SFLAG_FIXED) printf(" FIXED"); |
---|
149 | if (pimg->flags & img_SFLAG_ANON) printf(" ANON"); |
---|
150 | if (pimg->flags & img_SFLAG_WALL) printf(" WALL"); |
---|
151 | printf("\n"); |
---|
152 | break; |
---|
153 | case img_XSECT: |
---|
154 | printf("XSECT %.2f %.2f %.2f %.2f [%s]", |
---|
155 | pimg->l, pimg->r, pimg->u, pimg->d, pimg->label); |
---|
156 | if (show_dates && pimg->days1 != -1) { |
---|
157 | int y, m, d; |
---|
158 | ymd_from_days_since_1900(pimg->days1, &y, &m, &d); |
---|
159 | printf(" %04d.%02d.%02d", y, m, d); |
---|
160 | if (pimg->days1 != pimg->days2) { |
---|
161 | ymd_from_days_since_1900(pimg->days2, &y, &m, &d); |
---|
162 | printf("-%04d.%02d.%02d", y, m, d); |
---|
163 | } |
---|
164 | } |
---|
165 | printf("\n"); |
---|
166 | break; |
---|
167 | case img_XSECT_END: |
---|
168 | printf("XSECT_END\n"); |
---|
169 | break; |
---|
170 | case img_ERROR_INFO: |
---|
171 | printf("ERROR_INFO #legs %d, len %.2fm, E %.2f H %.2f V %.2f\n", |
---|
172 | pimg->n_legs, pimg->length, pimg->E, pimg->H, pimg->V); |
---|
173 | break; |
---|
174 | case img_BAD: |
---|
175 | img_close(pimg); |
---|
176 | fatalerror(img_error2msg(img_error()), fnm); |
---|
177 | /* fatalerror() won't return, but the compiler can't tell that and |
---|
178 | * may warn about dropping through into the next case without a |
---|
179 | * "break;" here. |
---|
180 | */ |
---|
181 | break; |
---|
182 | case img_STOP: |
---|
183 | printf("STOP\n"); |
---|
184 | break; |
---|
185 | default: |
---|
186 | printf("CODE_0x%02x\n", code); |
---|
187 | } |
---|
188 | } while (code != img_STOP); |
---|
189 | } while (fRewind); |
---|
190 | |
---|
191 | img_close(pimg); |
---|
192 | |
---|
193 | return 0; |
---|
194 | } |
---|