[0da6e60] | 1 | /* img.c |
---|
[d1b1380] | 2 | * Routines for reading and writing Survex ".3d" image files |
---|
[1908f42] | 3 | * Copyright (C) 1993-2004,2005,2006,2010,2011,2013,2014,2017 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 |
---|
[0e75725] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
[4f70ebc] | 24 | #include <ctype.h> |
---|
| 25 | #include <errno.h> |
---|
| 26 | #include <limits.h> |
---|
| 27 | #include <locale.h> |
---|
[d1b1380] | 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
[4f70ebc] | 30 | #include <string.h> |
---|
[d1b1380] | 31 | #include <time.h> |
---|
| 32 | |
---|
[72af530] | 33 | #include "img.h" |
---|
[7d27e36] | 34 | |
---|
[42bd3576] | 35 | #define TIMENA "?" |
---|
[72af530] | 36 | #ifdef IMG_HOSTED |
---|
[17edec7] | 37 | # define INT32_T int32_t |
---|
[d69255f] | 38 | # include "debug.h" |
---|
[d1b1380] | 39 | # include "filelist.h" |
---|
[d69255f] | 40 | # include "filename.h" |
---|
| 41 | # include "message.h" |
---|
| 42 | # include "useful.h" |
---|
[a420b49] | 43 | # define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107) |
---|
[17edec7] | 44 | #else |
---|
| 45 | # ifdef HAVE_STDINT_H |
---|
| 46 | # include <stdint.h> |
---|
[2acbff0a] | 47 | # define INT32_T int32_t |
---|
[17edec7] | 48 | # else |
---|
| 49 | # include <limits.h> |
---|
| 50 | # if INT_MAX >= 2147483647 |
---|
| 51 | # define INT32_T int |
---|
| 52 | # else |
---|
| 53 | # define INT32_T long |
---|
| 54 | # endif |
---|
[2acbff0a] | 55 | # endif |
---|
[d1b1380] | 56 | # define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z" |
---|
| 57 | # define EXT_SVX_3D "3d" |
---|
[be97baf] | 58 | # define EXT_SVX_POS "pos" |
---|
| 59 | # define FNM_SEP_EXT '.' |
---|
| 60 | # define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
[d1b1380] | 61 | # define xosmalloc(L) malloc((L)) |
---|
[72af530] | 62 | # define xosrealloc(L,S) realloc((L),(S)) |
---|
[d1b1380] | 63 | # define osfree(P) free((P)) |
---|
[81bb4bdc] | 64 | # define osnew(T) (T*)malloc(sizeof(T)) |
---|
| 65 | |
---|
[72af530] | 66 | /* in IMG_HOSTED mode, this tests if a filename refers to a directory */ |
---|
[d1b1380] | 67 | # define fDirectory(X) 0 |
---|
| 68 | /* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */ |
---|
| 69 | /* path isn't used in img.c, but EXT is */ |
---|
[b932a29] | 70 | # define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) \ |
---|
| 71 | ((*(X) = NULL), fopen(FNM,MODE)) |
---|
[8ec15cf] | 72 | # ifndef PUTC |
---|
| 73 | # define PUTC(C, FH) putc(C, FH) |
---|
| 74 | # endif |
---|
| 75 | # ifndef GETC |
---|
| 76 | # define GETC(FH) getc(FH) |
---|
| 77 | # endif |
---|
[0d9d980] | 78 | # define fputsnl(S, FH) (fputs((S), (FH)) == EOF ? EOF : putc('\n', (FH))) |
---|
[4c07c51] | 79 | # define SVX_ASSERT(X) |
---|
[d1b1380] | 80 | |
---|
[88b54df6] | 81 | #ifdef __cplusplus |
---|
| 82 | # include <algorithm> |
---|
| 83 | using std::max; |
---|
| 84 | using std::min; |
---|
| 85 | #else |
---|
| 86 | /* Return max/min of two numbers. */ |
---|
| 87 | /* May be defined already (e.g. by Borland C in stdlib.h) */ |
---|
| 88 | /* NB Bad news if X or Y has side-effects... */ |
---|
| 89 | # ifndef max |
---|
| 90 | # define max(X, Y) ((X) > (Y) ? (X) : (Y)) |
---|
| 91 | # endif |
---|
| 92 | # ifndef min |
---|
| 93 | # define min(X, Y) ((X) < (Y) ? (X) : (Y)) |
---|
| 94 | # endif |
---|
| 95 | #endif |
---|
| 96 | |
---|
[0e75725] | 97 | static INT32_T |
---|
[a420b49] | 98 | get32(FILE *fh) |
---|
| 99 | { |
---|
[e02a6a6] | 100 | INT32_T w = GETC(fh); |
---|
| 101 | w |= (INT32_T)GETC(fh) << 8l; |
---|
| 102 | w |= (INT32_T)GETC(fh) << 16l; |
---|
| 103 | w |= (INT32_T)GETC(fh) << 24l; |
---|
[a420b49] | 104 | return w; |
---|
[d1b1380] | 105 | } |
---|
| 106 | |
---|
[a420b49] | 107 | static void |
---|
| 108 | put32(long w, FILE *fh) |
---|
| 109 | { |
---|
[e02a6a6] | 110 | PUTC((char)(w), fh); |
---|
| 111 | PUTC((char)(w >> 8l), fh); |
---|
| 112 | PUTC((char)(w >> 16l), fh); |
---|
| 113 | PUTC((char)(w >> 24l), fh); |
---|
[d1b1380] | 114 | } |
---|
[5c3c61a] | 115 | |
---|
[be97baf] | 116 | static short |
---|
| 117 | get16(FILE *fh) |
---|
| 118 | { |
---|
[e02a6a6] | 119 | short w = GETC(fh); |
---|
| 120 | w |= (short)GETC(fh) << 8l; |
---|
[be97baf] | 121 | return w; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | static void |
---|
| 125 | put16(short w, FILE *fh) |
---|
| 126 | { |
---|
[e02a6a6] | 127 | PUTC((char)(w), fh); |
---|
| 128 | PUTC((char)(w >> 8l), fh); |
---|
[be97baf] | 129 | } |
---|
| 130 | |
---|
| 131 | static char * |
---|
| 132 | baseleaf_from_fnm(const char *fnm) |
---|
| 133 | { |
---|
| 134 | const char *p; |
---|
[f807d8bf] | 135 | const char *q; |
---|
| 136 | char * res; |
---|
[be97baf] | 137 | size_t len; |
---|
| 138 | |
---|
| 139 | p = fnm; |
---|
| 140 | q = strrchr(p, '/'); |
---|
| 141 | if (q) p = q + 1; |
---|
| 142 | q = strrchr(p, '\\'); |
---|
| 143 | if (q) p = q + 1; |
---|
| 144 | |
---|
| 145 | q = strrchr(p, FNM_SEP_EXT); |
---|
| 146 | if (q) len = (const char *)q - p; else len = strlen(p); |
---|
| 147 | |
---|
[f807d8bf] | 148 | res = (char *)xosmalloc(len + 1); |
---|
| 149 | if (!res) return NULL; |
---|
| 150 | memcpy(res, p, len); |
---|
| 151 | res[len] = '\0'; |
---|
| 152 | return res; |
---|
[be97baf] | 153 | } |
---|
[5c3c61a] | 154 | #endif |
---|
| 155 | |
---|
[4f70ebc] | 156 | static char * my_strdup(const char *str); |
---|
| 157 | |
---|
| 158 | static time_t |
---|
| 159 | mktime_with_tz(struct tm * tm, const char * tz) |
---|
| 160 | { |
---|
| 161 | time_t r; |
---|
| 162 | char * old_tz = getenv("TZ"); |
---|
[c891a7e] | 163 | #ifdef _MSC_VER |
---|
[4f70ebc] | 164 | if (old_tz) { |
---|
| 165 | old_tz = my_strdup(old_tz); |
---|
| 166 | if (!old_tz) |
---|
| 167 | return (time_t)-1; |
---|
| 168 | } |
---|
| 169 | if (_putenv_s("TZ", tz) != 0) { |
---|
| 170 | osfree(old_tz); |
---|
| 171 | return (time_t)-1; |
---|
| 172 | } |
---|
| 173 | #elif defined HAVE_SETENV |
---|
| 174 | if (old_tz) { |
---|
| 175 | old_tz = my_strdup(old_tz); |
---|
| 176 | if (!old_tz) |
---|
| 177 | return (time_t)-1; |
---|
| 178 | } |
---|
| 179 | if (setenv("TZ", tz, 1) < 0) { |
---|
| 180 | osfree(old_tz); |
---|
| 181 | return (time_t)-1; |
---|
| 182 | } |
---|
| 183 | #else |
---|
| 184 | char * p; |
---|
| 185 | if (old_tz) { |
---|
| 186 | size_t len = strlen(old_tz) + 1; |
---|
| 187 | p = (char *)xosmalloc(len + 3); |
---|
| 188 | if (!p) |
---|
| 189 | return (time_t)-1; |
---|
| 190 | memcpy(p, "TZ=", 3); |
---|
| 191 | memcpy(p + 3, tz, len); |
---|
| 192 | old_tz = p; |
---|
| 193 | } |
---|
| 194 | p = (char *)xosmalloc(strlen(tz) + 4); |
---|
| 195 | if (!p) { |
---|
| 196 | osfree(old_tz); |
---|
| 197 | return (time_t)-1; |
---|
| 198 | } |
---|
| 199 | memcpy(p, "TZ=", 3); |
---|
| 200 | strcpy(p + 3, tz); |
---|
| 201 | if (putenv(p) != 0) { |
---|
| 202 | osfree(p); |
---|
| 203 | osfree(old_tz); |
---|
| 204 | return (time_t)-1; |
---|
| 205 | } |
---|
[c891a7e] | 206 | #define CLEANUP() osfree(p) |
---|
[4f70ebc] | 207 | #endif |
---|
| 208 | tzset(); |
---|
| 209 | r = mktime(tm); |
---|
| 210 | if (old_tz) { |
---|
[c891a7e] | 211 | #ifdef _MSC_VER |
---|
[4f70ebc] | 212 | _putenv_s("TZ", old_tz); |
---|
| 213 | #elif !defined HAVE_SETENV |
---|
| 214 | putenv(old_tz); |
---|
| 215 | #else |
---|
| 216 | setenv("TZ", old_tz, 1); |
---|
| 217 | #endif |
---|
| 218 | osfree(old_tz); |
---|
| 219 | } else { |
---|
[c891a7e] | 220 | #ifdef _MSC_VER |
---|
[4f70ebc] | 221 | _putenv_s("TZ", ""); |
---|
| 222 | #elif !defined HAVE_UNSETENV |
---|
| 223 | putenv((char*)"TZ"); |
---|
| 224 | #else |
---|
| 225 | unsetenv("TZ"); |
---|
| 226 | #endif |
---|
| 227 | } |
---|
[c891a7e] | 228 | #ifdef CLEANUP |
---|
| 229 | CLEANUP(); |
---|
| 230 | #undef CLEANUP |
---|
| 231 | #endif |
---|
[4f70ebc] | 232 | return r; |
---|
| 233 | } |
---|
| 234 | |
---|
[1ee204e] | 235 | static unsigned short |
---|
| 236 | getu16(FILE *fh) |
---|
| 237 | { |
---|
| 238 | return (unsigned short)get16(fh); |
---|
| 239 | } |
---|
| 240 | |
---|
[be97baf] | 241 | #include <math.h> |
---|
[0a9b040] | 242 | |
---|
| 243 | #if !defined HAVE_LROUND && !defined HAVE_DECL_LROUND |
---|
| 244 | /* The autoconf tests are not in use, but C99 and C++11 both added lround(), |
---|
| 245 | * so set HAVE_LROUND and HAVE_DECL_LROUND conservatively based on the language |
---|
| 246 | * standard version the compiler claims to support. */ |
---|
| 247 | # if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \ |
---|
| 248 | (defined __cplusplus && __cplusplus >= 201103L) |
---|
| 249 | # define HAVE_LROUND 1 |
---|
| 250 | # define HAVE_DECL_LROUND 1 |
---|
| 251 | # endif |
---|
| 252 | #endif |
---|
| 253 | |
---|
| 254 | #ifdef HAVE_LROUND |
---|
| 255 | # if defined HAVE_DECL_LROUND && !HAVE_DECL_LROUND |
---|
| 256 | /* On older systems, the prototype may be missing. */ |
---|
| 257 | extern long lround(double); |
---|
| 258 | # endif |
---|
| 259 | # define my_lround lround |
---|
[dd1d6369] | 260 | #else |
---|
[0a9b040] | 261 | static long |
---|
| 262 | my_lround(double x) { |
---|
| 263 | return (x >= 0.0) ? (long)(x + 0.5) : -(long)(0.5 - x); |
---|
[dd1d6369] | 264 | } |
---|
| 265 | #endif |
---|
| 266 | |
---|
[ad4eaf3f] | 267 | /* portable case insensitive string compare */ |
---|
| 268 | #if defined(strcasecmp) || defined(HAVE_STRCASECMP) |
---|
| 269 | # define my_strcasecmp strcasecmp |
---|
| 270 | #else |
---|
[2adff93] | 271 | static int my_strcasecmp(const char *s1, const char *s2) { |
---|
[ad95991] | 272 | unsigned char c1, c2; |
---|
[ad4eaf3f] | 273 | do { |
---|
| 274 | c1 = *s1++; |
---|
| 275 | c2 = *s2++; |
---|
| 276 | } while (c1 && toupper(c1) == toupper(c2)); |
---|
| 277 | /* now calculate real difference */ |
---|
| 278 | return c1 - c2; |
---|
| 279 | } |
---|
| 280 | #endif |
---|
| 281 | |
---|
[2bb17c9] | 282 | unsigned int img_output_version = IMG_VERSION_MAX; |
---|
[5c3c61a] | 283 | |
---|
| 284 | static img_errcode img_errno = IMG_NONE; |
---|
[d1b1380] | 285 | |
---|
[7d27e36] | 286 | #define FILEID "Survex 3D Image File" |
---|
| 287 | |
---|
[ad4eaf3f] | 288 | #define EXT_PLT "plt" |
---|
[d16db18] | 289 | #define EXT_PLF "plf" |
---|
[ad4eaf3f] | 290 | |
---|
[0da6e60] | 291 | /* Attempt to string paste to ensure we are passed a literal string */ |
---|
| 292 | #define LITLEN(S) (sizeof(S"") - 1) |
---|
| 293 | |
---|
[db9e5bf] | 294 | /* Fake "version numbers" for non-3d formats we can read. */ |
---|
| 295 | #define VERSION_CMAP_SHOT -4 |
---|
| 296 | #define VERSION_CMAP_STATION -3 |
---|
| 297 | #define VERSION_COMPASS_PLT -2 |
---|
| 298 | #define VERSION_SURVEX_POS -1 |
---|
| 299 | |
---|
[7d27e36] | 300 | static char * |
---|
| 301 | my_strdup(const char *str) |
---|
[a420b49] | 302 | { |
---|
[7d27e36] | 303 | char *p; |
---|
| 304 | size_t len = strlen(str) + 1; |
---|
[f807d8bf] | 305 | p = (char *)xosmalloc(len); |
---|
[7d27e36] | 306 | if (p) memcpy(p, str, len); |
---|
| 307 | return p; |
---|
[d1b1380] | 308 | } |
---|
[c0563da] | 309 | |
---|
[43d63e57] | 310 | #define getline_alloc(FH) getline_alloc_len(FH, NULL) |
---|
| 311 | |
---|
[a2ad284] | 312 | static char * |
---|
[43d63e57] | 313 | getline_alloc_len(FILE *fh, size_t * p_len) |
---|
[a2ad284] | 314 | { |
---|
| 315 | int ch; |
---|
| 316 | size_t i = 0; |
---|
| 317 | size_t len = 16; |
---|
[f807d8bf] | 318 | char *buf = (char *)xosmalloc(len); |
---|
[a2ad284] | 319 | if (!buf) return NULL; |
---|
| 320 | |
---|
[e02a6a6] | 321 | ch = GETC(fh); |
---|
[a2ad284] | 322 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 323 | buf[i++] = ch; |
---|
| 324 | if (i == len - 1) { |
---|
| 325 | char *p; |
---|
| 326 | len += len; |
---|
[f807d8bf] | 327 | p = (char *)xosrealloc(buf, len); |
---|
[a2ad284] | 328 | if (!p) { |
---|
| 329 | osfree(buf); |
---|
| 330 | return NULL; |
---|
| 331 | } |
---|
| 332 | buf = p; |
---|
| 333 | } |
---|
[e02a6a6] | 334 | ch = GETC(fh); |
---|
[a2ad284] | 335 | } |
---|
| 336 | if (ch == '\n' || ch == '\r') { |
---|
[4ebc557] | 337 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
[e02a6a6] | 338 | ch = GETC(fh); |
---|
[4ebc557] | 339 | /* if it's not the other eol character, put it back */ |
---|
| 340 | if (ch != otherone) ungetc(ch, fh); |
---|
[a2ad284] | 341 | } |
---|
[43d63e57] | 342 | buf[i] = '\0'; |
---|
| 343 | if (p_len) *p_len = i; |
---|
[a2ad284] | 344 | return buf; |
---|
| 345 | } |
---|
| 346 | |
---|
[a420b49] | 347 | img_errcode |
---|
| 348 | img_error(void) |
---|
| 349 | { |
---|
| 350 | return img_errno; |
---|
[d1b1380] | 351 | } |
---|
| 352 | |
---|
[f807d8bf] | 353 | static int |
---|
[d4d3655] | 354 | check_label_space(img *pimg, size_t len) |
---|
| 355 | { |
---|
| 356 | if (len > pimg->buf_len) { |
---|
[f807d8bf] | 357 | char *b = (char *)xosrealloc(pimg->label_buf, len); |
---|
| 358 | if (!b) return 0; |
---|
[d4d3655] | 359 | pimg->label = (pimg->label - pimg->label_buf) + b; |
---|
| 360 | pimg->label_buf = b; |
---|
| 361 | pimg->buf_len = len; |
---|
| 362 | } |
---|
[f807d8bf] | 363 | return 1; |
---|
[d4d3655] | 364 | } |
---|
| 365 | |
---|
[4139b12] | 366 | /* Check if a station name should be included. */ |
---|
| 367 | static int |
---|
| 368 | stn_included(img *pimg) |
---|
| 369 | { |
---|
| 370 | if (!pimg->survey_len) return 1; |
---|
| 371 | size_t l = pimg->survey_len; |
---|
| 372 | const char *s = pimg->label_buf; |
---|
| 373 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
| 374 | return 0; |
---|
| 375 | } |
---|
| 376 | pimg->label += l + 1; |
---|
| 377 | return 1; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /* Check if a survey name should be included. */ |
---|
| 381 | static int |
---|
| 382 | survey_included(img *pimg) |
---|
| 383 | { |
---|
| 384 | if (!pimg->survey_len) return 1; |
---|
| 385 | size_t l = pimg->survey_len; |
---|
| 386 | const char *s = pimg->label_buf; |
---|
| 387 | if (strncmp(pimg->survey, s, l) != 0 || |
---|
| 388 | !(s[l] == '.' || s[l] == '\0')) { |
---|
| 389 | return 0; |
---|
| 390 | } |
---|
| 391 | pimg->label += l; |
---|
| 392 | /* skip the dot if there */ |
---|
| 393 | if (*pimg->label) pimg->label++; |
---|
| 394 | return 1; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /* Check if a survey name in a buffer should be included. |
---|
| 398 | * |
---|
| 399 | * For "foreign" formats which just have one level of surveys. |
---|
| 400 | */ |
---|
| 401 | static int |
---|
| 402 | buf_included(img *pimg, const char *buf, size_t len) |
---|
| 403 | { |
---|
| 404 | return pimg->survey_len == len && strncmp(buf, pimg->survey, len) == 0; |
---|
| 405 | } |
---|
| 406 | |
---|
[d16db18] | 407 | #define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\ |
---|
| 408 | (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\ |
---|
| 409 | my_strcasecmp((F) + (L) - LITLEN(E), E) == 0) |
---|
| 410 | |
---|
[a420b49] | 411 | img * |
---|
[a2ad284] | 412 | img_open_survey(const char *fnm, const char *survey) |
---|
[a420b49] | 413 | { |
---|
| 414 | img *pimg; |
---|
[0da6e60] | 415 | size_t len; |
---|
[27b8b59] | 416 | char buf[LITLEN(FILEID) + 9]; |
---|
[7d27e36] | 417 | int ch; |
---|
[a420b49] | 418 | |
---|
| 419 | if (fDirectory(fnm)) { |
---|
| 420 | img_errno = IMG_DIRECTORY; |
---|
| 421 | return NULL; |
---|
| 422 | } |
---|
[d1b1380] | 423 | |
---|
[81bb4bdc] | 424 | pimg = osnew(img); |
---|
[a420b49] | 425 | if (pimg == NULL) { |
---|
| 426 | img_errno = IMG_OUTOFMEMORY; |
---|
| 427 | return NULL; |
---|
| 428 | } |
---|
[d1b1380] | 429 | |
---|
[23f7ea7] | 430 | pimg->buf_len = 257; |
---|
[f807d8bf] | 431 | pimg->label_buf = (char *)xosmalloc(pimg->buf_len); |
---|
[0ff23cb] | 432 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 433 | osfree(pimg); |
---|
| 434 | img_errno = IMG_OUTOFMEMORY; |
---|
| 435 | return NULL; |
---|
| 436 | } |
---|
| 437 | |
---|
[cc9eb738] | 438 | pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", &(pimg->filename_opened)); |
---|
[a420b49] | 439 | if (pimg->fh == NULL) { |
---|
[ce1e5ac] | 440 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 441 | osfree(pimg); |
---|
[a420b49] | 442 | img_errno = IMG_FILENOTFOUND; |
---|
| 443 | return NULL; |
---|
| 444 | } |
---|
[d1b1380] | 445 | |
---|
[f807d8bf] | 446 | pimg->fRead = 1; /* reading from this file */ |
---|
[0da6e60] | 447 | img_errno = IMG_NONE; |
---|
| 448 | |
---|
| 449 | pimg->flags = 0; |
---|
| 450 | |
---|
[b5a3219] | 451 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[db9e5bf] | 452 | /* for VERSION_COMPASS_PLT, 0 value indicates we haven't |
---|
| 453 | * entered a survey yet */ |
---|
| 454 | /* for VERSION_CMAP_SHOT, we store the last station here |
---|
| 455 | * to detect whether we MOVE or LINE */ |
---|
[0da6e60] | 456 | pimg->label_len = 0; |
---|
[f1c7c9c7] | 457 | pimg->label_buf[0] = '\0'; |
---|
[0da6e60] | 458 | |
---|
| 459 | pimg->survey = NULL; |
---|
| 460 | pimg->survey_len = 0; |
---|
[ee1ec59] | 461 | pimg->separator = '.'; |
---|
[1ee204e] | 462 | #if IMG_API_VERSION == 0 |
---|
| 463 | pimg->date1 = pimg->date2 = 0; |
---|
| 464 | #else /* IMG_API_VERSION == 1 */ |
---|
| 465 | pimg->days1 = pimg->days2 = -1; |
---|
| 466 | #endif |
---|
[eef68f9] | 467 | pimg->is_extended_elevation = 0; |
---|
[0da6e60] | 468 | |
---|
[dec6b10] | 469 | pimg->style = pimg->oldstyle = img_STYLE_UNKNOWN; |
---|
| 470 | |
---|
[62a841c] | 471 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
| 472 | |
---|
[43d63e57] | 473 | pimg->title = pimg->datestamp = pimg->cs = NULL; |
---|
[4f70ebc] | 474 | pimg->datestamp_numeric = (time_t)-1; |
---|
| 475 | |
---|
[0da6e60] | 476 | if (survey) { |
---|
| 477 | len = strlen(survey); |
---|
| 478 | if (len) { |
---|
| 479 | if (survey[len - 1] == '.') len--; |
---|
| 480 | if (len) { |
---|
| 481 | char *p; |
---|
[f807d8bf] | 482 | pimg->survey = (char *)xosmalloc(len + 2); |
---|
[be97baf] | 483 | if (!pimg->survey) { |
---|
| 484 | img_errno = IMG_OUTOFMEMORY; |
---|
| 485 | goto error; |
---|
| 486 | } |
---|
[0da6e60] | 487 | memcpy(pimg->survey, survey, len); |
---|
| 488 | /* Set title to leaf survey name */ |
---|
| 489 | pimg->survey[len] = '\0'; |
---|
[1908f42] | 490 | p = strrchr(pimg->survey, '.'); |
---|
[0da6e60] | 491 | if (p) p++; else p = pimg->survey; |
---|
[7d27e36] | 492 | pimg->title = my_strdup(p); |
---|
| 493 | if (!pimg->title) { |
---|
| 494 | img_errno = IMG_OUTOFMEMORY; |
---|
| 495 | goto error; |
---|
| 496 | } |
---|
[0da6e60] | 497 | pimg->survey[len] = '.'; |
---|
| 498 | pimg->survey[len + 1] = '\0'; |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | pimg->survey_len = len; |
---|
| 502 | } |
---|
| 503 | |
---|
[db9e5bf] | 504 | /* [VERSION_COMPASS_PLT, VERSION_CMAP_STATION, VERSION_CMAP_SHOT] pending |
---|
| 505 | * IMG_LINE or IMG_MOVE - both have 4 added. |
---|
| 506 | * [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one |
---|
[0da6e60] | 507 | * [version 0] not in the middle of a 'LINE' command |
---|
[b5a3219] | 508 | * [version >= 3] not in the middle of turning a LINE into a MOVE |
---|
[0da6e60] | 509 | */ |
---|
| 510 | pimg->pending = 0; |
---|
| 511 | |
---|
| 512 | len = strlen(fnm); |
---|
[d16db18] | 513 | if (has_ext(fnm, len, EXT_SVX_POS)) { |
---|
[f830e0d] | 514 | pos_file: |
---|
[db9e5bf] | 515 | pimg->version = VERSION_SURVEX_POS; |
---|
[984f49e] | 516 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[7d27e36] | 517 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 518 | if (!pimg->datestamp) { |
---|
| 519 | img_errno = IMG_OUTOFMEMORY; |
---|
| 520 | goto error; |
---|
| 521 | } |
---|
[58f02ae] | 522 | pimg->start = 0; |
---|
[0da6e60] | 523 | return pimg; |
---|
| 524 | } |
---|
[5757725] | 525 | |
---|
[d16db18] | 526 | if (has_ext(fnm, len, EXT_PLT) || has_ext(fnm, len, EXT_PLF)) { |
---|
[4ebc557] | 527 | long fpos; |
---|
[27bd06b] | 528 | plt_file: |
---|
[db9e5bf] | 529 | pimg->version = VERSION_COMPASS_PLT; |
---|
[ee1ec59] | 530 | /* Spaces aren't legal in Compass station names, but dots are, so |
---|
| 531 | * use space as the level separator */ |
---|
| 532 | pimg->separator = ' '; |
---|
[4ebc557] | 533 | pimg->start = 0; |
---|
[984f49e] | 534 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[ad4eaf3f] | 535 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 536 | if (!pimg->datestamp) { |
---|
| 537 | img_errno = IMG_OUTOFMEMORY; |
---|
| 538 | goto error; |
---|
| 539 | } |
---|
[4ebc557] | 540 | while (1) { |
---|
[e02a6a6] | 541 | ch = GETC(pimg->fh); |
---|
[4ebc557] | 542 | switch (ch) { |
---|
| 543 | case '\x1a': |
---|
| 544 | fseek(pimg->fh, -1, SEEK_CUR); |
---|
| 545 | /* FALL THRU */ |
---|
| 546 | case EOF: |
---|
| 547 | pimg->start = ftell(pimg->fh); |
---|
| 548 | return pimg; |
---|
| 549 | case 'N': { |
---|
| 550 | char *line, *q; |
---|
| 551 | fpos = ftell(pimg->fh) - 1; |
---|
| 552 | if (!pimg->survey) { |
---|
| 553 | /* FIXME : if there's only one survey in the file, it'd be nice |
---|
| 554 | * to use its description as the title here... |
---|
| 555 | */ |
---|
[0d922fc] | 556 | ungetc('N', pimg->fh); |
---|
[4ebc557] | 557 | pimg->start = fpos; |
---|
[984f49e] | 558 | return pimg; |
---|
| 559 | } |
---|
[4ebc557] | 560 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 561 | if (!line) { |
---|
| 562 | img_errno = IMG_OUTOFMEMORY; |
---|
| 563 | goto error; |
---|
| 564 | } |
---|
[0d922fc] | 565 | len = 0; |
---|
[4ebc557] | 566 | while (line[len] > 32) ++len; |
---|
[4139b12] | 567 | if (!buf_included(pimg, line, len)) { |
---|
[ad4eaf3f] | 568 | osfree(line); |
---|
[4ebc557] | 569 | continue; |
---|
[ad4eaf3f] | 570 | } |
---|
[4ebc557] | 571 | q = strchr(line + len, 'C'); |
---|
| 572 | if (q && q[1]) { |
---|
| 573 | osfree(pimg->title); |
---|
[f1c7c9c7] | 574 | pimg->title = my_strdup(q + 1); |
---|
[4ebc557] | 575 | } else if (!pimg->title) { |
---|
[f1c7c9c7] | 576 | pimg->title = my_strdup(pimg->label); |
---|
[4ebc557] | 577 | } |
---|
| 578 | osfree(line); |
---|
| 579 | if (!pimg->title) { |
---|
| 580 | img_errno = IMG_OUTOFMEMORY; |
---|
| 581 | goto error; |
---|
| 582 | } |
---|
| 583 | if (!pimg->start) pimg->start = fpos; |
---|
[27bd06b] | 584 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[4ebc557] | 585 | return pimg; |
---|
| 586 | } |
---|
| 587 | case 'M': case 'D': |
---|
| 588 | pimg->start = ftell(pimg->fh) - 1; |
---|
| 589 | break; |
---|
[984f49e] | 590 | } |
---|
[4ebc557] | 591 | while (ch != '\n' && ch != '\r') { |
---|
[e02a6a6] | 592 | ch = GETC(pimg->fh); |
---|
[984f49e] | 593 | } |
---|
| 594 | } |
---|
[ad4eaf3f] | 595 | } |
---|
[0da6e60] | 596 | |
---|
[d9cd8ae] | 597 | /* Although these are often referred to as "CMAP .XYZ files", it seems |
---|
| 598 | * that actually, the extension .XYZ isn't used, rather .SHT (shot |
---|
| 599 | * variant, produced by CMAP v16 and later), .UNA (unadjusted) and |
---|
| 600 | * .ADJ (adjusted) extensions are. Since img has long checked for |
---|
| 601 | * .XYZ, we continue to do so in case anyone is relying on it. |
---|
| 602 | */ |
---|
| 603 | if (has_ext(fnm, len, "sht") || |
---|
| 604 | has_ext(fnm, len, "adj") || |
---|
| 605 | has_ext(fnm, len, "una") || |
---|
| 606 | has_ext(fnm, len, "xyz")) { |
---|
[f1c7c9c7] | 607 | char *line; |
---|
| 608 | xyz_file: |
---|
[ee1ec59] | 609 | /* Spaces aren't legal in CMAP station names, but dots are, so |
---|
[6d4c4b9] | 610 | * use space as the level separator. */ |
---|
[ee1ec59] | 611 | pimg->separator = ' '; |
---|
[f1c7c9c7] | 612 | line = getline_alloc(pimg->fh); |
---|
| 613 | if (!line) { |
---|
| 614 | img_errno = IMG_OUTOFMEMORY; |
---|
| 615 | goto error; |
---|
| 616 | } |
---|
[4f70ebc] | 617 | /* There doesn't seem to be a spec for what happens after 1999 with cmap |
---|
| 618 | * files, so this code allows for: |
---|
| 619 | * * 21xx -> xx (up to 2150) |
---|
| 620 | * * 21xx -> 1xx (up to 2199) |
---|
| 621 | * * full year being specified instead of 2 digits |
---|
| 622 | */ |
---|
[f1c7c9c7] | 623 | len = strlen(line); |
---|
[4f70ebc] | 624 | if (len > 59) { |
---|
| 625 | /* Don't just truncate at column 59, allow for a > 2 digit year. */ |
---|
| 626 | char * p = strstr(line + len, "Page"); |
---|
| 627 | if (p) { |
---|
| 628 | while (p > line && p[-1] == ' ') |
---|
| 629 | --p; |
---|
| 630 | *p = '\0'; |
---|
| 631 | len = p - line; |
---|
| 632 | } else { |
---|
| 633 | line[59] = '\0'; |
---|
| 634 | } |
---|
| 635 | } |
---|
[f1c7c9c7] | 636 | if (len > 45) { |
---|
[4f70ebc] | 637 | /* YY/MM/DD HH:MM */ |
---|
| 638 | struct tm tm; |
---|
| 639 | unsigned long v; |
---|
| 640 | char * p; |
---|
[f1c7c9c7] | 641 | pimg->datestamp = my_strdup(line + 45); |
---|
[4f70ebc] | 642 | p = pimg->datestamp; |
---|
| 643 | v = strtoul(p, &p, 10); |
---|
| 644 | if (v <= 50) { |
---|
| 645 | /* In the absence of a spec for cmap files, assume <= 50 means 21st |
---|
| 646 | * century. */ |
---|
| 647 | v += 2000; |
---|
| 648 | } else if (v < 200) { |
---|
| 649 | /* Map 100-199 to 21st century. */ |
---|
| 650 | v += 1900; |
---|
| 651 | } |
---|
| 652 | if (v == ULONG_MAX || *p++ != '/') |
---|
| 653 | goto bad_cmap_date; |
---|
| 654 | tm.tm_year = v - 1900; |
---|
| 655 | v = strtoul(p, &p, 10); |
---|
| 656 | if (v < 1 || v > 12 || *p++ != '/') |
---|
| 657 | goto bad_cmap_date; |
---|
| 658 | tm.tm_mon = v - 1; |
---|
| 659 | v = strtoul(p, &p, 10); |
---|
| 660 | if (v < 1 || v > 31 || *p++ != ' ') |
---|
| 661 | goto bad_cmap_date; |
---|
| 662 | tm.tm_mday = v; |
---|
| 663 | v = strtoul(p, &p, 10); |
---|
| 664 | if (v >= 24 || *p++ != ':') |
---|
| 665 | goto bad_cmap_date; |
---|
| 666 | tm.tm_hour = v; |
---|
| 667 | v = strtoul(p, &p, 10); |
---|
| 668 | if (v >= 60) |
---|
| 669 | goto bad_cmap_date; |
---|
| 670 | tm.tm_min = v; |
---|
| 671 | if (*p == ':') { |
---|
| 672 | v = strtoul(p + 1, &p, 10); |
---|
| 673 | if (v > 60) |
---|
| 674 | goto bad_cmap_date; |
---|
| 675 | tm.tm_sec = v; |
---|
| 676 | } else { |
---|
| 677 | tm.tm_sec = 0; |
---|
| 678 | } |
---|
| 679 | tm.tm_isdst = 0; |
---|
| 680 | /* We have no indication of what timezone this timestamp is in. It's |
---|
| 681 | * probably local time for whoever processed the data, so just assume |
---|
| 682 | * UTC, which is at least fairly central in the possibilities. |
---|
| 683 | */ |
---|
| 684 | pimg->datestamp_numeric = mktime_with_tz(&tm, ""); |
---|
[f1c7c9c7] | 685 | } else { |
---|
| 686 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 687 | } |
---|
[4f70ebc] | 688 | bad_cmap_date: |
---|
[f1c7c9c7] | 689 | if (strncmp(line, " Cave Survey Data Processed by CMAP ", |
---|
| 690 | LITLEN(" Cave Survey Data Processed by CMAP ")) == 0) { |
---|
| 691 | len = 0; |
---|
| 692 | } else { |
---|
| 693 | if (len > 45) { |
---|
| 694 | line[45] = '\0'; |
---|
| 695 | len = 45; |
---|
| 696 | } |
---|
| 697 | while (len > 2 && line[len - 1] == ' ') --len; |
---|
| 698 | if (len > 2) { |
---|
| 699 | line[len] = '\0'; |
---|
| 700 | pimg->title = my_strdup(line + 2); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | if (len <= 2) pimg->title = baseleaf_from_fnm(fnm); |
---|
| 704 | osfree(line); |
---|
| 705 | if (!pimg->datestamp || !pimg->title) { |
---|
| 706 | img_errno = IMG_OUTOFMEMORY; |
---|
| 707 | goto error; |
---|
| 708 | } |
---|
| 709 | line = getline_alloc(pimg->fh); |
---|
| 710 | if (!line) { |
---|
| 711 | img_errno = IMG_OUTOFMEMORY; |
---|
| 712 | goto error; |
---|
| 713 | } |
---|
| 714 | if (line[0] != ' ' || (line[1] != 'S' && line[1] != 'O')) { |
---|
| 715 | img_errno = IMG_BADFORMAT; |
---|
| 716 | goto error; |
---|
| 717 | } |
---|
| 718 | if (line[1] == 'S') { |
---|
[db9e5bf] | 719 | pimg->version = VERSION_CMAP_STATION; |
---|
[f1c7c9c7] | 720 | } else { |
---|
[db9e5bf] | 721 | pimg->version = VERSION_CMAP_SHOT; |
---|
[f1c7c9c7] | 722 | } |
---|
| 723 | osfree(line); |
---|
| 724 | line = getline_alloc(pimg->fh); |
---|
| 725 | if (!line) { |
---|
| 726 | img_errno = IMG_OUTOFMEMORY; |
---|
| 727 | goto error; |
---|
| 728 | } |
---|
| 729 | if (line[0] != ' ' || line[1] != '-') { |
---|
| 730 | img_errno = IMG_BADFORMAT; |
---|
| 731 | goto error; |
---|
| 732 | } |
---|
| 733 | osfree(line); |
---|
| 734 | pimg->start = ftell(pimg->fh); |
---|
| 735 | return pimg; |
---|
| 736 | } |
---|
| 737 | |
---|
[7d27e36] | 738 | if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 || |
---|
[d16db18] | 739 | memcmp(buf, FILEID"\n", LITLEN(FILEID) + 1) != 0) { |
---|
[27b8b59] | 740 | if (fread(buf + LITLEN(FILEID) + 1, 8, 1, pimg->fh) == 1 && |
---|
| 741 | memcmp(buf, FILEID"\r\nv0.01\r\n", LITLEN(FILEID) + 9) == 0) { |
---|
| 742 | /* v0 3d file with DOS EOLs */ |
---|
| 743 | pimg->version = 0; |
---|
| 744 | goto v03d; |
---|
| 745 | } |
---|
[d16db18] | 746 | rewind(pimg->fh); |
---|
[f1c7c9c7] | 747 | if (buf[1] == ' ') { |
---|
| 748 | if (buf[0] == ' ') { |
---|
| 749 | /* Looks like a CMAP .xyz file ... */ |
---|
| 750 | goto xyz_file; |
---|
| 751 | } else if (strchr("ZSNF", buf[0])) { |
---|
| 752 | /* Looks like a Compass .plt file ... */ |
---|
| 753 | /* Almost certainly it'll start "Z " */ |
---|
| 754 | goto plt_file; |
---|
| 755 | } |
---|
[27bd06b] | 756 | } |
---|
[f830e0d] | 757 | if (buf[0] == '(') { |
---|
| 758 | /* Looks like a Survex .pos file ... */ |
---|
| 759 | goto pos_file; |
---|
| 760 | } |
---|
[a420b49] | 761 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 762 | goto error; |
---|
[a420b49] | 763 | } |
---|
[d1b1380] | 764 | |
---|
[7d27e36] | 765 | /* check file format version */ |
---|
[e02a6a6] | 766 | ch = GETC(pimg->fh); |
---|
[7d27e36] | 767 | pimg->version = 0; |
---|
| 768 | if (tolower(ch) == 'b') { |
---|
| 769 | /* binary file iff B/b prefix */ |
---|
| 770 | pimg->version = 1; |
---|
[e02a6a6] | 771 | ch = GETC(pimg->fh); |
---|
[7d27e36] | 772 | } |
---|
| 773 | if (ch != 'v') { |
---|
| 774 | img_errno = IMG_BADFORMAT; |
---|
| 775 | goto error; |
---|
| 776 | } |
---|
[e02a6a6] | 777 | ch = GETC(pimg->fh); |
---|
[7d27e36] | 778 | if (ch == '0') { |
---|
| 779 | if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) { |
---|
| 780 | img_errno = IMG_BADFORMAT; |
---|
| 781 | goto error; |
---|
| 782 | } |
---|
[0da6e60] | 783 | /* nothing special to do */ |
---|
[7d27e36] | 784 | } else if (pimg->version == 0) { |
---|
[2bb17c9] | 785 | if (ch < '2' || ch > '0' + IMG_VERSION_MAX || GETC(pimg->fh) != '\n') { |
---|
[6c1c4f3] | 786 | img_errno = IMG_TOONEW; |
---|
[58f02ae] | 787 | goto error; |
---|
[6c1c4f3] | 788 | } |
---|
[7d27e36] | 789 | pimg->version = ch - '0'; |
---|
[5c3c61a] | 790 | } else { |
---|
[647407d] | 791 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 792 | goto error; |
---|
[a420b49] | 793 | } |
---|
[421b7d2] | 794 | |
---|
[27b8b59] | 795 | v03d: |
---|
[43d63e57] | 796 | { |
---|
| 797 | size_t title_len; |
---|
| 798 | char * title = getline_alloc_len(pimg->fh, &title_len); |
---|
| 799 | if (pimg->version == 8 && title) { |
---|
| 800 | /* We sneak in an extra field after a zero byte here, containing the |
---|
| 801 | * specified coordinate system (if any). Older readers will just |
---|
| 802 | * not see it (which is fine), and this trick avoids us having to |
---|
| 803 | * bump the 3d format version. |
---|
| 804 | */ |
---|
| 805 | size_t real_len = strlen(title); |
---|
| 806 | if (real_len != title_len) { |
---|
| 807 | pimg->cs = my_strdup(title + real_len + 1); |
---|
| 808 | } |
---|
| 809 | } |
---|
| 810 | if (!pimg->title) { |
---|
| 811 | pimg->title = title; |
---|
| 812 | } else { |
---|
| 813 | osfree(title); |
---|
| 814 | } |
---|
| 815 | } |
---|
[a2ad284] | 816 | pimg->datestamp = getline_alloc(pimg->fh); |
---|
| 817 | if (!pimg->title || !pimg->datestamp) { |
---|
[58f02ae] | 818 | img_errno = IMG_OUTOFMEMORY; |
---|
| 819 | error: |
---|
[a2ad284] | 820 | osfree(pimg->title); |
---|
[43d63e57] | 821 | osfree(pimg->cs); |
---|
[984f49e] | 822 | osfree(pimg->datestamp); |
---|
[452e200] | 823 | osfree(pimg->filename_opened); |
---|
[a2ad284] | 824 | fclose(pimg->fh); |
---|
| 825 | osfree(pimg); |
---|
| 826 | return NULL; |
---|
| 827 | } |
---|
| 828 | |
---|
[73fea40] | 829 | if (pimg->version >= 8) { |
---|
| 830 | int flags = GETC(pimg->fh); |
---|
| 831 | if (flags & img_FFLAG_EXTENDED) pimg->is_extended_elevation = 1; |
---|
| 832 | } else { |
---|
| 833 | len = strlen(pimg->title); |
---|
| 834 | if (len > 11 && strcmp(pimg->title + len - 11, " (extended)") == 0) { |
---|
| 835 | pimg->title[len - 11] = '\0'; |
---|
| 836 | pimg->is_extended_elevation = 1; |
---|
| 837 | } |
---|
[eef68f9] | 838 | } |
---|
| 839 | |
---|
[4f70ebc] | 840 | if (pimg->datestamp[0] == '@') { |
---|
| 841 | unsigned long v; |
---|
| 842 | char * p; |
---|
| 843 | errno = 0; |
---|
| 844 | v = strtoul(pimg->datestamp + 1, &p, 10); |
---|
| 845 | if (errno == 0 && *p == '\0') |
---|
| 846 | pimg->datestamp_numeric = v; |
---|
| 847 | /* FIXME: We're assuming here that the C time_t epoch is 1970, which is |
---|
| 848 | * true for Unix-like systems, Mac OS X and Windows, but isn't guaranteed |
---|
| 849 | * by ISO C. |
---|
| 850 | */ |
---|
| 851 | } else { |
---|
| 852 | /* %a,%Y.%m.%d %H:%M:%S %Z */ |
---|
| 853 | struct tm tm; |
---|
| 854 | unsigned long v; |
---|
| 855 | char * p = pimg->datestamp; |
---|
| 856 | while (isalpha((unsigned char)*p)) ++p; |
---|
| 857 | if (*p == ',') ++p; |
---|
| 858 | while (isspace((unsigned char)*p)) ++p; |
---|
| 859 | v = strtoul(p, &p, 10); |
---|
| 860 | if (v == ULONG_MAX || *p++ != '.') |
---|
| 861 | goto bad_3d_date; |
---|
| 862 | tm.tm_year = v - 1900; |
---|
| 863 | v = strtoul(p, &p, 10); |
---|
| 864 | if (v < 1 || v > 12 || *p++ != '.') |
---|
| 865 | goto bad_3d_date; |
---|
| 866 | tm.tm_mon = v - 1; |
---|
| 867 | v = strtoul(p, &p, 10); |
---|
| 868 | if (v < 1 || v > 31 || *p++ != ' ') |
---|
| 869 | goto bad_3d_date; |
---|
| 870 | tm.tm_mday = v; |
---|
| 871 | v = strtoul(p, &p, 10); |
---|
| 872 | if (v >= 24 || *p++ != ':') |
---|
| 873 | goto bad_3d_date; |
---|
| 874 | tm.tm_hour = v; |
---|
| 875 | v = strtoul(p, &p, 10); |
---|
| 876 | if (v >= 60 || *p++ != ':') |
---|
| 877 | goto bad_3d_date; |
---|
| 878 | tm.tm_min = v; |
---|
| 879 | v = strtoul(p, &p, 10); |
---|
| 880 | if (v > 60) |
---|
| 881 | goto bad_3d_date; |
---|
| 882 | tm.tm_sec = v; |
---|
| 883 | tm.tm_isdst = 0; |
---|
| 884 | while (isspace((unsigned char)*p)) ++p; |
---|
| 885 | /* p now points to the timezone string. |
---|
| 886 | * |
---|
| 887 | * However, it's likely to be a string like "BST", and such strings can |
---|
| 888 | * be ambiguous (BST could be UTC+1 or UTC+6), so it is impossible to |
---|
| 889 | * reliably convert in all cases. Just pass what we have to tzset() - if |
---|
| 890 | * it doesn't handle it, UTC will be used. |
---|
| 891 | */ |
---|
| 892 | pimg->datestamp_numeric = mktime_with_tz(&tm, p); |
---|
| 893 | } |
---|
| 894 | bad_3d_date: |
---|
| 895 | |
---|
[73fea40] | 896 | pimg->start = ftell(pimg->fh); |
---|
| 897 | |
---|
[a420b49] | 898 | return pimg; |
---|
[d1b1380] | 899 | } |
---|
| 900 | |
---|
[9f3e5df] | 901 | int |
---|
[f2588ca] | 902 | img_rewind(img *pimg) |
---|
| 903 | { |
---|
[9f3e5df] | 904 | if (!pimg->fRead) { |
---|
| 905 | img_errno = IMG_WRITEERROR; |
---|
| 906 | return 0; |
---|
| 907 | } |
---|
| 908 | if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) { |
---|
| 909 | img_errno = IMG_READERROR; |
---|
| 910 | return 0; |
---|
| 911 | } |
---|
[58f02ae] | 912 | clearerr(pimg->fh); |
---|
[db9e5bf] | 913 | /* [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one |
---|
[58f02ae] | 914 | * [version 0] not in the middle of a 'LINE' command |
---|
[b5a3219] | 915 | * [version >= 3] not in the middle of turning a LINE into a MOVE */ |
---|
[4ebc557] | 916 | pimg->pending = 0; |
---|
[d69255f] | 917 | |
---|
| 918 | img_errno = IMG_NONE; |
---|
[99cf51a] | 919 | |
---|
[b5a3219] | 920 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[db9e5bf] | 921 | /* for VERSION_COMPASS_PLT, 0 value indicates we haven't entered a survey |
---|
| 922 | * yet */ |
---|
| 923 | /* for VERSION_CMAP_SHOT, we store the last station here to detect whether |
---|
[f1c7c9c7] | 924 | * we MOVE or LINE */ |
---|
[d69255f] | 925 | pimg->label_len = 0; |
---|
[dec6b10] | 926 | pimg->style = img_STYLE_UNKNOWN; |
---|
[9f3e5df] | 927 | return 1; |
---|
[f2588ca] | 928 | } |
---|
| 929 | |
---|
[a420b49] | 930 | img * |
---|
[43d63e57] | 931 | img_open_write_cs(const char *fnm, const char *title, const char * cs, int flags) |
---|
[a420b49] | 932 | { |
---|
| 933 | time_t tm; |
---|
| 934 | img *pimg; |
---|
[d1b1380] | 935 | |
---|
[a420b49] | 936 | if (fDirectory(fnm)) { |
---|
| 937 | img_errno = IMG_DIRECTORY; |
---|
| 938 | return NULL; |
---|
| 939 | } |
---|
[d1b1380] | 940 | |
---|
[81bb4bdc] | 941 | pimg = osnew(img); |
---|
[a420b49] | 942 | if (pimg == NULL) { |
---|
| 943 | img_errno = IMG_OUTOFMEMORY; |
---|
| 944 | return NULL; |
---|
| 945 | } |
---|
[d1b1380] | 946 | |
---|
[fe8e80e] | 947 | pimg->buf_len = 257; |
---|
[f807d8bf] | 948 | pimg->label_buf = (char *)xosmalloc(pimg->buf_len); |
---|
[0ff23cb] | 949 | if (!pimg->label_buf) { |
---|
[fe8e80e] | 950 | osfree(pimg); |
---|
| 951 | img_errno = IMG_OUTOFMEMORY; |
---|
| 952 | return NULL; |
---|
| 953 | } |
---|
| 954 | |
---|
[a420b49] | 955 | pimg->fh = fopen(fnm, "wb"); |
---|
| 956 | if (!pimg->fh) { |
---|
[ce1e5ac] | 957 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 958 | osfree(pimg); |
---|
[a420b49] | 959 | img_errno = IMG_CANTOPENOUT; |
---|
| 960 | return NULL; |
---|
| 961 | } |
---|
[d1b1380] | 962 | |
---|
[452e200] | 963 | pimg->filename_opened = NULL; |
---|
| 964 | |
---|
[a420b49] | 965 | /* Output image file header */ |
---|
| 966 | fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */ |
---|
[5c3c61a] | 967 | if (img_output_version < 2) { |
---|
[d69255f] | 968 | pimg->version = 1; |
---|
| 969 | fputs("Bv0.01\n", pimg->fh); /* binary file format version number */ |
---|
[5c3c61a] | 970 | } else { |
---|
[2bb17c9] | 971 | pimg->version = (img_output_version > IMG_VERSION_MAX) ? IMG_VERSION_MAX : img_output_version; |
---|
[5c3c61a] | 972 | fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */ |
---|
| 973 | } |
---|
[0c50ce3] | 974 | |
---|
| 975 | fputs(title, pimg->fh); |
---|
[73fea40] | 976 | if (pimg->version < 8 && (flags & img_FFLAG_EXTENDED)) { |
---|
| 977 | /* Older format versions append " (extended)" to the title to mark |
---|
[0c50ce3] | 978 | * extended elevations. */ |
---|
| 979 | size_t len = strlen(title); |
---|
| 980 | if (len < 11 || strcmp(title + len - 11, " (extended)") != 0) |
---|
| 981 | fputs(" (extended)", pimg->fh); |
---|
| 982 | } |
---|
[43d63e57] | 983 | if (pimg->version == 8 && cs && *cs) { |
---|
| 984 | /* We sneak in an extra field after a zero byte here, containing the |
---|
| 985 | * specified coordinate system (if any). Older readers will just not |
---|
| 986 | * see it (which is fine), and this trick avoids us having to bump the |
---|
| 987 | * 3d format version. |
---|
| 988 | */ |
---|
| 989 | PUTC('\0', pimg->fh); |
---|
| 990 | fputs(cs, pimg->fh); |
---|
| 991 | } |
---|
[0c50ce3] | 992 | PUTC('\n', pimg->fh); |
---|
| 993 | |
---|
[a420b49] | 994 | tm = time(NULL); |
---|
| 995 | if (tm == (time_t)-1) { |
---|
| 996 | fputsnl(TIMENA, pimg->fh); |
---|
[b189252] | 997 | } else if (pimg->version <= 7) { |
---|
| 998 | char date[256]; |
---|
| 999 | /* output current date and time in format specified */ |
---|
| 1000 | strftime(date, 256, TIMEFMT, localtime(&tm)); |
---|
| 1001 | fputsnl(date, pimg->fh); |
---|
[a420b49] | 1002 | } else { |
---|
[42bd3576] | 1003 | fprintf(pimg->fh, "@%ld\n", (long)tm); |
---|
[a420b49] | 1004 | } |
---|
[73fea40] | 1005 | |
---|
| 1006 | if (pimg->version >= 8) { |
---|
| 1007 | /* Clear bit one in case anyone has been passing true for fBinary. */ |
---|
| 1008 | flags &=~ 1; |
---|
| 1009 | PUTC(flags, pimg->fh); |
---|
| 1010 | } |
---|
| 1011 | |
---|
[ee05463] | 1012 | #if 0 |
---|
| 1013 | if (img_output_version >= 5) { |
---|
| 1014 | static const unsigned char codelengths[32] = { |
---|
| 1015 | 4, 8, 8, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 1016 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
---|
| 1017 | }; |
---|
| 1018 | fwrite(codelengths, 32, 1, pimg->fh); |
---|
| 1019 | } |
---|
| 1020 | #endif |
---|
[f807d8bf] | 1021 | pimg->fRead = 0; /* writing to this file */ |
---|
[a420b49] | 1022 | img_errno = IMG_NONE; |
---|
[d69255f] | 1023 | |
---|
[b5a3219] | 1024 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[0ff23cb] | 1025 | pimg->label_buf[0] = '\0'; |
---|
[d69255f] | 1026 | pimg->label_len = 0; |
---|
| 1027 | |
---|
[1ee204e] | 1028 | #if IMG_API_VERSION == 0 |
---|
| 1029 | pimg->date1 = pimg->date2 = 0; |
---|
| 1030 | pimg->olddate1 = pimg->olddate2 = 0; |
---|
| 1031 | #else /* IMG_API_VERSION == 1 */ |
---|
| 1032 | pimg->days1 = pimg->days2 = -1; |
---|
| 1033 | pimg->olddays1 = pimg->olddays2 = -1; |
---|
| 1034 | #endif |
---|
[eb5aea0] | 1035 | pimg->style = pimg->oldstyle = img_STYLE_UNKNOWN; |
---|
[ee05463] | 1036 | |
---|
[62a841c] | 1037 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
[ee7af72] | 1038 | |
---|
[c61aa79] | 1039 | pimg->n_legs = 0; |
---|
| 1040 | pimg->length = 0.0; |
---|
| 1041 | pimg->E = pimg->H = pimg->V = 0.0; |
---|
| 1042 | |
---|
[ad9a5cd] | 1043 | /* Don't check for write errors now - let img_close() report them... */ |
---|
[a420b49] | 1044 | return pimg; |
---|
[d1b1380] | 1045 | } |
---|
| 1046 | |
---|
[f1c7c9c7] | 1047 | static void |
---|
| 1048 | read_xyz_station_coords(img_point *pt, const char *line) |
---|
| 1049 | { |
---|
| 1050 | char num[12]; |
---|
| 1051 | memcpy(num, line + 6, 9); |
---|
| 1052 | num[9] = '\0'; |
---|
| 1053 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 1054 | memcpy(num, line + 15, 9); |
---|
| 1055 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 1056 | memcpy(num, line + 24, 8); |
---|
| 1057 | num[8] = '\0'; |
---|
| 1058 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 1059 | } |
---|
| 1060 | |
---|
| 1061 | static void |
---|
| 1062 | read_xyz_shot_coords(img_point *pt, const char *line) |
---|
| 1063 | { |
---|
| 1064 | char num[12]; |
---|
| 1065 | memcpy(num, line + 40, 10); |
---|
| 1066 | num[10] = '\0'; |
---|
| 1067 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 1068 | memcpy(num, line + 50, 10); |
---|
| 1069 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 1070 | memcpy(num, line + 60, 9); |
---|
| 1071 | num[9] = '\0'; |
---|
| 1072 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 1073 | } |
---|
| 1074 | |
---|
| 1075 | static void |
---|
| 1076 | subtract_xyz_shot_deltas(img_point *pt, const char *line) |
---|
| 1077 | { |
---|
| 1078 | char num[12]; |
---|
| 1079 | memcpy(num, line + 15, 9); |
---|
| 1080 | num[9] = '\0'; |
---|
| 1081 | pt->x -= atof(num) / METRES_PER_FOOT; |
---|
| 1082 | memcpy(num, line + 24, 8); |
---|
| 1083 | num[8] = '\0'; |
---|
| 1084 | pt->y -= atof(num) / METRES_PER_FOOT; |
---|
| 1085 | memcpy(num, line + 32, 8); |
---|
| 1086 | pt->z -= atof(num) / METRES_PER_FOOT; |
---|
| 1087 | } |
---|
| 1088 | |
---|
[ad9a5cd] | 1089 | static int |
---|
| 1090 | read_coord(FILE *fh, img_point *pt) |
---|
| 1091 | { |
---|
[4c07c51] | 1092 | SVX_ASSERT(fh); |
---|
| 1093 | SVX_ASSERT(pt); |
---|
[ad9a5cd] | 1094 | pt->x = get32(fh) / 100.0; |
---|
| 1095 | pt->y = get32(fh) / 100.0; |
---|
| 1096 | pt->z = get32(fh) / 100.0; |
---|
| 1097 | if (ferror(fh) || feof(fh)) { |
---|
| 1098 | img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1099 | return 0; |
---|
[421b7d2] | 1100 | } |
---|
[ad9a5cd] | 1101 | return 1; |
---|
| 1102 | } |
---|
| 1103 | |
---|
[4d3dfdf] | 1104 | static int |
---|
| 1105 | skip_coord(FILE *fh) |
---|
| 1106 | { |
---|
| 1107 | return (fseek(fh, 12, SEEK_CUR) == 0); |
---|
| 1108 | } |
---|
| 1109 | |
---|
[ee05463] | 1110 | static int |
---|
| 1111 | read_v3label(img *pimg) |
---|
| 1112 | { |
---|
| 1113 | char *q; |
---|
[e02a6a6] | 1114 | long len = GETC(pimg->fh); |
---|
[ee05463] | 1115 | if (len == EOF) { |
---|
| 1116 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1117 | return img_BAD; |
---|
| 1118 | } |
---|
| 1119 | if (len == 0xfe) { |
---|
| 1120 | len += get16(pimg->fh); |
---|
| 1121 | if (feof(pimg->fh)) { |
---|
| 1122 | img_errno = IMG_BADFORMAT; |
---|
| 1123 | return img_BAD; |
---|
| 1124 | } |
---|
| 1125 | if (ferror(pimg->fh)) { |
---|
| 1126 | img_errno = IMG_READERROR; |
---|
| 1127 | return img_BAD; |
---|
| 1128 | } |
---|
| 1129 | } else if (len == 0xff) { |
---|
| 1130 | len = get32(pimg->fh); |
---|
| 1131 | if (ferror(pimg->fh)) { |
---|
| 1132 | img_errno = IMG_READERROR; |
---|
| 1133 | return img_BAD; |
---|
| 1134 | } |
---|
| 1135 | if (feof(pimg->fh) || len < 0xfe + 0xffff) { |
---|
| 1136 | img_errno = IMG_BADFORMAT; |
---|
| 1137 | return img_BAD; |
---|
| 1138 | } |
---|
| 1139 | } |
---|
| 1140 | |
---|
| 1141 | if (!check_label_space(pimg, pimg->label_len + len + 1)) { |
---|
| 1142 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1143 | return img_BAD; |
---|
| 1144 | } |
---|
| 1145 | q = pimg->label_buf + pimg->label_len; |
---|
| 1146 | pimg->label_len += len; |
---|
| 1147 | if (len && fread(q, len, 1, pimg->fh) != 1) { |
---|
| 1148 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1149 | return img_BAD; |
---|
| 1150 | } |
---|
| 1151 | q[len] = '\0'; |
---|
| 1152 | return 0; |
---|
| 1153 | } |
---|
| 1154 | |
---|
[dec6b10] | 1155 | static int |
---|
[e133186] | 1156 | read_v8label(img *pimg, int common_flag, size_t common_val) |
---|
[dec6b10] | 1157 | { |
---|
| 1158 | char *q; |
---|
| 1159 | size_t del, add; |
---|
[e133186] | 1160 | if (common_flag) { |
---|
| 1161 | if (common_val == 0) return 0; |
---|
| 1162 | add = del = common_val; |
---|
[dec6b10] | 1163 | } else { |
---|
[e133186] | 1164 | int ch = GETC(pimg->fh); |
---|
[dec6b10] | 1165 | if (ch == EOF) { |
---|
| 1166 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1167 | return img_BAD; |
---|
| 1168 | } |
---|
[e133186] | 1169 | if (ch != 0x00) { |
---|
| 1170 | del = ch >> 4; |
---|
| 1171 | add = ch & 0x0f; |
---|
[dec6b10] | 1172 | } else { |
---|
[e133186] | 1173 | ch = GETC(pimg->fh); |
---|
| 1174 | if (ch == EOF) { |
---|
| 1175 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[dec6b10] | 1176 | return img_BAD; |
---|
| 1177 | } |
---|
[e133186] | 1178 | if (ch != 0xff) { |
---|
| 1179 | del = ch; |
---|
| 1180 | } else { |
---|
| 1181 | del = get32(pimg->fh); |
---|
| 1182 | if (ferror(pimg->fh)) { |
---|
| 1183 | img_errno = IMG_READERROR; |
---|
| 1184 | return img_BAD; |
---|
| 1185 | } |
---|
| 1186 | } |
---|
| 1187 | ch = GETC(pimg->fh); |
---|
| 1188 | if (ch == EOF) { |
---|
| 1189 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[dec6b10] | 1190 | return img_BAD; |
---|
| 1191 | } |
---|
[e133186] | 1192 | if (ch != 0xff) { |
---|
| 1193 | add = ch; |
---|
| 1194 | } else { |
---|
| 1195 | add = get32(pimg->fh); |
---|
| 1196 | if (ferror(pimg->fh)) { |
---|
| 1197 | img_errno = IMG_READERROR; |
---|
| 1198 | return img_BAD; |
---|
| 1199 | } |
---|
| 1200 | } |
---|
[dec6b10] | 1201 | } |
---|
| 1202 | |
---|
[e133186] | 1203 | if (add > del && !check_label_space(pimg, pimg->label_len + add - del + 1)) { |
---|
| 1204 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1205 | return img_BAD; |
---|
| 1206 | } |
---|
[dec6b10] | 1207 | } |
---|
| 1208 | if (del > pimg->label_len) { |
---|
| 1209 | img_errno = IMG_BADFORMAT; |
---|
| 1210 | return img_BAD; |
---|
| 1211 | } |
---|
| 1212 | pimg->label_len -= del; |
---|
| 1213 | q = pimg->label_buf + pimg->label_len; |
---|
| 1214 | pimg->label_len += add; |
---|
| 1215 | if (add && fread(q, add, 1, pimg->fh) != 1) { |
---|
| 1216 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1217 | return img_BAD; |
---|
| 1218 | } |
---|
| 1219 | q[add] = '\0'; |
---|
| 1220 | return 0; |
---|
| 1221 | } |
---|
| 1222 | |
---|
| 1223 | static int img_read_item_new(img *pimg, img_point *p); |
---|
| 1224 | static int img_read_item_v3to7(img *pimg, img_point *p); |
---|
| 1225 | static int img_read_item_ancient(img *pimg, img_point *p); |
---|
| 1226 | static int img_read_item_ascii_wrapper(img *pimg, img_point *p); |
---|
[898bc69] | 1227 | static int img_read_item_ascii(img *pimg, img_point *p); |
---|
| 1228 | |
---|
[a420b49] | 1229 | int |
---|
[23f7ea7] | 1230 | img_read_item(img *pimg, img_point *p) |
---|
[a420b49] | 1231 | { |
---|
[95c3272] | 1232 | pimg->flags = 0; |
---|
[d69255f] | 1233 | |
---|
[dec6b10] | 1234 | if (pimg->version >= 8) { |
---|
| 1235 | return img_read_item_new(pimg, p); |
---|
| 1236 | } else if (pimg->version >= 3) { |
---|
| 1237 | return img_read_item_v3to7(pimg, p); |
---|
| 1238 | } else if (pimg->version >= 1) { |
---|
| 1239 | return img_read_item_ancient(pimg, p); |
---|
| 1240 | } else { |
---|
| 1241 | return img_read_item_ascii_wrapper(pimg, p); |
---|
| 1242 | } |
---|
| 1243 | } |
---|
| 1244 | |
---|
[2adff93] | 1245 | static int |
---|
[dec6b10] | 1246 | img_read_item_new(img *pimg, img_point *p) |
---|
| 1247 | { |
---|
| 1248 | int result; |
---|
| 1249 | int opt; |
---|
| 1250 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
| 1251 | if (pimg->pending >= 0x40) { |
---|
[ded184e2] | 1252 | if (pimg->pending == 256) { |
---|
| 1253 | pimg->pending = 0; |
---|
| 1254 | return img_XSECT_END; |
---|
| 1255 | } |
---|
[dec6b10] | 1256 | *p = pimg->mv; |
---|
| 1257 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 1258 | pimg->pending = 0; |
---|
| 1259 | return img_LINE; |
---|
| 1260 | } |
---|
| 1261 | again3: /* label to goto if we get a prefix, date, or lrud */ |
---|
| 1262 | pimg->label = pimg->label_buf; |
---|
| 1263 | opt = GETC(pimg->fh); |
---|
| 1264 | if (opt == EOF) { |
---|
| 1265 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1266 | return img_BAD; |
---|
| 1267 | } |
---|
| 1268 | if (opt >> 6 == 0) { |
---|
| 1269 | if (opt <= 4) { |
---|
| 1270 | if (opt == 0 && pimg->style == 0) |
---|
| 1271 | return img_STOP; /* end of data marker */ |
---|
| 1272 | /* STYLE */ |
---|
| 1273 | pimg->style = opt; |
---|
| 1274 | goto again3; |
---|
[76bbb7c9] | 1275 | } |
---|
[22114ef] | 1276 | if (opt >= 0x10) { |
---|
[dec6b10] | 1277 | switch (opt) { |
---|
[22114ef] | 1278 | case 0x10: { /* No date info */ |
---|
| 1279 | #if IMG_API_VERSION == 0 |
---|
| 1280 | pimg->date1 = pimg->date2 = 0; |
---|
| 1281 | #else /* IMG_API_VERSION == 1 */ |
---|
| 1282 | pimg->days1 = pimg->days2 = -1; |
---|
| 1283 | #endif |
---|
| 1284 | break; |
---|
| 1285 | } |
---|
| 1286 | case 0x11: { /* Single date */ |
---|
[dec6b10] | 1287 | int days1 = (int)getu16(pimg->fh); |
---|
| 1288 | #if IMG_API_VERSION == 0 |
---|
| 1289 | pimg->date2 = pimg->date1 = (days1 - 25567) * 86400; |
---|
| 1290 | #else /* IMG_API_VERSION == 1 */ |
---|
| 1291 | pimg->days2 = pimg->days1 = days1; |
---|
| 1292 | #endif |
---|
| 1293 | break; |
---|
| 1294 | } |
---|
[22114ef] | 1295 | case 0x12: { /* Date range (short) */ |
---|
[dec6b10] | 1296 | int days1 = (int)getu16(pimg->fh); |
---|
| 1297 | int days2 = days1 + GETC(pimg->fh) + 1; |
---|
| 1298 | #if IMG_API_VERSION == 0 |
---|
| 1299 | pimg->date1 = (days1 - 25567) * 86400; |
---|
| 1300 | pimg->date2 = (days2 - 25567) * 86400; |
---|
| 1301 | #else /* IMG_API_VERSION == 1 */ |
---|
| 1302 | pimg->days1 = days1; |
---|
| 1303 | pimg->days2 = days2; |
---|
| 1304 | #endif |
---|
| 1305 | break; |
---|
| 1306 | } |
---|
[22114ef] | 1307 | case 0x13: { /* Date range (long) */ |
---|
[dec6b10] | 1308 | int days1 = (int)getu16(pimg->fh); |
---|
| 1309 | int days2 = (int)getu16(pimg->fh); |
---|
| 1310 | #if IMG_API_VERSION == 0 |
---|
| 1311 | pimg->date1 = (days1 - 25567) * 86400; |
---|
| 1312 | pimg->date2 = (days2 - 25567) * 86400; |
---|
| 1313 | #else /* IMG_API_VERSION == 1 */ |
---|
| 1314 | pimg->days1 = days1; |
---|
| 1315 | pimg->days2 = days2; |
---|
| 1316 | #endif |
---|
| 1317 | break; |
---|
| 1318 | } |
---|
[22114ef] | 1319 | case 0x1f: /* Error info */ |
---|
| 1320 | pimg->n_legs = get32(pimg->fh); |
---|
| 1321 | pimg->length = get32(pimg->fh) / 100.0; |
---|
| 1322 | pimg->E = get32(pimg->fh) / 100.0; |
---|
| 1323 | pimg->H = get32(pimg->fh) / 100.0; |
---|
| 1324 | pimg->V = get32(pimg->fh) / 100.0; |
---|
| 1325 | return img_ERROR_INFO; |
---|
[dec6b10] | 1326 | case 0x30: case 0x31: /* LRUD */ |
---|
| 1327 | case 0x32: case 0x33: /* Big LRUD! */ |
---|
[e133186] | 1328 | if (read_v8label(pimg, 0, 0) == img_BAD) return img_BAD; |
---|
[dec6b10] | 1329 | pimg->flags = (int)opt & 0x01; |
---|
| 1330 | if (opt < 0x32) { |
---|
| 1331 | pimg->l = get16(pimg->fh) / 100.0; |
---|
| 1332 | pimg->r = get16(pimg->fh) / 100.0; |
---|
| 1333 | pimg->u = get16(pimg->fh) / 100.0; |
---|
| 1334 | pimg->d = get16(pimg->fh) / 100.0; |
---|
| 1335 | } else { |
---|
| 1336 | pimg->l = get32(pimg->fh) / 100.0; |
---|
| 1337 | pimg->r = get32(pimg->fh) / 100.0; |
---|
| 1338 | pimg->u = get32(pimg->fh) / 100.0; |
---|
| 1339 | pimg->d = get32(pimg->fh) / 100.0; |
---|
| 1340 | } |
---|
[4139b12] | 1341 | if (!stn_included(pimg)) { |
---|
| 1342 | return img_XSECT_END; |
---|
[dec6b10] | 1343 | } |
---|
| 1344 | /* If this is the last cross-section in this passage, set |
---|
| 1345 | * pending so we return img_XSECT_END next time. */ |
---|
| 1346 | if (pimg->flags & 0x01) { |
---|
| 1347 | pimg->pending = 256; |
---|
| 1348 | pimg->flags &= ~0x01; |
---|
| 1349 | } |
---|
| 1350 | return img_XSECT; |
---|
| 1351 | default: /* 0x25 - 0x2f and 0x34 - 0x3f are currently unallocated. */ |
---|
| 1352 | img_errno = IMG_BADFORMAT; |
---|
| 1353 | return img_BAD; |
---|
| 1354 | } |
---|
| 1355 | goto again3; |
---|
| 1356 | } |
---|
| 1357 | if (opt != 15) { |
---|
| 1358 | /* 1-14 and 16-31 reserved */ |
---|
| 1359 | img_errno = IMG_BADFORMAT; |
---|
[ad9a5cd] | 1360 | return img_BAD; |
---|
| 1361 | } |
---|
[dec6b10] | 1362 | result = img_MOVE; |
---|
| 1363 | } else if (opt >= 0x80) { |
---|
[e133186] | 1364 | if (read_v8label(pimg, 0, 0) == img_BAD) return img_BAD; |
---|
[dec6b10] | 1365 | |
---|
| 1366 | result = img_LABEL; |
---|
| 1367 | |
---|
[4139b12] | 1368 | if (!stn_included(pimg)) { |
---|
| 1369 | if (!skip_coord(pimg->fh)) return img_BAD; |
---|
| 1370 | pimg->pending = 0; |
---|
| 1371 | goto again3; |
---|
[dec6b10] | 1372 | } |
---|
| 1373 | |
---|
| 1374 | pimg->flags = (int)opt & 0x7f; |
---|
| 1375 | } else if ((opt >> 6) == 1) { |
---|
[e133186] | 1376 | if (read_v8label(pimg, opt & 0x20, 0) == img_BAD) return img_BAD; |
---|
[dec6b10] | 1377 | |
---|
| 1378 | result = img_LINE; |
---|
| 1379 | |
---|
[4139b12] | 1380 | if (!survey_included(pimg)) { |
---|
| 1381 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
| 1382 | pimg->pending = 15; |
---|
| 1383 | goto again3; |
---|
[dec6b10] | 1384 | } |
---|
| 1385 | |
---|
| 1386 | if (pimg->pending) { |
---|
| 1387 | *p = pimg->mv; |
---|
| 1388 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
| 1389 | pimg->pending = opt; |
---|
| 1390 | return img_MOVE; |
---|
| 1391 | } |
---|
[900b32a] | 1392 | pimg->flags = (int)opt & 0x1f; |
---|
[dec6b10] | 1393 | } else { |
---|
| 1394 | img_errno = IMG_BADFORMAT; |
---|
| 1395 | return img_BAD; |
---|
| 1396 | } |
---|
| 1397 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
| 1398 | pimg->pending = 0; |
---|
| 1399 | return result; |
---|
| 1400 | } |
---|
| 1401 | |
---|
[2adff93] | 1402 | static int |
---|
[dec6b10] | 1403 | img_read_item_v3to7(img *pimg, img_point *p) |
---|
| 1404 | { |
---|
| 1405 | int result; |
---|
| 1406 | int opt; |
---|
| 1407 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
| 1408 | if (pimg->pending == 256) { |
---|
| 1409 | pimg->pending = 0; |
---|
| 1410 | return img_XSECT_END; |
---|
| 1411 | } |
---|
| 1412 | if (pimg->pending >= 0x80) { |
---|
| 1413 | *p = pimg->mv; |
---|
| 1414 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 1415 | pimg->pending = 0; |
---|
| 1416 | return img_LINE; |
---|
| 1417 | } |
---|
| 1418 | again3: /* label to goto if we get a prefix, date, or lrud */ |
---|
| 1419 | pimg->label = pimg->label_buf; |
---|
| 1420 | opt = GETC(pimg->fh); |
---|
| 1421 | if (opt == EOF) { |
---|
| 1422 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1423 | return img_BAD; |
---|
| 1424 | } |
---|
| 1425 | switch (opt >> 6) { |
---|
| 1426 | case 0: |
---|
| 1427 | if (opt == 0) { |
---|
| 1428 | if (!pimg->label_len) return img_STOP; /* end of data marker */ |
---|
| 1429 | pimg->label_len = 0; |
---|
| 1430 | goto again3; |
---|
| 1431 | } |
---|
| 1432 | if (opt < 15) { |
---|
| 1433 | /* 1-14 mean trim that many levels from current prefix */ |
---|
| 1434 | int c; |
---|
| 1435 | if (pimg->label_len <= 17) { |
---|
| 1436 | /* zero prefix using "0" */ |
---|
| 1437 | img_errno = IMG_BADFORMAT; |
---|
| 1438 | return img_BAD; |
---|
| 1439 | } |
---|
| 1440 | /* extra - 1 because label_len points to one past the end */ |
---|
| 1441 | c = pimg->label_len - 17 - 1; |
---|
| 1442 | while (pimg->label_buf[c] != '.' || --opt > 0) { |
---|
| 1443 | if (--c < 0) { |
---|
[4d3dfdf] | 1444 | /* zero prefix using "0" */ |
---|
[d69255f] | 1445 | img_errno = IMG_BADFORMAT; |
---|
| 1446 | return img_BAD; |
---|
| 1447 | } |
---|
| 1448 | } |
---|
[dec6b10] | 1449 | c++; |
---|
| 1450 | pimg->label_len = c; |
---|
| 1451 | goto again3; |
---|
| 1452 | } |
---|
| 1453 | if (opt == 15) { |
---|
| 1454 | result = img_MOVE; |
---|
| 1455 | break; |
---|
| 1456 | } |
---|
| 1457 | if (opt >= 0x20) { |
---|
| 1458 | switch (opt) { |
---|
| 1459 | case 0x20: /* Single date */ |
---|
| 1460 | if (pimg->version < 7) { |
---|
| 1461 | int date1 = get32(pimg->fh); |
---|
[1ee204e] | 1462 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1463 | pimg->date2 = pimg->date1 = date1; |
---|
[1ee204e] | 1464 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1465 | if (date1 != 0) { |
---|
| 1466 | pimg->days2 = pimg->days1 = (date1 / 86400) + 25567; |
---|
| 1467 | } else { |
---|
| 1468 | pimg->days2 = pimg->days1 = -1; |
---|
| 1469 | } |
---|
[1ee204e] | 1470 | #endif |
---|
[dec6b10] | 1471 | } else { |
---|
| 1472 | int days1 = (int)getu16(pimg->fh); |
---|
[1ee204e] | 1473 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1474 | pimg->date2 = pimg->date1 = (days1 - 25567) * 86400; |
---|
[1ee204e] | 1475 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1476 | pimg->days2 = pimg->days1 = days1; |
---|
[1ee204e] | 1477 | #endif |
---|
[dec6b10] | 1478 | } |
---|
| 1479 | break; |
---|
| 1480 | case 0x21: /* Date range (short for v7+) */ |
---|
| 1481 | if (pimg->version < 7) { |
---|
| 1482 | INT32_T date1 = get32(pimg->fh); |
---|
| 1483 | INT32_T date2 = get32(pimg->fh); |
---|
[1ee204e] | 1484 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1485 | pimg->date1 = date1; |
---|
| 1486 | pimg->date2 = date2; |
---|
[1ee204e] | 1487 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1488 | pimg->days1 = (date1 / 86400) + 25567; |
---|
| 1489 | pimg->days2 = (date2 / 86400) + 25567; |
---|
[1ee204e] | 1490 | #endif |
---|
[dec6b10] | 1491 | } else { |
---|
| 1492 | int days1 = (int)getu16(pimg->fh); |
---|
| 1493 | int days2 = days1 + GETC(pimg->fh) + 1; |
---|
[1ee204e] | 1494 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1495 | pimg->date1 = (days1 - 25567) * 86400; |
---|
| 1496 | pimg->date2 = (days2 - 25567) * 86400; |
---|
[1ee204e] | 1497 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1498 | pimg->days1 = days1; |
---|
| 1499 | pimg->days2 = days2; |
---|
[1ee204e] | 1500 | #endif |
---|
[dec6b10] | 1501 | } |
---|
| 1502 | break; |
---|
| 1503 | case 0x22: /* Error info */ |
---|
| 1504 | pimg->n_legs = get32(pimg->fh); |
---|
| 1505 | pimg->length = get32(pimg->fh) / 100.0; |
---|
| 1506 | pimg->E = get32(pimg->fh) / 100.0; |
---|
| 1507 | pimg->H = get32(pimg->fh) / 100.0; |
---|
| 1508 | pimg->V = get32(pimg->fh) / 100.0; |
---|
[a4454620] | 1509 | if (feof(pimg->fh)) { |
---|
| 1510 | img_errno = IMG_BADFORMAT; |
---|
| 1511 | return img_BAD; |
---|
| 1512 | } |
---|
| 1513 | if (ferror(pimg->fh)) { |
---|
| 1514 | img_errno = IMG_READERROR; |
---|
| 1515 | return img_BAD; |
---|
| 1516 | } |
---|
[dec6b10] | 1517 | return img_ERROR_INFO; |
---|
| 1518 | case 0x23: { /* v7+: Date range (long) */ |
---|
| 1519 | if (pimg->version < 7) { |
---|
| 1520 | img_errno = IMG_BADFORMAT; |
---|
| 1521 | return img_BAD; |
---|
| 1522 | } |
---|
| 1523 | int days1 = (int)getu16(pimg->fh); |
---|
| 1524 | int days2 = (int)getu16(pimg->fh); |
---|
[a4454620] | 1525 | if (feof(pimg->fh)) { |
---|
| 1526 | img_errno = IMG_BADFORMAT; |
---|
| 1527 | return img_BAD; |
---|
| 1528 | } |
---|
| 1529 | if (ferror(pimg->fh)) { |
---|
| 1530 | img_errno = IMG_READERROR; |
---|
| 1531 | return img_BAD; |
---|
| 1532 | } |
---|
[1ee204e] | 1533 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1534 | pimg->date1 = (days1 - 25567) * 86400; |
---|
| 1535 | pimg->date2 = (days2 - 25567) * 86400; |
---|
[1ee204e] | 1536 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1537 | pimg->days1 = days1; |
---|
| 1538 | pimg->days2 = days2; |
---|
[1ee204e] | 1539 | #endif |
---|
[dec6b10] | 1540 | break; |
---|
| 1541 | } |
---|
| 1542 | case 0x24: { /* v7+: No date info */ |
---|
[1ee204e] | 1543 | #if IMG_API_VERSION == 0 |
---|
[dec6b10] | 1544 | pimg->date1 = pimg->date2 = 0; |
---|
[1ee204e] | 1545 | #else /* IMG_API_VERSION == 1 */ |
---|
[dec6b10] | 1546 | pimg->days1 = pimg->days2 = -1; |
---|
[1ee204e] | 1547 | #endif |
---|
[dec6b10] | 1548 | break; |
---|
| 1549 | } |
---|
| 1550 | case 0x30: case 0x31: /* LRUD */ |
---|
| 1551 | case 0x32: case 0x33: /* Big LRUD! */ |
---|
| 1552 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
| 1553 | pimg->flags = (int)opt & 0x01; |
---|
| 1554 | if (opt < 0x32) { |
---|
| 1555 | pimg->l = get16(pimg->fh) / 100.0; |
---|
| 1556 | pimg->r = get16(pimg->fh) / 100.0; |
---|
| 1557 | pimg->u = get16(pimg->fh) / 100.0; |
---|
| 1558 | pimg->d = get16(pimg->fh) / 100.0; |
---|
| 1559 | } else { |
---|
| 1560 | pimg->l = get32(pimg->fh) / 100.0; |
---|
| 1561 | pimg->r = get32(pimg->fh) / 100.0; |
---|
| 1562 | pimg->u = get32(pimg->fh) / 100.0; |
---|
| 1563 | pimg->d = get32(pimg->fh) / 100.0; |
---|
| 1564 | } |
---|
[a4454620] | 1565 | if (feof(pimg->fh)) { |
---|
| 1566 | img_errno = IMG_BADFORMAT; |
---|
| 1567 | return img_BAD; |
---|
| 1568 | } |
---|
| 1569 | if (ferror(pimg->fh)) { |
---|
| 1570 | img_errno = IMG_READERROR; |
---|
| 1571 | return img_BAD; |
---|
| 1572 | } |
---|
[4139b12] | 1573 | if (!stn_included(pimg)) { |
---|
| 1574 | return img_XSECT_END; |
---|
[dec6b10] | 1575 | } |
---|
| 1576 | /* If this is the last cross-section in this passage, set |
---|
| 1577 | * pending so we return img_XSECT_END next time. */ |
---|
| 1578 | if (pimg->flags & 0x01) { |
---|
| 1579 | pimg->pending = 256; |
---|
| 1580 | pimg->flags &= ~0x01; |
---|
| 1581 | } |
---|
| 1582 | return img_XSECT; |
---|
| 1583 | default: /* 0x25 - 0x2f and 0x34 - 0x3f are currently unallocated. */ |
---|
| 1584 | img_errno = IMG_BADFORMAT; |
---|
| 1585 | return img_BAD; |
---|
| 1586 | } |
---|
[a4454620] | 1587 | if (feof(pimg->fh)) { |
---|
| 1588 | img_errno = IMG_BADFORMAT; |
---|
| 1589 | return img_BAD; |
---|
| 1590 | } |
---|
| 1591 | if (ferror(pimg->fh)) { |
---|
| 1592 | img_errno = IMG_READERROR; |
---|
| 1593 | return img_BAD; |
---|
| 1594 | } |
---|
[dec6b10] | 1595 | goto again3; |
---|
| 1596 | } |
---|
| 1597 | /* 16-31 mean remove (n - 15) characters from the prefix */ |
---|
| 1598 | /* zero prefix using 0 */ |
---|
| 1599 | if (pimg->label_len <= (size_t)(opt - 15)) { |
---|
| 1600 | img_errno = IMG_BADFORMAT; |
---|
| 1601 | return img_BAD; |
---|
| 1602 | } |
---|
| 1603 | pimg->label_len -= (opt - 15); |
---|
| 1604 | goto again3; |
---|
| 1605 | case 1: |
---|
| 1606 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
[ee05463] | 1607 | |
---|
[dec6b10] | 1608 | result = img_LABEL; |
---|
[ee05463] | 1609 | |
---|
[4139b12] | 1610 | if (!stn_included(pimg)) { |
---|
| 1611 | if (!skip_coord(pimg->fh)) return img_BAD; |
---|
| 1612 | pimg->pending = 0; |
---|
| 1613 | goto again3; |
---|
[dec6b10] | 1614 | } |
---|
[d69255f] | 1615 | |
---|
[dec6b10] | 1616 | pimg->flags = (int)opt & 0x3f; |
---|
| 1617 | break; |
---|
| 1618 | case 2: |
---|
| 1619 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
[76bbb7c9] | 1620 | |
---|
[dec6b10] | 1621 | result = img_LINE; |
---|
[76bbb7c9] | 1622 | |
---|
[4139b12] | 1623 | if (!survey_included(pimg)) { |
---|
| 1624 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
| 1625 | pimg->pending = 15; |
---|
| 1626 | goto again3; |
---|
[d69255f] | 1627 | } |
---|
[dec6b10] | 1628 | |
---|
| 1629 | if (pimg->pending) { |
---|
| 1630 | *p = pimg->mv; |
---|
| 1631 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
| 1632 | pimg->pending = opt; |
---|
| 1633 | return img_MOVE; |
---|
| 1634 | } |
---|
| 1635 | pimg->flags = (int)opt & 0x3f; |
---|
| 1636 | break; |
---|
| 1637 | default: |
---|
| 1638 | img_errno = IMG_BADFORMAT; |
---|
| 1639 | return img_BAD; |
---|
[4d3dfdf] | 1640 | } |
---|
[dec6b10] | 1641 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
| 1642 | pimg->pending = 0; |
---|
| 1643 | return result; |
---|
| 1644 | } |
---|
| 1645 | |
---|
[2adff93] | 1646 | static int |
---|
[dec6b10] | 1647 | img_read_item_ancient(img *pimg, img_point *p) |
---|
| 1648 | { |
---|
| 1649 | int result; |
---|
| 1650 | static long opt_lookahead = 0; |
---|
| 1651 | static img_point pt = { 0.0, 0.0, 0.0 }; |
---|
| 1652 | long opt; |
---|
[4d3dfdf] | 1653 | |
---|
[dec6b10] | 1654 | again: /* label to goto if we get a cross */ |
---|
[15e50b0] | 1655 | pimg->label = pimg->label_buf; |
---|
[dec6b10] | 1656 | pimg->label[0] = '\0'; |
---|
[ad9a5cd] | 1657 | |
---|
[dec6b10] | 1658 | if (pimg->version == 1) { |
---|
| 1659 | if (opt_lookahead) { |
---|
| 1660 | opt = opt_lookahead; |
---|
| 1661 | opt_lookahead = 0; |
---|
[5c3c61a] | 1662 | } else { |
---|
[dec6b10] | 1663 | opt = get32(pimg->fh); |
---|
| 1664 | } |
---|
| 1665 | } else { |
---|
| 1666 | opt = GETC(pimg->fh); |
---|
| 1667 | } |
---|
| 1668 | |
---|
| 1669 | if (feof(pimg->fh)) { |
---|
| 1670 | img_errno = IMG_BADFORMAT; |
---|
| 1671 | return img_BAD; |
---|
| 1672 | } |
---|
| 1673 | if (ferror(pimg->fh)) { |
---|
| 1674 | img_errno = IMG_READERROR; |
---|
| 1675 | return img_BAD; |
---|
| 1676 | } |
---|
| 1677 | |
---|
| 1678 | switch (opt) { |
---|
| 1679 | case -1: case 0: |
---|
| 1680 | return img_STOP; /* end of data marker */ |
---|
| 1681 | case 1: |
---|
| 1682 | /* skip coordinates */ |
---|
| 1683 | if (!skip_coord(pimg->fh)) { |
---|
| 1684 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1685 | return img_BAD; |
---|
| 1686 | } |
---|
| 1687 | goto again; |
---|
| 1688 | case 2: case 3: { |
---|
[15e50b0] | 1689 | size_t len; |
---|
[dec6b10] | 1690 | result = img_LABEL; |
---|
[15e50b0] | 1691 | if (!fgets(pimg->label_buf, pimg->buf_len, pimg->fh)) { |
---|
[dec6b10] | 1692 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1693 | return img_BAD; |
---|
| 1694 | } |
---|
[15e50b0] | 1695 | if (pimg->label[0] == '\\') pimg->label++; |
---|
| 1696 | len = strlen(pimg->label); |
---|
| 1697 | if (len == 0 || pimg->label[len - 1] != '\n') { |
---|
[dec6b10] | 1698 | img_errno = IMG_BADFORMAT; |
---|
| 1699 | return img_BAD; |
---|
| 1700 | } |
---|
| 1701 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
[15e50b0] | 1702 | if (len == 1) goto again; |
---|
| 1703 | pimg->label[len - 1] = '\0'; |
---|
[dec6b10] | 1704 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
| 1705 | if (opt == 2) goto done; |
---|
| 1706 | break; |
---|
| 1707 | } |
---|
| 1708 | case 6: case 7: { |
---|
| 1709 | long len; |
---|
| 1710 | result = img_LABEL; |
---|
| 1711 | |
---|
| 1712 | if (opt == 7) |
---|
| 1713 | pimg->flags = GETC(pimg->fh); |
---|
| 1714 | else |
---|
| 1715 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
| 1716 | |
---|
| 1717 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 1718 | |
---|
| 1719 | if (feof(pimg->fh)) { |
---|
| 1720 | img_errno = IMG_BADFORMAT; |
---|
| 1721 | return img_BAD; |
---|
| 1722 | } |
---|
| 1723 | if (ferror(pimg->fh)) { |
---|
| 1724 | img_errno = IMG_READERROR; |
---|
| 1725 | return img_BAD; |
---|
| 1726 | } |
---|
| 1727 | |
---|
[dec6b10] | 1728 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
| 1729 | if (len == 0) goto again; |
---|
| 1730 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1731 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1732 | return img_BAD; |
---|
| 1733 | } |
---|
| 1734 | if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) { |
---|
| 1735 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1736 | return img_BAD; |
---|
| 1737 | } |
---|
| 1738 | pimg->label_buf[len] = '\0'; |
---|
| 1739 | break; |
---|
| 1740 | } |
---|
| 1741 | case 4: |
---|
| 1742 | result = img_MOVE; |
---|
| 1743 | break; |
---|
| 1744 | case 5: |
---|
| 1745 | result = img_LINE; |
---|
| 1746 | break; |
---|
| 1747 | default: |
---|
| 1748 | switch ((int)opt & 0xc0) { |
---|
| 1749 | case 0x80: |
---|
| 1750 | pimg->flags = (int)opt & 0x3f; |
---|
| 1751 | result = img_LINE; |
---|
| 1752 | break; |
---|
| 1753 | case 0x40: { |
---|
[fe8e80e] | 1754 | char *q; |
---|
[dec6b10] | 1755 | pimg->flags = (int)opt & 0x3f; |
---|
[437caf3] | 1756 | result = img_LABEL; |
---|
[ba0f22b] | 1757 | if (!fgets(pimg->label_buf, pimg->buf_len, pimg->fh)) { |
---|
[ad9a5cd] | 1758 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 1759 | return img_BAD; |
---|
| 1760 | } |
---|
[0ff23cb] | 1761 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[dec6b10] | 1762 | /* Ignore empty-labels in some .3d files (caused by a bug) */ |
---|
| 1763 | if (q == pimg->label_buf) goto again; |
---|
[d69255f] | 1764 | if (*q != '\n') { |
---|
| 1765 | img_errno = IMG_BADFORMAT; |
---|
| 1766 | return img_BAD; |
---|
| 1767 | } |
---|
[fe8e80e] | 1768 | *q = '\0'; |
---|
[437caf3] | 1769 | break; |
---|
[23f7ea7] | 1770 | } |
---|
[a420b49] | 1771 | default: |
---|
[dec6b10] | 1772 | img_errno = IMG_BADFORMAT; |
---|
| 1773 | return img_BAD; |
---|
[a420b49] | 1774 | } |
---|
[dec6b10] | 1775 | break; |
---|
| 1776 | } |
---|
[ad9a5cd] | 1777 | |
---|
[dec6b10] | 1778 | if (!read_coord(pimg->fh, &pt)) return img_BAD; |
---|
[76bbb7c9] | 1779 | |
---|
[4139b12] | 1780 | if (result == img_LABEL && !stn_included(pimg)) { |
---|
| 1781 | goto again; |
---|
[dec6b10] | 1782 | } |
---|
[76bbb7c9] | 1783 | |
---|
[dec6b10] | 1784 | done: |
---|
| 1785 | *p = pt; |
---|
[c4d4649] | 1786 | |
---|
[dec6b10] | 1787 | if (result == img_MOVE && pimg->version == 1) { |
---|
| 1788 | /* peek at next code and see if it's an old-style label */ |
---|
| 1789 | opt_lookahead = get32(pimg->fh); |
---|
[ad9a5cd] | 1790 | |
---|
[dec6b10] | 1791 | if (feof(pimg->fh)) { |
---|
| 1792 | img_errno = IMG_BADFORMAT; |
---|
| 1793 | return img_BAD; |
---|
| 1794 | } |
---|
| 1795 | if (ferror(pimg->fh)) { |
---|
| 1796 | img_errno = IMG_READERROR; |
---|
| 1797 | return img_BAD; |
---|
[c4d4649] | 1798 | } |
---|
[23f7ea7] | 1799 | |
---|
[a8638a6] | 1800 | if (opt_lookahead == 2) return img_read_item_ancient(pimg, p); |
---|
[898bc69] | 1801 | } |
---|
[dec6b10] | 1802 | |
---|
| 1803 | return result; |
---|
| 1804 | } |
---|
| 1805 | |
---|
| 1806 | static int |
---|
| 1807 | img_read_item_ascii_wrapper(img *pimg, img_point *p) |
---|
| 1808 | { |
---|
| 1809 | /* We need to set the default locale for fscanf() to work on |
---|
| 1810 | * numbers with "." as decimal point. */ |
---|
| 1811 | int result; |
---|
| 1812 | char * current_locale = my_strdup(setlocale(LC_NUMERIC, NULL)); |
---|
| 1813 | setlocale(LC_NUMERIC, "C"); |
---|
| 1814 | result = img_read_item_ascii(pimg, p); |
---|
| 1815 | setlocale(LC_NUMERIC, current_locale); |
---|
| 1816 | free(current_locale); |
---|
| 1817 | return result; |
---|
[898bc69] | 1818 | } |
---|
[eef68f9] | 1819 | |
---|
[dec6b10] | 1820 | /* Handle all ASCII formats. */ |
---|
[898bc69] | 1821 | static int |
---|
| 1822 | img_read_item_ascii(img *pimg, img_point *p) |
---|
| 1823 | { |
---|
| 1824 | int result; |
---|
[dec6b10] | 1825 | pimg->label = pimg->label_buf; |
---|
[898bc69] | 1826 | if (pimg->version == 0) { |
---|
[a420b49] | 1827 | ascii_again: |
---|
[5d7280f8] | 1828 | pimg->label[0] = '\0'; |
---|
[a420b49] | 1829 | if (feof(pimg->fh)) return img_STOP; |
---|
[76bbb7c9] | 1830 | if (pimg->pending) { |
---|
| 1831 | pimg->pending = 0; |
---|
[a420b49] | 1832 | result = img_LINE; |
---|
| 1833 | } else { |
---|
[7d27e36] | 1834 | char cmd[7]; |
---|
[a420b49] | 1835 | /* Stop if nothing found */ |
---|
[7d27e36] | 1836 | if (fscanf(pimg->fh, "%6s", cmd) < 1) return img_STOP; |
---|
| 1837 | if (strcmp(cmd, "move") == 0) |
---|
[a420b49] | 1838 | result = img_MOVE; |
---|
[7d27e36] | 1839 | else if (strcmp(cmd, "draw") == 0) |
---|
[a420b49] | 1840 | result = img_LINE; |
---|
[7d27e36] | 1841 | else if (strcmp(cmd, "line") == 0) { |
---|
[bd1913f] | 1842 | /* set flag to indicate to process second triplet as LINE */ |
---|
[76bbb7c9] | 1843 | pimg->pending = 1; |
---|
[a420b49] | 1844 | result = img_MOVE; |
---|
[7d27e36] | 1845 | } else if (strcmp(cmd, "cross") == 0) { |
---|
[d69255f] | 1846 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1847 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[0ed0e16] | 1848 | return img_BAD; |
---|
[d69255f] | 1849 | } |
---|
[a420b49] | 1850 | goto ascii_again; |
---|
[7d27e36] | 1851 | } else if (strcmp(cmd, "name") == 0) { |
---|
[0da6e60] | 1852 | size_t off = 0; |
---|
[e02a6a6] | 1853 | int ch = GETC(pimg->fh); |
---|
| 1854 | if (ch == ' ') ch = GETC(pimg->fh); |
---|
[58f02ae] | 1855 | while (ch != ' ') { |
---|
| 1856 | if (ch == '\n' || ch == EOF) { |
---|
| 1857 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[ad9a5cd] | 1858 | return img_BAD; |
---|
| 1859 | } |
---|
[58f02ae] | 1860 | if (off == pimg->buf_len) { |
---|
[d4d3655] | 1861 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1862 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1863 | return img_BAD; |
---|
| 1864 | } |
---|
[0da6e60] | 1865 | } |
---|
[58f02ae] | 1866 | pimg->label_buf[off++] = ch; |
---|
[e02a6a6] | 1867 | ch = GETC(pimg->fh); |
---|
[ad9a5cd] | 1868 | } |
---|
[58f02ae] | 1869 | pimg->label_buf[off] = '\0'; |
---|
| 1870 | |
---|
| 1871 | pimg->label = pimg->label_buf; |
---|
| 1872 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[0da6e60] | 1873 | |
---|
[1182e94] | 1874 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
| 1875 | |
---|
[a420b49] | 1876 | result = img_LABEL; |
---|
[d69255f] | 1877 | } else { |
---|
| 1878 | img_errno = IMG_BADFORMAT; |
---|
[a420b49] | 1879 | return img_BAD; /* unknown keyword */ |
---|
[d69255f] | 1880 | } |
---|
[a420b49] | 1881 | } |
---|
[d1b1380] | 1882 | |
---|
[d69255f] | 1883 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1884 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[fe8e80e] | 1885 | return img_BAD; |
---|
[d69255f] | 1886 | } |
---|
| 1887 | |
---|
[4139b12] | 1888 | if (result == img_LABEL && !stn_included(pimg)) { |
---|
| 1889 | goto ascii_again; |
---|
[0ff23cb] | 1890 | } |
---|
[76bbb7c9] | 1891 | |
---|
[a420b49] | 1892 | return result; |
---|
[db9e5bf] | 1893 | } else if (pimg->version == VERSION_SURVEX_POS) { |
---|
| 1894 | /* Survex .pos file */ |
---|
[a69a767] | 1895 | int ch; |
---|
[58f02ae] | 1896 | size_t off; |
---|
[5eea574] | 1897 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[58f02ae] | 1898 | againpos: |
---|
[a69a767] | 1899 | while (fscanf(pimg->fh, "(%lf,%lf,%lf )", &p->x, &p->y, &p->z) != 3) { |
---|
[0da6e60] | 1900 | if (ferror(pimg->fh)) { |
---|
| 1901 | img_errno = IMG_READERROR; |
---|
| 1902 | return img_BAD; |
---|
| 1903 | } |
---|
| 1904 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1905 | if (pimg->pending) { |
---|
| 1906 | img_errno = IMG_BADFORMAT; |
---|
| 1907 | return img_BAD; |
---|
| 1908 | } |
---|
| 1909 | pimg->pending = 1; |
---|
| 1910 | /* ignore rest of line */ |
---|
| 1911 | do { |
---|
[e02a6a6] | 1912 | ch = GETC(pimg->fh); |
---|
[0da6e60] | 1913 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1914 | } |
---|
| 1915 | |
---|
| 1916 | pimg->label_buf[0] = '\0'; |
---|
[a69a767] | 1917 | do { |
---|
| 1918 | ch = GETC(pimg->fh); |
---|
| 1919 | } while (ch == ' ' || ch == '\t'); |
---|
| 1920 | if (ch == '\n' || ch == EOF) { |
---|
[55a0527] | 1921 | /* If there's no label, set img_SFLAG_ANON. */ |
---|
[a69a767] | 1922 | pimg->flags |= img_SFLAG_ANON; |
---|
| 1923 | return img_LABEL; |
---|
| 1924 | } |
---|
| 1925 | pimg->label_buf[0] = ch; |
---|
| 1926 | off = 1; |
---|
[0da6e60] | 1927 | while (!feof(pimg->fh)) { |
---|
| 1928 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 1929 | img_errno = IMG_READERROR; |
---|
| 1930 | return img_BAD; |
---|
| 1931 | } |
---|
| 1932 | |
---|
| 1933 | off += strlen(pimg->label_buf + off); |
---|
| 1934 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 1935 | pimg->label_buf[off - 1] = '\0'; |
---|
| 1936 | break; |
---|
| 1937 | } |
---|
[d4d3655] | 1938 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1939 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1940 | return img_BAD; |
---|
| 1941 | } |
---|
[0da6e60] | 1942 | } |
---|
| 1943 | |
---|
[58f02ae] | 1944 | pimg->label = pimg->label_buf; |
---|
| 1945 | |
---|
[421b7d2] | 1946 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[58f02ae] | 1947 | |
---|
[4139b12] | 1948 | if (!stn_included(pimg)) goto againpos; |
---|
[58f02ae] | 1949 | |
---|
[0da6e60] | 1950 | return img_LABEL; |
---|
[db9e5bf] | 1951 | } else if (pimg->version == VERSION_COMPASS_PLT) { |
---|
| 1952 | /* Compass .plt file */ |
---|
[4ebc557] | 1953 | if (pimg->pending > 0) { |
---|
| 1954 | /* -1 signals we've entered the first survey we want to |
---|
| 1955 | * read, and need to fudge lots if the first action is 'D'... |
---|
| 1956 | */ |
---|
| 1957 | /* pending MOVE or LINE */ |
---|
[984f49e] | 1958 | int r = pimg->pending - 4; |
---|
[ad4eaf3f] | 1959 | pimg->pending = 0; |
---|
| 1960 | pimg->flags = 0; |
---|
[f710a3f] | 1961 | pimg->label[pimg->label_len] = '\0'; |
---|
[ad4eaf3f] | 1962 | return r; |
---|
| 1963 | } |
---|
| 1964 | |
---|
| 1965 | while (1) { |
---|
[4ebc557] | 1966 | char *line; |
---|
[ad4eaf3f] | 1967 | char *q; |
---|
| 1968 | size_t len = 0; |
---|
[e02a6a6] | 1969 | int ch = GETC(pimg->fh); |
---|
[4ebc557] | 1970 | |
---|
| 1971 | switch (ch) { |
---|
| 1972 | case '\x1a': case EOF: /* Don't insist on ^Z at end of file */ |
---|
| 1973 | return img_STOP; |
---|
[27bd06b] | 1974 | case 'X': case 'F': case 'S': |
---|
| 1975 | /* bounding boX (marks end of survey), Feature survey, or |
---|
| 1976 | * new Section - skip to next survey */ |
---|
| 1977 | if (pimg->survey) return img_STOP; |
---|
| 1978 | skip_to_N: |
---|
| 1979 | while (1) { |
---|
| 1980 | do { |
---|
[e02a6a6] | 1981 | ch = GETC(pimg->fh); |
---|
[27bd06b] | 1982 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
[e02a6a6] | 1983 | while (ch == '\n' || ch == '\r') ch = GETC(pimg->fh); |
---|
[27bd06b] | 1984 | if (ch == 'N') break; |
---|
| 1985 | if (ch == '\x1a' || ch == EOF) return img_STOP; |
---|
| 1986 | } |
---|
| 1987 | /* FALLTHRU */ |
---|
[4ebc557] | 1988 | case 'N': |
---|
| 1989 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1990 | if (!line) { |
---|
| 1991 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1992 | return img_BAD; |
---|
| 1993 | } |
---|
[ee1ec59] | 1994 | while (line[len] > 32) ++len; |
---|
[ef59f5e] | 1995 | if (pimg->label_len == 0) pimg->pending = -1; |
---|
[4ebc557] | 1996 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1997 | osfree(line); |
---|
| 1998 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1999 | return img_BAD; |
---|
| 2000 | } |
---|
| 2001 | pimg->label_len = len; |
---|
| 2002 | pimg->label = pimg->label_buf; |
---|
| 2003 | memcpy(pimg->label, line, len); |
---|
| 2004 | pimg->label[len] = '\0'; |
---|
| 2005 | osfree(line); |
---|
| 2006 | break; |
---|
| 2007 | case 'M': case 'D': { |
---|
[ad4eaf3f] | 2008 | /* Move or Draw */ |
---|
[4ebc557] | 2009 | long fpos = -1; |
---|
[27bd06b] | 2010 | if (pimg->survey && pimg->label_len == 0) { |
---|
[4ebc557] | 2011 | /* We're only holding onto this line in case the first line |
---|
[27bd06b] | 2012 | * of the 'N' is a 'D', so skip it for now... |
---|
[4ebc557] | 2013 | */ |
---|
[27bd06b] | 2014 | goto skip_to_N; |
---|
[4ebc557] | 2015 | } |
---|
| 2016 | if (ch == 'D' && pimg->pending == -1) { |
---|
[ef59f5e] | 2017 | if (pimg->survey) { |
---|
| 2018 | fpos = ftell(pimg->fh) - 1; |
---|
| 2019 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[e02a6a6] | 2020 | ch = GETC(pimg->fh); |
---|
[ef59f5e] | 2021 | pimg->pending = 0; |
---|
| 2022 | } else { |
---|
| 2023 | /* If a file actually has a 'D' before any 'M', then |
---|
| 2024 | * pretend the 'D' is an 'M' - one of the examples |
---|
| 2025 | * in the docs was like this! */ |
---|
| 2026 | ch = 'M'; |
---|
| 2027 | } |
---|
[4ebc557] | 2028 | } |
---|
| 2029 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 2030 | if (!line) { |
---|
| 2031 | img_errno = IMG_OUTOFMEMORY; |
---|
| 2032 | return img_BAD; |
---|
| 2033 | } |
---|
[898bc69] | 2034 | /* Compass stores coordinates as North, East, Up = (y,x,z)! */ |
---|
[88c780d] | 2035 | if (sscanf(line, "%lf%lf%lf", &p->y, &p->x, &p->z) != 3) { |
---|
[ad4eaf3f] | 2036 | osfree(line); |
---|
| 2037 | if (ferror(pimg->fh)) { |
---|
| 2038 | img_errno = IMG_READERROR; |
---|
| 2039 | } else { |
---|
| 2040 | img_errno = IMG_BADFORMAT; |
---|
| 2041 | } |
---|
| 2042 | return img_BAD; |
---|
| 2043 | } |
---|
[a9ab65a] | 2044 | p->x *= METRES_PER_FOOT; |
---|
| 2045 | p->y *= METRES_PER_FOOT; |
---|
| 2046 | p->z *= METRES_PER_FOOT; |
---|
[ad4eaf3f] | 2047 | q = strchr(line, 'S'); |
---|
| 2048 | if (!q) { |
---|
| 2049 | osfree(line); |
---|
[5757725] | 2050 | img_errno = IMG_BADFORMAT; |
---|
[ad4eaf3f] | 2051 | return img_BAD; |
---|
| 2052 | } |
---|
| 2053 | ++q; |
---|
| 2054 | len = 0; |
---|
[ee1ec59] | 2055 | while (q[len] > ' ') ++len; |
---|
[ad4eaf3f] | 2056 | q[len] = '\0'; |
---|
[ee1ec59] | 2057 | len += 2; /* ' ' and '\0' */ |
---|
[d4d3655] | 2058 | if (!check_label_space(pimg, pimg->label_len + len)) { |
---|
| 2059 | img_errno = IMG_OUTOFMEMORY; |
---|
| 2060 | return img_BAD; |
---|
[ad4eaf3f] | 2061 | } |
---|
| 2062 | pimg->label = pimg->label_buf; |
---|
[be97baf] | 2063 | if (pimg->label_len) { |
---|
| 2064 | pimg->label[pimg->label_len] = ' '; |
---|
| 2065 | memcpy(pimg->label + pimg->label_len + 1, q, len - 1); |
---|
| 2066 | } else { |
---|
| 2067 | memcpy(pimg->label, q, len - 1); |
---|
| 2068 | } |
---|
[ee7af72] | 2069 | q += len - 1; |
---|
| 2070 | /* Now read LRUD. Technically, this is optional but virtually |
---|
[ee05463] | 2071 | * all PLT files have it (with dummy negative values if no LRUD |
---|
| 2072 | * was measured) and some versions of Compass can't read PLT |
---|
| 2073 | * files without it! |
---|
[ee7af72] | 2074 | */ |
---|
| 2075 | while (*q && *q <= ' ') q++; |
---|
| 2076 | if (*q == 'P') { |
---|
| 2077 | if (sscanf(q + 1, "%lf%lf%lf%lf", |
---|
| 2078 | &pimg->l, &pimg->r, &pimg->u, &pimg->d) != 4) { |
---|
| 2079 | osfree(line); |
---|
| 2080 | if (ferror(pimg->fh)) { |
---|
| 2081 | img_errno = IMG_READERROR; |
---|
| 2082 | } else { |
---|
| 2083 | img_errno = IMG_BADFORMAT; |
---|
| 2084 | } |
---|
| 2085 | return img_BAD; |
---|
| 2086 | } |
---|
| 2087 | pimg->l *= METRES_PER_FOOT; |
---|
| 2088 | pimg->r *= METRES_PER_FOOT; |
---|
| 2089 | pimg->u *= METRES_PER_FOOT; |
---|
| 2090 | pimg->d *= METRES_PER_FOOT; |
---|
| 2091 | } else { |
---|
| 2092 | pimg->l = pimg->r = pimg->u = pimg->d = -1; |
---|
| 2093 | } |
---|
[ad4eaf3f] | 2094 | osfree(line); |
---|
| 2095 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[0d922fc] | 2096 | if (fpos != -1) { |
---|
| 2097 | fseek(pimg->fh, fpos, SEEK_SET); |
---|
| 2098 | } else { |
---|
| 2099 | pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4; |
---|
| 2100 | } |
---|
[ad4eaf3f] | 2101 | return img_LABEL; |
---|
[4ebc557] | 2102 | } |
---|
[ad4eaf3f] | 2103 | default: |
---|
| 2104 | img_errno = IMG_BADFORMAT; |
---|
| 2105 | return img_BAD; |
---|
| 2106 | } |
---|
| 2107 | } |
---|
[f1c7c9c7] | 2108 | } else { |
---|
[db9e5bf] | 2109 | /* CMAP .xyz file */ |
---|
[f1c7c9c7] | 2110 | char *line = NULL; |
---|
| 2111 | char *q; |
---|
| 2112 | size_t len; |
---|
| 2113 | |
---|
| 2114 | if (pimg->pending) { |
---|
| 2115 | /* pending MOVE or LINE or LABEL or STOP */ |
---|
| 2116 | int r = pimg->pending - 4; |
---|
[c3e5ba5] | 2117 | /* Set label to empty - don't use "" as we adjust label relative |
---|
| 2118 | * to label_buf when label_buf is reallocated. */ |
---|
| 2119 | pimg->label = pimg->label_buf + strlen(pimg->label_buf); |
---|
[f1c7c9c7] | 2120 | pimg->flags = 0; |
---|
| 2121 | if (r == img_LABEL) { |
---|
| 2122 | /* nasty magic */ |
---|
| 2123 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 2124 | subtract_xyz_shot_deltas(p, pimg->label_buf + 16); |
---|
| 2125 | pimg->pending = img_STOP + 4; |
---|
| 2126 | return img_MOVE; |
---|
| 2127 | } |
---|
[6d4c4b9] | 2128 | |
---|
[f1c7c9c7] | 2129 | pimg->pending = 0; |
---|
| 2130 | |
---|
| 2131 | if (r == img_STOP) { |
---|
| 2132 | /* nasty magic */ |
---|
| 2133 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 2134 | return img_LINE; |
---|
| 2135 | } |
---|
[ee05463] | 2136 | |
---|
[f1c7c9c7] | 2137 | return r; |
---|
| 2138 | } |
---|
| 2139 | |
---|
| 2140 | pimg->label = pimg->label_buf; |
---|
| 2141 | do { |
---|
| 2142 | osfree(line); |
---|
| 2143 | if (feof(pimg->fh)) return img_STOP; |
---|
| 2144 | line = getline_alloc(pimg->fh); |
---|
| 2145 | if (!line) { |
---|
| 2146 | img_errno = IMG_OUTOFMEMORY; |
---|
| 2147 | return img_BAD; |
---|
| 2148 | } |
---|
| 2149 | } while (line[0] == ' ' || line[0] == '\0'); |
---|
| 2150 | if (line[0] == '\x1a') return img_STOP; |
---|
| 2151 | |
---|
| 2152 | len = strlen(line); |
---|
[db9e5bf] | 2153 | if (pimg->version == VERSION_CMAP_STATION) { |
---|
[f1c7c9c7] | 2154 | /* station variant */ |
---|
| 2155 | if (len < 37) { |
---|
| 2156 | osfree(line); |
---|
| 2157 | img_errno = IMG_BADFORMAT; |
---|
| 2158 | return img_BAD; |
---|
| 2159 | } |
---|
| 2160 | memcpy(pimg->label, line, 6); |
---|
[f807d8bf] | 2161 | q = (char *)memchr(pimg->label, ' ', 6); |
---|
[f1c7c9c7] | 2162 | if (!q) q = pimg->label + 6; |
---|
| 2163 | *q = '\0'; |
---|
| 2164 | |
---|
| 2165 | read_xyz_station_coords(p, line); |
---|
[6d4c4b9] | 2166 | |
---|
[f1c7c9c7] | 2167 | /* FIXME: look at prev for lines (line + 32, 5) */ |
---|
| 2168 | /* FIXME: duplicate stations... */ |
---|
| 2169 | return img_LABEL; |
---|
| 2170 | } else { |
---|
[d9cd8ae] | 2171 | /* Shot variant (VERSION_CMAP_SHOT) */ |
---|
[f807d8bf] | 2172 | char old[8], new_[8]; |
---|
[f1c7c9c7] | 2173 | if (len < 61) { |
---|
| 2174 | osfree(line); |
---|
| 2175 | img_errno = IMG_BADFORMAT; |
---|
| 2176 | return img_BAD; |
---|
| 2177 | } |
---|
[6d4c4b9] | 2178 | |
---|
[f1c7c9c7] | 2179 | memcpy(old, line, 7); |
---|
[f807d8bf] | 2180 | q = (char *)memchr(old, ' ', 7); |
---|
[f1c7c9c7] | 2181 | if (!q) q = old + 7; |
---|
[6d4c4b9] | 2182 | *q = '\0'; |
---|
[ee1ec59] | 2183 | |
---|
[f807d8bf] | 2184 | memcpy(new_, line + 7, 7); |
---|
| 2185 | q = (char *)memchr(new_, ' ', 7); |
---|
| 2186 | if (!q) q = new_ + 7; |
---|
[eef68f9] | 2187 | *q = '\0'; |
---|
[ee1ec59] | 2188 | |
---|
[f1c7c9c7] | 2189 | pimg->flags = img_SFLAG_UNDERGROUND; |
---|
| 2190 | |
---|
[f807d8bf] | 2191 | if (strcmp(old, new_) == 0) { |
---|
[f1c7c9c7] | 2192 | pimg->pending = img_MOVE + 4; |
---|
| 2193 | read_xyz_shot_coords(p, line); |
---|
[f807d8bf] | 2194 | strcpy(pimg->label, new_); |
---|
[f1c7c9c7] | 2195 | osfree(line); |
---|
| 2196 | return img_LABEL; |
---|
| 2197 | } |
---|
[6d4c4b9] | 2198 | |
---|
[f1c7c9c7] | 2199 | if (strcmp(old, pimg->label) == 0) { |
---|
| 2200 | pimg->pending = img_LINE + 4; |
---|
| 2201 | read_xyz_shot_coords(p, line); |
---|
[f807d8bf] | 2202 | strcpy(pimg->label, new_); |
---|
[f1c7c9c7] | 2203 | osfree(line); |
---|
| 2204 | return img_LABEL; |
---|
| 2205 | } |
---|
[6d4c4b9] | 2206 | |
---|
[f1c7c9c7] | 2207 | pimg->pending = img_LABEL + 4; |
---|
| 2208 | read_xyz_shot_coords(p, line); |
---|
[f807d8bf] | 2209 | strcpy(pimg->label, new_); |
---|
[f1c7c9c7] | 2210 | memcpy(pimg->label + 16, line, 70); |
---|
| 2211 | |
---|
| 2212 | osfree(line); |
---|
| 2213 | return img_LABEL; |
---|
| 2214 | } |
---|
[a420b49] | 2215 | } |
---|
[d1b1380] | 2216 | } |
---|
| 2217 | |
---|
[dec6b10] | 2218 | static void |
---|
| 2219 | write_coord(FILE *fh, double x, double y, double z) |
---|
| 2220 | { |
---|
| 2221 | SVX_ASSERT(fh); |
---|
| 2222 | /* Output in cm */ |
---|
| 2223 | static INT32_T X_, Y_, Z_; |
---|
[0a9b040] | 2224 | INT32_T X = my_lround(x * 100.0); |
---|
| 2225 | INT32_T Y = my_lround(y * 100.0); |
---|
| 2226 | INT32_T Z = my_lround(z * 100.0); |
---|
[dec6b10] | 2227 | |
---|
| 2228 | X_ -= X; |
---|
| 2229 | Y_ -= Y; |
---|
| 2230 | Z_ -= Z; |
---|
| 2231 | put32(X, fh); |
---|
| 2232 | put32(Y, fh); |
---|
| 2233 | put32(Z, fh); |
---|
| 2234 | X_ = X; Y_ = Y; Z_ = Z; |
---|
| 2235 | } |
---|
| 2236 | |
---|
[ad9a5cd] | 2237 | static int |
---|
[d69255f] | 2238 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 2239 | { |
---|
| 2240 | size_t len, n, dot; |
---|
| 2241 | |
---|
| 2242 | /* find length of common prefix */ |
---|
| 2243 | dot = 0; |
---|
[0ff23cb] | 2244 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 2245 | if (s[len] == '.') dot = len + 1; |
---|
| 2246 | } |
---|
| 2247 | |
---|
[4c07c51] | 2248 | SVX_ASSERT(len <= pimg->label_len); |
---|
[d69255f] | 2249 | n = pimg->label_len - len; |
---|
| 2250 | if (len == 0) { |
---|
[e02a6a6] | 2251 | if (pimg->label_len) PUTC(0, pimg->fh); |
---|
[d69255f] | 2252 | } else if (n <= 16) { |
---|
[e02a6a6] | 2253 | if (n) PUTC(n + 15, pimg->fh); |
---|
[d69255f] | 2254 | } else if (dot == 0) { |
---|
[e02a6a6] | 2255 | if (pimg->label_len) PUTC(0, pimg->fh); |
---|
[99cf51a] | 2256 | len = 0; |
---|
[d69255f] | 2257 | } else { |
---|
[0ff23cb] | 2258 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 2259 | n = 1; |
---|
| 2260 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 2261 | if (*p++ == '.') n++; |
---|
| 2262 | } |
---|
| 2263 | if (n <= 14) { |
---|
[e02a6a6] | 2264 | PUTC(n, pimg->fh); |
---|
[d69255f] | 2265 | len = dot; |
---|
| 2266 | } else { |
---|
[e02a6a6] | 2267 | if (pimg->label_len) PUTC(0, pimg->fh); |
---|
[d69255f] | 2268 | len = 0; |
---|
| 2269 | } |
---|
| 2270 | } |
---|
| 2271 | |
---|
| 2272 | n = strlen(s + len); |
---|
[e02a6a6] | 2273 | PUTC(opt, pimg->fh); |
---|
[d69255f] | 2274 | if (n < 0xfe) { |
---|
[e02a6a6] | 2275 | PUTC(n, pimg->fh); |
---|
[d69255f] | 2276 | } else if (n < 0xffff + 0xfe) { |
---|
[e02a6a6] | 2277 | PUTC(0xfe, pimg->fh); |
---|
[63dc4eb] | 2278 | put16((short)(n - 0xfe), pimg->fh); |
---|
[d69255f] | 2279 | } else { |
---|
[e02a6a6] | 2280 | PUTC(0xff, pimg->fh); |
---|
[d69255f] | 2281 | put32(n, pimg->fh); |
---|
| 2282 | } |
---|
| 2283 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 2284 | |
---|
| 2285 | n += len; |
---|
| 2286 | pimg->label_len = n; |
---|
[d4d3655] | 2287 | if (!check_label_space(pimg, n + 1)) |
---|
| 2288 | return 0; /* FIXME: distinguish out of memory... */ |
---|
[0ff23cb] | 2289 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 2290 | |
---|
| 2291 | return !ferror(pimg->fh); |
---|
[d69255f] | 2292 | } |
---|
| 2293 | |
---|
[dec6b10] | 2294 | static int |
---|
[e133186] | 2295 | write_v8label(img *pimg, int opt, int common_flag, size_t common_val, |
---|
| 2296 | const char *s) |
---|
[dec6b10] | 2297 | { |
---|
| 2298 | size_t len, del, add; |
---|
| 2299 | |
---|
| 2300 | /* find length of common prefix */ |
---|
| 2301 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
| 2302 | } |
---|
| 2303 | |
---|
| 2304 | SVX_ASSERT(len <= pimg->label_len); |
---|
| 2305 | del = pimg->label_len - len; |
---|
| 2306 | add = strlen(s + len); |
---|
| 2307 | |
---|
[e133186] | 2308 | if (add == common_val && del == common_val) { |
---|
| 2309 | PUTC(opt | common_flag, pimg->fh); |
---|
[dec6b10] | 2310 | } else { |
---|
[e133186] | 2311 | PUTC(opt, pimg->fh); |
---|
| 2312 | if (del <= 15 && add <= 15 && (del || add)) { |
---|
| 2313 | PUTC((del << 4) | add, pimg->fh); |
---|
[dec6b10] | 2314 | } else { |
---|
[e133186] | 2315 | PUTC(0x00, pimg->fh); |
---|
| 2316 | if (del < 0xff) { |
---|
| 2317 | PUTC(del, pimg->fh); |
---|
| 2318 | } else { |
---|
| 2319 | PUTC(0xff, pimg->fh); |
---|
| 2320 | put32(del, pimg->fh); |
---|
| 2321 | } |
---|
| 2322 | if (add < 0xff) { |
---|
| 2323 | PUTC(add, pimg->fh); |
---|
| 2324 | } else { |
---|
| 2325 | PUTC(0xff, pimg->fh); |
---|
| 2326 | put32(add, pimg->fh); |
---|
| 2327 | } |
---|
[dec6b10] | 2328 | } |
---|
| 2329 | } |
---|
| 2330 | |
---|
| 2331 | if (add) |
---|
| 2332 | fwrite(s + len, add, 1, pimg->fh); |
---|
| 2333 | |
---|
| 2334 | pimg->label_len = len + add; |
---|
| 2335 | if (add > del && !check_label_space(pimg, pimg->label_len + 1)) |
---|
| 2336 | return 0; /* FIXME: distinguish out of memory... */ |
---|
| 2337 | |
---|
| 2338 | memcpy(pimg->label_buf + len, s + len, add + 1); |
---|
| 2339 | |
---|
| 2340 | return !ferror(pimg->fh); |
---|
| 2341 | } |
---|
| 2342 | |
---|
[22114ef] | 2343 | static void |
---|
| 2344 | img_write_item_date_new(img *pimg) |
---|
| 2345 | { |
---|
| 2346 | int same, unset; |
---|
| 2347 | /* Only write dates when they've changed. */ |
---|
| 2348 | #if IMG_API_VERSION == 0 |
---|
| 2349 | if (pimg->date1 == pimg->olddate1 && pimg->date2 == pimg->olddate2) |
---|
| 2350 | return; |
---|
| 2351 | |
---|
| 2352 | same = (pimg->date1 == pimg->date2); |
---|
| 2353 | unset = (pimg->date1 == 0); |
---|
| 2354 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2355 | if (pimg->days1 == pimg->olddays1 && pimg->days2 == pimg->olddays2) |
---|
| 2356 | return; |
---|
| 2357 | |
---|
| 2358 | same = (pimg->days1 == pimg->days2); |
---|
| 2359 | unset = (pimg->days1 == -1); |
---|
| 2360 | #endif |
---|
| 2361 | |
---|
| 2362 | if (same) { |
---|
| 2363 | if (unset) { |
---|
| 2364 | PUTC(0x10, pimg->fh); |
---|
| 2365 | } else { |
---|
| 2366 | PUTC(0x11, pimg->fh); |
---|
| 2367 | #if IMG_API_VERSION == 0 |
---|
| 2368 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
| 2369 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2370 | put16(pimg->days1, pimg->fh); |
---|
| 2371 | #endif |
---|
| 2372 | } |
---|
| 2373 | } else { |
---|
| 2374 | #if IMG_API_VERSION == 0 |
---|
| 2375 | int diff = (pimg->date2 - pimg->date1) / 86400; |
---|
| 2376 | if (diff > 0 && diff <= 256) { |
---|
| 2377 | PUTC(0x12, pimg->fh); |
---|
| 2378 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
| 2379 | PUTC(diff - 1, pimg->fh); |
---|
| 2380 | } else { |
---|
| 2381 | PUTC(0x13, pimg->fh); |
---|
| 2382 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
| 2383 | put16(pimg->date2 / 86400 + 25567, pimg->fh); |
---|
| 2384 | } |
---|
| 2385 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2386 | int diff = pimg->days2 - pimg->days1; |
---|
| 2387 | if (diff > 0 && diff <= 256) { |
---|
| 2388 | PUTC(0x12, pimg->fh); |
---|
| 2389 | put16(pimg->days1, pimg->fh); |
---|
| 2390 | PUTC(diff - 1, pimg->fh); |
---|
| 2391 | } else { |
---|
| 2392 | PUTC(0x13, pimg->fh); |
---|
| 2393 | put16(pimg->days1, pimg->fh); |
---|
| 2394 | put16(pimg->days2, pimg->fh); |
---|
| 2395 | } |
---|
| 2396 | #endif |
---|
| 2397 | } |
---|
| 2398 | #if IMG_API_VERSION == 0 |
---|
| 2399 | pimg->olddate1 = pimg->date1; |
---|
| 2400 | pimg->olddate2 = pimg->date2; |
---|
| 2401 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2402 | pimg->olddays1 = pimg->days1; |
---|
| 2403 | pimg->olddays2 = pimg->days2; |
---|
| 2404 | #endif |
---|
| 2405 | } |
---|
| 2406 | |
---|
[d4650b3] | 2407 | static void |
---|
| 2408 | img_write_item_date(img *pimg) |
---|
| 2409 | { |
---|
[1ee204e] | 2410 | int same, unset; |
---|
| 2411 | /* Only write dates when they've changed. */ |
---|
| 2412 | #if IMG_API_VERSION == 0 |
---|
| 2413 | if (pimg->date1 == pimg->olddate1 && pimg->date2 == pimg->olddate2) |
---|
| 2414 | return; |
---|
| 2415 | |
---|
| 2416 | same = (pimg->date1 == pimg->date2); |
---|
| 2417 | unset = (pimg->date1 == 0); |
---|
| 2418 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2419 | if (pimg->days1 == pimg->olddays1 && pimg->days2 == pimg->olddays2) |
---|
| 2420 | return; |
---|
| 2421 | |
---|
| 2422 | same = (pimg->days1 == pimg->days2); |
---|
| 2423 | unset = (pimg->days1 == -1); |
---|
| 2424 | #endif |
---|
| 2425 | |
---|
| 2426 | if (same) { |
---|
| 2427 | if (img_output_version < 7) { |
---|
[e02a6a6] | 2428 | PUTC(0x20, pimg->fh); |
---|
[1ee204e] | 2429 | #if IMG_API_VERSION == 0 |
---|
[d4650b3] | 2430 | put32(pimg->date1, pimg->fh); |
---|
[1ee204e] | 2431 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2432 | put32((pimg->days1 - 25567) * 86400, pimg->fh); |
---|
| 2433 | #endif |
---|
[d4650b3] | 2434 | } else { |
---|
[1ee204e] | 2435 | if (unset) { |
---|
[e02a6a6] | 2436 | PUTC(0x24, pimg->fh); |
---|
[1ee204e] | 2437 | } else { |
---|
[e02a6a6] | 2438 | PUTC(0x20, pimg->fh); |
---|
[1ee204e] | 2439 | #if IMG_API_VERSION == 0 |
---|
| 2440 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
| 2441 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2442 | put16(pimg->days1, pimg->fh); |
---|
| 2443 | #endif |
---|
| 2444 | } |
---|
| 2445 | } |
---|
| 2446 | } else { |
---|
| 2447 | if (img_output_version < 7) { |
---|
[e02a6a6] | 2448 | PUTC(0x21, pimg->fh); |
---|
[1ee204e] | 2449 | #if IMG_API_VERSION == 0 |
---|
[d4650b3] | 2450 | put32(pimg->date1, pimg->fh); |
---|
| 2451 | put32(pimg->date2, pimg->fh); |
---|
[1ee204e] | 2452 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2453 | put32((pimg->days1 - 25567) * 86400, pimg->fh); |
---|
| 2454 | put32((pimg->days2 - 25567) * 86400, pimg->fh); |
---|
| 2455 | #endif |
---|
| 2456 | } else { |
---|
| 2457 | #if IMG_API_VERSION == 0 |
---|
| 2458 | int diff = (pimg->date2 - pimg->date1) / 86400; |
---|
| 2459 | if (diff > 0 && diff <= 256) { |
---|
[e02a6a6] | 2460 | PUTC(0x21, pimg->fh); |
---|
[1ee204e] | 2461 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
[e02a6a6] | 2462 | PUTC(diff - 1, pimg->fh); |
---|
[1ee204e] | 2463 | } else { |
---|
[e02a6a6] | 2464 | PUTC(0x23, pimg->fh); |
---|
[1ee204e] | 2465 | put16(pimg->date1 / 86400 + 25567, pimg->fh); |
---|
| 2466 | put16(pimg->date2 / 86400 + 25567, pimg->fh); |
---|
| 2467 | } |
---|
| 2468 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2469 | int diff = pimg->days2 - pimg->days1; |
---|
| 2470 | if (diff > 0 && diff <= 256) { |
---|
[e02a6a6] | 2471 | PUTC(0x21, pimg->fh); |
---|
[1ee204e] | 2472 | put16(pimg->days1, pimg->fh); |
---|
[e02a6a6] | 2473 | PUTC(diff - 1, pimg->fh); |
---|
[1ee204e] | 2474 | } else { |
---|
[e02a6a6] | 2475 | PUTC(0x23, pimg->fh); |
---|
[1ee204e] | 2476 | put16(pimg->days1, pimg->fh); |
---|
| 2477 | put16(pimg->days2, pimg->fh); |
---|
| 2478 | } |
---|
| 2479 | #endif |
---|
[d4650b3] | 2480 | } |
---|
| 2481 | } |
---|
[1ee204e] | 2482 | #if IMG_API_VERSION == 0 |
---|
| 2483 | pimg->olddate1 = pimg->date1; |
---|
| 2484 | pimg->olddate2 = pimg->date2; |
---|
| 2485 | #else /* IMG_API_VERSION == 1 */ |
---|
| 2486 | pimg->olddays1 = pimg->days1; |
---|
| 2487 | pimg->olddays2 = pimg->days2; |
---|
| 2488 | #endif |
---|
[d4650b3] | 2489 | } |
---|
| 2490 | |
---|
[dec6b10] | 2491 | static void |
---|
| 2492 | img_write_item_new(img *pimg, int code, int flags, const char *s, |
---|
| 2493 | double x, double y, double z); |
---|
| 2494 | static void |
---|
| 2495 | img_write_item_v3to7(img *pimg, int code, int flags, const char *s, |
---|
| 2496 | double x, double y, double z); |
---|
| 2497 | static void |
---|
| 2498 | img_write_item_ancient(img *pimg, int code, int flags, const char *s, |
---|
| 2499 | double x, double y, double z); |
---|
| 2500 | |
---|
[a420b49] | 2501 | void |
---|
[95c3272] | 2502 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 2503 | double x, double y, double z) |
---|
[a420b49] | 2504 | { |
---|
[647407d] | 2505 | if (!pimg) return; |
---|
[dec6b10] | 2506 | if (pimg->version >= 8) { |
---|
| 2507 | img_write_item_new(pimg, code, flags, s, x, y, z); |
---|
| 2508 | } else if (pimg->version >= 3) { |
---|
| 2509 | img_write_item_v3to7(pimg, code, flags, s, x, y, z); |
---|
[d69255f] | 2510 | } else { |
---|
[dec6b10] | 2511 | img_write_item_ancient(pimg, code, flags, s, x, y, z); |
---|
| 2512 | } |
---|
| 2513 | } |
---|
| 2514 | |
---|
| 2515 | static void |
---|
| 2516 | img_write_item_new(img *pimg, int code, int flags, const char *s, |
---|
| 2517 | double x, double y, double z) |
---|
| 2518 | { |
---|
| 2519 | switch (code) { |
---|
| 2520 | case img_LABEL: |
---|
[e133186] | 2521 | write_v8label(pimg, 0x80 | flags, 0, -1, s); |
---|
[dec6b10] | 2522 | break; |
---|
| 2523 | case img_XSECT: { |
---|
| 2524 | INT32_T l, r, u, d, max_dim; |
---|
[22114ef] | 2525 | img_write_item_date_new(pimg); |
---|
[0a9b040] | 2526 | l = (INT32_T)my_lround(pimg->l * 100.0); |
---|
| 2527 | r = (INT32_T)my_lround(pimg->r * 100.0); |
---|
| 2528 | u = (INT32_T)my_lround(pimg->u * 100.0); |
---|
| 2529 | d = (INT32_T)my_lround(pimg->d * 100.0); |
---|
[dec6b10] | 2530 | if (l < 0) l = -1; |
---|
| 2531 | if (r < 0) r = -1; |
---|
| 2532 | if (u < 0) u = -1; |
---|
| 2533 | if (d < 0) d = -1; |
---|
| 2534 | max_dim = max(max(l, r), max(u, d)); |
---|
| 2535 | flags = (flags & img_XFLAG_END) ? 1 : 0; |
---|
| 2536 | if (max_dim >= 32768) flags |= 2; |
---|
[e133186] | 2537 | write_v8label(pimg, 0x30 | flags, 0, -1, s); |
---|
[dec6b10] | 2538 | if (flags & 2) { |
---|
| 2539 | /* Big passage! Need to use 4 bytes. */ |
---|
| 2540 | put32(l, pimg->fh); |
---|
| 2541 | put32(r, pimg->fh); |
---|
| 2542 | put32(u, pimg->fh); |
---|
| 2543 | put32(d, pimg->fh); |
---|
| 2544 | } else { |
---|
| 2545 | put16(l, pimg->fh); |
---|
| 2546 | put16(r, pimg->fh); |
---|
| 2547 | put16(u, pimg->fh); |
---|
| 2548 | put16(d, pimg->fh); |
---|
| 2549 | } |
---|
| 2550 | return; |
---|
| 2551 | } |
---|
| 2552 | case img_MOVE: |
---|
| 2553 | PUTC(15, pimg->fh); |
---|
| 2554 | break; |
---|
| 2555 | case img_LINE: |
---|
[22114ef] | 2556 | img_write_item_date_new(pimg); |
---|
[dec6b10] | 2557 | if (pimg->style != pimg->oldstyle) { |
---|
| 2558 | switch (pimg->style) { |
---|
| 2559 | case img_STYLE_NORMAL: |
---|
| 2560 | case img_STYLE_DIVING: |
---|
| 2561 | case img_STYLE_CARTESIAN: |
---|
| 2562 | case img_STYLE_CYLPOLAR: |
---|
| 2563 | case img_STYLE_NOSURVEY: |
---|
| 2564 | PUTC(pimg->style, pimg->fh); |
---|
| 2565 | break; |
---|
| 2566 | } |
---|
| 2567 | pimg->oldstyle = pimg->style; |
---|
[a420b49] | 2568 | } |
---|
[e133186] | 2569 | write_v8label(pimg, 0x40 | flags, 0x20, 0x00, s ? s : ""); |
---|
[dec6b10] | 2570 | break; |
---|
| 2571 | default: /* ignore for now */ |
---|
| 2572 | return; |
---|
| 2573 | } |
---|
| 2574 | write_coord(pimg->fh, x, y, z); |
---|
| 2575 | } |
---|
| 2576 | |
---|
| 2577 | static void |
---|
| 2578 | img_write_item_v3to7(img *pimg, int code, int flags, const char *s, |
---|
| 2579 | double x, double y, double z) |
---|
| 2580 | { |
---|
| 2581 | switch (code) { |
---|
| 2582 | case img_LABEL: |
---|
| 2583 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 2584 | break; |
---|
| 2585 | case img_XSECT: { |
---|
| 2586 | INT32_T l, r, u, d, max_dim; |
---|
| 2587 | /* Need at least version 5 for img_XSECT. */ |
---|
| 2588 | if (pimg->version < 5) return; |
---|
| 2589 | img_write_item_date(pimg); |
---|
[0a9b040] | 2590 | l = (INT32_T)my_lround(pimg->l * 100.0); |
---|
| 2591 | r = (INT32_T)my_lround(pimg->r * 100.0); |
---|
| 2592 | u = (INT32_T)my_lround(pimg->u * 100.0); |
---|
| 2593 | d = (INT32_T)my_lround(pimg->d * 100.0); |
---|
[dec6b10] | 2594 | if (l < 0) l = -1; |
---|
| 2595 | if (r < 0) r = -1; |
---|
| 2596 | if (u < 0) u = -1; |
---|
| 2597 | if (d < 0) d = -1; |
---|
| 2598 | max_dim = max(max(l, r), max(u, d)); |
---|
| 2599 | flags = (flags & img_XFLAG_END) ? 1 : 0; |
---|
| 2600 | if (max_dim >= 32768) flags |= 2; |
---|
| 2601 | write_v3label(pimg, 0x30 | flags, s); |
---|
| 2602 | if (flags & 2) { |
---|
| 2603 | /* Big passage! Need to use 4 bytes. */ |
---|
| 2604 | put32(l, pimg->fh); |
---|
| 2605 | put32(r, pimg->fh); |
---|
| 2606 | put32(u, pimg->fh); |
---|
| 2607 | put32(d, pimg->fh); |
---|
| 2608 | } else { |
---|
| 2609 | put16(l, pimg->fh); |
---|
| 2610 | put16(r, pimg->fh); |
---|
| 2611 | put16(u, pimg->fh); |
---|
| 2612 | put16(d, pimg->fh); |
---|
| 2613 | } |
---|
| 2614 | return; |
---|
| 2615 | } |
---|
| 2616 | case img_MOVE: |
---|
| 2617 | PUTC(15, pimg->fh); |
---|
| 2618 | break; |
---|
| 2619 | case img_LINE: |
---|
| 2620 | if (pimg->version >= 4) { |
---|
| 2621 | img_write_item_date(pimg); |
---|
| 2622 | } |
---|
| 2623 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 2624 | break; |
---|
| 2625 | default: /* ignore for now */ |
---|
| 2626 | return; |
---|
| 2627 | } |
---|
| 2628 | write_coord(pimg->fh, x, y, z); |
---|
| 2629 | } |
---|
| 2630 | |
---|
| 2631 | static void |
---|
| 2632 | img_write_item_ancient(img *pimg, int code, int flags, const char *s, |
---|
| 2633 | double x, double y, double z) |
---|
| 2634 | { |
---|
| 2635 | size_t len; |
---|
| 2636 | INT32_T opt = 0; |
---|
| 2637 | SVX_ASSERT(pimg->version > 0); |
---|
| 2638 | switch (code) { |
---|
| 2639 | case img_LABEL: |
---|
[437caf3] | 2640 | if (pimg->version == 1) { |
---|
[dec6b10] | 2641 | /* put a move before each label */ |
---|
[a8638a6] | 2642 | img_write_item_ancient(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[dec6b10] | 2643 | put32(2, pimg->fh); |
---|
| 2644 | fputsnl(s, pimg->fh); |
---|
| 2645 | return; |
---|
| 2646 | } |
---|
| 2647 | len = strlen(s); |
---|
| 2648 | if (len > 255 || strchr(s, '\n')) { |
---|
| 2649 | /* long label - not in early incarnations of v2 format, but few |
---|
| 2650 | * 3d files will need these, so better not to force incompatibility |
---|
| 2651 | * with a new version I think... */ |
---|
| 2652 | PUTC(7, pimg->fh); |
---|
| 2653 | PUTC(flags, pimg->fh); |
---|
| 2654 | put32(len, pimg->fh); |
---|
| 2655 | fputs(s, pimg->fh); |
---|
[437caf3] | 2656 | } else { |
---|
[dec6b10] | 2657 | PUTC(0x40 | (flags & 0x3f), pimg->fh); |
---|
| 2658 | fputsnl(s, pimg->fh); |
---|
[a420b49] | 2659 | } |
---|
[dec6b10] | 2660 | opt = 0; |
---|
| 2661 | break; |
---|
| 2662 | case img_MOVE: |
---|
| 2663 | opt = 4; |
---|
| 2664 | break; |
---|
| 2665 | case img_LINE: |
---|
| 2666 | if (pimg->version > 1) { |
---|
| 2667 | opt = 0x80 | (flags & 0x3f); |
---|
| 2668 | break; |
---|
| 2669 | } |
---|
| 2670 | opt = 5; |
---|
| 2671 | break; |
---|
| 2672 | default: /* ignore for now */ |
---|
| 2673 | return; |
---|
[a420b49] | 2674 | } |
---|
[dec6b10] | 2675 | if (pimg->version == 1) { |
---|
| 2676 | put32(opt, pimg->fh); |
---|
| 2677 | } else { |
---|
| 2678 | if (opt) PUTC(opt, pimg->fh); |
---|
| 2679 | } |
---|
| 2680 | write_coord(pimg->fh, x, y, z); |
---|
[d1b1380] | 2681 | } |
---|
| 2682 | |
---|
[c61aa79] | 2683 | /* Write error information for the current traverse |
---|
| 2684 | * n_legs is the number of legs in the traverse |
---|
| 2685 | * length is the traverse length (in m) |
---|
| 2686 | * E is the ratio of the observed misclosure to the theoretical one |
---|
| 2687 | * H is the ratio of the observed horizontal misclosure to the theoretical one |
---|
| 2688 | * V is the ratio of the observed vertical misclosure to the theoretical one |
---|
| 2689 | */ |
---|
| 2690 | void |
---|
| 2691 | img_write_errors(img *pimg, int n_legs, double length, |
---|
| 2692 | double E, double H, double V) |
---|
| 2693 | { |
---|
[22114ef] | 2694 | PUTC((pimg->version >= 8 ? 0x1f : 0x22), pimg->fh); |
---|
[c61aa79] | 2695 | put32(n_legs, pimg->fh); |
---|
[0a9b040] | 2696 | put32((INT32_T)my_lround(length * 100.0), pimg->fh); |
---|
| 2697 | put32((INT32_T)my_lround(E * 100.0), pimg->fh); |
---|
| 2698 | put32((INT32_T)my_lround(H * 100.0), pimg->fh); |
---|
| 2699 | put32((INT32_T)my_lround(V * 100.0), pimg->fh); |
---|
[c61aa79] | 2700 | } |
---|
| 2701 | |
---|
[ad9a5cd] | 2702 | int |
---|
[a420b49] | 2703 | img_close(img *pimg) |
---|
| 2704 | { |
---|
[ad9a5cd] | 2705 | int result = 1; |
---|
[dbb4e19] | 2706 | if (pimg) { |
---|
| 2707 | if (pimg->fh) { |
---|
[76bbb7c9] | 2708 | if (pimg->fRead) { |
---|
| 2709 | osfree(pimg->survey); |
---|
[a2ad284] | 2710 | osfree(pimg->title); |
---|
[43d63e57] | 2711 | osfree(pimg->cs); |
---|
[a2ad284] | 2712 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 2713 | } else { |
---|
| 2714 | /* write end of data marker */ |
---|
[d69255f] | 2715 | switch (pimg->version) { |
---|
| 2716 | case 1: |
---|
[5c3c61a] | 2717 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 2718 | break; |
---|
[ee05463] | 2719 | default: |
---|
[dec6b10] | 2720 | if (pimg->version <= 7 ? |
---|
| 2721 | (pimg->label_len != 0) : |
---|
| 2722 | (pimg->style != img_STYLE_NORMAL)) { |
---|
| 2723 | PUTC(0, pimg->fh); |
---|
| 2724 | } |
---|
| 2725 | /* FALL THROUGH */ |
---|
| 2726 | case 2: |
---|
[e02a6a6] | 2727 | PUTC(0, pimg->fh); |
---|
[d69255f] | 2728 | break; |
---|
[5c3c61a] | 2729 | } |
---|
| 2730 | } |
---|
[ad9a5cd] | 2731 | if (ferror(pimg->fh)) result = 0; |
---|
| 2732 | if (fclose(pimg->fh)) result = 0; |
---|
[0d922fc] | 2733 | if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 2734 | } |
---|
[0ff23cb] | 2735 | osfree(pimg->label_buf); |
---|
[452e200] | 2736 | osfree(pimg->filename_opened); |
---|
[dbb4e19] | 2737 | osfree(pimg); |
---|
[a420b49] | 2738 | } |
---|
[ad9a5cd] | 2739 | return result; |
---|
[d1b1380] | 2740 | } |
---|