[0da6e60] | 1 | /* img.c |
---|
[d1b1380] | 2 | * Routines for reading and writing Survex ".3d" image files |
---|
[ad4eaf3f] | 3 | * Copyright (C) 1993-2002 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 |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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> |
---|
| 29 | |
---|
[72af530] | 30 | #include "img.h" |
---|
[7d27e36] | 31 | |
---|
[72af530] | 32 | #ifdef IMG_HOSTED |
---|
[d69255f] | 33 | # include "debug.h" |
---|
[d1b1380] | 34 | # include "filelist.h" |
---|
[d69255f] | 35 | # include "filename.h" |
---|
| 36 | # include "message.h" |
---|
| 37 | # include "useful.h" |
---|
[a420b49] | 38 | # define TIMENA msg(/*Date and time not available.*/108) |
---|
| 39 | # define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107) |
---|
[d1b1380] | 40 | #else |
---|
[72af530] | 41 | # define INT32_T long |
---|
[d1b1380] | 42 | # define TIMENA "Time not available." |
---|
| 43 | # define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z" |
---|
| 44 | # define EXT_SVX_3D "3d" |
---|
| 45 | # define xosmalloc(L) malloc((L)) |
---|
[72af530] | 46 | # define xosrealloc(L,S) realloc((L),(S)) |
---|
[d1b1380] | 47 | # define osfree(P) free((P)) |
---|
| 48 | # define ossizeof(T) sizeof(T) |
---|
[72af530] | 49 | /* in IMG_HOSTED mode, this tests if a filename refers to a directory */ |
---|
[d1b1380] | 50 | # define fDirectory(X) 0 |
---|
| 51 | /* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */ |
---|
| 52 | /* path isn't used in img.c, but EXT is */ |
---|
| 53 | # define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) fopen(FNM,MODE) |
---|
| 54 | # define fFalse 0 |
---|
| 55 | # define fTrue 1 |
---|
| 56 | # define bool int |
---|
| 57 | /* dummy do {...} while(0) hack to permit stuff like |
---|
| 58 | * if (s) fputsnl(s,fh); else exit(1); |
---|
| 59 | * to work as intended |
---|
| 60 | */ |
---|
[23f7ea7] | 61 | # define fputsnl(S, FH) do {fputs((S), (FH)); putc('\n', (FH));} while(0) |
---|
[4c07c51] | 62 | # define SVX_ASSERT(X) |
---|
[d1b1380] | 63 | |
---|
[a420b49] | 64 | static long |
---|
| 65 | get32(FILE *fh) |
---|
| 66 | { |
---|
[ad9a5cd] | 67 | long w = getc(fh); |
---|
[a420b49] | 68 | w |= (long)getc(fh) << 8l; |
---|
| 69 | w |= (long)getc(fh) << 16l; |
---|
| 70 | w |= (long)getc(fh) << 24l; |
---|
| 71 | return w; |
---|
[d1b1380] | 72 | } |
---|
| 73 | |
---|
[a420b49] | 74 | static void |
---|
| 75 | put32(long w, FILE *fh) |
---|
| 76 | { |
---|
| 77 | putc((char)(w), fh); |
---|
| 78 | putc((char)(w >> 8l), fh); |
---|
| 79 | putc((char)(w >> 16l), fh); |
---|
| 80 | putc((char)(w >> 24l), fh); |
---|
[d1b1380] | 81 | } |
---|
[5c3c61a] | 82 | |
---|
| 83 | #endif |
---|
| 84 | |
---|
[dd1d6369] | 85 | #ifdef HAVE_ROUND |
---|
| 86 | # include <math.h> |
---|
| 87 | extern double round(double); /* prototype is often missing... */ |
---|
| 88 | # define my_round round |
---|
| 89 | #else |
---|
| 90 | static double |
---|
| 91 | my_round(double x) { |
---|
| 92 | if (x >= 0.0) return floor(x + 0.5); |
---|
| 93 | return ceil(x - 0.5); |
---|
| 94 | } |
---|
| 95 | #endif |
---|
| 96 | |
---|
[ad4eaf3f] | 97 | /* portable case insensitive string compare */ |
---|
| 98 | #if defined(strcasecmp) || defined(HAVE_STRCASECMP) |
---|
| 99 | # define my_strcasecmp strcasecmp |
---|
| 100 | #else |
---|
| 101 | /* What about top bit set chars? */ |
---|
| 102 | int my_strcasecmp(const char *s1, const char *s2) { |
---|
| 103 | register c1, c2; |
---|
| 104 | do { |
---|
| 105 | c1 = *s1++; |
---|
| 106 | c2 = *s2++; |
---|
| 107 | } while (c1 && toupper(c1) == toupper(c2)); |
---|
| 108 | /* now calculate real difference */ |
---|
| 109 | return c1 - c2; |
---|
| 110 | } |
---|
| 111 | #endif |
---|
| 112 | |
---|
[d69255f] | 113 | unsigned int img_output_version = 3; |
---|
[5c3c61a] | 114 | |
---|
[72af530] | 115 | #ifdef IMG_HOSTED |
---|
[5c3c61a] | 116 | static enum { |
---|
| 117 | IMG_NONE = 0, |
---|
[759fb47] | 118 | IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24, |
---|
[5c3c61a] | 119 | IMG_OUTOFMEMORY = /*Out of memory %.0s*/38, |
---|
[759fb47] | 120 | IMG_DIRECTORY = /*Filename `%s' refers to directory*/44, |
---|
| 121 | IMG_CANTOPENOUT = /*Failed to open output file `%s'*/47, |
---|
[ad9a5cd] | 122 | IMG_BADFORMAT = /*Bad 3d image file `%s'*/106, |
---|
| 123 | IMG_READERROR = /*Error reading from file `%s'*/109, |
---|
[6c1c4f3] | 124 | IMG_WRITEERROR = /*Error writing to file `%s'*/110, |
---|
| 125 | IMG_TOONEW = /*File `%s' has a newer format than this program can understand*/114 |
---|
[5c3c61a] | 126 | } img_errno = IMG_NONE; |
---|
| 127 | #else |
---|
| 128 | static img_errcode img_errno = IMG_NONE; |
---|
[c0563da] | 129 | #endif |
---|
[d1b1380] | 130 | |
---|
[7d27e36] | 131 | #define FILEID "Survex 3D Image File" |
---|
| 132 | |
---|
[ad4eaf3f] | 133 | #define EXT_PLT "plt" |
---|
[d16db18] | 134 | #define EXT_PLF "plf" |
---|
[f1c7c9c7] | 135 | #define EXT_XYZ "xyz" |
---|
[ad4eaf3f] | 136 | |
---|
[0da6e60] | 137 | /* Attempt to string paste to ensure we are passed a literal string */ |
---|
| 138 | #define LITLEN(S) (sizeof(S"") - 1) |
---|
| 139 | |
---|
[7d27e36] | 140 | static char * |
---|
| 141 | my_strdup(const char *str) |
---|
[a420b49] | 142 | { |
---|
[7d27e36] | 143 | char *p; |
---|
| 144 | size_t len = strlen(str) + 1; |
---|
| 145 | p = xosmalloc(len); |
---|
| 146 | if (p) memcpy(p, str, len); |
---|
| 147 | return p; |
---|
[d1b1380] | 148 | } |
---|
[c0563da] | 149 | |
---|
[a2ad284] | 150 | static char * |
---|
| 151 | getline_alloc(FILE *fh) |
---|
| 152 | { |
---|
| 153 | int ch; |
---|
| 154 | size_t i = 0; |
---|
| 155 | size_t len = 16; |
---|
| 156 | char *buf = xosmalloc(len); |
---|
| 157 | if (!buf) return NULL; |
---|
| 158 | |
---|
| 159 | ch = getc(fh); |
---|
| 160 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 161 | buf[i++] = ch; |
---|
| 162 | if (i == len - 1) { |
---|
| 163 | char *p; |
---|
| 164 | len += len; |
---|
| 165 | p = xosrealloc(buf, len); |
---|
| 166 | if (!p) { |
---|
| 167 | osfree(buf); |
---|
| 168 | return NULL; |
---|
| 169 | } |
---|
| 170 | buf = p; |
---|
| 171 | } |
---|
| 172 | ch = getc(fh); |
---|
| 173 | } |
---|
| 174 | if (ch == '\n' || ch == '\r') { |
---|
[4ebc557] | 175 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
| 176 | ch = getc(fh); |
---|
| 177 | /* if it's not the other eol character, put it back */ |
---|
| 178 | if (ch != otherone) ungetc(ch, fh); |
---|
[a2ad284] | 179 | } |
---|
[421b7d2] | 180 | buf[i++] = '\0'; |
---|
[a2ad284] | 181 | return buf; |
---|
| 182 | } |
---|
| 183 | |
---|
[72af530] | 184 | #ifndef IMG_HOSTED |
---|
[a420b49] | 185 | img_errcode |
---|
| 186 | img_error(void) |
---|
| 187 | { |
---|
| 188 | return img_errno; |
---|
[d1b1380] | 189 | } |
---|
| 190 | #else |
---|
[a420b49] | 191 | int |
---|
| 192 | img_error(void) |
---|
| 193 | { |
---|
| 194 | return (int)img_errno; |
---|
[d1b1380] | 195 | } |
---|
| 196 | #endif |
---|
| 197 | |
---|
[d4d3655] | 198 | static bool |
---|
| 199 | check_label_space(img *pimg, size_t len) |
---|
| 200 | { |
---|
| 201 | if (len > pimg->buf_len) { |
---|
| 202 | char *b = xosrealloc(pimg->label_buf, len); |
---|
| 203 | if (!b) return fFalse; |
---|
| 204 | pimg->label = (pimg->label - pimg->label_buf) + b; |
---|
| 205 | pimg->label_buf = b; |
---|
| 206 | pimg->buf_len = len; |
---|
| 207 | } |
---|
| 208 | return fTrue; |
---|
| 209 | } |
---|
| 210 | |
---|
[d16db18] | 211 | #define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\ |
---|
| 212 | (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\ |
---|
| 213 | my_strcasecmp((F) + (L) - LITLEN(E), E) == 0) |
---|
| 214 | |
---|
[a420b49] | 215 | img * |
---|
[a2ad284] | 216 | img_open_survey(const char *fnm, const char *survey) |
---|
[a420b49] | 217 | { |
---|
| 218 | img *pimg; |
---|
[0da6e60] | 219 | size_t len; |
---|
[c5850207] | 220 | char buf[LITLEN(FILEID) + 9]; |
---|
[7d27e36] | 221 | int ch; |
---|
[a420b49] | 222 | |
---|
| 223 | if (fDirectory(fnm)) { |
---|
| 224 | img_errno = IMG_DIRECTORY; |
---|
| 225 | return NULL; |
---|
| 226 | } |
---|
[d1b1380] | 227 | |
---|
[a420b49] | 228 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 229 | if (pimg == NULL) { |
---|
| 230 | img_errno = IMG_OUTOFMEMORY; |
---|
| 231 | return NULL; |
---|
| 232 | } |
---|
[d1b1380] | 233 | |
---|
[23f7ea7] | 234 | pimg->buf_len = 257; |
---|
[0ff23cb] | 235 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 236 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 237 | osfree(pimg); |
---|
| 238 | img_errno = IMG_OUTOFMEMORY; |
---|
| 239 | return NULL; |
---|
| 240 | } |
---|
| 241 | |
---|
[a420b49] | 242 | pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL); |
---|
| 243 | if (pimg->fh == NULL) { |
---|
[ce1e5ac] | 244 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 245 | osfree(pimg); |
---|
[a420b49] | 246 | img_errno = IMG_FILENOTFOUND; |
---|
| 247 | return NULL; |
---|
| 248 | } |
---|
[d1b1380] | 249 | |
---|
[0da6e60] | 250 | pimg->fRead = fTrue; /* reading from this file */ |
---|
| 251 | img_errno = IMG_NONE; |
---|
| 252 | |
---|
| 253 | pimg->flags = 0; |
---|
| 254 | |
---|
| 255 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[4ebc557] | 256 | /* for version -2, 0 value indicates we haven't entered a survey yet */ |
---|
[f1c7c9c7] | 257 | /* for version -4, we store the last station here to detect whether |
---|
| 258 | * we MOVE or LINE */ |
---|
[0da6e60] | 259 | pimg->label_len = 0; |
---|
[f1c7c9c7] | 260 | pimg->label_buf[0] = '\0'; |
---|
[0da6e60] | 261 | |
---|
| 262 | pimg->survey = NULL; |
---|
| 263 | pimg->survey_len = 0; |
---|
[9f9c05e] | 264 | pimg->separator = '.'; |
---|
[0da6e60] | 265 | |
---|
[c637fad] | 266 | pimg->title = pimg->datestamp = NULL; |
---|
[0da6e60] | 267 | if (survey) { |
---|
| 268 | len = strlen(survey); |
---|
| 269 | if (len) { |
---|
| 270 | if (survey[len - 1] == '.') len--; |
---|
| 271 | if (len) { |
---|
| 272 | char *p; |
---|
| 273 | pimg->survey = osmalloc(len + 2); |
---|
| 274 | memcpy(pimg->survey, survey, len); |
---|
| 275 | /* Set title to leaf survey name */ |
---|
| 276 | pimg->survey[len] = '\0'; |
---|
| 277 | p = strchr(pimg->survey, '.'); |
---|
| 278 | if (p) p++; else p = pimg->survey; |
---|
[7d27e36] | 279 | pimg->title = my_strdup(p); |
---|
| 280 | if (!pimg->title) { |
---|
| 281 | img_errno = IMG_OUTOFMEMORY; |
---|
| 282 | goto error; |
---|
| 283 | } |
---|
[0da6e60] | 284 | pimg->survey[len] = '.'; |
---|
| 285 | pimg->survey[len + 1] = '\0'; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | pimg->survey_len = len; |
---|
| 289 | } |
---|
| 290 | |
---|
[f1c7c9c7] | 291 | /* [version -2, -3, -4] pending IMG_LINE or IMG_MOVE - both have 4 added |
---|
[ad4eaf3f] | 292 | * [version -1] already skipped heading line, or there wasn't one |
---|
[0da6e60] | 293 | * [version 0] not in the middle of a 'LINE' command |
---|
| 294 | * [version 3] not in the middle of turning a LINE into a MOVE |
---|
| 295 | */ |
---|
| 296 | pimg->pending = 0; |
---|
| 297 | |
---|
| 298 | len = strlen(fnm); |
---|
[d16db18] | 299 | if (has_ext(fnm, len, EXT_SVX_POS)) { |
---|
[f830e0d] | 300 | pos_file: |
---|
[0da6e60] | 301 | pimg->version = -1; |
---|
[984f49e] | 302 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[7d27e36] | 303 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 304 | if (!pimg->datestamp) { |
---|
| 305 | img_errno = IMG_OUTOFMEMORY; |
---|
| 306 | goto error; |
---|
| 307 | } |
---|
[58f02ae] | 308 | pimg->start = 0; |
---|
[0da6e60] | 309 | return pimg; |
---|
| 310 | } |
---|
[5757725] | 311 | |
---|
[d16db18] | 312 | if (has_ext(fnm, len, EXT_PLT) || has_ext(fnm, len, EXT_PLF)) { |
---|
[4ebc557] | 313 | long fpos; |
---|
[27bd06b] | 314 | plt_file: |
---|
[ad4eaf3f] | 315 | pimg->version = -2; |
---|
[9f9c05e] | 316 | /* Spaces aren't legal in Compass station names, but dots are, so |
---|
| 317 | * use space as the level separator */ |
---|
| 318 | pimg->separator = ' '; |
---|
[4ebc557] | 319 | pimg->start = 0; |
---|
[984f49e] | 320 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[ad4eaf3f] | 321 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 322 | if (!pimg->datestamp) { |
---|
| 323 | img_errno = IMG_OUTOFMEMORY; |
---|
| 324 | goto error; |
---|
| 325 | } |
---|
[4ebc557] | 326 | while (1) { |
---|
| 327 | int ch = getc(pimg->fh); |
---|
| 328 | switch (ch) { |
---|
| 329 | case '\x1a': |
---|
| 330 | fseek(pimg->fh, -1, SEEK_CUR); |
---|
| 331 | /* FALL THRU */ |
---|
| 332 | case EOF: |
---|
| 333 | pimg->start = ftell(pimg->fh); |
---|
| 334 | return pimg; |
---|
| 335 | case 'N': { |
---|
| 336 | char *line, *q; |
---|
| 337 | fpos = ftell(pimg->fh) - 1; |
---|
| 338 | if (!pimg->survey) { |
---|
| 339 | /* FIXME : if there's only one survey in the file, it'd be nice |
---|
| 340 | * to use its description as the title here... |
---|
| 341 | */ |
---|
[0d922fc] | 342 | ungetc('N', pimg->fh); |
---|
[4ebc557] | 343 | pimg->start = fpos; |
---|
[984f49e] | 344 | return pimg; |
---|
| 345 | } |
---|
[4ebc557] | 346 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 347 | if (!line) { |
---|
| 348 | img_errno = IMG_OUTOFMEMORY; |
---|
| 349 | goto error; |
---|
| 350 | } |
---|
[0d922fc] | 351 | len = 0; |
---|
[4ebc557] | 352 | while (line[len] > 32) ++len; |
---|
| 353 | if (pimg->survey_len != len || |
---|
| 354 | memcmp(line, pimg->survey, len) != 0) { |
---|
[ad4eaf3f] | 355 | osfree(line); |
---|
[4ebc557] | 356 | continue; |
---|
[ad4eaf3f] | 357 | } |
---|
[4ebc557] | 358 | q = strchr(line + len, 'C'); |
---|
| 359 | if (q && q[1]) { |
---|
| 360 | osfree(pimg->title); |
---|
[f1c7c9c7] | 361 | pimg->title = my_strdup(q + 1); |
---|
[4ebc557] | 362 | } else if (!pimg->title) { |
---|
[f1c7c9c7] | 363 | pimg->title = my_strdup(pimg->label); |
---|
[4ebc557] | 364 | } |
---|
| 365 | osfree(line); |
---|
| 366 | if (!pimg->title) { |
---|
| 367 | img_errno = IMG_OUTOFMEMORY; |
---|
| 368 | goto error; |
---|
| 369 | } |
---|
| 370 | if (!pimg->start) pimg->start = fpos; |
---|
[27bd06b] | 371 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[4ebc557] | 372 | return pimg; |
---|
| 373 | } |
---|
| 374 | case 'M': case 'D': |
---|
| 375 | pimg->start = ftell(pimg->fh) - 1; |
---|
| 376 | break; |
---|
[984f49e] | 377 | } |
---|
[4ebc557] | 378 | while (ch != '\n' && ch != '\r') { |
---|
| 379 | ch = getc(pimg->fh); |
---|
[984f49e] | 380 | } |
---|
| 381 | } |
---|
[ad4eaf3f] | 382 | } |
---|
[0da6e60] | 383 | |
---|
[d16db18] | 384 | if (has_ext(fnm, len, EXT_XYZ)) { |
---|
[f1c7c9c7] | 385 | char *line; |
---|
| 386 | xyz_file: |
---|
[9f9c05e] | 387 | /* Spaces aren't legal in CMAP station names, but dots are, so |
---|
| 388 | * use space as the level separator */ |
---|
| 389 | pimg->separator = ' '; |
---|
[f1c7c9c7] | 390 | line = getline_alloc(pimg->fh); |
---|
| 391 | if (!line) { |
---|
| 392 | img_errno = IMG_OUTOFMEMORY; |
---|
| 393 | goto error; |
---|
| 394 | } |
---|
| 395 | /* FIXME: reparse date? */ |
---|
| 396 | len = strlen(line); |
---|
| 397 | if (len > 59) line[59] = '\0'; |
---|
| 398 | if (len > 45) { |
---|
| 399 | pimg->datestamp = my_strdup(line + 45); |
---|
| 400 | } else { |
---|
| 401 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 402 | } |
---|
| 403 | if (strncmp(line, " Cave Survey Data Processed by CMAP ", |
---|
| 404 | LITLEN(" Cave Survey Data Processed by CMAP ")) == 0) { |
---|
| 405 | len = 0; |
---|
| 406 | } else { |
---|
| 407 | if (len > 45) { |
---|
| 408 | line[45] = '\0'; |
---|
| 409 | len = 45; |
---|
| 410 | } |
---|
| 411 | while (len > 2 && line[len - 1] == ' ') --len; |
---|
| 412 | if (len > 2) { |
---|
| 413 | line[len] = '\0'; |
---|
| 414 | pimg->title = my_strdup(line + 2); |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | if (len <= 2) pimg->title = baseleaf_from_fnm(fnm); |
---|
| 418 | osfree(line); |
---|
| 419 | if (!pimg->datestamp || !pimg->title) { |
---|
| 420 | img_errno = IMG_OUTOFMEMORY; |
---|
| 421 | goto error; |
---|
| 422 | } |
---|
| 423 | line = getline_alloc(pimg->fh); |
---|
| 424 | if (!line) { |
---|
| 425 | img_errno = IMG_OUTOFMEMORY; |
---|
| 426 | goto error; |
---|
| 427 | } |
---|
| 428 | if (line[0] != ' ' || (line[1] != 'S' && line[1] != 'O')) { |
---|
| 429 | img_errno = IMG_BADFORMAT; |
---|
| 430 | goto error; |
---|
| 431 | } |
---|
| 432 | if (line[1] == 'S') { |
---|
| 433 | pimg->version = -3; /* Station format */ |
---|
| 434 | } else { |
---|
| 435 | pimg->version = -4; /* Shot format */ |
---|
| 436 | } |
---|
| 437 | osfree(line); |
---|
| 438 | line = getline_alloc(pimg->fh); |
---|
| 439 | if (!line) { |
---|
| 440 | img_errno = IMG_OUTOFMEMORY; |
---|
| 441 | goto error; |
---|
| 442 | } |
---|
| 443 | if (line[0] != ' ' || line[1] != '-') { |
---|
| 444 | img_errno = IMG_BADFORMAT; |
---|
| 445 | goto error; |
---|
| 446 | } |
---|
| 447 | osfree(line); |
---|
| 448 | pimg->start = ftell(pimg->fh); |
---|
| 449 | return pimg; |
---|
| 450 | } |
---|
| 451 | |
---|
[7d27e36] | 452 | if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 || |
---|
[d16db18] | 453 | memcmp(buf, FILEID"\n", LITLEN(FILEID) + 1) != 0) { |
---|
[c5850207] | 454 | if (fread(buf + LITLEN(FILEID) + 1, 8, 1, pimg->fh) == 1 && |
---|
| 455 | memcmp(buf, FILEID"\r\nv0.01\r\n", LITLEN(FILEID) + 9) == 0) { |
---|
| 456 | /* v0 3d file with DOS EOLs */ |
---|
| 457 | pimg->version = 0; |
---|
| 458 | goto v03d; |
---|
| 459 | } |
---|
[d16db18] | 460 | rewind(pimg->fh); |
---|
[f1c7c9c7] | 461 | if (buf[1] == ' ') { |
---|
| 462 | if (buf[0] == ' ') { |
---|
| 463 | /* Looks like a CMAP .xyz file ... */ |
---|
| 464 | goto xyz_file; |
---|
| 465 | } else if (strchr("ZSNF", buf[0])) { |
---|
| 466 | /* Looks like a Compass .plt file ... */ |
---|
| 467 | /* Almost certainly it'll start "Z " */ |
---|
| 468 | goto plt_file; |
---|
| 469 | } |
---|
[27bd06b] | 470 | } |
---|
[f830e0d] | 471 | if (buf[0] == '(') { |
---|
| 472 | /* Looks like a Survex .pos file ... */ |
---|
| 473 | goto pos_file; |
---|
| 474 | } |
---|
[a420b49] | 475 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 476 | goto error; |
---|
[a420b49] | 477 | } |
---|
[d1b1380] | 478 | |
---|
[7d27e36] | 479 | /* check file format version */ |
---|
| 480 | ch = getc(pimg->fh); |
---|
| 481 | pimg->version = 0; |
---|
| 482 | if (tolower(ch) == 'b') { |
---|
| 483 | /* binary file iff B/b prefix */ |
---|
| 484 | pimg->version = 1; |
---|
| 485 | ch = getc(pimg->fh); |
---|
| 486 | } |
---|
| 487 | if (ch != 'v') { |
---|
| 488 | img_errno = IMG_BADFORMAT; |
---|
| 489 | goto error; |
---|
| 490 | } |
---|
| 491 | ch = getc(pimg->fh); |
---|
| 492 | if (ch == '0') { |
---|
| 493 | if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) { |
---|
| 494 | img_errno = IMG_BADFORMAT; |
---|
| 495 | goto error; |
---|
| 496 | } |
---|
[0da6e60] | 497 | /* nothing special to do */ |
---|
[7d27e36] | 498 | } else if (pimg->version == 0) { |
---|
| 499 | if (ch < '2' || ch > '3' || getc(pimg->fh) != '\n') { |
---|
[6c1c4f3] | 500 | img_errno = IMG_TOONEW; |
---|
[58f02ae] | 501 | goto error; |
---|
[6c1c4f3] | 502 | } |
---|
[7d27e36] | 503 | pimg->version = ch - '0'; |
---|
[5c3c61a] | 504 | } else { |
---|
[647407d] | 505 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 506 | goto error; |
---|
[a420b49] | 507 | } |
---|
[421b7d2] | 508 | |
---|
[c5850207] | 509 | v03d: |
---|
[4d3dfdf] | 510 | if (!pimg->title) |
---|
| 511 | pimg->title = getline_alloc(pimg->fh); |
---|
| 512 | else |
---|
| 513 | osfree(getline_alloc(pimg->fh)); |
---|
[a2ad284] | 514 | pimg->datestamp = getline_alloc(pimg->fh); |
---|
| 515 | if (!pimg->title || !pimg->datestamp) { |
---|
[58f02ae] | 516 | img_errno = IMG_OUTOFMEMORY; |
---|
| 517 | error: |
---|
[a2ad284] | 518 | osfree(pimg->title); |
---|
[984f49e] | 519 | osfree(pimg->datestamp); |
---|
[a2ad284] | 520 | fclose(pimg->fh); |
---|
| 521 | osfree(pimg); |
---|
| 522 | return NULL; |
---|
| 523 | } |
---|
| 524 | |
---|
[58f02ae] | 525 | pimg->start = ftell(pimg->fh); |
---|
[421b7d2] | 526 | |
---|
[a420b49] | 527 | return pimg; |
---|
[d1b1380] | 528 | } |
---|
| 529 | |
---|
[9f3e5df] | 530 | int |
---|
[f2588ca] | 531 | img_rewind(img *pimg) |
---|
| 532 | { |
---|
[9f3e5df] | 533 | if (!pimg->fRead) { |
---|
| 534 | img_errno = IMG_WRITEERROR; |
---|
| 535 | return 0; |
---|
| 536 | } |
---|
| 537 | if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) { |
---|
| 538 | img_errno = IMG_READERROR; |
---|
| 539 | return 0; |
---|
| 540 | } |
---|
[58f02ae] | 541 | clearerr(pimg->fh); |
---|
| 542 | /* [version -1] already skipped heading line, or there wasn't one |
---|
| 543 | * [version 0] not in the middle of a 'LINE' command |
---|
[76bbb7c9] | 544 | * [version 3] not in the middle of turning a LINE into a MOVE */ |
---|
[4ebc557] | 545 | pimg->pending = 0; |
---|
[d69255f] | 546 | |
---|
| 547 | img_errno = IMG_NONE; |
---|
[99cf51a] | 548 | |
---|
[5d7280f8] | 549 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[4ebc557] | 550 | /* for version -2, 0 value indicates we haven't entered a survey yet */ |
---|
[f1c7c9c7] | 551 | /* for version -4, we store the last station here to detect whether |
---|
| 552 | * we MOVE or LINE */ |
---|
[d69255f] | 553 | pimg->label_len = 0; |
---|
[9f3e5df] | 554 | return 1; |
---|
[f2588ca] | 555 | } |
---|
| 556 | |
---|
[a420b49] | 557 | img * |
---|
[fe8e80e] | 558 | img_open_write(const char *fnm, char *title_buf, bool fBinary) |
---|
[a420b49] | 559 | { |
---|
| 560 | time_t tm; |
---|
| 561 | img *pimg; |
---|
[d1b1380] | 562 | |
---|
[d69255f] | 563 | fBinary = fBinary; |
---|
| 564 | |
---|
[a420b49] | 565 | if (fDirectory(fnm)) { |
---|
| 566 | img_errno = IMG_DIRECTORY; |
---|
| 567 | return NULL; |
---|
| 568 | } |
---|
[d1b1380] | 569 | |
---|
[a420b49] | 570 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 571 | if (pimg == NULL) { |
---|
| 572 | img_errno = IMG_OUTOFMEMORY; |
---|
| 573 | return NULL; |
---|
| 574 | } |
---|
[d1b1380] | 575 | |
---|
[fe8e80e] | 576 | pimg->buf_len = 257; |
---|
[0ff23cb] | 577 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 578 | if (!pimg->label_buf) { |
---|
[fe8e80e] | 579 | osfree(pimg); |
---|
| 580 | img_errno = IMG_OUTOFMEMORY; |
---|
| 581 | return NULL; |
---|
| 582 | } |
---|
| 583 | |
---|
[a420b49] | 584 | pimg->fh = fopen(fnm, "wb"); |
---|
| 585 | if (!pimg->fh) { |
---|
[ce1e5ac] | 586 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 587 | osfree(pimg); |
---|
[a420b49] | 588 | img_errno = IMG_CANTOPENOUT; |
---|
| 589 | return NULL; |
---|
| 590 | } |
---|
[d1b1380] | 591 | |
---|
[a420b49] | 592 | /* Output image file header */ |
---|
| 593 | fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */ |
---|
[5c3c61a] | 594 | if (img_output_version < 2) { |
---|
[d69255f] | 595 | pimg->version = 1; |
---|
| 596 | fputs("Bv0.01\n", pimg->fh); /* binary file format version number */ |
---|
[5c3c61a] | 597 | } else { |
---|
[d69255f] | 598 | pimg->version = (img_output_version > 2) ? 3 : 2; |
---|
[5c3c61a] | 599 | fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */ |
---|
| 600 | } |
---|
[fe8e80e] | 601 | fputsnl(title_buf, pimg->fh); |
---|
[a420b49] | 602 | tm = time(NULL); |
---|
| 603 | if (tm == (time_t)-1) { |
---|
| 604 | fputsnl(TIMENA, pimg->fh); |
---|
| 605 | } else { |
---|
[7d27e36] | 606 | char date[256]; |
---|
[a420b49] | 607 | /* output current date and time in format specified */ |
---|
[7d27e36] | 608 | strftime(date, 256, TIMEFMT, localtime(&tm)); |
---|
| 609 | fputsnl(date, pimg->fh); |
---|
[a420b49] | 610 | } |
---|
| 611 | pimg->fRead = fFalse; /* writing to this file */ |
---|
| 612 | img_errno = IMG_NONE; |
---|
[d69255f] | 613 | |
---|
[5d7280f8] | 614 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[0ff23cb] | 615 | pimg->label_buf[0] = '\0'; |
---|
[d69255f] | 616 | pimg->label_len = 0; |
---|
| 617 | |
---|
[ad9a5cd] | 618 | /* Don't check for write errors now - let img_close() report them... */ |
---|
[a420b49] | 619 | return pimg; |
---|
[d1b1380] | 620 | } |
---|
| 621 | |
---|
[f1c7c9c7] | 622 | static void |
---|
| 623 | read_xyz_station_coords(img_point *pt, const char *line) |
---|
| 624 | { |
---|
| 625 | char num[12]; |
---|
| 626 | memcpy(num, line + 6, 9); |
---|
| 627 | num[9] = '\0'; |
---|
| 628 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 629 | memcpy(num, line + 15, 9); |
---|
| 630 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 631 | memcpy(num, line + 24, 8); |
---|
| 632 | num[8] = '\0'; |
---|
| 633 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | static void |
---|
| 637 | read_xyz_shot_coords(img_point *pt, const char *line) |
---|
| 638 | { |
---|
| 639 | char num[12]; |
---|
| 640 | memcpy(num, line + 40, 10); |
---|
| 641 | num[10] = '\0'; |
---|
| 642 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 643 | memcpy(num, line + 50, 10); |
---|
| 644 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 645 | memcpy(num, line + 60, 9); |
---|
| 646 | num[9] = '\0'; |
---|
| 647 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | static void |
---|
| 651 | subtract_xyz_shot_deltas(img_point *pt, const char *line) |
---|
| 652 | { |
---|
| 653 | char num[12]; |
---|
| 654 | memcpy(num, line + 15, 9); |
---|
| 655 | num[9] = '\0'; |
---|
| 656 | pt->x -= atof(num) / METRES_PER_FOOT; |
---|
| 657 | memcpy(num, line + 24, 8); |
---|
| 658 | num[8] = '\0'; |
---|
| 659 | pt->y -= atof(num) / METRES_PER_FOOT; |
---|
| 660 | memcpy(num, line + 32, 8); |
---|
| 661 | pt->z -= atof(num) / METRES_PER_FOOT; |
---|
| 662 | } |
---|
| 663 | |
---|
[ad9a5cd] | 664 | static int |
---|
| 665 | read_coord(FILE *fh, img_point *pt) |
---|
| 666 | { |
---|
[4c07c51] | 667 | SVX_ASSERT(fh); |
---|
| 668 | SVX_ASSERT(pt); |
---|
[ad9a5cd] | 669 | pt->x = get32(fh) / 100.0; |
---|
| 670 | pt->y = get32(fh) / 100.0; |
---|
| 671 | pt->z = get32(fh) / 100.0; |
---|
| 672 | if (ferror(fh) || feof(fh)) { |
---|
| 673 | img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 674 | return 0; |
---|
[421b7d2] | 675 | } |
---|
[ad9a5cd] | 676 | return 1; |
---|
| 677 | } |
---|
| 678 | |
---|
[4d3dfdf] | 679 | static int |
---|
| 680 | skip_coord(FILE *fh) |
---|
| 681 | { |
---|
| 682 | return (fseek(fh, 12, SEEK_CUR) == 0); |
---|
| 683 | } |
---|
| 684 | |
---|
[a420b49] | 685 | int |
---|
[23f7ea7] | 686 | img_read_item(img *pimg, img_point *p) |
---|
[a420b49] | 687 | { |
---|
| 688 | int result; |
---|
[95c3272] | 689 | pimg->flags = 0; |
---|
[d69255f] | 690 | |
---|
| 691 | if (pimg->version == 3) { |
---|
| 692 | int opt; |
---|
[76bbb7c9] | 693 | if (pimg->pending >= 0x80) { |
---|
| 694 | *p = pimg->mv; |
---|
| 695 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 696 | pimg->pending = 0; |
---|
[421b7d2] | 697 | return img_LINE; |
---|
[76bbb7c9] | 698 | } |
---|
[d69255f] | 699 | again3: /* label to goto if we get a prefix */ |
---|
[4d3dfdf] | 700 | pimg->label = pimg->label_buf; |
---|
[d69255f] | 701 | opt = getc(pimg->fh); |
---|
[ad9a5cd] | 702 | if (opt == EOF) { |
---|
| 703 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 704 | return img_BAD; |
---|
| 705 | } |
---|
[d69255f] | 706 | switch (opt >> 6) { |
---|
| 707 | case 0: |
---|
| 708 | if (opt == 0) { |
---|
| 709 | if (!pimg->label_len) return img_STOP; /* end of data marker */ |
---|
| 710 | pimg->label_len = 0; |
---|
| 711 | goto again3; |
---|
| 712 | } |
---|
| 713 | if (opt < 15) { |
---|
| 714 | /* 1-14 mean trim that many levels from current prefix */ |
---|
| 715 | int c; |
---|
| 716 | if (pimg->label_len <= 16) { |
---|
[4d3dfdf] | 717 | /* zero prefix using "0" */ |
---|
[d69255f] | 718 | img_errno = IMG_BADFORMAT; |
---|
| 719 | return img_BAD; |
---|
| 720 | } |
---|
[4b156b3] | 721 | c = pimg->label_len - 16 - 1; |
---|
[0ff23cb] | 722 | while (pimg->label_buf[c] != '.' || --opt > 0) { |
---|
[d69255f] | 723 | if (--c < 0) { |
---|
| 724 | /* zero prefix using "0" */ |
---|
| 725 | img_errno = IMG_BADFORMAT; |
---|
| 726 | return img_BAD; |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | c++; |
---|
| 730 | pimg->label_len = c; |
---|
| 731 | goto again3; |
---|
| 732 | } |
---|
| 733 | if (opt == 15) { |
---|
| 734 | result = img_MOVE; |
---|
| 735 | break; |
---|
| 736 | } |
---|
| 737 | /* 16-31 mean remove (n - 15) characters from the prefix */ |
---|
| 738 | /* zero prefix using 0 */ |
---|
[ad4eaf3f] | 739 | if (pimg->label_len <= (size_t)(opt - 15)) { |
---|
[d69255f] | 740 | img_errno = IMG_BADFORMAT; |
---|
| 741 | return img_BAD; |
---|
| 742 | } |
---|
[d245d71] | 743 | pimg->label_len -= (opt - 15); |
---|
[d69255f] | 744 | goto again3; |
---|
| 745 | case 1: case 2: { |
---|
| 746 | char *q; |
---|
[ad9a5cd] | 747 | long len = getc(pimg->fh); |
---|
| 748 | if (len == EOF) { |
---|
| 749 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 750 | return img_BAD; |
---|
| 751 | } |
---|
[d69255f] | 752 | if (len == 0xfe) { |
---|
| 753 | len += get16(pimg->fh); |
---|
[ad9a5cd] | 754 | if (feof(pimg->fh)) { |
---|
| 755 | img_errno = IMG_BADFORMAT; |
---|
| 756 | return img_BAD; |
---|
| 757 | } |
---|
| 758 | if (ferror(pimg->fh)) { |
---|
| 759 | img_errno = IMG_READERROR; |
---|
| 760 | return img_BAD; |
---|
| 761 | } |
---|
[d69255f] | 762 | } else if (len == 0xff) { |
---|
| 763 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 764 | if (ferror(pimg->fh)) { |
---|
| 765 | img_errno = IMG_READERROR; |
---|
| 766 | return img_BAD; |
---|
| 767 | } |
---|
| 768 | if (feof(pimg->fh) || len < 0xfe + 0xffff) { |
---|
[d69255f] | 769 | img_errno = IMG_BADFORMAT; |
---|
| 770 | return img_BAD; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
[6dd3a20] | 774 | if (!check_label_space(pimg, pimg->label_len + len + 1)) { |
---|
[d4d3655] | 775 | img_errno = IMG_OUTOFMEMORY; |
---|
| 776 | return img_BAD; |
---|
[d69255f] | 777 | } |
---|
[d4d3655] | 778 | q = pimg->label_buf + pimg->label_len; |
---|
[6dd3a20] | 779 | pimg->label_len += len; |
---|
[d69255f] | 780 | if (len && fread(q, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 781 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 782 | return img_BAD; |
---|
| 783 | } |
---|
| 784 | q[len] = '\0'; |
---|
[76bbb7c9] | 785 | |
---|
| 786 | result = opt & 0x40 ? img_LABEL : img_LINE; |
---|
| 787 | |
---|
| 788 | if (pimg->survey_len) { |
---|
| 789 | size_t l = pimg->survey_len; |
---|
[0ff23cb] | 790 | const char *s = pimg->label_buf; |
---|
[76bbb7c9] | 791 | if (result == img_LINE) { |
---|
| 792 | if (strncmp(pimg->survey, s, l) != 0 || |
---|
| 793 | !(s[l] == '.' || s[l] == '\0')) { |
---|
[421b7d2] | 794 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 795 | pimg->pending = 15; |
---|
[421b7d2] | 796 | goto again3; |
---|
[76bbb7c9] | 797 | } |
---|
| 798 | } else { |
---|
| 799 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
[4d3dfdf] | 800 | if (!skip_coord(pimg->fh)) return img_BAD; |
---|
| 801 | pimg->pending = 0; |
---|
[76bbb7c9] | 802 | goto again3; |
---|
| 803 | } |
---|
| 804 | } |
---|
[0ff23cb] | 805 | pimg->label += l; |
---|
| 806 | /* skip the dot if there */ |
---|
| 807 | if (*pimg->label) pimg->label++; |
---|
[76bbb7c9] | 808 | } |
---|
| 809 | |
---|
| 810 | if (result == img_LINE && pimg->pending) { |
---|
| 811 | *p = pimg->mv; |
---|
[ad9a5cd] | 812 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 813 | pimg->pending = opt; |
---|
| 814 | return img_MOVE; |
---|
| 815 | } |
---|
| 816 | pimg->flags = (int)opt & 0x3f; |
---|
[d69255f] | 817 | break; |
---|
| 818 | } |
---|
| 819 | default: |
---|
| 820 | img_errno = IMG_BADFORMAT; |
---|
| 821 | return img_BAD; |
---|
| 822 | } |
---|
[ad9a5cd] | 823 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
[76bbb7c9] | 824 | pimg->pending = 0; |
---|
[d69255f] | 825 | return result; |
---|
[4d3dfdf] | 826 | } |
---|
| 827 | |
---|
| 828 | pimg->label = pimg->label_buf; |
---|
| 829 | |
---|
| 830 | if (pimg->version > 0) { |
---|
[d69255f] | 831 | static long opt_lookahead = 0; |
---|
[ad9a5cd] | 832 | static img_point pt = { 0.0, 0.0, 0.0 }; |
---|
[a420b49] | 833 | long opt; |
---|
| 834 | again: /* label to goto if we get a cross */ |
---|
[5d7280f8] | 835 | pimg->label[0] = '\0'; |
---|
[ad9a5cd] | 836 | |
---|
[5c3c61a] | 837 | if (pimg->version == 1) { |
---|
[c4d4649] | 838 | if (opt_lookahead) { |
---|
| 839 | opt = opt_lookahead; |
---|
| 840 | opt_lookahead = 0; |
---|
| 841 | } else { |
---|
| 842 | opt = get32(pimg->fh); |
---|
| 843 | } |
---|
[5c3c61a] | 844 | } else { |
---|
[421b7d2] | 845 | opt = getc(pimg->fh); |
---|
[5c3c61a] | 846 | } |
---|
[ad9a5cd] | 847 | |
---|
| 848 | if (feof(pimg->fh)) { |
---|
| 849 | img_errno = IMG_BADFORMAT; |
---|
| 850 | return img_BAD; |
---|
| 851 | } |
---|
| 852 | if (ferror(pimg->fh)) { |
---|
| 853 | img_errno = IMG_READERROR; |
---|
| 854 | return img_BAD; |
---|
| 855 | } |
---|
| 856 | |
---|
[a420b49] | 857 | switch (opt) { |
---|
[437caf3] | 858 | case -1: case 0: |
---|
[a420b49] | 859 | return img_STOP; /* end of data marker */ |
---|
| 860 | case 1: |
---|
[ad9a5cd] | 861 | /* skip coordinates */ |
---|
[4d3dfdf] | 862 | if (!skip_coord(pimg->fh)) { |
---|
[ad9a5cd] | 863 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 864 | return img_BAD; |
---|
| 865 | } |
---|
[a420b49] | 866 | goto again; |
---|
[23f7ea7] | 867 | case 2: case 3: { |
---|
[fe8e80e] | 868 | char *q; |
---|
[3a1f8da] | 869 | int ch; |
---|
[437caf3] | 870 | result = img_LABEL; |
---|
[3a1f8da] | 871 | ch = getc(pimg->fh); |
---|
[d69255f] | 872 | if (ch == EOF) { |
---|
[ad9a5cd] | 873 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 874 | return img_BAD; |
---|
| 875 | } |
---|
[3a1f8da] | 876 | if (ch != '\\') ungetc(ch, pimg->fh); |
---|
[0ff23cb] | 877 | fgets(pimg->label_buf, 257, pimg->fh); |
---|
[421b7d2] | 878 | if (feof(pimg->fh)) { |
---|
[ad9a5cd] | 879 | img_errno = IMG_BADFORMAT; |
---|
| 880 | return img_BAD; |
---|
| 881 | } |
---|
[421b7d2] | 882 | if (ferror(pimg->fh)) { |
---|
[ad9a5cd] | 883 | img_errno = IMG_READERROR; |
---|
| 884 | return img_BAD; |
---|
| 885 | } |
---|
[0ff23cb] | 886 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[d69255f] | 887 | if (*q != '\n') { |
---|
| 888 | img_errno = IMG_BADFORMAT; |
---|
| 889 | return img_BAD; |
---|
| 890 | } |
---|
[9968ebf] | 891 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 892 | if (q == pimg->label_buf) goto again; |
---|
[fe8e80e] | 893 | *q = '\0'; |
---|
[509e099] | 894 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[437caf3] | 895 | if (opt == 2) goto done; |
---|
| 896 | break; |
---|
[23f7ea7] | 897 | } |
---|
[95c3272] | 898 | case 6: case 7: { |
---|
[ad9a5cd] | 899 | long len; |
---|
[23f7ea7] | 900 | result = img_LABEL; |
---|
[ad9a5cd] | 901 | |
---|
[509e099] | 902 | if (opt == 7) |
---|
| 903 | pimg->flags = getc(pimg->fh); |
---|
| 904 | else |
---|
| 905 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[ad9a5cd] | 906 | |
---|
[23f7ea7] | 907 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 908 | |
---|
| 909 | if (feof(pimg->fh)) { |
---|
| 910 | img_errno = IMG_BADFORMAT; |
---|
| 911 | return img_BAD; |
---|
| 912 | } |
---|
| 913 | if (ferror(pimg->fh)) { |
---|
| 914 | img_errno = IMG_READERROR; |
---|
| 915 | return img_BAD; |
---|
| 916 | } |
---|
| 917 | |
---|
[9968ebf] | 918 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
| 919 | if (len == 0) goto again; |
---|
[d4d3655] | 920 | if (!check_label_space(pimg, len + 1)) { |
---|
| 921 | img_errno = IMG_OUTOFMEMORY; |
---|
| 922 | return img_BAD; |
---|
[23f7ea7] | 923 | } |
---|
[0ff23cb] | 924 | if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 925 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 926 | return img_BAD; |
---|
| 927 | } |
---|
[c3e5ba5] | 928 | pimg->label_buf[len] = '\0'; |
---|
[23f7ea7] | 929 | break; |
---|
| 930 | } |
---|
[a420b49] | 931 | case 4: |
---|
| 932 | result = img_MOVE; |
---|
| 933 | break; |
---|
| 934 | case 5: |
---|
| 935 | result = img_LINE; |
---|
| 936 | break; |
---|
| 937 | default: |
---|
[95c3272] | 938 | switch ((int)opt & 0xc0) { |
---|
| 939 | case 0x80: |
---|
| 940 | pimg->flags = (int)opt & 0x3f; |
---|
| 941 | result = img_LINE; |
---|
| 942 | break; |
---|
| 943 | case 0x40: { |
---|
| 944 | char *q; |
---|
| 945 | pimg->flags = (int)opt & 0x3f; |
---|
| 946 | result = img_LABEL; |
---|
[0ff23cb] | 947 | if (!fgets(pimg->label_buf, 257, pimg->fh)) { |
---|
[ad9a5cd] | 948 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 949 | return img_BAD; |
---|
| 950 | } |
---|
[0ff23cb] | 951 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[9968ebf] | 952 | /* Ignore empty-labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 953 | if (q == pimg->label_buf) goto again; |
---|
[d69255f] | 954 | if (*q != '\n') { |
---|
| 955 | img_errno = IMG_BADFORMAT; |
---|
| 956 | return img_BAD; |
---|
| 957 | } |
---|
[95c3272] | 958 | *q = '\0'; |
---|
| 959 | break; |
---|
| 960 | } |
---|
| 961 | case 0xc0: |
---|
| 962 | /* use this for an extra leg or station flag if we need it */ |
---|
| 963 | default: |
---|
[d69255f] | 964 | img_errno = IMG_BADFORMAT; |
---|
[95c3272] | 965 | return img_BAD; |
---|
| 966 | } |
---|
[437caf3] | 967 | break; |
---|
[a420b49] | 968 | } |
---|
[ad9a5cd] | 969 | |
---|
| 970 | if (!read_coord(pimg->fh, &pt)) return img_BAD; |
---|
[76bbb7c9] | 971 | |
---|
[ed0f5b6] | 972 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 973 | if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 974 | goto again; |
---|
| 975 | pimg->label += pimg->survey_len + 1; |
---|
| 976 | } |
---|
[76bbb7c9] | 977 | |
---|
[a420b49] | 978 | done: |
---|
[ad9a5cd] | 979 | *p = pt; |
---|
[c4d4649] | 980 | |
---|
| 981 | if (result == img_MOVE && pimg->version == 1) { |
---|
[d69255f] | 982 | /* peek at next code and see if it's an old-style label */ |
---|
[23f7ea7] | 983 | opt_lookahead = get32(pimg->fh); |
---|
[ad9a5cd] | 984 | |
---|
| 985 | if (feof(pimg->fh)) { |
---|
| 986 | img_errno = IMG_BADFORMAT; |
---|
| 987 | return img_BAD; |
---|
| 988 | } |
---|
| 989 | if (ferror(pimg->fh)) { |
---|
| 990 | img_errno = IMG_READERROR; |
---|
| 991 | return img_BAD; |
---|
| 992 | } |
---|
| 993 | |
---|
[d69255f] | 994 | if (opt_lookahead == 2) return img_read_item(pimg, p); |
---|
[c4d4649] | 995 | } |
---|
[23f7ea7] | 996 | |
---|
[a420b49] | 997 | return result; |
---|
[0da6e60] | 998 | } else if (pimg->version == 0) { |
---|
[a420b49] | 999 | ascii_again: |
---|
[5d7280f8] | 1000 | pimg->label[0] = '\0'; |
---|
[a420b49] | 1001 | if (feof(pimg->fh)) return img_STOP; |
---|
[76bbb7c9] | 1002 | if (pimg->pending) { |
---|
| 1003 | pimg->pending = 0; |
---|
[a420b49] | 1004 | result = img_LINE; |
---|
| 1005 | } else { |
---|
[7d27e36] | 1006 | char cmd[7]; |
---|
[a420b49] | 1007 | /* Stop if nothing found */ |
---|
[7d27e36] | 1008 | if (fscanf(pimg->fh, "%6s", cmd) < 1) return img_STOP; |
---|
| 1009 | if (strcmp(cmd, "move") == 0) |
---|
[a420b49] | 1010 | result = img_MOVE; |
---|
[7d27e36] | 1011 | else if (strcmp(cmd, "draw") == 0) |
---|
[a420b49] | 1012 | result = img_LINE; |
---|
[7d27e36] | 1013 | else if (strcmp(cmd, "line") == 0) { |
---|
[bd1913f] | 1014 | /* set flag to indicate to process second triplet as LINE */ |
---|
[76bbb7c9] | 1015 | pimg->pending = 1; |
---|
[a420b49] | 1016 | result = img_MOVE; |
---|
[7d27e36] | 1017 | } else if (strcmp(cmd, "cross") == 0) { |
---|
[d69255f] | 1018 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1019 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[0ed0e16] | 1020 | return img_BAD; |
---|
[d69255f] | 1021 | } |
---|
[a420b49] | 1022 | goto ascii_again; |
---|
[7d27e36] | 1023 | } else if (strcmp(cmd, "name") == 0) { |
---|
[0da6e60] | 1024 | size_t off = 0; |
---|
[58f02ae] | 1025 | int ch = getc(pimg->fh); |
---|
| 1026 | if (ch == ' ') ch = getc(pimg->fh); |
---|
| 1027 | while (ch != ' ') { |
---|
| 1028 | if (ch == '\n' || ch == EOF) { |
---|
| 1029 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[ad9a5cd] | 1030 | return img_BAD; |
---|
| 1031 | } |
---|
[58f02ae] | 1032 | if (off == pimg->buf_len) { |
---|
[d4d3655] | 1033 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1034 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1035 | return img_BAD; |
---|
| 1036 | } |
---|
[0da6e60] | 1037 | } |
---|
[58f02ae] | 1038 | pimg->label_buf[off++] = ch; |
---|
| 1039 | ch = getc(pimg->fh); |
---|
[ad9a5cd] | 1040 | } |
---|
[58f02ae] | 1041 | pimg->label_buf[off] = '\0'; |
---|
| 1042 | |
---|
| 1043 | pimg->label = pimg->label_buf; |
---|
| 1044 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[0da6e60] | 1045 | |
---|
[a420b49] | 1046 | result = img_LABEL; |
---|
[d69255f] | 1047 | } else { |
---|
| 1048 | img_errno = IMG_BADFORMAT; |
---|
[a420b49] | 1049 | return img_BAD; /* unknown keyword */ |
---|
[d69255f] | 1050 | } |
---|
[a420b49] | 1051 | } |
---|
[d1b1380] | 1052 | |
---|
[d69255f] | 1053 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1054 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[fe8e80e] | 1055 | return img_BAD; |
---|
[d69255f] | 1056 | } |
---|
| 1057 | |
---|
[58f02ae] | 1058 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 1059 | if (strncmp(pimg->label, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 1060 | goto ascii_again; |
---|
| 1061 | pimg->label += pimg->survey_len + 1; |
---|
| 1062 | } |
---|
[c5850207] | 1063 | /* skip whitespace to end of line */ |
---|
| 1064 | { |
---|
| 1065 | int ch; |
---|
| 1066 | do { |
---|
| 1067 | ch = getc(pimg->fh); |
---|
| 1068 | } while (isspace((unsigned char)ch)); |
---|
| 1069 | if (ch != EOF) ungetc(ch, pimg->fh); |
---|
| 1070 | } |
---|
[76bbb7c9] | 1071 | |
---|
[a420b49] | 1072 | return result; |
---|
[ad4eaf3f] | 1073 | } else if (pimg->version == -1) { |
---|
[0da6e60] | 1074 | /* version -1: .pos file */ |
---|
[58f02ae] | 1075 | size_t off; |
---|
[5eea574] | 1076 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[58f02ae] | 1077 | againpos: |
---|
| 1078 | off = 0; |
---|
[0da6e60] | 1079 | while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) { |
---|
| 1080 | int ch; |
---|
| 1081 | if (ferror(pimg->fh)) { |
---|
| 1082 | img_errno = IMG_READERROR; |
---|
| 1083 | return img_BAD; |
---|
| 1084 | } |
---|
| 1085 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1086 | if (pimg->pending) { |
---|
| 1087 | img_errno = IMG_BADFORMAT; |
---|
| 1088 | return img_BAD; |
---|
| 1089 | } |
---|
| 1090 | pimg->pending = 1; |
---|
| 1091 | /* ignore rest of line */ |
---|
| 1092 | do { |
---|
| 1093 | ch = getc(pimg->fh); |
---|
| 1094 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1095 | } |
---|
| 1096 | |
---|
| 1097 | pimg->label_buf[0] = '\0'; |
---|
| 1098 | while (!feof(pimg->fh)) { |
---|
| 1099 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 1100 | img_errno = IMG_READERROR; |
---|
| 1101 | return img_BAD; |
---|
| 1102 | } |
---|
| 1103 | |
---|
| 1104 | off += strlen(pimg->label_buf + off); |
---|
| 1105 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 1106 | pimg->label_buf[off - 1] = '\0'; |
---|
| 1107 | break; |
---|
| 1108 | } |
---|
[d4d3655] | 1109 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1110 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1111 | return img_BAD; |
---|
| 1112 | } |
---|
[0da6e60] | 1113 | } |
---|
| 1114 | |
---|
[58f02ae] | 1115 | pimg->label = pimg->label_buf; |
---|
| 1116 | |
---|
[421b7d2] | 1117 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[58f02ae] | 1118 | |
---|
| 1119 | if (pimg->survey_len) { |
---|
| 1120 | size_t l = pimg->survey_len + 1; |
---|
| 1121 | if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos; |
---|
| 1122 | pimg->label += l; |
---|
| 1123 | } |
---|
| 1124 | |
---|
[0da6e60] | 1125 | return img_LABEL; |
---|
[f1c7c9c7] | 1126 | } else if (pimg->version == -2) { |
---|
[ad4eaf3f] | 1127 | /* version -2: Compass .plt file */ |
---|
[4ebc557] | 1128 | if (pimg->pending > 0) { |
---|
| 1129 | /* -1 signals we've entered the first survey we want to |
---|
| 1130 | * read, and need to fudge lots if the first action is 'D'... |
---|
| 1131 | */ |
---|
| 1132 | /* pending MOVE or LINE */ |
---|
[984f49e] | 1133 | int r = pimg->pending - 4; |
---|
[ad4eaf3f] | 1134 | pimg->pending = 0; |
---|
| 1135 | pimg->flags = 0; |
---|
[f710a3f] | 1136 | pimg->label[pimg->label_len] = '\0'; |
---|
[ad4eaf3f] | 1137 | return r; |
---|
| 1138 | } |
---|
| 1139 | |
---|
| 1140 | while (1) { |
---|
[4ebc557] | 1141 | char *line; |
---|
[ad4eaf3f] | 1142 | char *q; |
---|
| 1143 | size_t len = 0; |
---|
[4ebc557] | 1144 | int ch = getc(pimg->fh); |
---|
| 1145 | |
---|
| 1146 | switch (ch) { |
---|
| 1147 | case '\x1a': case EOF: /* Don't insist on ^Z at end of file */ |
---|
| 1148 | return img_STOP; |
---|
[27bd06b] | 1149 | case 'X': case 'F': case 'S': |
---|
| 1150 | /* bounding boX (marks end of survey), Feature survey, or |
---|
| 1151 | * new Section - skip to next survey */ |
---|
| 1152 | if (pimg->survey) return img_STOP; |
---|
| 1153 | skip_to_N: |
---|
| 1154 | while (1) { |
---|
| 1155 | do { |
---|
| 1156 | ch = getc(pimg->fh); |
---|
| 1157 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1158 | while (ch == '\n' || ch == '\r') ch = getc(pimg->fh); |
---|
| 1159 | if (ch == 'N') break; |
---|
| 1160 | if (ch == '\x1a' || ch == EOF) return img_STOP; |
---|
| 1161 | } |
---|
| 1162 | /* FALLTHRU */ |
---|
[4ebc557] | 1163 | case 'N': |
---|
| 1164 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1165 | if (!line) { |
---|
| 1166 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1167 | return img_BAD; |
---|
| 1168 | } |
---|
[9f9c05e] | 1169 | while (line[len] > 32) ++len; |
---|
[ef59f5e] | 1170 | if (pimg->label_len == 0) pimg->pending = -1; |
---|
[4ebc557] | 1171 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1172 | osfree(line); |
---|
| 1173 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1174 | return img_BAD; |
---|
| 1175 | } |
---|
| 1176 | pimg->label_len = len; |
---|
| 1177 | pimg->label = pimg->label_buf; |
---|
| 1178 | memcpy(pimg->label, line, len); |
---|
| 1179 | pimg->label[len] = '\0'; |
---|
| 1180 | osfree(line); |
---|
| 1181 | break; |
---|
| 1182 | case 'M': case 'D': { |
---|
[ad4eaf3f] | 1183 | /* Move or Draw */ |
---|
[4ebc557] | 1184 | long fpos = -1; |
---|
[27bd06b] | 1185 | if (pimg->survey && pimg->label_len == 0) { |
---|
[4ebc557] | 1186 | /* We're only holding onto this line in case the first line |
---|
[27bd06b] | 1187 | * of the 'N' is a 'D', so skip it for now... |
---|
[4ebc557] | 1188 | */ |
---|
[27bd06b] | 1189 | goto skip_to_N; |
---|
[4ebc557] | 1190 | } |
---|
| 1191 | if (ch == 'D' && pimg->pending == -1) { |
---|
[ef59f5e] | 1192 | if (pimg->survey) { |
---|
| 1193 | fpos = ftell(pimg->fh) - 1; |
---|
| 1194 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
| 1195 | ch = getc(pimg->fh); |
---|
| 1196 | pimg->pending = 0; |
---|
| 1197 | } else { |
---|
| 1198 | /* If a file actually has a 'D' before any 'M', then |
---|
| 1199 | * pretend the 'D' is an 'M' - one of the examples |
---|
| 1200 | * in the docs was like this! */ |
---|
| 1201 | ch = 'M'; |
---|
| 1202 | } |
---|
[4ebc557] | 1203 | } |
---|
| 1204 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1205 | if (!line) { |
---|
| 1206 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1207 | return img_BAD; |
---|
| 1208 | } |
---|
[88c780d] | 1209 | /* Compass store coordinates as North, East, Up = (y,x,z)! */ |
---|
| 1210 | if (sscanf(line, "%lf%lf%lf", &p->y, &p->x, &p->z) != 3) { |
---|
[ad4eaf3f] | 1211 | osfree(line); |
---|
| 1212 | if (ferror(pimg->fh)) { |
---|
| 1213 | img_errno = IMG_READERROR; |
---|
| 1214 | } else { |
---|
| 1215 | img_errno = IMG_BADFORMAT; |
---|
| 1216 | } |
---|
| 1217 | return img_BAD; |
---|
| 1218 | } |
---|
[a9ab65a] | 1219 | p->x *= METRES_PER_FOOT; |
---|
| 1220 | p->y *= METRES_PER_FOOT; |
---|
| 1221 | p->z *= METRES_PER_FOOT; |
---|
[ad4eaf3f] | 1222 | q = strchr(line, 'S'); |
---|
| 1223 | if (!q) { |
---|
| 1224 | osfree(line); |
---|
[5757725] | 1225 | img_errno = IMG_BADFORMAT; |
---|
[ad4eaf3f] | 1226 | return img_BAD; |
---|
| 1227 | } |
---|
| 1228 | ++q; |
---|
| 1229 | len = 0; |
---|
[9f9c05e] | 1230 | while (q[len] > ' ') ++len; |
---|
[ad4eaf3f] | 1231 | q[len] = '\0'; |
---|
[9f9c05e] | 1232 | len += 2; /* ' ' and '\0' */ |
---|
[d4d3655] | 1233 | if (!check_label_space(pimg, pimg->label_len + len)) { |
---|
| 1234 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1235 | return img_BAD; |
---|
[ad4eaf3f] | 1236 | } |
---|
| 1237 | pimg->label = pimg->label_buf; |
---|
[9f9c05e] | 1238 | pimg->label[pimg->label_len] = ' '; |
---|
[f710a3f] | 1239 | memcpy(pimg->label + pimg->label_len + 1, q, len - 1); |
---|
[ad4eaf3f] | 1240 | osfree(line); |
---|
| 1241 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[0d922fc] | 1242 | if (fpos != -1) { |
---|
| 1243 | fseek(pimg->fh, fpos, SEEK_SET); |
---|
| 1244 | } else { |
---|
| 1245 | pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4; |
---|
| 1246 | } |
---|
[ad4eaf3f] | 1247 | return img_LABEL; |
---|
[4ebc557] | 1248 | } |
---|
[ad4eaf3f] | 1249 | default: |
---|
| 1250 | img_errno = IMG_BADFORMAT; |
---|
| 1251 | return img_BAD; |
---|
| 1252 | } |
---|
| 1253 | } |
---|
[f1c7c9c7] | 1254 | } else { |
---|
| 1255 | /* version -3 or -4: CMAP .xyz file */ |
---|
| 1256 | char *line = NULL; |
---|
| 1257 | char *q; |
---|
| 1258 | size_t len; |
---|
| 1259 | |
---|
| 1260 | if (pimg->pending) { |
---|
| 1261 | /* pending MOVE or LINE or LABEL or STOP */ |
---|
| 1262 | int r = pimg->pending - 4; |
---|
[c3e5ba5] | 1263 | /* Set label to empty - don't use "" as we adjust label relative |
---|
| 1264 | * to label_buf when label_buf is reallocated. */ |
---|
| 1265 | pimg->label = pimg->label_buf + strlen(pimg->label_buf); |
---|
[f1c7c9c7] | 1266 | pimg->flags = 0; |
---|
| 1267 | if (r == img_LABEL) { |
---|
| 1268 | /* nasty magic */ |
---|
| 1269 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1270 | subtract_xyz_shot_deltas(p, pimg->label_buf + 16); |
---|
| 1271 | pimg->pending = img_STOP + 4; |
---|
| 1272 | return img_MOVE; |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | pimg->pending = 0; |
---|
| 1276 | |
---|
| 1277 | if (r == img_STOP) { |
---|
| 1278 | /* nasty magic */ |
---|
| 1279 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1280 | return img_LINE; |
---|
| 1281 | } |
---|
| 1282 | |
---|
| 1283 | return r; |
---|
| 1284 | } |
---|
| 1285 | |
---|
| 1286 | pimg->label = pimg->label_buf; |
---|
| 1287 | do { |
---|
| 1288 | osfree(line); |
---|
| 1289 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1290 | line = getline_alloc(pimg->fh); |
---|
| 1291 | if (!line) { |
---|
| 1292 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1293 | return img_BAD; |
---|
| 1294 | } |
---|
| 1295 | } while (line[0] == ' ' || line[0] == '\0'); |
---|
| 1296 | if (line[0] == '\x1a') return img_STOP; |
---|
| 1297 | |
---|
| 1298 | len = strlen(line); |
---|
| 1299 | if (pimg->version == -3) { |
---|
| 1300 | /* station variant */ |
---|
| 1301 | if (len < 37) { |
---|
| 1302 | osfree(line); |
---|
| 1303 | img_errno = IMG_BADFORMAT; |
---|
| 1304 | return img_BAD; |
---|
| 1305 | } |
---|
| 1306 | memcpy(pimg->label, line, 6); |
---|
| 1307 | q = memchr(pimg->label, ' ', 6); |
---|
| 1308 | if (!q) q = pimg->label + 6; |
---|
| 1309 | *q = '\0'; |
---|
| 1310 | |
---|
| 1311 | read_xyz_station_coords(p, line); |
---|
[9f9c05e] | 1312 | |
---|
[f1c7c9c7] | 1313 | /* FIXME: look at prev for lines (line + 32, 5) */ |
---|
| 1314 | /* FIXME: duplicate stations... */ |
---|
| 1315 | return img_LABEL; |
---|
| 1316 | } else { |
---|
| 1317 | /* Shot variant */ |
---|
| 1318 | char old[8], new[8]; |
---|
| 1319 | if (len < 61) { |
---|
| 1320 | osfree(line); |
---|
| 1321 | img_errno = IMG_BADFORMAT; |
---|
| 1322 | return img_BAD; |
---|
| 1323 | } |
---|
[9f9c05e] | 1324 | |
---|
[f1c7c9c7] | 1325 | memcpy(old, line, 7); |
---|
| 1326 | q = memchr(old, ' ', 7); |
---|
| 1327 | if (!q) q = old + 7; |
---|
| 1328 | *q = '\0'; |
---|
[9f9c05e] | 1329 | |
---|
[f1c7c9c7] | 1330 | memcpy(new, line + 7, 7); |
---|
| 1331 | q = memchr(new, ' ', 7); |
---|
| 1332 | if (!q) q = new + 7; |
---|
| 1333 | *q = '\0'; |
---|
[9f9c05e] | 1334 | |
---|
[f1c7c9c7] | 1335 | pimg->flags = img_SFLAG_UNDERGROUND; |
---|
| 1336 | |
---|
| 1337 | if (strcmp(old, new) == 0) { |
---|
| 1338 | pimg->pending = img_MOVE + 4; |
---|
| 1339 | read_xyz_shot_coords(p, line); |
---|
| 1340 | strcpy(pimg->label, new); |
---|
| 1341 | osfree(line); |
---|
| 1342 | return img_LABEL; |
---|
| 1343 | } |
---|
[9f9c05e] | 1344 | |
---|
[f1c7c9c7] | 1345 | if (strcmp(old, pimg->label) == 0) { |
---|
| 1346 | pimg->pending = img_LINE + 4; |
---|
| 1347 | read_xyz_shot_coords(p, line); |
---|
| 1348 | strcpy(pimg->label, new); |
---|
| 1349 | osfree(line); |
---|
| 1350 | return img_LABEL; |
---|
| 1351 | } |
---|
| 1352 | |
---|
| 1353 | pimg->pending = img_LABEL + 4; |
---|
| 1354 | read_xyz_shot_coords(p, line); |
---|
| 1355 | strcpy(pimg->label, new); |
---|
| 1356 | memcpy(pimg->label + 16, line, 70); |
---|
| 1357 | |
---|
| 1358 | osfree(line); |
---|
| 1359 | return img_LABEL; |
---|
| 1360 | } |
---|
[a420b49] | 1361 | } |
---|
[d1b1380] | 1362 | } |
---|
| 1363 | |
---|
[ad9a5cd] | 1364 | static int |
---|
[d69255f] | 1365 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 1366 | { |
---|
| 1367 | size_t len, n, dot; |
---|
| 1368 | |
---|
| 1369 | /* find length of common prefix */ |
---|
| 1370 | dot = 0; |
---|
[0ff23cb] | 1371 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 1372 | if (s[len] == '.') dot = len + 1; |
---|
| 1373 | } |
---|
| 1374 | |
---|
[4c07c51] | 1375 | SVX_ASSERT(len <= pimg->label_len); |
---|
[d69255f] | 1376 | n = pimg->label_len - len; |
---|
| 1377 | if (len == 0) { |
---|
| 1378 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1379 | } else if (n <= 16) { |
---|
| 1380 | if (n) putc(n + 15, pimg->fh); |
---|
| 1381 | } else if (dot == 0) { |
---|
| 1382 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[99cf51a] | 1383 | len = 0; |
---|
[d69255f] | 1384 | } else { |
---|
[0ff23cb] | 1385 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 1386 | n = 1; |
---|
| 1387 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 1388 | if (*p++ == '.') n++; |
---|
| 1389 | } |
---|
| 1390 | if (n <= 14) { |
---|
| 1391 | putc(n, pimg->fh); |
---|
| 1392 | len = dot; |
---|
| 1393 | } else { |
---|
| 1394 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1395 | len = 0; |
---|
| 1396 | } |
---|
| 1397 | } |
---|
| 1398 | |
---|
| 1399 | n = strlen(s + len); |
---|
| 1400 | putc(opt, pimg->fh); |
---|
| 1401 | if (n < 0xfe) { |
---|
| 1402 | putc(n, pimg->fh); |
---|
| 1403 | } else if (n < 0xffff + 0xfe) { |
---|
| 1404 | putc(0xfe, pimg->fh); |
---|
| 1405 | put16(n - 0xfe, pimg->fh); |
---|
| 1406 | } else { |
---|
| 1407 | putc(0xff, pimg->fh); |
---|
| 1408 | put32(n, pimg->fh); |
---|
| 1409 | } |
---|
| 1410 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 1411 | |
---|
| 1412 | n += len; |
---|
| 1413 | pimg->label_len = n; |
---|
[d4d3655] | 1414 | if (!check_label_space(pimg, n + 1)) |
---|
| 1415 | return 0; /* FIXME: distinguish out of memory... */ |
---|
[0ff23cb] | 1416 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 1417 | |
---|
| 1418 | return !ferror(pimg->fh); |
---|
[d69255f] | 1419 | } |
---|
| 1420 | |
---|
[a420b49] | 1421 | void |
---|
[95c3272] | 1422 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 1423 | double x, double y, double z) |
---|
[a420b49] | 1424 | { |
---|
[647407d] | 1425 | if (!pimg) return; |
---|
[d69255f] | 1426 | if (pimg->version == 3) { |
---|
| 1427 | int opt = 0; |
---|
| 1428 | switch (code) { |
---|
| 1429 | case img_LABEL: |
---|
| 1430 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 1431 | opt = 0; |
---|
| 1432 | break; |
---|
| 1433 | case img_MOVE: |
---|
| 1434 | opt = 15; |
---|
| 1435 | break; |
---|
| 1436 | case img_LINE: |
---|
| 1437 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 1438 | opt = 0; |
---|
| 1439 | break; |
---|
| 1440 | default: /* ignore for now */ |
---|
| 1441 | return; |
---|
| 1442 | } |
---|
| 1443 | if (opt) putc(opt, pimg->fh); |
---|
| 1444 | /* Output in cm */ |
---|
[dd1d6369] | 1445 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1446 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1447 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[d69255f] | 1448 | } else { |
---|
[23f7ea7] | 1449 | size_t len; |
---|
[badeec8] | 1450 | INT32_T opt = 0; |
---|
[4c07c51] | 1451 | SVX_ASSERT(pimg->version > 0); |
---|
[a420b49] | 1452 | switch (code) { |
---|
[437caf3] | 1453 | case img_LABEL: |
---|
| 1454 | if (pimg->version == 1) { |
---|
| 1455 | /* put a move before each label */ |
---|
[95c3272] | 1456 | img_write_item(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[437caf3] | 1457 | put32(2, pimg->fh); |
---|
[0ed0e16] | 1458 | fputsnl(s, pimg->fh); |
---|
[437caf3] | 1459 | return; |
---|
| 1460 | } |
---|
[23f7ea7] | 1461 | len = strlen(s); |
---|
[af56069] | 1462 | if (len > 255 || strchr(s, '\n')) { |
---|
[23f7ea7] | 1463 | /* long label - not in early incarnations of v2 format, but few |
---|
| 1464 | * 3d files will need these, so better not to force incompatibility |
---|
| 1465 | * with a new version I think... */ |
---|
[95c3272] | 1466 | putc(7, pimg->fh); |
---|
| 1467 | putc(flags, pimg->fh); |
---|
[23f7ea7] | 1468 | put32(len, pimg->fh); |
---|
| 1469 | fputs(s, pimg->fh); |
---|
| 1470 | } else { |
---|
[95c3272] | 1471 | putc(0x40 | (flags & 0x3f), pimg->fh); |
---|
[23f7ea7] | 1472 | fputsnl(s, pimg->fh); |
---|
| 1473 | } |
---|
[437caf3] | 1474 | opt = 0; |
---|
[23f7ea7] | 1475 | break; |
---|
[a420b49] | 1476 | case img_MOVE: |
---|
| 1477 | opt = 4; |
---|
| 1478 | break; |
---|
| 1479 | case img_LINE: |
---|
[421b7d2] | 1480 | if (pimg->version > 1) { |
---|
[d69255f] | 1481 | opt = 0x80 | (flags & 0x3f); |
---|
[437caf3] | 1482 | break; |
---|
[421b7d2] | 1483 | } |
---|
[437caf3] | 1484 | opt = 5; |
---|
[a420b49] | 1485 | break; |
---|
[437caf3] | 1486 | default: /* ignore for now */ |
---|
| 1487 | return; |
---|
[a420b49] | 1488 | } |
---|
[437caf3] | 1489 | if (pimg->version == 1) { |
---|
| 1490 | put32(opt, pimg->fh); |
---|
| 1491 | } else { |
---|
| 1492 | if (opt) putc(opt, pimg->fh); |
---|
[a420b49] | 1493 | } |
---|
[d69255f] | 1494 | /* Output in cm */ |
---|
[dd1d6369] | 1495 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1496 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1497 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[a420b49] | 1498 | } |
---|
[d1b1380] | 1499 | } |
---|
| 1500 | |
---|
[ad9a5cd] | 1501 | int |
---|
[a420b49] | 1502 | img_close(img *pimg) |
---|
| 1503 | { |
---|
[ad9a5cd] | 1504 | int result = 1; |
---|
[dbb4e19] | 1505 | if (pimg) { |
---|
| 1506 | if (pimg->fh) { |
---|
[76bbb7c9] | 1507 | if (pimg->fRead) { |
---|
| 1508 | osfree(pimg->survey); |
---|
[a2ad284] | 1509 | osfree(pimg->title); |
---|
| 1510 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 1511 | } else { |
---|
| 1512 | /* write end of data marker */ |
---|
[d69255f] | 1513 | switch (pimg->version) { |
---|
| 1514 | case 1: |
---|
[5c3c61a] | 1515 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 1516 | break; |
---|
| 1517 | case 2: |
---|
| 1518 | putc(0, pimg->fh); |
---|
| 1519 | break; |
---|
| 1520 | case 3: |
---|
| 1521 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[437caf3] | 1522 | putc(0, pimg->fh); |
---|
[d69255f] | 1523 | break; |
---|
[5c3c61a] | 1524 | } |
---|
| 1525 | } |
---|
[ad9a5cd] | 1526 | if (ferror(pimg->fh)) result = 0; |
---|
| 1527 | if (fclose(pimg->fh)) result = 0; |
---|
[0d922fc] | 1528 | if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 1529 | } |
---|
[0ff23cb] | 1530 | osfree(pimg->label_buf); |
---|
[dbb4e19] | 1531 | osfree(pimg); |
---|
[a420b49] | 1532 | } |
---|
[ad9a5cd] | 1533 | return result; |
---|
[d1b1380] | 1534 | } |
---|