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