[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) |
---|
[d69255f] | 62 | # define 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" |
---|
| 134 | |
---|
[0da6e60] | 135 | /* Attempt to string paste to ensure we are passed a literal string */ |
---|
| 136 | #define LITLEN(S) (sizeof(S"") - 1) |
---|
| 137 | |
---|
[7d27e36] | 138 | static char * |
---|
| 139 | my_strdup(const char *str) |
---|
[a420b49] | 140 | { |
---|
[7d27e36] | 141 | char *p; |
---|
| 142 | size_t len = strlen(str) + 1; |
---|
| 143 | p = xosmalloc(len); |
---|
| 144 | if (p) memcpy(p, str, len); |
---|
| 145 | return p; |
---|
[d1b1380] | 146 | } |
---|
[c0563da] | 147 | |
---|
[a2ad284] | 148 | static char * |
---|
| 149 | getline_alloc(FILE *fh) |
---|
| 150 | { |
---|
| 151 | int ch; |
---|
| 152 | size_t i = 0; |
---|
| 153 | size_t len = 16; |
---|
| 154 | char *buf = xosmalloc(len); |
---|
| 155 | if (!buf) return NULL; |
---|
| 156 | |
---|
| 157 | ch = getc(fh); |
---|
| 158 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 159 | buf[i++] = ch; |
---|
| 160 | if (i == len - 1) { |
---|
| 161 | char *p; |
---|
| 162 | len += len; |
---|
| 163 | p = xosrealloc(buf, len); |
---|
| 164 | if (!p) { |
---|
| 165 | osfree(buf); |
---|
| 166 | return NULL; |
---|
| 167 | } |
---|
| 168 | buf = p; |
---|
| 169 | } |
---|
| 170 | ch = getc(fh); |
---|
| 171 | } |
---|
| 172 | if (ch == '\n' || ch == '\r') { |
---|
[4ebc557] | 173 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
| 174 | ch = getc(fh); |
---|
| 175 | /* if it's not the other eol character, put it back */ |
---|
| 176 | if (ch != otherone) ungetc(ch, fh); |
---|
[a2ad284] | 177 | } |
---|
[421b7d2] | 178 | buf[i++] = '\0'; |
---|
[a2ad284] | 179 | return buf; |
---|
| 180 | } |
---|
| 181 | |
---|
[72af530] | 182 | #ifndef IMG_HOSTED |
---|
[a420b49] | 183 | img_errcode |
---|
| 184 | img_error(void) |
---|
| 185 | { |
---|
| 186 | return img_errno; |
---|
[d1b1380] | 187 | } |
---|
| 188 | #else |
---|
[a420b49] | 189 | int |
---|
| 190 | img_error(void) |
---|
| 191 | { |
---|
| 192 | return (int)img_errno; |
---|
[d1b1380] | 193 | } |
---|
| 194 | #endif |
---|
| 195 | |
---|
[d4d3655] | 196 | static bool |
---|
| 197 | check_label_space(img *pimg, size_t len) |
---|
| 198 | { |
---|
| 199 | if (len > pimg->buf_len) { |
---|
| 200 | char *b = xosrealloc(pimg->label_buf, len); |
---|
| 201 | if (!b) return fFalse; |
---|
| 202 | pimg->label = (pimg->label - pimg->label_buf) + b; |
---|
| 203 | pimg->label_buf = b; |
---|
| 204 | pimg->buf_len = len; |
---|
| 205 | } |
---|
| 206 | return fTrue; |
---|
| 207 | } |
---|
| 208 | |
---|
[a420b49] | 209 | img * |
---|
[a2ad284] | 210 | img_open_survey(const char *fnm, const char *survey) |
---|
[a420b49] | 211 | { |
---|
| 212 | img *pimg; |
---|
[0da6e60] | 213 | size_t len; |
---|
[7d27e36] | 214 | char buf[LITLEN(FILEID) + 1]; |
---|
| 215 | int ch; |
---|
[a420b49] | 216 | |
---|
| 217 | if (fDirectory(fnm)) { |
---|
| 218 | img_errno = IMG_DIRECTORY; |
---|
| 219 | return NULL; |
---|
| 220 | } |
---|
[d1b1380] | 221 | |
---|
[a420b49] | 222 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 223 | if (pimg == NULL) { |
---|
| 224 | img_errno = IMG_OUTOFMEMORY; |
---|
| 225 | return NULL; |
---|
| 226 | } |
---|
[d1b1380] | 227 | |
---|
[23f7ea7] | 228 | pimg->buf_len = 257; |
---|
[0ff23cb] | 229 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 230 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 231 | osfree(pimg); |
---|
| 232 | img_errno = IMG_OUTOFMEMORY; |
---|
| 233 | return NULL; |
---|
| 234 | } |
---|
| 235 | |
---|
[a420b49] | 236 | pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL); |
---|
| 237 | if (pimg->fh == NULL) { |
---|
| 238 | osfree(pimg); |
---|
| 239 | img_errno = IMG_FILENOTFOUND; |
---|
| 240 | return NULL; |
---|
| 241 | } |
---|
[d1b1380] | 242 | |
---|
[0da6e60] | 243 | pimg->fRead = fTrue; /* reading from this file */ |
---|
| 244 | img_errno = IMG_NONE; |
---|
| 245 | |
---|
| 246 | pimg->flags = 0; |
---|
| 247 | |
---|
| 248 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[4ebc557] | 249 | /* for version -2, 0 value indicates we haven't entered a survey yet */ |
---|
[0da6e60] | 250 | pimg->label_len = 0; |
---|
| 251 | |
---|
| 252 | pimg->survey = NULL; |
---|
| 253 | pimg->survey_len = 0; |
---|
| 254 | |
---|
[58f02ae] | 255 | pimg->title = NULL; |
---|
[0da6e60] | 256 | if (survey) { |
---|
| 257 | len = strlen(survey); |
---|
| 258 | if (len) { |
---|
| 259 | if (survey[len - 1] == '.') len--; |
---|
| 260 | if (len) { |
---|
| 261 | char *p; |
---|
| 262 | pimg->survey = osmalloc(len + 2); |
---|
| 263 | memcpy(pimg->survey, survey, len); |
---|
| 264 | /* Set title to leaf survey name */ |
---|
| 265 | pimg->survey[len] = '\0'; |
---|
| 266 | p = strchr(pimg->survey, '.'); |
---|
| 267 | if (p) p++; else p = pimg->survey; |
---|
[7d27e36] | 268 | pimg->title = my_strdup(p); |
---|
| 269 | if (!pimg->title) { |
---|
| 270 | img_errno = IMG_OUTOFMEMORY; |
---|
| 271 | goto error; |
---|
| 272 | } |
---|
[0da6e60] | 273 | pimg->survey[len] = '.'; |
---|
| 274 | pimg->survey[len + 1] = '\0'; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | pimg->survey_len = len; |
---|
| 278 | } |
---|
| 279 | |
---|
[ad4eaf3f] | 280 | /* [version -2] pending IMG_LINE ('D') or IMG_MOVE ('M') |
---|
| 281 | * [version -1] already skipped heading line, or there wasn't one |
---|
[0da6e60] | 282 | * [version 0] not in the middle of a 'LINE' command |
---|
| 283 | * [version 3] not in the middle of turning a LINE into a MOVE |
---|
| 284 | */ |
---|
| 285 | pimg->pending = 0; |
---|
| 286 | |
---|
| 287 | len = strlen(fnm); |
---|
| 288 | if (len > LITLEN(EXT_SVX_POS) + 1 && |
---|
| 289 | fnm[len - LITLEN(EXT_SVX_POS) - 1] == FNM_SEP_EXT && |
---|
[ad4eaf3f] | 290 | my_strcasecmp(fnm + len - LITLEN(EXT_SVX_POS), EXT_SVX_POS) == 0) { |
---|
[f830e0d] | 291 | pos_file: |
---|
[0da6e60] | 292 | pimg->version = -1; |
---|
[984f49e] | 293 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[7d27e36] | 294 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 295 | if (!pimg->datestamp) { |
---|
| 296 | img_errno = IMG_OUTOFMEMORY; |
---|
| 297 | goto error; |
---|
| 298 | } |
---|
[58f02ae] | 299 | pimg->start = 0; |
---|
[0da6e60] | 300 | return pimg; |
---|
| 301 | } |
---|
[5757725] | 302 | |
---|
[ad4eaf3f] | 303 | if (len > LITLEN(EXT_PLT) + 1 && |
---|
| 304 | fnm[len - LITLEN(EXT_PLT) - 1] == FNM_SEP_EXT && |
---|
| 305 | my_strcasecmp(fnm + len - LITLEN(EXT_PLT), EXT_PLT) == 0) { |
---|
[4ebc557] | 306 | long fpos; |
---|
[27bd06b] | 307 | plt_file: |
---|
[ad4eaf3f] | 308 | pimg->version = -2; |
---|
[4ebc557] | 309 | pimg->start = 0; |
---|
[984f49e] | 310 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[ad4eaf3f] | 311 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 312 | if (!pimg->datestamp) { |
---|
| 313 | img_errno = IMG_OUTOFMEMORY; |
---|
| 314 | goto error; |
---|
| 315 | } |
---|
[4ebc557] | 316 | while (1) { |
---|
| 317 | int ch = getc(pimg->fh); |
---|
| 318 | switch (ch) { |
---|
| 319 | case '\x1a': |
---|
| 320 | fseek(pimg->fh, -1, SEEK_CUR); |
---|
| 321 | /* FALL THRU */ |
---|
| 322 | case EOF: |
---|
| 323 | pimg->start = ftell(pimg->fh); |
---|
| 324 | return pimg; |
---|
| 325 | case 'N': { |
---|
| 326 | char *line, *q; |
---|
| 327 | fpos = ftell(pimg->fh) - 1; |
---|
| 328 | if (!pimg->survey) { |
---|
| 329 | /* FIXME : if there's only one survey in the file, it'd be nice |
---|
| 330 | * to use its description as the title here... |
---|
| 331 | */ |
---|
[0d922fc] | 332 | ungetc('N', pimg->fh); |
---|
[4ebc557] | 333 | pimg->start = fpos; |
---|
[984f49e] | 334 | return pimg; |
---|
| 335 | } |
---|
[4ebc557] | 336 | line = getline_alloc(pimg->fh); |
---|
[0d922fc] | 337 | len = 0; |
---|
[4ebc557] | 338 | while (line[len] > 32) ++len; |
---|
| 339 | if (pimg->survey_len != len || |
---|
| 340 | memcmp(line, pimg->survey, len) != 0) { |
---|
[ad4eaf3f] | 341 | osfree(line); |
---|
[4ebc557] | 342 | continue; |
---|
[ad4eaf3f] | 343 | } |
---|
[4ebc557] | 344 | q = strchr(line + len, 'C'); |
---|
| 345 | if (q && q[1]) { |
---|
| 346 | osfree(pimg->title); |
---|
| 347 | pimg->title = osstrdup(q + 1); |
---|
| 348 | } else if (!pimg->title) { |
---|
| 349 | pimg->title = osstrdup(pimg->label); |
---|
| 350 | } |
---|
| 351 | osfree(line); |
---|
| 352 | if (!pimg->title) { |
---|
| 353 | img_errno = IMG_OUTOFMEMORY; |
---|
| 354 | goto error; |
---|
| 355 | } |
---|
| 356 | if (!pimg->start) pimg->start = fpos; |
---|
[27bd06b] | 357 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[4ebc557] | 358 | return pimg; |
---|
| 359 | } |
---|
| 360 | case 'M': case 'D': |
---|
| 361 | pimg->start = ftell(pimg->fh) - 1; |
---|
| 362 | break; |
---|
[984f49e] | 363 | } |
---|
[4ebc557] | 364 | while (ch != '\n' && ch != '\r') { |
---|
| 365 | ch = getc(pimg->fh); |
---|
[984f49e] | 366 | } |
---|
| 367 | } |
---|
[ad4eaf3f] | 368 | } |
---|
[0da6e60] | 369 | |
---|
[7d27e36] | 370 | if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 || |
---|
| 371 | memcmp(buf, FILEID"\n", LITLEN(FILEID)) != 0) { |
---|
[27bd06b] | 372 | if (buf[0] == 'Z' && buf[1] == ' ') { |
---|
| 373 | /* Looks like a Compass .plt file ... */ |
---|
| 374 | rewind(pimg->fh); |
---|
| 375 | goto plt_file; |
---|
| 376 | } |
---|
[f830e0d] | 377 | if (buf[0] == '(') { |
---|
| 378 | /* Looks like a Survex .pos file ... */ |
---|
| 379 | rewind(pimg->fh); |
---|
| 380 | goto pos_file; |
---|
| 381 | } |
---|
[a420b49] | 382 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 383 | goto error; |
---|
[a420b49] | 384 | } |
---|
[d1b1380] | 385 | |
---|
[7d27e36] | 386 | /* check file format version */ |
---|
| 387 | ch = getc(pimg->fh); |
---|
| 388 | pimg->version = 0; |
---|
| 389 | if (tolower(ch) == 'b') { |
---|
| 390 | /* binary file iff B/b prefix */ |
---|
| 391 | pimg->version = 1; |
---|
| 392 | ch = getc(pimg->fh); |
---|
| 393 | } |
---|
| 394 | if (ch != 'v') { |
---|
| 395 | img_errno = IMG_BADFORMAT; |
---|
| 396 | goto error; |
---|
| 397 | } |
---|
| 398 | ch = getc(pimg->fh); |
---|
| 399 | if (ch == '0') { |
---|
| 400 | if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) { |
---|
| 401 | img_errno = IMG_BADFORMAT; |
---|
| 402 | goto error; |
---|
| 403 | } |
---|
[0da6e60] | 404 | /* nothing special to do */ |
---|
[7d27e36] | 405 | } else if (pimg->version == 0) { |
---|
| 406 | if (ch < '2' || ch > '3' || getc(pimg->fh) != '\n') { |
---|
[6c1c4f3] | 407 | img_errno = IMG_TOONEW; |
---|
[58f02ae] | 408 | goto error; |
---|
[6c1c4f3] | 409 | } |
---|
[7d27e36] | 410 | pimg->version = ch - '0'; |
---|
[5c3c61a] | 411 | } else { |
---|
[647407d] | 412 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 413 | goto error; |
---|
[a420b49] | 414 | } |
---|
[421b7d2] | 415 | |
---|
[4d3dfdf] | 416 | if (!pimg->title) |
---|
| 417 | pimg->title = getline_alloc(pimg->fh); |
---|
| 418 | else |
---|
| 419 | osfree(getline_alloc(pimg->fh)); |
---|
[a2ad284] | 420 | pimg->datestamp = getline_alloc(pimg->fh); |
---|
| 421 | if (!pimg->title || !pimg->datestamp) { |
---|
[58f02ae] | 422 | img_errno = IMG_OUTOFMEMORY; |
---|
| 423 | error: |
---|
[a2ad284] | 424 | osfree(pimg->title); |
---|
[984f49e] | 425 | osfree(pimg->datestamp); |
---|
[a2ad284] | 426 | fclose(pimg->fh); |
---|
| 427 | osfree(pimg); |
---|
| 428 | return NULL; |
---|
| 429 | } |
---|
| 430 | |
---|
[58f02ae] | 431 | pimg->start = ftell(pimg->fh); |
---|
[421b7d2] | 432 | |
---|
[a420b49] | 433 | return pimg; |
---|
[d1b1380] | 434 | } |
---|
| 435 | |
---|
[9f3e5df] | 436 | int |
---|
[f2588ca] | 437 | img_rewind(img *pimg) |
---|
| 438 | { |
---|
[9f3e5df] | 439 | if (!pimg->fRead) { |
---|
| 440 | img_errno = IMG_WRITEERROR; |
---|
| 441 | return 0; |
---|
| 442 | } |
---|
| 443 | if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) { |
---|
| 444 | img_errno = IMG_READERROR; |
---|
| 445 | return 0; |
---|
| 446 | } |
---|
[58f02ae] | 447 | clearerr(pimg->fh); |
---|
| 448 | /* [version -1] already skipped heading line, or there wasn't one |
---|
| 449 | * [version 0] not in the middle of a 'LINE' command |
---|
[76bbb7c9] | 450 | * [version 3] not in the middle of turning a LINE into a MOVE */ |
---|
[4ebc557] | 451 | pimg->pending = 0; |
---|
[d69255f] | 452 | |
---|
| 453 | img_errno = IMG_NONE; |
---|
[99cf51a] | 454 | |
---|
[5d7280f8] | 455 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[4ebc557] | 456 | /* for version -2, 0 value indicates we haven't entered a survey yet */ |
---|
[d69255f] | 457 | pimg->label_len = 0; |
---|
[9f3e5df] | 458 | return 1; |
---|
[f2588ca] | 459 | } |
---|
| 460 | |
---|
[a420b49] | 461 | img * |
---|
[fe8e80e] | 462 | img_open_write(const char *fnm, char *title_buf, bool fBinary) |
---|
[a420b49] | 463 | { |
---|
| 464 | time_t tm; |
---|
| 465 | img *pimg; |
---|
[d1b1380] | 466 | |
---|
[d69255f] | 467 | fBinary = fBinary; |
---|
| 468 | |
---|
[a420b49] | 469 | if (fDirectory(fnm)) { |
---|
| 470 | img_errno = IMG_DIRECTORY; |
---|
| 471 | return NULL; |
---|
| 472 | } |
---|
[d1b1380] | 473 | |
---|
[a420b49] | 474 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 475 | if (pimg == NULL) { |
---|
| 476 | img_errno = IMG_OUTOFMEMORY; |
---|
| 477 | return NULL; |
---|
| 478 | } |
---|
[d1b1380] | 479 | |
---|
[fe8e80e] | 480 | pimg->buf_len = 257; |
---|
[0ff23cb] | 481 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 482 | if (!pimg->label_buf) { |
---|
[fe8e80e] | 483 | osfree(pimg); |
---|
| 484 | img_errno = IMG_OUTOFMEMORY; |
---|
| 485 | return NULL; |
---|
| 486 | } |
---|
| 487 | |
---|
[a420b49] | 488 | pimg->fh = fopen(fnm, "wb"); |
---|
| 489 | if (!pimg->fh) { |
---|
| 490 | osfree(pimg); |
---|
| 491 | img_errno = IMG_CANTOPENOUT; |
---|
| 492 | return NULL; |
---|
| 493 | } |
---|
[d1b1380] | 494 | |
---|
[a420b49] | 495 | /* Output image file header */ |
---|
| 496 | fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */ |
---|
[5c3c61a] | 497 | if (img_output_version < 2) { |
---|
[d69255f] | 498 | pimg->version = 1; |
---|
| 499 | fputs("Bv0.01\n", pimg->fh); /* binary file format version number */ |
---|
[5c3c61a] | 500 | } else { |
---|
[d69255f] | 501 | pimg->version = (img_output_version > 2) ? 3 : 2; |
---|
[5c3c61a] | 502 | fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */ |
---|
| 503 | } |
---|
[fe8e80e] | 504 | fputsnl(title_buf, pimg->fh); |
---|
[a420b49] | 505 | tm = time(NULL); |
---|
| 506 | if (tm == (time_t)-1) { |
---|
| 507 | fputsnl(TIMENA, pimg->fh); |
---|
| 508 | } else { |
---|
[7d27e36] | 509 | char date[256]; |
---|
[a420b49] | 510 | /* output current date and time in format specified */ |
---|
[7d27e36] | 511 | strftime(date, 256, TIMEFMT, localtime(&tm)); |
---|
| 512 | fputsnl(date, pimg->fh); |
---|
[a420b49] | 513 | } |
---|
| 514 | pimg->fRead = fFalse; /* writing to this file */ |
---|
| 515 | img_errno = IMG_NONE; |
---|
[d69255f] | 516 | |
---|
[5d7280f8] | 517 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[0ff23cb] | 518 | pimg->label_buf[0] = '\0'; |
---|
[d69255f] | 519 | pimg->label_len = 0; |
---|
| 520 | |
---|
[ad9a5cd] | 521 | /* Don't check for write errors now - let img_close() report them... */ |
---|
[a420b49] | 522 | return pimg; |
---|
[d1b1380] | 523 | } |
---|
| 524 | |
---|
[ad9a5cd] | 525 | static int |
---|
| 526 | read_coord(FILE *fh, img_point *pt) |
---|
| 527 | { |
---|
| 528 | ASSERT(fh); |
---|
| 529 | ASSERT(pt); |
---|
| 530 | pt->x = get32(fh) / 100.0; |
---|
| 531 | pt->y = get32(fh) / 100.0; |
---|
| 532 | pt->z = get32(fh) / 100.0; |
---|
| 533 | if (ferror(fh) || feof(fh)) { |
---|
| 534 | img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 535 | return 0; |
---|
[421b7d2] | 536 | } |
---|
[ad9a5cd] | 537 | return 1; |
---|
| 538 | } |
---|
| 539 | |
---|
[4d3dfdf] | 540 | static int |
---|
| 541 | skip_coord(FILE *fh) |
---|
| 542 | { |
---|
| 543 | return (fseek(fh, 12, SEEK_CUR) == 0); |
---|
| 544 | } |
---|
| 545 | |
---|
[a420b49] | 546 | int |
---|
[23f7ea7] | 547 | img_read_item(img *pimg, img_point *p) |
---|
[a420b49] | 548 | { |
---|
| 549 | int result; |
---|
[95c3272] | 550 | pimg->flags = 0; |
---|
[d69255f] | 551 | |
---|
| 552 | if (pimg->version == 3) { |
---|
| 553 | int opt; |
---|
[76bbb7c9] | 554 | if (pimg->pending >= 0x80) { |
---|
| 555 | *p = pimg->mv; |
---|
| 556 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 557 | pimg->pending = 0; |
---|
[421b7d2] | 558 | return img_LINE; |
---|
[76bbb7c9] | 559 | } |
---|
[d69255f] | 560 | again3: /* label to goto if we get a prefix */ |
---|
[4d3dfdf] | 561 | pimg->label = pimg->label_buf; |
---|
[d69255f] | 562 | opt = getc(pimg->fh); |
---|
[ad9a5cd] | 563 | if (opt == EOF) { |
---|
| 564 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 565 | return img_BAD; |
---|
| 566 | } |
---|
[d69255f] | 567 | switch (opt >> 6) { |
---|
| 568 | case 0: |
---|
| 569 | if (opt == 0) { |
---|
| 570 | if (!pimg->label_len) return img_STOP; /* end of data marker */ |
---|
| 571 | pimg->label_len = 0; |
---|
| 572 | goto again3; |
---|
| 573 | } |
---|
| 574 | if (opt < 15) { |
---|
| 575 | /* 1-14 mean trim that many levels from current prefix */ |
---|
| 576 | int c; |
---|
| 577 | if (pimg->label_len <= 16) { |
---|
[4d3dfdf] | 578 | /* zero prefix using "0" */ |
---|
[d69255f] | 579 | img_errno = IMG_BADFORMAT; |
---|
| 580 | return img_BAD; |
---|
| 581 | } |
---|
[4b156b3] | 582 | c = pimg->label_len - 16 - 1; |
---|
[0ff23cb] | 583 | while (pimg->label_buf[c] != '.' || --opt > 0) { |
---|
[d69255f] | 584 | if (--c < 0) { |
---|
| 585 | /* zero prefix using "0" */ |
---|
| 586 | img_errno = IMG_BADFORMAT; |
---|
| 587 | return img_BAD; |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | c++; |
---|
| 591 | pimg->label_len = c; |
---|
| 592 | goto again3; |
---|
| 593 | } |
---|
| 594 | if (opt == 15) { |
---|
| 595 | result = img_MOVE; |
---|
| 596 | break; |
---|
| 597 | } |
---|
| 598 | /* 16-31 mean remove (n - 15) characters from the prefix */ |
---|
| 599 | /* zero prefix using 0 */ |
---|
[ad4eaf3f] | 600 | if (pimg->label_len <= (size_t)(opt - 15)) { |
---|
[d69255f] | 601 | img_errno = IMG_BADFORMAT; |
---|
| 602 | return img_BAD; |
---|
| 603 | } |
---|
[d245d71] | 604 | pimg->label_len -= (opt - 15); |
---|
[d69255f] | 605 | goto again3; |
---|
| 606 | case 1: case 2: { |
---|
| 607 | char *q; |
---|
[ad9a5cd] | 608 | long len = getc(pimg->fh); |
---|
| 609 | if (len == EOF) { |
---|
| 610 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 611 | return img_BAD; |
---|
| 612 | } |
---|
[d69255f] | 613 | if (len == 0xfe) { |
---|
| 614 | len += get16(pimg->fh); |
---|
[ad9a5cd] | 615 | if (feof(pimg->fh)) { |
---|
| 616 | img_errno = IMG_BADFORMAT; |
---|
| 617 | return img_BAD; |
---|
| 618 | } |
---|
| 619 | if (ferror(pimg->fh)) { |
---|
| 620 | img_errno = IMG_READERROR; |
---|
| 621 | return img_BAD; |
---|
| 622 | } |
---|
[d69255f] | 623 | } else if (len == 0xff) { |
---|
| 624 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 625 | if (ferror(pimg->fh)) { |
---|
| 626 | img_errno = IMG_READERROR; |
---|
| 627 | return img_BAD; |
---|
| 628 | } |
---|
| 629 | if (feof(pimg->fh) || len < 0xfe + 0xffff) { |
---|
[d69255f] | 630 | img_errno = IMG_BADFORMAT; |
---|
| 631 | return img_BAD; |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | |
---|
[6dd3a20] | 635 | if (!check_label_space(pimg, pimg->label_len + len + 1)) { |
---|
[d4d3655] | 636 | img_errno = IMG_OUTOFMEMORY; |
---|
| 637 | return img_BAD; |
---|
[d69255f] | 638 | } |
---|
[d4d3655] | 639 | q = pimg->label_buf + pimg->label_len; |
---|
[6dd3a20] | 640 | pimg->label_len += len; |
---|
[d69255f] | 641 | if (len && fread(q, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 642 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 643 | return img_BAD; |
---|
| 644 | } |
---|
| 645 | q[len] = '\0'; |
---|
[76bbb7c9] | 646 | |
---|
| 647 | result = opt & 0x40 ? img_LABEL : img_LINE; |
---|
| 648 | |
---|
| 649 | if (pimg->survey_len) { |
---|
| 650 | size_t l = pimg->survey_len; |
---|
[0ff23cb] | 651 | const char *s = pimg->label_buf; |
---|
[76bbb7c9] | 652 | if (result == img_LINE) { |
---|
| 653 | if (strncmp(pimg->survey, s, l) != 0 || |
---|
| 654 | !(s[l] == '.' || s[l] == '\0')) { |
---|
[421b7d2] | 655 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 656 | pimg->pending = 15; |
---|
[421b7d2] | 657 | goto again3; |
---|
[76bbb7c9] | 658 | } |
---|
| 659 | } else { |
---|
| 660 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
[4d3dfdf] | 661 | if (!skip_coord(pimg->fh)) return img_BAD; |
---|
| 662 | pimg->pending = 0; |
---|
[76bbb7c9] | 663 | goto again3; |
---|
| 664 | } |
---|
| 665 | } |
---|
[0ff23cb] | 666 | pimg->label += l; |
---|
| 667 | /* skip the dot if there */ |
---|
| 668 | if (*pimg->label) pimg->label++; |
---|
[76bbb7c9] | 669 | } |
---|
| 670 | |
---|
| 671 | if (result == img_LINE && pimg->pending) { |
---|
| 672 | *p = pimg->mv; |
---|
[ad9a5cd] | 673 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 674 | pimg->pending = opt; |
---|
| 675 | return img_MOVE; |
---|
| 676 | } |
---|
| 677 | pimg->flags = (int)opt & 0x3f; |
---|
[d69255f] | 678 | break; |
---|
| 679 | } |
---|
| 680 | default: |
---|
| 681 | img_errno = IMG_BADFORMAT; |
---|
| 682 | return img_BAD; |
---|
| 683 | } |
---|
[ad9a5cd] | 684 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
[76bbb7c9] | 685 | pimg->pending = 0; |
---|
[d69255f] | 686 | return result; |
---|
[4d3dfdf] | 687 | } |
---|
| 688 | |
---|
| 689 | pimg->label = pimg->label_buf; |
---|
| 690 | |
---|
| 691 | if (pimg->version > 0) { |
---|
[d69255f] | 692 | static long opt_lookahead = 0; |
---|
[ad9a5cd] | 693 | static img_point pt = { 0.0, 0.0, 0.0 }; |
---|
[a420b49] | 694 | long opt; |
---|
| 695 | again: /* label to goto if we get a cross */ |
---|
[5d7280f8] | 696 | pimg->label[0] = '\0'; |
---|
[ad9a5cd] | 697 | |
---|
[5c3c61a] | 698 | if (pimg->version == 1) { |
---|
[c4d4649] | 699 | if (opt_lookahead) { |
---|
| 700 | opt = opt_lookahead; |
---|
| 701 | opt_lookahead = 0; |
---|
| 702 | } else { |
---|
| 703 | opt = get32(pimg->fh); |
---|
| 704 | } |
---|
[5c3c61a] | 705 | } else { |
---|
[421b7d2] | 706 | opt = getc(pimg->fh); |
---|
[5c3c61a] | 707 | } |
---|
[ad9a5cd] | 708 | |
---|
| 709 | if (feof(pimg->fh)) { |
---|
| 710 | img_errno = IMG_BADFORMAT; |
---|
| 711 | return img_BAD; |
---|
| 712 | } |
---|
| 713 | if (ferror(pimg->fh)) { |
---|
| 714 | img_errno = IMG_READERROR; |
---|
| 715 | return img_BAD; |
---|
| 716 | } |
---|
| 717 | |
---|
[a420b49] | 718 | switch (opt) { |
---|
[437caf3] | 719 | case -1: case 0: |
---|
[a420b49] | 720 | return img_STOP; /* end of data marker */ |
---|
| 721 | case 1: |
---|
[ad9a5cd] | 722 | /* skip coordinates */ |
---|
[4d3dfdf] | 723 | if (!skip_coord(pimg->fh)) { |
---|
[ad9a5cd] | 724 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 725 | return img_BAD; |
---|
| 726 | } |
---|
[a420b49] | 727 | goto again; |
---|
[23f7ea7] | 728 | case 2: case 3: { |
---|
[fe8e80e] | 729 | char *q; |
---|
[3a1f8da] | 730 | int ch; |
---|
[437caf3] | 731 | result = img_LABEL; |
---|
[3a1f8da] | 732 | ch = getc(pimg->fh); |
---|
[d69255f] | 733 | if (ch == EOF) { |
---|
[ad9a5cd] | 734 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 735 | return img_BAD; |
---|
| 736 | } |
---|
[3a1f8da] | 737 | if (ch != '\\') ungetc(ch, pimg->fh); |
---|
[0ff23cb] | 738 | fgets(pimg->label_buf, 257, pimg->fh); |
---|
[421b7d2] | 739 | if (feof(pimg->fh)) { |
---|
[ad9a5cd] | 740 | img_errno = IMG_BADFORMAT; |
---|
| 741 | return img_BAD; |
---|
| 742 | } |
---|
[421b7d2] | 743 | if (ferror(pimg->fh)) { |
---|
[ad9a5cd] | 744 | img_errno = IMG_READERROR; |
---|
| 745 | return img_BAD; |
---|
| 746 | } |
---|
[0ff23cb] | 747 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[d69255f] | 748 | if (*q != '\n') { |
---|
| 749 | img_errno = IMG_BADFORMAT; |
---|
| 750 | return img_BAD; |
---|
| 751 | } |
---|
[9968ebf] | 752 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 753 | if (q == pimg->label_buf) goto again; |
---|
[fe8e80e] | 754 | *q = '\0'; |
---|
[509e099] | 755 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[437caf3] | 756 | if (opt == 2) goto done; |
---|
| 757 | break; |
---|
[23f7ea7] | 758 | } |
---|
[95c3272] | 759 | case 6: case 7: { |
---|
[ad9a5cd] | 760 | long len; |
---|
[23f7ea7] | 761 | result = img_LABEL; |
---|
[ad9a5cd] | 762 | |
---|
[509e099] | 763 | if (opt == 7) |
---|
| 764 | pimg->flags = getc(pimg->fh); |
---|
| 765 | else |
---|
| 766 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[ad9a5cd] | 767 | |
---|
[23f7ea7] | 768 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 769 | |
---|
| 770 | if (feof(pimg->fh)) { |
---|
| 771 | img_errno = IMG_BADFORMAT; |
---|
| 772 | return img_BAD; |
---|
| 773 | } |
---|
| 774 | if (ferror(pimg->fh)) { |
---|
| 775 | img_errno = IMG_READERROR; |
---|
| 776 | return img_BAD; |
---|
| 777 | } |
---|
| 778 | |
---|
[9968ebf] | 779 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
| 780 | if (len == 0) goto again; |
---|
[d4d3655] | 781 | if (!check_label_space(pimg, len + 1)) { |
---|
| 782 | img_errno = IMG_OUTOFMEMORY; |
---|
| 783 | return img_BAD; |
---|
[23f7ea7] | 784 | } |
---|
[0ff23cb] | 785 | if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 786 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 787 | return img_BAD; |
---|
| 788 | } |
---|
[23f7ea7] | 789 | break; |
---|
| 790 | } |
---|
[a420b49] | 791 | case 4: |
---|
| 792 | result = img_MOVE; |
---|
| 793 | break; |
---|
| 794 | case 5: |
---|
| 795 | result = img_LINE; |
---|
| 796 | break; |
---|
| 797 | default: |
---|
[95c3272] | 798 | switch ((int)opt & 0xc0) { |
---|
| 799 | case 0x80: |
---|
| 800 | pimg->flags = (int)opt & 0x3f; |
---|
| 801 | result = img_LINE; |
---|
| 802 | break; |
---|
| 803 | case 0x40: { |
---|
| 804 | char *q; |
---|
| 805 | pimg->flags = (int)opt & 0x3f; |
---|
| 806 | result = img_LABEL; |
---|
[0ff23cb] | 807 | if (!fgets(pimg->label_buf, 257, pimg->fh)) { |
---|
[ad9a5cd] | 808 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 809 | return img_BAD; |
---|
| 810 | } |
---|
[0ff23cb] | 811 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[9968ebf] | 812 | /* Ignore empty-labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 813 | if (q == pimg->label_buf) goto again; |
---|
[d69255f] | 814 | if (*q != '\n') { |
---|
| 815 | img_errno = IMG_BADFORMAT; |
---|
| 816 | return img_BAD; |
---|
| 817 | } |
---|
[95c3272] | 818 | *q = '\0'; |
---|
| 819 | break; |
---|
| 820 | } |
---|
| 821 | case 0xc0: |
---|
| 822 | /* use this for an extra leg or station flag if we need it */ |
---|
| 823 | default: |
---|
[d69255f] | 824 | img_errno = IMG_BADFORMAT; |
---|
[95c3272] | 825 | return img_BAD; |
---|
| 826 | } |
---|
[437caf3] | 827 | break; |
---|
[a420b49] | 828 | } |
---|
[ad9a5cd] | 829 | |
---|
| 830 | if (!read_coord(pimg->fh, &pt)) return img_BAD; |
---|
[76bbb7c9] | 831 | |
---|
[ed0f5b6] | 832 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 833 | if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 834 | goto again; |
---|
| 835 | pimg->label += pimg->survey_len + 1; |
---|
| 836 | } |
---|
[76bbb7c9] | 837 | |
---|
[a420b49] | 838 | done: |
---|
[ad9a5cd] | 839 | *p = pt; |
---|
[c4d4649] | 840 | |
---|
| 841 | if (result == img_MOVE && pimg->version == 1) { |
---|
[d69255f] | 842 | /* peek at next code and see if it's an old-style label */ |
---|
[23f7ea7] | 843 | opt_lookahead = get32(pimg->fh); |
---|
[ad9a5cd] | 844 | |
---|
| 845 | if (feof(pimg->fh)) { |
---|
| 846 | img_errno = IMG_BADFORMAT; |
---|
| 847 | return img_BAD; |
---|
| 848 | } |
---|
| 849 | if (ferror(pimg->fh)) { |
---|
| 850 | img_errno = IMG_READERROR; |
---|
| 851 | return img_BAD; |
---|
| 852 | } |
---|
| 853 | |
---|
[d69255f] | 854 | if (opt_lookahead == 2) return img_read_item(pimg, p); |
---|
[c4d4649] | 855 | } |
---|
[23f7ea7] | 856 | |
---|
[a420b49] | 857 | return result; |
---|
[0da6e60] | 858 | } else if (pimg->version == 0) { |
---|
[a420b49] | 859 | ascii_again: |
---|
[5d7280f8] | 860 | pimg->label[0] = '\0'; |
---|
[a420b49] | 861 | if (feof(pimg->fh)) return img_STOP; |
---|
[76bbb7c9] | 862 | if (pimg->pending) { |
---|
| 863 | pimg->pending = 0; |
---|
[a420b49] | 864 | result = img_LINE; |
---|
| 865 | } else { |
---|
[7d27e36] | 866 | char cmd[7]; |
---|
[a420b49] | 867 | /* Stop if nothing found */ |
---|
[7d27e36] | 868 | if (fscanf(pimg->fh, "%6s", cmd) < 1) return img_STOP; |
---|
| 869 | if (strcmp(cmd, "move") == 0) |
---|
[a420b49] | 870 | result = img_MOVE; |
---|
[7d27e36] | 871 | else if (strcmp(cmd, "draw") == 0) |
---|
[a420b49] | 872 | result = img_LINE; |
---|
[7d27e36] | 873 | else if (strcmp(cmd, "line") == 0) { |
---|
[bd1913f] | 874 | /* set flag to indicate to process second triplet as LINE */ |
---|
[76bbb7c9] | 875 | pimg->pending = 1; |
---|
[a420b49] | 876 | result = img_MOVE; |
---|
[7d27e36] | 877 | } else if (strcmp(cmd, "cross") == 0) { |
---|
[d69255f] | 878 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 879 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[0ed0e16] | 880 | return img_BAD; |
---|
[d69255f] | 881 | } |
---|
[a420b49] | 882 | goto ascii_again; |
---|
[7d27e36] | 883 | } else if (strcmp(cmd, "name") == 0) { |
---|
[0da6e60] | 884 | size_t off = 0; |
---|
[58f02ae] | 885 | int ch = getc(pimg->fh); |
---|
| 886 | if (ch == ' ') ch = getc(pimg->fh); |
---|
| 887 | while (ch != ' ') { |
---|
| 888 | if (ch == '\n' || ch == EOF) { |
---|
| 889 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[ad9a5cd] | 890 | return img_BAD; |
---|
| 891 | } |
---|
[58f02ae] | 892 | if (off == pimg->buf_len) { |
---|
[d4d3655] | 893 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 894 | img_errno = IMG_OUTOFMEMORY; |
---|
| 895 | return img_BAD; |
---|
| 896 | } |
---|
[0da6e60] | 897 | } |
---|
[58f02ae] | 898 | pimg->label_buf[off++] = ch; |
---|
| 899 | ch = getc(pimg->fh); |
---|
[ad9a5cd] | 900 | } |
---|
[58f02ae] | 901 | pimg->label_buf[off] = '\0'; |
---|
| 902 | |
---|
| 903 | pimg->label = pimg->label_buf; |
---|
| 904 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[0da6e60] | 905 | |
---|
[a420b49] | 906 | result = img_LABEL; |
---|
[d69255f] | 907 | } else { |
---|
| 908 | img_errno = IMG_BADFORMAT; |
---|
[a420b49] | 909 | return img_BAD; /* unknown keyword */ |
---|
[d69255f] | 910 | } |
---|
[a420b49] | 911 | } |
---|
[d1b1380] | 912 | |
---|
[d69255f] | 913 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 914 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[fe8e80e] | 915 | return img_BAD; |
---|
[d69255f] | 916 | } |
---|
| 917 | |
---|
[58f02ae] | 918 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 919 | if (strncmp(pimg->label, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 920 | goto ascii_again; |
---|
| 921 | pimg->label += pimg->survey_len + 1; |
---|
| 922 | } |
---|
[76bbb7c9] | 923 | |
---|
[a420b49] | 924 | return result; |
---|
[ad4eaf3f] | 925 | } else if (pimg->version == -1) { |
---|
[0da6e60] | 926 | /* version -1: .pos file */ |
---|
[58f02ae] | 927 | size_t off; |
---|
[5eea574] | 928 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[58f02ae] | 929 | againpos: |
---|
| 930 | off = 0; |
---|
[0da6e60] | 931 | while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) { |
---|
| 932 | int ch; |
---|
| 933 | if (ferror(pimg->fh)) { |
---|
| 934 | img_errno = IMG_READERROR; |
---|
| 935 | return img_BAD; |
---|
| 936 | } |
---|
| 937 | if (feof(pimg->fh)) return img_STOP; |
---|
| 938 | if (pimg->pending) { |
---|
| 939 | img_errno = IMG_BADFORMAT; |
---|
| 940 | return img_BAD; |
---|
| 941 | } |
---|
| 942 | pimg->pending = 1; |
---|
| 943 | /* ignore rest of line */ |
---|
| 944 | do { |
---|
| 945 | ch = getc(pimg->fh); |
---|
| 946 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 947 | } |
---|
| 948 | |
---|
| 949 | pimg->label_buf[0] = '\0'; |
---|
| 950 | while (!feof(pimg->fh)) { |
---|
[9b56f4d] | 951 | char *b; |
---|
[0da6e60] | 952 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 953 | img_errno = IMG_READERROR; |
---|
| 954 | return img_BAD; |
---|
| 955 | } |
---|
| 956 | |
---|
| 957 | off += strlen(pimg->label_buf + off); |
---|
| 958 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 959 | pimg->label_buf[off - 1] = '\0'; |
---|
| 960 | break; |
---|
| 961 | } |
---|
[d4d3655] | 962 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 963 | img_errno = IMG_OUTOFMEMORY; |
---|
| 964 | return img_BAD; |
---|
| 965 | } |
---|
[0da6e60] | 966 | } |
---|
| 967 | |
---|
[58f02ae] | 968 | pimg->label = pimg->label_buf; |
---|
| 969 | |
---|
[421b7d2] | 970 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[58f02ae] | 971 | |
---|
| 972 | if (pimg->survey_len) { |
---|
| 973 | size_t l = pimg->survey_len + 1; |
---|
| 974 | if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos; |
---|
| 975 | pimg->label += l; |
---|
| 976 | } |
---|
| 977 | |
---|
[0da6e60] | 978 | return img_LABEL; |
---|
[ad4eaf3f] | 979 | } else { |
---|
| 980 | /* version -2: Compass .plt file */ |
---|
[4ebc557] | 981 | if (pimg->pending > 0) { |
---|
| 982 | /* -1 signals we've entered the first survey we want to |
---|
| 983 | * read, and need to fudge lots if the first action is 'D'... |
---|
| 984 | */ |
---|
| 985 | /* pending MOVE or LINE */ |
---|
[984f49e] | 986 | int r = pimg->pending - 4; |
---|
[ad4eaf3f] | 987 | pimg->pending = 0; |
---|
| 988 | pimg->flags = 0; |
---|
[f710a3f] | 989 | pimg->label[pimg->label_len] = '\0'; |
---|
[ad4eaf3f] | 990 | return r; |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | while (1) { |
---|
[4ebc557] | 994 | char *line; |
---|
[ad4eaf3f] | 995 | char *q; |
---|
| 996 | size_t len = 0; |
---|
[4ebc557] | 997 | int ch = getc(pimg->fh); |
---|
| 998 | |
---|
| 999 | switch (ch) { |
---|
| 1000 | case '\x1a': case EOF: /* Don't insist on ^Z at end of file */ |
---|
| 1001 | return img_STOP; |
---|
[27bd06b] | 1002 | case 'X': case 'F': case 'S': |
---|
| 1003 | /* bounding boX (marks end of survey), Feature survey, or |
---|
| 1004 | * new Section - skip to next survey */ |
---|
| 1005 | if (pimg->survey) return img_STOP; |
---|
| 1006 | skip_to_N: |
---|
| 1007 | while (1) { |
---|
| 1008 | do { |
---|
| 1009 | ch = getc(pimg->fh); |
---|
| 1010 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1011 | while (ch == '\n' || ch == '\r') ch = getc(pimg->fh); |
---|
| 1012 | if (ch == 'N') break; |
---|
| 1013 | if (ch == '\x1a' || ch == EOF) return img_STOP; |
---|
| 1014 | } |
---|
| 1015 | /* FALLTHRU */ |
---|
[4ebc557] | 1016 | case 'N': |
---|
| 1017 | line = getline_alloc(pimg->fh); |
---|
| 1018 | while (line[len] > 32) ++len; |
---|
| 1019 | if (pimg->survey && pimg->label_len == 0) |
---|
| 1020 | pimg->pending = -1; |
---|
| 1021 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1022 | osfree(line); |
---|
| 1023 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1024 | return img_BAD; |
---|
| 1025 | } |
---|
| 1026 | pimg->label_len = len; |
---|
| 1027 | pimg->label = pimg->label_buf; |
---|
| 1028 | memcpy(pimg->label, line, len); |
---|
| 1029 | pimg->label[len] = '\0'; |
---|
| 1030 | osfree(line); |
---|
| 1031 | break; |
---|
| 1032 | case 'M': case 'D': { |
---|
[ad4eaf3f] | 1033 | /* Move or Draw */ |
---|
[4ebc557] | 1034 | long fpos = -1; |
---|
[27bd06b] | 1035 | if (pimg->survey && pimg->label_len == 0) { |
---|
[4ebc557] | 1036 | /* We're only holding onto this line in case the first line |
---|
[27bd06b] | 1037 | * of the 'N' is a 'D', so skip it for now... |
---|
[4ebc557] | 1038 | */ |
---|
[27bd06b] | 1039 | goto skip_to_N; |
---|
[4ebc557] | 1040 | } |
---|
| 1041 | if (ch == 'D' && pimg->pending == -1) { |
---|
| 1042 | fpos = ftell(pimg->fh) - 1; |
---|
| 1043 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
| 1044 | ch = getc(pimg->fh); |
---|
[0d922fc] | 1045 | pimg->pending = 0; |
---|
[4ebc557] | 1046 | } |
---|
| 1047 | line = getline_alloc(pimg->fh); |
---|
| 1048 | if (sscanf(line, "%lf %lf %lf", &p->x, &p->y, &p->z) != 3) { |
---|
[ad4eaf3f] | 1049 | osfree(line); |
---|
| 1050 | if (ferror(pimg->fh)) { |
---|
| 1051 | img_errno = IMG_READERROR; |
---|
| 1052 | } else { |
---|
| 1053 | img_errno = IMG_BADFORMAT; |
---|
| 1054 | } |
---|
| 1055 | return img_BAD; |
---|
| 1056 | } |
---|
| 1057 | q = strchr(line, 'S'); |
---|
| 1058 | if (!q) { |
---|
| 1059 | osfree(line); |
---|
[5757725] | 1060 | img_errno = IMG_BADFORMAT; |
---|
[ad4eaf3f] | 1061 | return img_BAD; |
---|
| 1062 | } |
---|
| 1063 | ++q; |
---|
| 1064 | len = 0; |
---|
| 1065 | while (q[len] > ' ') ++len; |
---|
| 1066 | q[len] = '\0'; |
---|
[f710a3f] | 1067 | len += 2; /* '.' and '\0' */ |
---|
[d4d3655] | 1068 | if (!check_label_space(pimg, pimg->label_len + len)) { |
---|
| 1069 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1070 | return img_BAD; |
---|
[ad4eaf3f] | 1071 | } |
---|
| 1072 | pimg->label = pimg->label_buf; |
---|
[f710a3f] | 1073 | pimg->label[pimg->label_len] = '.'; |
---|
| 1074 | memcpy(pimg->label + pimg->label_len + 1, q, len - 1); |
---|
[ad4eaf3f] | 1075 | osfree(line); |
---|
| 1076 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[0d922fc] | 1077 | if (fpos != -1) { |
---|
| 1078 | fseek(pimg->fh, fpos, SEEK_SET); |
---|
| 1079 | } else { |
---|
| 1080 | pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4; |
---|
| 1081 | } |
---|
[ad4eaf3f] | 1082 | return img_LABEL; |
---|
[4ebc557] | 1083 | } |
---|
[ad4eaf3f] | 1084 | default: |
---|
| 1085 | img_errno = IMG_BADFORMAT; |
---|
| 1086 | return img_BAD; |
---|
| 1087 | } |
---|
| 1088 | } |
---|
[a420b49] | 1089 | } |
---|
[d1b1380] | 1090 | } |
---|
| 1091 | |
---|
[ad9a5cd] | 1092 | static int |
---|
[d69255f] | 1093 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 1094 | { |
---|
| 1095 | size_t len, n, dot; |
---|
| 1096 | |
---|
| 1097 | /* find length of common prefix */ |
---|
| 1098 | dot = 0; |
---|
[0ff23cb] | 1099 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 1100 | if (s[len] == '.') dot = len + 1; |
---|
| 1101 | } |
---|
| 1102 | |
---|
| 1103 | ASSERT(len <= pimg->label_len); |
---|
| 1104 | n = pimg->label_len - len; |
---|
| 1105 | if (len == 0) { |
---|
| 1106 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1107 | } else if (n <= 16) { |
---|
| 1108 | if (n) putc(n + 15, pimg->fh); |
---|
| 1109 | } else if (dot == 0) { |
---|
| 1110 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[99cf51a] | 1111 | len = 0; |
---|
[d69255f] | 1112 | } else { |
---|
[0ff23cb] | 1113 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 1114 | n = 1; |
---|
| 1115 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 1116 | if (*p++ == '.') n++; |
---|
| 1117 | } |
---|
| 1118 | if (n <= 14) { |
---|
| 1119 | putc(n, pimg->fh); |
---|
| 1120 | len = dot; |
---|
| 1121 | } else { |
---|
| 1122 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1123 | len = 0; |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | |
---|
| 1127 | n = strlen(s + len); |
---|
| 1128 | putc(opt, pimg->fh); |
---|
| 1129 | if (n < 0xfe) { |
---|
| 1130 | putc(n, pimg->fh); |
---|
| 1131 | } else if (n < 0xffff + 0xfe) { |
---|
| 1132 | putc(0xfe, pimg->fh); |
---|
| 1133 | put16(n - 0xfe, pimg->fh); |
---|
| 1134 | } else { |
---|
| 1135 | putc(0xff, pimg->fh); |
---|
| 1136 | put32(n, pimg->fh); |
---|
| 1137 | } |
---|
| 1138 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 1139 | |
---|
| 1140 | n += len; |
---|
| 1141 | pimg->label_len = n; |
---|
[d4d3655] | 1142 | if (!check_label_space(pimg, n + 1)) |
---|
| 1143 | return 0; /* FIXME: distinguish out of memory... */ |
---|
[0ff23cb] | 1144 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 1145 | |
---|
| 1146 | return !ferror(pimg->fh); |
---|
[d69255f] | 1147 | } |
---|
| 1148 | |
---|
[a420b49] | 1149 | void |
---|
[95c3272] | 1150 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 1151 | double x, double y, double z) |
---|
[a420b49] | 1152 | { |
---|
[647407d] | 1153 | if (!pimg) return; |
---|
[d69255f] | 1154 | if (pimg->version == 3) { |
---|
| 1155 | int opt = 0; |
---|
| 1156 | switch (code) { |
---|
| 1157 | case img_LABEL: |
---|
| 1158 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 1159 | opt = 0; |
---|
| 1160 | break; |
---|
| 1161 | case img_MOVE: |
---|
| 1162 | opt = 15; |
---|
| 1163 | break; |
---|
| 1164 | case img_LINE: |
---|
| 1165 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 1166 | opt = 0; |
---|
| 1167 | break; |
---|
| 1168 | default: /* ignore for now */ |
---|
| 1169 | return; |
---|
| 1170 | } |
---|
| 1171 | if (opt) putc(opt, pimg->fh); |
---|
| 1172 | /* Output in cm */ |
---|
[dd1d6369] | 1173 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1174 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1175 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[d69255f] | 1176 | } else { |
---|
[23f7ea7] | 1177 | size_t len; |
---|
[badeec8] | 1178 | INT32_T opt = 0; |
---|
[d69255f] | 1179 | ASSERT(pimg->version > 0); |
---|
[a420b49] | 1180 | switch (code) { |
---|
[437caf3] | 1181 | case img_LABEL: |
---|
| 1182 | if (pimg->version == 1) { |
---|
| 1183 | /* put a move before each label */ |
---|
[95c3272] | 1184 | img_write_item(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[437caf3] | 1185 | put32(2, pimg->fh); |
---|
[0ed0e16] | 1186 | fputsnl(s, pimg->fh); |
---|
[437caf3] | 1187 | return; |
---|
| 1188 | } |
---|
[23f7ea7] | 1189 | len = strlen(s); |
---|
[af56069] | 1190 | if (len > 255 || strchr(s, '\n')) { |
---|
[23f7ea7] | 1191 | /* long label - not in early incarnations of v2 format, but few |
---|
| 1192 | * 3d files will need these, so better not to force incompatibility |
---|
| 1193 | * with a new version I think... */ |
---|
[95c3272] | 1194 | putc(7, pimg->fh); |
---|
| 1195 | putc(flags, pimg->fh); |
---|
[23f7ea7] | 1196 | put32(len, pimg->fh); |
---|
| 1197 | fputs(s, pimg->fh); |
---|
| 1198 | } else { |
---|
[95c3272] | 1199 | putc(0x40 | (flags & 0x3f), pimg->fh); |
---|
[23f7ea7] | 1200 | fputsnl(s, pimg->fh); |
---|
| 1201 | } |
---|
[437caf3] | 1202 | opt = 0; |
---|
[23f7ea7] | 1203 | break; |
---|
[a420b49] | 1204 | case img_MOVE: |
---|
| 1205 | opt = 4; |
---|
| 1206 | break; |
---|
| 1207 | case img_LINE: |
---|
[421b7d2] | 1208 | if (pimg->version > 1) { |
---|
[d69255f] | 1209 | opt = 0x80 | (flags & 0x3f); |
---|
[437caf3] | 1210 | break; |
---|
[421b7d2] | 1211 | } |
---|
[437caf3] | 1212 | opt = 5; |
---|
[a420b49] | 1213 | break; |
---|
[437caf3] | 1214 | default: /* ignore for now */ |
---|
| 1215 | return; |
---|
[a420b49] | 1216 | } |
---|
[437caf3] | 1217 | if (pimg->version == 1) { |
---|
| 1218 | put32(opt, pimg->fh); |
---|
| 1219 | } else { |
---|
| 1220 | if (opt) putc(opt, pimg->fh); |
---|
[a420b49] | 1221 | } |
---|
[d69255f] | 1222 | /* Output in cm */ |
---|
[dd1d6369] | 1223 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1224 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1225 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[a420b49] | 1226 | } |
---|
[d1b1380] | 1227 | } |
---|
| 1228 | |
---|
[ad9a5cd] | 1229 | int |
---|
[a420b49] | 1230 | img_close(img *pimg) |
---|
| 1231 | { |
---|
[ad9a5cd] | 1232 | int result = 1; |
---|
[dbb4e19] | 1233 | if (pimg) { |
---|
| 1234 | if (pimg->fh) { |
---|
[76bbb7c9] | 1235 | if (pimg->fRead) { |
---|
| 1236 | osfree(pimg->survey); |
---|
[a2ad284] | 1237 | osfree(pimg->title); |
---|
| 1238 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 1239 | } else { |
---|
| 1240 | /* write end of data marker */ |
---|
[d69255f] | 1241 | switch (pimg->version) { |
---|
| 1242 | case 1: |
---|
[5c3c61a] | 1243 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 1244 | break; |
---|
| 1245 | case 2: |
---|
| 1246 | putc(0, pimg->fh); |
---|
| 1247 | break; |
---|
| 1248 | case 3: |
---|
| 1249 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[437caf3] | 1250 | putc(0, pimg->fh); |
---|
[d69255f] | 1251 | break; |
---|
[5c3c61a] | 1252 | } |
---|
| 1253 | } |
---|
[ad9a5cd] | 1254 | if (ferror(pimg->fh)) result = 0; |
---|
| 1255 | if (fclose(pimg->fh)) result = 0; |
---|
[0d922fc] | 1256 | if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 1257 | } |
---|
[0ff23cb] | 1258 | osfree(pimg->label_buf); |
---|
[dbb4e19] | 1259 | osfree(pimg); |
---|
[a420b49] | 1260 | } |
---|
[ad9a5cd] | 1261 | return result; |
---|
[d1b1380] | 1262 | } |
---|