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