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