[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 | } |
---|
[76bbb7c9] | 1063 | |
---|
[a420b49] | 1064 | return result; |
---|
[ad4eaf3f] | 1065 | } else if (pimg->version == -1) { |
---|
[0da6e60] | 1066 | /* version -1: .pos file */ |
---|
[58f02ae] | 1067 | size_t off; |
---|
[5eea574] | 1068 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[58f02ae] | 1069 | againpos: |
---|
| 1070 | off = 0; |
---|
[0da6e60] | 1071 | while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) { |
---|
| 1072 | int ch; |
---|
| 1073 | if (ferror(pimg->fh)) { |
---|
| 1074 | img_errno = IMG_READERROR; |
---|
| 1075 | return img_BAD; |
---|
| 1076 | } |
---|
| 1077 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1078 | if (pimg->pending) { |
---|
| 1079 | img_errno = IMG_BADFORMAT; |
---|
| 1080 | return img_BAD; |
---|
| 1081 | } |
---|
| 1082 | pimg->pending = 1; |
---|
| 1083 | /* ignore rest of line */ |
---|
| 1084 | do { |
---|
| 1085 | ch = getc(pimg->fh); |
---|
| 1086 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1087 | } |
---|
| 1088 | |
---|
| 1089 | pimg->label_buf[0] = '\0'; |
---|
| 1090 | while (!feof(pimg->fh)) { |
---|
| 1091 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 1092 | img_errno = IMG_READERROR; |
---|
| 1093 | return img_BAD; |
---|
| 1094 | } |
---|
| 1095 | |
---|
| 1096 | off += strlen(pimg->label_buf + off); |
---|
| 1097 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 1098 | pimg->label_buf[off - 1] = '\0'; |
---|
| 1099 | break; |
---|
| 1100 | } |
---|
[d4d3655] | 1101 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1102 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1103 | return img_BAD; |
---|
| 1104 | } |
---|
[0da6e60] | 1105 | } |
---|
| 1106 | |
---|
[58f02ae] | 1107 | pimg->label = pimg->label_buf; |
---|
| 1108 | |
---|
[421b7d2] | 1109 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[58f02ae] | 1110 | |
---|
| 1111 | if (pimg->survey_len) { |
---|
| 1112 | size_t l = pimg->survey_len + 1; |
---|
| 1113 | if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos; |
---|
| 1114 | pimg->label += l; |
---|
| 1115 | } |
---|
| 1116 | |
---|
[0da6e60] | 1117 | return img_LABEL; |
---|
[f1c7c9c7] | 1118 | } else if (pimg->version == -2) { |
---|
[ad4eaf3f] | 1119 | /* version -2: Compass .plt file */ |
---|
[4ebc557] | 1120 | if (pimg->pending > 0) { |
---|
| 1121 | /* -1 signals we've entered the first survey we want to |
---|
| 1122 | * read, and need to fudge lots if the first action is 'D'... |
---|
| 1123 | */ |
---|
| 1124 | /* pending MOVE or LINE */ |
---|
[984f49e] | 1125 | int r = pimg->pending - 4; |
---|
[ad4eaf3f] | 1126 | pimg->pending = 0; |
---|
| 1127 | pimg->flags = 0; |
---|
[f710a3f] | 1128 | pimg->label[pimg->label_len] = '\0'; |
---|
[ad4eaf3f] | 1129 | return r; |
---|
| 1130 | } |
---|
| 1131 | |
---|
| 1132 | while (1) { |
---|
[4ebc557] | 1133 | char *line; |
---|
[ad4eaf3f] | 1134 | char *q; |
---|
| 1135 | size_t len = 0; |
---|
[4ebc557] | 1136 | int ch = getc(pimg->fh); |
---|
| 1137 | |
---|
| 1138 | switch (ch) { |
---|
| 1139 | case '\x1a': case EOF: /* Don't insist on ^Z at end of file */ |
---|
| 1140 | return img_STOP; |
---|
[27bd06b] | 1141 | case 'X': case 'F': case 'S': |
---|
| 1142 | /* bounding boX (marks end of survey), Feature survey, or |
---|
| 1143 | * new Section - skip to next survey */ |
---|
| 1144 | if (pimg->survey) return img_STOP; |
---|
| 1145 | skip_to_N: |
---|
| 1146 | while (1) { |
---|
| 1147 | do { |
---|
| 1148 | ch = getc(pimg->fh); |
---|
| 1149 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1150 | while (ch == '\n' || ch == '\r') ch = getc(pimg->fh); |
---|
| 1151 | if (ch == 'N') break; |
---|
| 1152 | if (ch == '\x1a' || ch == EOF) return img_STOP; |
---|
| 1153 | } |
---|
| 1154 | /* FALLTHRU */ |
---|
[4ebc557] | 1155 | case 'N': |
---|
| 1156 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1157 | if (!line) { |
---|
| 1158 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1159 | return img_BAD; |
---|
| 1160 | } |
---|
[9f9c05e] | 1161 | while (line[len] > 32) ++len; |
---|
[ef59f5e] | 1162 | if (pimg->label_len == 0) pimg->pending = -1; |
---|
[4ebc557] | 1163 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1164 | osfree(line); |
---|
| 1165 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1166 | return img_BAD; |
---|
| 1167 | } |
---|
| 1168 | pimg->label_len = len; |
---|
| 1169 | pimg->label = pimg->label_buf; |
---|
| 1170 | memcpy(pimg->label, line, len); |
---|
| 1171 | pimg->label[len] = '\0'; |
---|
| 1172 | osfree(line); |
---|
| 1173 | break; |
---|
| 1174 | case 'M': case 'D': { |
---|
[ad4eaf3f] | 1175 | /* Move or Draw */ |
---|
[4ebc557] | 1176 | long fpos = -1; |
---|
[27bd06b] | 1177 | if (pimg->survey && pimg->label_len == 0) { |
---|
[4ebc557] | 1178 | /* We're only holding onto this line in case the first line |
---|
[27bd06b] | 1179 | * of the 'N' is a 'D', so skip it for now... |
---|
[4ebc557] | 1180 | */ |
---|
[27bd06b] | 1181 | goto skip_to_N; |
---|
[4ebc557] | 1182 | } |
---|
| 1183 | if (ch == 'D' && pimg->pending == -1) { |
---|
[ef59f5e] | 1184 | if (pimg->survey) { |
---|
| 1185 | fpos = ftell(pimg->fh) - 1; |
---|
| 1186 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
| 1187 | ch = getc(pimg->fh); |
---|
| 1188 | pimg->pending = 0; |
---|
| 1189 | } else { |
---|
| 1190 | /* If a file actually has a 'D' before any 'M', then |
---|
| 1191 | * pretend the 'D' is an 'M' - one of the examples |
---|
| 1192 | * in the docs was like this! */ |
---|
| 1193 | ch = 'M'; |
---|
| 1194 | } |
---|
[4ebc557] | 1195 | } |
---|
| 1196 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1197 | if (!line) { |
---|
| 1198 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1199 | return img_BAD; |
---|
| 1200 | } |
---|
[88c780d] | 1201 | /* Compass store coordinates as North, East, Up = (y,x,z)! */ |
---|
| 1202 | if (sscanf(line, "%lf%lf%lf", &p->y, &p->x, &p->z) != 3) { |
---|
[ad4eaf3f] | 1203 | osfree(line); |
---|
| 1204 | if (ferror(pimg->fh)) { |
---|
| 1205 | img_errno = IMG_READERROR; |
---|
| 1206 | } else { |
---|
| 1207 | img_errno = IMG_BADFORMAT; |
---|
| 1208 | } |
---|
| 1209 | return img_BAD; |
---|
| 1210 | } |
---|
[a9ab65a] | 1211 | p->x *= METRES_PER_FOOT; |
---|
| 1212 | p->y *= METRES_PER_FOOT; |
---|
| 1213 | p->z *= METRES_PER_FOOT; |
---|
[ad4eaf3f] | 1214 | q = strchr(line, 'S'); |
---|
| 1215 | if (!q) { |
---|
| 1216 | osfree(line); |
---|
[5757725] | 1217 | img_errno = IMG_BADFORMAT; |
---|
[ad4eaf3f] | 1218 | return img_BAD; |
---|
| 1219 | } |
---|
| 1220 | ++q; |
---|
| 1221 | len = 0; |
---|
[9f9c05e] | 1222 | while (q[len] > ' ') ++len; |
---|
[ad4eaf3f] | 1223 | q[len] = '\0'; |
---|
[9f9c05e] | 1224 | len += 2; /* ' ' and '\0' */ |
---|
[d4d3655] | 1225 | if (!check_label_space(pimg, pimg->label_len + len)) { |
---|
| 1226 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1227 | return img_BAD; |
---|
[ad4eaf3f] | 1228 | } |
---|
| 1229 | pimg->label = pimg->label_buf; |
---|
[9f9c05e] | 1230 | pimg->label[pimg->label_len] = ' '; |
---|
[f710a3f] | 1231 | memcpy(pimg->label + pimg->label_len + 1, q, len - 1); |
---|
[ad4eaf3f] | 1232 | osfree(line); |
---|
| 1233 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[0d922fc] | 1234 | if (fpos != -1) { |
---|
| 1235 | fseek(pimg->fh, fpos, SEEK_SET); |
---|
| 1236 | } else { |
---|
| 1237 | pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4; |
---|
| 1238 | } |
---|
[ad4eaf3f] | 1239 | return img_LABEL; |
---|
[4ebc557] | 1240 | } |
---|
[ad4eaf3f] | 1241 | default: |
---|
| 1242 | img_errno = IMG_BADFORMAT; |
---|
| 1243 | return img_BAD; |
---|
| 1244 | } |
---|
| 1245 | } |
---|
[f1c7c9c7] | 1246 | } else { |
---|
| 1247 | /* version -3 or -4: CMAP .xyz file */ |
---|
| 1248 | char *line = NULL; |
---|
| 1249 | char *q; |
---|
| 1250 | size_t len; |
---|
| 1251 | |
---|
| 1252 | if (pimg->pending) { |
---|
| 1253 | /* pending MOVE or LINE or LABEL or STOP */ |
---|
| 1254 | int r = pimg->pending - 4; |
---|
[c3e5ba5] | 1255 | /* Set label to empty - don't use "" as we adjust label relative |
---|
| 1256 | * to label_buf when label_buf is reallocated. */ |
---|
| 1257 | pimg->label = pimg->label_buf + strlen(pimg->label_buf); |
---|
[f1c7c9c7] | 1258 | pimg->flags = 0; |
---|
| 1259 | if (r == img_LABEL) { |
---|
| 1260 | /* nasty magic */ |
---|
| 1261 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1262 | subtract_xyz_shot_deltas(p, pimg->label_buf + 16); |
---|
| 1263 | pimg->pending = img_STOP + 4; |
---|
| 1264 | return img_MOVE; |
---|
| 1265 | } |
---|
| 1266 | |
---|
| 1267 | pimg->pending = 0; |
---|
| 1268 | |
---|
| 1269 | if (r == img_STOP) { |
---|
| 1270 | /* nasty magic */ |
---|
| 1271 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1272 | return img_LINE; |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | return r; |
---|
| 1276 | } |
---|
| 1277 | |
---|
| 1278 | pimg->label = pimg->label_buf; |
---|
| 1279 | do { |
---|
| 1280 | osfree(line); |
---|
| 1281 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1282 | line = getline_alloc(pimg->fh); |
---|
| 1283 | if (!line) { |
---|
| 1284 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1285 | return img_BAD; |
---|
| 1286 | } |
---|
| 1287 | } while (line[0] == ' ' || line[0] == '\0'); |
---|
| 1288 | if (line[0] == '\x1a') return img_STOP; |
---|
| 1289 | |
---|
| 1290 | len = strlen(line); |
---|
| 1291 | if (pimg->version == -3) { |
---|
| 1292 | /* station variant */ |
---|
| 1293 | if (len < 37) { |
---|
| 1294 | osfree(line); |
---|
| 1295 | img_errno = IMG_BADFORMAT; |
---|
| 1296 | return img_BAD; |
---|
| 1297 | } |
---|
| 1298 | memcpy(pimg->label, line, 6); |
---|
| 1299 | q = memchr(pimg->label, ' ', 6); |
---|
| 1300 | if (!q) q = pimg->label + 6; |
---|
| 1301 | *q = '\0'; |
---|
| 1302 | |
---|
| 1303 | read_xyz_station_coords(p, line); |
---|
[9f9c05e] | 1304 | |
---|
[f1c7c9c7] | 1305 | /* FIXME: look at prev for lines (line + 32, 5) */ |
---|
| 1306 | /* FIXME: duplicate stations... */ |
---|
| 1307 | return img_LABEL; |
---|
| 1308 | } else { |
---|
| 1309 | /* Shot variant */ |
---|
| 1310 | char old[8], new[8]; |
---|
| 1311 | if (len < 61) { |
---|
| 1312 | osfree(line); |
---|
| 1313 | img_errno = IMG_BADFORMAT; |
---|
| 1314 | return img_BAD; |
---|
| 1315 | } |
---|
[9f9c05e] | 1316 | |
---|
[f1c7c9c7] | 1317 | memcpy(old, line, 7); |
---|
| 1318 | q = memchr(old, ' ', 7); |
---|
| 1319 | if (!q) q = old + 7; |
---|
| 1320 | *q = '\0'; |
---|
[9f9c05e] | 1321 | |
---|
[f1c7c9c7] | 1322 | memcpy(new, line + 7, 7); |
---|
| 1323 | q = memchr(new, ' ', 7); |
---|
| 1324 | if (!q) q = new + 7; |
---|
| 1325 | *q = '\0'; |
---|
[9f9c05e] | 1326 | |
---|
[f1c7c9c7] | 1327 | pimg->flags = img_SFLAG_UNDERGROUND; |
---|
| 1328 | |
---|
| 1329 | if (strcmp(old, new) == 0) { |
---|
| 1330 | pimg->pending = img_MOVE + 4; |
---|
| 1331 | read_xyz_shot_coords(p, line); |
---|
| 1332 | strcpy(pimg->label, new); |
---|
| 1333 | osfree(line); |
---|
| 1334 | return img_LABEL; |
---|
| 1335 | } |
---|
[9f9c05e] | 1336 | |
---|
[f1c7c9c7] | 1337 | if (strcmp(old, pimg->label) == 0) { |
---|
| 1338 | pimg->pending = img_LINE + 4; |
---|
| 1339 | read_xyz_shot_coords(p, line); |
---|
| 1340 | strcpy(pimg->label, new); |
---|
| 1341 | osfree(line); |
---|
| 1342 | return img_LABEL; |
---|
| 1343 | } |
---|
| 1344 | |
---|
| 1345 | pimg->pending = img_LABEL + 4; |
---|
| 1346 | read_xyz_shot_coords(p, line); |
---|
| 1347 | strcpy(pimg->label, new); |
---|
| 1348 | memcpy(pimg->label + 16, line, 70); |
---|
| 1349 | |
---|
| 1350 | osfree(line); |
---|
| 1351 | return img_LABEL; |
---|
| 1352 | } |
---|
[a420b49] | 1353 | } |
---|
[d1b1380] | 1354 | } |
---|
| 1355 | |
---|
[ad9a5cd] | 1356 | static int |
---|
[d69255f] | 1357 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 1358 | { |
---|
| 1359 | size_t len, n, dot; |
---|
| 1360 | |
---|
| 1361 | /* find length of common prefix */ |
---|
| 1362 | dot = 0; |
---|
[0ff23cb] | 1363 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 1364 | if (s[len] == '.') dot = len + 1; |
---|
| 1365 | } |
---|
| 1366 | |
---|
[4c07c51] | 1367 | SVX_ASSERT(len <= pimg->label_len); |
---|
[d69255f] | 1368 | n = pimg->label_len - len; |
---|
| 1369 | if (len == 0) { |
---|
| 1370 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1371 | } else if (n <= 16) { |
---|
| 1372 | if (n) putc(n + 15, pimg->fh); |
---|
| 1373 | } else if (dot == 0) { |
---|
| 1374 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[99cf51a] | 1375 | len = 0; |
---|
[d69255f] | 1376 | } else { |
---|
[0ff23cb] | 1377 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 1378 | n = 1; |
---|
| 1379 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 1380 | if (*p++ == '.') n++; |
---|
| 1381 | } |
---|
| 1382 | if (n <= 14) { |
---|
| 1383 | putc(n, pimg->fh); |
---|
| 1384 | len = dot; |
---|
| 1385 | } else { |
---|
| 1386 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1387 | len = 0; |
---|
| 1388 | } |
---|
| 1389 | } |
---|
| 1390 | |
---|
| 1391 | n = strlen(s + len); |
---|
| 1392 | putc(opt, pimg->fh); |
---|
| 1393 | if (n < 0xfe) { |
---|
| 1394 | putc(n, pimg->fh); |
---|
| 1395 | } else if (n < 0xffff + 0xfe) { |
---|
| 1396 | putc(0xfe, pimg->fh); |
---|
| 1397 | put16(n - 0xfe, pimg->fh); |
---|
| 1398 | } else { |
---|
| 1399 | putc(0xff, pimg->fh); |
---|
| 1400 | put32(n, pimg->fh); |
---|
| 1401 | } |
---|
| 1402 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 1403 | |
---|
| 1404 | n += len; |
---|
| 1405 | pimg->label_len = n; |
---|
[d4d3655] | 1406 | if (!check_label_space(pimg, n + 1)) |
---|
| 1407 | return 0; /* FIXME: distinguish out of memory... */ |
---|
[0ff23cb] | 1408 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 1409 | |
---|
| 1410 | return !ferror(pimg->fh); |
---|
[d69255f] | 1411 | } |
---|
| 1412 | |
---|
[a420b49] | 1413 | void |
---|
[95c3272] | 1414 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 1415 | double x, double y, double z) |
---|
[a420b49] | 1416 | { |
---|
[647407d] | 1417 | if (!pimg) return; |
---|
[d69255f] | 1418 | if (pimg->version == 3) { |
---|
| 1419 | int opt = 0; |
---|
| 1420 | switch (code) { |
---|
| 1421 | case img_LABEL: |
---|
| 1422 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 1423 | opt = 0; |
---|
| 1424 | break; |
---|
| 1425 | case img_MOVE: |
---|
| 1426 | opt = 15; |
---|
| 1427 | break; |
---|
| 1428 | case img_LINE: |
---|
| 1429 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 1430 | opt = 0; |
---|
| 1431 | break; |
---|
| 1432 | default: /* ignore for now */ |
---|
| 1433 | return; |
---|
| 1434 | } |
---|
| 1435 | if (opt) putc(opt, pimg->fh); |
---|
| 1436 | /* Output in cm */ |
---|
[dd1d6369] | 1437 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1438 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1439 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[d69255f] | 1440 | } else { |
---|
[23f7ea7] | 1441 | size_t len; |
---|
[badeec8] | 1442 | INT32_T opt = 0; |
---|
[4c07c51] | 1443 | SVX_ASSERT(pimg->version > 0); |
---|
[a420b49] | 1444 | switch (code) { |
---|
[437caf3] | 1445 | case img_LABEL: |
---|
| 1446 | if (pimg->version == 1) { |
---|
| 1447 | /* put a move before each label */ |
---|
[95c3272] | 1448 | img_write_item(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[437caf3] | 1449 | put32(2, pimg->fh); |
---|
[0ed0e16] | 1450 | fputsnl(s, pimg->fh); |
---|
[437caf3] | 1451 | return; |
---|
| 1452 | } |
---|
[23f7ea7] | 1453 | len = strlen(s); |
---|
[af56069] | 1454 | if (len > 255 || strchr(s, '\n')) { |
---|
[23f7ea7] | 1455 | /* long label - not in early incarnations of v2 format, but few |
---|
| 1456 | * 3d files will need these, so better not to force incompatibility |
---|
| 1457 | * with a new version I think... */ |
---|
[95c3272] | 1458 | putc(7, pimg->fh); |
---|
| 1459 | putc(flags, pimg->fh); |
---|
[23f7ea7] | 1460 | put32(len, pimg->fh); |
---|
| 1461 | fputs(s, pimg->fh); |
---|
| 1462 | } else { |
---|
[95c3272] | 1463 | putc(0x40 | (flags & 0x3f), pimg->fh); |
---|
[23f7ea7] | 1464 | fputsnl(s, pimg->fh); |
---|
| 1465 | } |
---|
[437caf3] | 1466 | opt = 0; |
---|
[23f7ea7] | 1467 | break; |
---|
[a420b49] | 1468 | case img_MOVE: |
---|
| 1469 | opt = 4; |
---|
| 1470 | break; |
---|
| 1471 | case img_LINE: |
---|
[421b7d2] | 1472 | if (pimg->version > 1) { |
---|
[d69255f] | 1473 | opt = 0x80 | (flags & 0x3f); |
---|
[437caf3] | 1474 | break; |
---|
[421b7d2] | 1475 | } |
---|
[437caf3] | 1476 | opt = 5; |
---|
[a420b49] | 1477 | break; |
---|
[437caf3] | 1478 | default: /* ignore for now */ |
---|
| 1479 | return; |
---|
[a420b49] | 1480 | } |
---|
[437caf3] | 1481 | if (pimg->version == 1) { |
---|
| 1482 | put32(opt, pimg->fh); |
---|
| 1483 | } else { |
---|
| 1484 | if (opt) putc(opt, pimg->fh); |
---|
[a420b49] | 1485 | } |
---|
[d69255f] | 1486 | /* Output in cm */ |
---|
[dd1d6369] | 1487 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1488 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1489 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[a420b49] | 1490 | } |
---|
[d1b1380] | 1491 | } |
---|
| 1492 | |
---|
[ad9a5cd] | 1493 | int |
---|
[a420b49] | 1494 | img_close(img *pimg) |
---|
| 1495 | { |
---|
[ad9a5cd] | 1496 | int result = 1; |
---|
[dbb4e19] | 1497 | if (pimg) { |
---|
| 1498 | if (pimg->fh) { |
---|
[76bbb7c9] | 1499 | if (pimg->fRead) { |
---|
| 1500 | osfree(pimg->survey); |
---|
[a2ad284] | 1501 | osfree(pimg->title); |
---|
| 1502 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 1503 | } else { |
---|
| 1504 | /* write end of data marker */ |
---|
[d69255f] | 1505 | switch (pimg->version) { |
---|
| 1506 | case 1: |
---|
[5c3c61a] | 1507 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 1508 | break; |
---|
| 1509 | case 2: |
---|
| 1510 | putc(0, pimg->fh); |
---|
| 1511 | break; |
---|
| 1512 | case 3: |
---|
| 1513 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[437caf3] | 1514 | putc(0, pimg->fh); |
---|
[d69255f] | 1515 | break; |
---|
[5c3c61a] | 1516 | } |
---|
| 1517 | } |
---|
[ad9a5cd] | 1518 | if (ferror(pimg->fh)) result = 0; |
---|
| 1519 | if (fclose(pimg->fh)) result = 0; |
---|
[0d922fc] | 1520 | if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 1521 | } |
---|
[0ff23cb] | 1522 | osfree(pimg->label_buf); |
---|
[dbb4e19] | 1523 | osfree(pimg); |
---|
[a420b49] | 1524 | } |
---|
[ad9a5cd] | 1525 | return result; |
---|
[d1b1380] | 1526 | } |
---|