[0da6e60] | 1 | /* img.c |
---|
[d1b1380] | 2 | * Routines for reading and writing Survex ".3d" image files |
---|
[bd1913f] | 3 | * Copyright (C) 1993-2001 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" |
---|
| 31 | #ifdef IMG_HOSTED |
---|
[d69255f] | 32 | # include "debug.h" |
---|
[d1b1380] | 33 | # include "filelist.h" |
---|
[d69255f] | 34 | # include "filename.h" |
---|
| 35 | # include "message.h" |
---|
| 36 | # include "useful.h" |
---|
[a420b49] | 37 | # define TIMENA msg(/*Date and time not available.*/108) |
---|
| 38 | # define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107) |
---|
[d1b1380] | 39 | #else |
---|
[72af530] | 40 | # define INT32_T long |
---|
[d1b1380] | 41 | # define TIMENA "Time not available." |
---|
| 42 | # define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z" |
---|
| 43 | # define EXT_SVX_3D "3d" |
---|
| 44 | # define xosmalloc(L) malloc((L)) |
---|
[72af530] | 45 | # define xosrealloc(L,S) realloc((L),(S)) |
---|
[d1b1380] | 46 | # define osfree(P) free((P)) |
---|
| 47 | # define ossizeof(T) sizeof(T) |
---|
[72af530] | 48 | /* in IMG_HOSTED mode, this tests if a filename refers to a directory */ |
---|
[d1b1380] | 49 | # define fDirectory(X) 0 |
---|
| 50 | /* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */ |
---|
| 51 | /* path isn't used in img.c, but EXT is */ |
---|
| 52 | # define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) fopen(FNM,MODE) |
---|
| 53 | # define fFalse 0 |
---|
| 54 | # define fTrue 1 |
---|
| 55 | # define bool int |
---|
| 56 | /* dummy do {...} while(0) hack to permit stuff like |
---|
| 57 | * if (s) fputsnl(s,fh); else exit(1); |
---|
| 58 | * to work as intended |
---|
| 59 | */ |
---|
[23f7ea7] | 60 | # define fputsnl(S, FH) do {fputs((S), (FH)); putc('\n', (FH));} while(0) |
---|
[d69255f] | 61 | # define ASSERT(X) |
---|
[d1b1380] | 62 | |
---|
[a420b49] | 63 | static long |
---|
| 64 | get32(FILE *fh) |
---|
| 65 | { |
---|
[ad9a5cd] | 66 | long w = getc(fh); |
---|
[a420b49] | 67 | w |= (long)getc(fh) << 8l; |
---|
| 68 | w |= (long)getc(fh) << 16l; |
---|
| 69 | w |= (long)getc(fh) << 24l; |
---|
| 70 | return w; |
---|
[d1b1380] | 71 | } |
---|
| 72 | |
---|
[a420b49] | 73 | static void |
---|
| 74 | put32(long w, FILE *fh) |
---|
| 75 | { |
---|
| 76 | putc((char)(w), fh); |
---|
| 77 | putc((char)(w >> 8l), fh); |
---|
| 78 | putc((char)(w >> 16l), fh); |
---|
| 79 | putc((char)(w >> 24l), fh); |
---|
[d1b1380] | 80 | } |
---|
[5c3c61a] | 81 | |
---|
| 82 | #endif |
---|
| 83 | |
---|
[dd1d6369] | 84 | #ifdef HAVE_ROUND |
---|
| 85 | # include <math.h> |
---|
| 86 | extern double round(double); /* prototype is often missing... */ |
---|
| 87 | # define my_round round |
---|
| 88 | #else |
---|
| 89 | static double |
---|
| 90 | my_round(double x) { |
---|
| 91 | if (x >= 0.0) return floor(x + 0.5); |
---|
| 92 | return ceil(x - 0.5); |
---|
| 93 | } |
---|
| 94 | #endif |
---|
| 95 | |
---|
[d69255f] | 96 | unsigned int img_output_version = 3; |
---|
[5c3c61a] | 97 | |
---|
[23f7ea7] | 98 | #define TMPBUFLEN 256 |
---|
| 99 | static char tmpbuf[TMPBUFLEN]; |
---|
[5c3c61a] | 100 | |
---|
[72af530] | 101 | #ifdef IMG_HOSTED |
---|
[5c3c61a] | 102 | static enum { |
---|
| 103 | IMG_NONE = 0, |
---|
[759fb47] | 104 | IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24, |
---|
[5c3c61a] | 105 | IMG_OUTOFMEMORY = /*Out of memory %.0s*/38, |
---|
[759fb47] | 106 | IMG_DIRECTORY = /*Filename `%s' refers to directory*/44, |
---|
| 107 | IMG_CANTOPENOUT = /*Failed to open output file `%s'*/47, |
---|
[ad9a5cd] | 108 | IMG_BADFORMAT = /*Bad 3d image file `%s'*/106, |
---|
| 109 | IMG_READERROR = /*Error reading from file `%s'*/109, |
---|
[6c1c4f3] | 110 | IMG_WRITEERROR = /*Error writing to file `%s'*/110, |
---|
| 111 | IMG_TOONEW = /*File `%s' has a newer format than this program can understand*/114 |
---|
[5c3c61a] | 112 | } img_errno = IMG_NONE; |
---|
| 113 | #else |
---|
| 114 | static img_errcode img_errno = IMG_NONE; |
---|
[c0563da] | 115 | #endif |
---|
[d1b1380] | 116 | |
---|
[0da6e60] | 117 | /* Attempt to string paste to ensure we are passed a literal string */ |
---|
| 118 | #define LITLEN(S) (sizeof(S"") - 1) |
---|
| 119 | |
---|
[5c3c61a] | 120 | /* Read a line from a stream to a buffer. Any eol chars are removed |
---|
| 121 | * from the file and the length of the string excluding '\0' is returned */ |
---|
[a420b49] | 122 | static int |
---|
| 123 | getline(char *buf, size_t len, FILE *fh) |
---|
| 124 | { |
---|
[eb18f4d] | 125 | size_t i = 0; |
---|
[a420b49] | 126 | int ch; |
---|
[d1b1380] | 127 | |
---|
[a420b49] | 128 | ch = getc(fh); |
---|
[647407d] | 129 | while (ch != '\n' && ch != '\r' && ch != EOF && i < len - 1) { |
---|
[a420b49] | 130 | buf[i++] = ch; |
---|
| 131 | ch = getc(fh); |
---|
| 132 | } |
---|
[647407d] | 133 | if (ch == '\n' || ch == '\r') { |
---|
| 134 | /* remove any further eol chars (for DOS text files) */ |
---|
[a420b49] | 135 | do { |
---|
| 136 | ch = getc(fh); |
---|
| 137 | } while (ch == '\n' || ch == '\r'); |
---|
| 138 | ungetc(ch, fh); /* we don't want it, so put it back */ |
---|
| 139 | } |
---|
| 140 | buf[i] = '\0'; |
---|
[5c3c61a] | 141 | return i; |
---|
[d1b1380] | 142 | } |
---|
[c0563da] | 143 | |
---|
[a2ad284] | 144 | static char * |
---|
| 145 | getline_alloc(FILE *fh) |
---|
| 146 | { |
---|
| 147 | int ch; |
---|
| 148 | size_t i = 0; |
---|
| 149 | size_t len = 16; |
---|
| 150 | char *buf = xosmalloc(len); |
---|
| 151 | if (!buf) return NULL; |
---|
| 152 | |
---|
| 153 | ch = getc(fh); |
---|
| 154 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 155 | buf[i++] = ch; |
---|
| 156 | if (i == len - 1) { |
---|
| 157 | char *p; |
---|
| 158 | len += len; |
---|
| 159 | p = xosrealloc(buf, len); |
---|
| 160 | if (!p) { |
---|
| 161 | osfree(buf); |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | buf = p; |
---|
| 165 | } |
---|
| 166 | ch = getc(fh); |
---|
| 167 | } |
---|
| 168 | if (ch == '\n' || ch == '\r') { |
---|
| 169 | /* remove any further eol chars (for DOS text files) */ |
---|
| 170 | do { |
---|
| 171 | ch = getc(fh); |
---|
| 172 | } while (ch == '\n' || ch == '\r'); |
---|
| 173 | ungetc(ch, fh); /* we don't want it, so put it back */ |
---|
| 174 | } |
---|
| 175 | buf[i++] = '\0'; |
---|
| 176 | return buf; |
---|
| 177 | } |
---|
| 178 | |
---|
[72af530] | 179 | #ifndef IMG_HOSTED |
---|
[a420b49] | 180 | img_errcode |
---|
| 181 | img_error(void) |
---|
| 182 | { |
---|
| 183 | return img_errno; |
---|
[d1b1380] | 184 | } |
---|
| 185 | #else |
---|
[a420b49] | 186 | int |
---|
| 187 | img_error(void) |
---|
| 188 | { |
---|
| 189 | return (int)img_errno; |
---|
[d1b1380] | 190 | } |
---|
| 191 | #endif |
---|
| 192 | |
---|
[a420b49] | 193 | img * |
---|
[a2ad284] | 194 | img_open_survey(const char *fnm, const char *survey) |
---|
[a420b49] | 195 | { |
---|
| 196 | img *pimg; |
---|
[0da6e60] | 197 | size_t len; |
---|
[a420b49] | 198 | |
---|
| 199 | if (fDirectory(fnm)) { |
---|
| 200 | img_errno = IMG_DIRECTORY; |
---|
| 201 | return NULL; |
---|
| 202 | } |
---|
[d1b1380] | 203 | |
---|
[a420b49] | 204 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 205 | if (pimg == NULL) { |
---|
| 206 | img_errno = IMG_OUTOFMEMORY; |
---|
| 207 | return NULL; |
---|
| 208 | } |
---|
[d1b1380] | 209 | |
---|
[23f7ea7] | 210 | pimg->buf_len = 257; |
---|
[0ff23cb] | 211 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 212 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 213 | osfree(pimg); |
---|
| 214 | img_errno = IMG_OUTOFMEMORY; |
---|
| 215 | return NULL; |
---|
| 216 | } |
---|
| 217 | |
---|
[a420b49] | 218 | pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL); |
---|
| 219 | if (pimg->fh == NULL) { |
---|
| 220 | osfree(pimg); |
---|
| 221 | img_errno = IMG_FILENOTFOUND; |
---|
| 222 | return NULL; |
---|
| 223 | } |
---|
[d1b1380] | 224 | |
---|
[0da6e60] | 225 | pimg->fRead = fTrue; /* reading from this file */ |
---|
| 226 | img_errno = IMG_NONE; |
---|
| 227 | |
---|
| 228 | pimg->flags = 0; |
---|
| 229 | |
---|
| 230 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
| 231 | pimg->label_len = 0; |
---|
| 232 | |
---|
| 233 | pimg->survey = NULL; |
---|
| 234 | pimg->survey_len = 0; |
---|
| 235 | |
---|
[58f02ae] | 236 | pimg->title = NULL; |
---|
[0da6e60] | 237 | if (survey) { |
---|
| 238 | len = strlen(survey); |
---|
| 239 | if (len) { |
---|
| 240 | if (survey[len - 1] == '.') len--; |
---|
| 241 | if (len) { |
---|
| 242 | char *p; |
---|
| 243 | pimg->survey = osmalloc(len + 2); |
---|
| 244 | memcpy(pimg->survey, survey, len); |
---|
| 245 | /* Set title to leaf survey name */ |
---|
| 246 | pimg->survey[len] = '\0'; |
---|
| 247 | p = strchr(pimg->survey, '.'); |
---|
| 248 | if (p) p++; else p = pimg->survey; |
---|
[58f02ae] | 249 | pimg->title = osstrdup(p); /* FIXME: osstrdup for non IMG_HOSTED case */ |
---|
[0da6e60] | 250 | pimg->survey[len] = '.'; |
---|
| 251 | pimg->survey[len + 1] = '\0'; |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | pimg->survey_len = len; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /* [version -1] already skipped heading line, or there wasn't one |
---|
| 258 | * [version 0] not in the middle of a 'LINE' command |
---|
| 259 | * [version 3] not in the middle of turning a LINE into a MOVE |
---|
| 260 | */ |
---|
| 261 | pimg->pending = 0; |
---|
| 262 | |
---|
| 263 | len = strlen(fnm); |
---|
| 264 | if (len > LITLEN(EXT_SVX_POS) + 1 && |
---|
| 265 | fnm[len - LITLEN(EXT_SVX_POS) - 1] == FNM_SEP_EXT && |
---|
| 266 | strcmp(fnm + len - LITLEN(EXT_SVX_POS), EXT_SVX_POS) == 0) { |
---|
| 267 | pimg->version = -1; |
---|
[58f02ae] | 268 | if (!survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
| 269 | /* FIXME: osstrdup for non IMG_HOSTED case */ |
---|
| 270 | pimg->datestamp = osstrdup(TIMENA); |
---|
| 271 | pimg->start = 0; |
---|
[0da6e60] | 272 | return pimg; |
---|
| 273 | } |
---|
| 274 | |
---|
[23f7ea7] | 275 | getline(tmpbuf, TMPBUFLEN, pimg->fh); /* id string */ |
---|
| 276 | if (strcmp(tmpbuf, "Survex 3D Image File") != 0) { |
---|
[a420b49] | 277 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 278 | goto error; |
---|
[a420b49] | 279 | } |
---|
[d1b1380] | 280 | |
---|
[23f7ea7] | 281 | getline(tmpbuf, TMPBUFLEN, pimg->fh); /* file format version */ |
---|
| 282 | pimg->version = (tolower(*tmpbuf) == 'b'); /* binary file iff B/b prefix */ |
---|
[a420b49] | 283 | /* knock off the 'B' or 'b' if it's there */ |
---|
[23f7ea7] | 284 | if (strcmp(tmpbuf + pimg->version, "v0.01") == 0) { |
---|
[0da6e60] | 285 | /* nothing special to do */ |
---|
[6c1c4f3] | 286 | } else if (pimg->version == 0 && tmpbuf[0] == 'v') { |
---|
| 287 | if (tmpbuf[1] < '2' || tmpbuf[1] > '3' || tmpbuf[2] != '\0') { |
---|
| 288 | img_errno = IMG_TOONEW; |
---|
[58f02ae] | 289 | goto error; |
---|
[6c1c4f3] | 290 | } |
---|
[d69255f] | 291 | pimg->version = tmpbuf[1] - '0'; |
---|
[5c3c61a] | 292 | } else { |
---|
[647407d] | 293 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 294 | goto error; |
---|
[a420b49] | 295 | } |
---|
[58ad9da] | 296 | |
---|
[58f02ae] | 297 | if (!pimg->title) pimg->title = getline_alloc(pimg->fh); |
---|
[a2ad284] | 298 | pimg->datestamp = getline_alloc(pimg->fh); |
---|
| 299 | if (!pimg->title || !pimg->datestamp) { |
---|
[58f02ae] | 300 | img_errno = IMG_OUTOFMEMORY; |
---|
| 301 | error: |
---|
[a2ad284] | 302 | osfree(pimg->title); |
---|
| 303 | fclose(pimg->fh); |
---|
| 304 | osfree(pimg); |
---|
| 305 | return NULL; |
---|
| 306 | } |
---|
| 307 | |
---|
[58f02ae] | 308 | pimg->start = ftell(pimg->fh); |
---|
| 309 | |
---|
[a420b49] | 310 | return pimg; |
---|
[d1b1380] | 311 | } |
---|
| 312 | |
---|
[f2588ca] | 313 | void |
---|
| 314 | img_rewind(img *pimg) |
---|
| 315 | { |
---|
| 316 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[58f02ae] | 317 | clearerr(pimg->fh); |
---|
| 318 | /* [version -1] already skipped heading line, or there wasn't one |
---|
| 319 | * [version 0] not in the middle of a 'LINE' command |
---|
[76bbb7c9] | 320 | * [version 3] not in the middle of turning a LINE into a MOVE */ |
---|
| 321 | pimg->pending = 0; |
---|
[d69255f] | 322 | |
---|
| 323 | img_errno = IMG_NONE; |
---|
[99cf51a] | 324 | |
---|
[5d7280f8] | 325 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[d69255f] | 326 | pimg->label_len = 0; |
---|
[f2588ca] | 327 | } |
---|
| 328 | |
---|
[a420b49] | 329 | img * |
---|
[fe8e80e] | 330 | img_open_write(const char *fnm, char *title_buf, bool fBinary) |
---|
[a420b49] | 331 | { |
---|
| 332 | time_t tm; |
---|
| 333 | img *pimg; |
---|
[d1b1380] | 334 | |
---|
[d69255f] | 335 | fBinary = fBinary; |
---|
| 336 | |
---|
[a420b49] | 337 | if (fDirectory(fnm)) { |
---|
| 338 | img_errno = IMG_DIRECTORY; |
---|
| 339 | return NULL; |
---|
| 340 | } |
---|
[d1b1380] | 341 | |
---|
[a420b49] | 342 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 343 | if (pimg == NULL) { |
---|
| 344 | img_errno = IMG_OUTOFMEMORY; |
---|
| 345 | return NULL; |
---|
| 346 | } |
---|
[d1b1380] | 347 | |
---|
[fe8e80e] | 348 | pimg->buf_len = 257; |
---|
[0ff23cb] | 349 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 350 | if (!pimg->label_buf) { |
---|
[fe8e80e] | 351 | osfree(pimg); |
---|
| 352 | img_errno = IMG_OUTOFMEMORY; |
---|
| 353 | return NULL; |
---|
| 354 | } |
---|
| 355 | |
---|
[a420b49] | 356 | pimg->fh = fopen(fnm, "wb"); |
---|
| 357 | if (!pimg->fh) { |
---|
| 358 | osfree(pimg); |
---|
| 359 | img_errno = IMG_CANTOPENOUT; |
---|
| 360 | return NULL; |
---|
| 361 | } |
---|
[d1b1380] | 362 | |
---|
[a420b49] | 363 | /* Output image file header */ |
---|
| 364 | fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */ |
---|
[5c3c61a] | 365 | if (img_output_version < 2) { |
---|
[d69255f] | 366 | pimg->version = 1; |
---|
| 367 | fputs("Bv0.01\n", pimg->fh); /* binary file format version number */ |
---|
[5c3c61a] | 368 | } else { |
---|
[d69255f] | 369 | pimg->version = (img_output_version > 2) ? 3 : 2; |
---|
[5c3c61a] | 370 | fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */ |
---|
| 371 | } |
---|
[fe8e80e] | 372 | fputsnl(title_buf, pimg->fh); |
---|
[a420b49] | 373 | tm = time(NULL); |
---|
| 374 | if (tm == (time_t)-1) { |
---|
| 375 | fputsnl(TIMENA, pimg->fh); |
---|
| 376 | } else { |
---|
| 377 | /* output current date and time in format specified */ |
---|
[23f7ea7] | 378 | strftime(tmpbuf, TMPBUFLEN, TIMEFMT, localtime(&tm)); |
---|
| 379 | fputsnl(tmpbuf, pimg->fh); |
---|
[a420b49] | 380 | } |
---|
| 381 | pimg->fRead = fFalse; /* writing to this file */ |
---|
| 382 | img_errno = IMG_NONE; |
---|
[d69255f] | 383 | |
---|
[5d7280f8] | 384 | /* for version 3 we use label_buf to store the prefix for reuse */ |
---|
[0ff23cb] | 385 | pimg->label_buf[0] = '\0'; |
---|
[d69255f] | 386 | pimg->label_len = 0; |
---|
| 387 | |
---|
[ad9a5cd] | 388 | /* Don't check for write errors now - let img_close() report them... */ |
---|
[a420b49] | 389 | return pimg; |
---|
[d1b1380] | 390 | } |
---|
| 391 | |
---|
[ad9a5cd] | 392 | static int |
---|
| 393 | read_coord(FILE *fh, img_point *pt) |
---|
| 394 | { |
---|
| 395 | ASSERT(fh); |
---|
| 396 | ASSERT(pt); |
---|
| 397 | pt->x = get32(fh) / 100.0; |
---|
| 398 | pt->y = get32(fh) / 100.0; |
---|
| 399 | pt->z = get32(fh) / 100.0; |
---|
| 400 | if (ferror(fh) || feof(fh)) { |
---|
| 401 | img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 402 | return 0; |
---|
| 403 | } |
---|
| 404 | return 1; |
---|
| 405 | } |
---|
| 406 | |
---|
[a420b49] | 407 | int |
---|
[23f7ea7] | 408 | img_read_item(img *pimg, img_point *p) |
---|
[a420b49] | 409 | { |
---|
| 410 | int result; |
---|
[95c3272] | 411 | pimg->flags = 0; |
---|
[0ff23cb] | 412 | pimg->label = pimg->label_buf; |
---|
[d69255f] | 413 | |
---|
| 414 | if (pimg->version == 3) { |
---|
| 415 | int opt; |
---|
[76bbb7c9] | 416 | if (pimg->pending >= 0x80) { |
---|
| 417 | *p = pimg->mv; |
---|
| 418 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 419 | pimg->pending = 0; |
---|
[99cf51a] | 420 | return img_LINE; |
---|
[76bbb7c9] | 421 | } |
---|
[d69255f] | 422 | again3: /* label to goto if we get a prefix */ |
---|
| 423 | opt = getc(pimg->fh); |
---|
[ad9a5cd] | 424 | if (opt == EOF) { |
---|
| 425 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 426 | return img_BAD; |
---|
| 427 | } |
---|
[d69255f] | 428 | switch (opt >> 6) { |
---|
| 429 | case 0: |
---|
| 430 | if (opt == 0) { |
---|
| 431 | if (!pimg->label_len) return img_STOP; /* end of data marker */ |
---|
| 432 | pimg->label_len = 0; |
---|
| 433 | goto again3; |
---|
| 434 | } |
---|
| 435 | if (opt < 15) { |
---|
| 436 | /* 1-14 mean trim that many levels from current prefix */ |
---|
| 437 | int c; |
---|
| 438 | /* zero prefix using "0" */ |
---|
| 439 | if (pimg->label_len <= 16) { |
---|
| 440 | img_errno = IMG_BADFORMAT; |
---|
| 441 | return img_BAD; |
---|
| 442 | } |
---|
| 443 | c = pimg->label_len - 16; |
---|
| 444 | opt &= 0x07; |
---|
[0ff23cb] | 445 | while (pimg->label_buf[c] != '.' || --opt > 0) { |
---|
[d69255f] | 446 | if (--c < 0) { |
---|
| 447 | /* zero prefix using "0" */ |
---|
| 448 | img_errno = IMG_BADFORMAT; |
---|
| 449 | return img_BAD; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | c++; |
---|
[0ff23cb] | 453 | pimg->label_buf[c] = '\0'; |
---|
[d69255f] | 454 | pimg->label_len = c; |
---|
| 455 | goto again3; |
---|
| 456 | } |
---|
| 457 | if (opt == 15) { |
---|
| 458 | result = img_MOVE; |
---|
| 459 | break; |
---|
| 460 | } |
---|
| 461 | /* 16-31 mean remove (n - 15) characters from the prefix */ |
---|
| 462 | pimg->label_len -= (opt - 15); |
---|
| 463 | /* zero prefix using 0 */ |
---|
| 464 | if (pimg->label_len <= 0) { |
---|
| 465 | img_errno = IMG_BADFORMAT; |
---|
| 466 | return img_BAD; |
---|
| 467 | } |
---|
| 468 | goto again3; |
---|
| 469 | case 1: case 2: { |
---|
| 470 | char *q; |
---|
[ad9a5cd] | 471 | long len = getc(pimg->fh); |
---|
| 472 | if (len == EOF) { |
---|
| 473 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 474 | return img_BAD; |
---|
| 475 | } |
---|
[d69255f] | 476 | if (len == 0xfe) { |
---|
| 477 | len += get16(pimg->fh); |
---|
[ad9a5cd] | 478 | if (feof(pimg->fh)) { |
---|
| 479 | img_errno = IMG_BADFORMAT; |
---|
| 480 | return img_BAD; |
---|
| 481 | } |
---|
| 482 | if (ferror(pimg->fh)) { |
---|
| 483 | img_errno = IMG_READERROR; |
---|
| 484 | return img_BAD; |
---|
| 485 | } |
---|
[d69255f] | 486 | } else if (len == 0xff) { |
---|
| 487 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 488 | if (ferror(pimg->fh)) { |
---|
| 489 | img_errno = IMG_READERROR; |
---|
| 490 | return img_BAD; |
---|
| 491 | } |
---|
| 492 | if (feof(pimg->fh) || len < 0xfe + 0xffff) { |
---|
[d69255f] | 493 | img_errno = IMG_BADFORMAT; |
---|
| 494 | return img_BAD; |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | |
---|
[0ff23cb] | 498 | q = pimg->label_buf + pimg->label_len; |
---|
[d69255f] | 499 | pimg->label_len += len; |
---|
| 500 | if (pimg->label_len >= pimg->buf_len) { |
---|
[58f02ae] | 501 | char *b = xosrealloc(pimg->label_buf, pimg->label_len + 1); |
---|
| 502 | if (!b) { |
---|
[d69255f] | 503 | img_errno = IMG_OUTOFMEMORY; |
---|
| 504 | return img_BAD; |
---|
| 505 | } |
---|
[58f02ae] | 506 | q = (q - pimg->label_buf) + b; |
---|
| 507 | pimg->label = pimg->label_buf = b; |
---|
[d69255f] | 508 | pimg->buf_len = pimg->label_len + 1; |
---|
| 509 | } |
---|
| 510 | if (len && fread(q, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 511 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 512 | return img_BAD; |
---|
| 513 | } |
---|
| 514 | q[len] = '\0'; |
---|
[76bbb7c9] | 515 | |
---|
| 516 | result = opt & 0x40 ? img_LABEL : img_LINE; |
---|
| 517 | |
---|
| 518 | if (pimg->survey_len) { |
---|
| 519 | size_t l = pimg->survey_len; |
---|
[0ff23cb] | 520 | const char *s = pimg->label_buf; |
---|
[76bbb7c9] | 521 | if (result == img_LINE) { |
---|
| 522 | if (strncmp(pimg->survey, s, l) != 0 || |
---|
| 523 | !(s[l] == '.' || s[l] == '\0')) { |
---|
[ad9a5cd] | 524 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 525 | pimg->pending = 15; |
---|
| 526 | goto again3; |
---|
| 527 | } |
---|
| 528 | } else { |
---|
| 529 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
| 530 | fseek(pimg->fh, 12, SEEK_CUR); |
---|
| 531 | goto again3; |
---|
| 532 | } |
---|
| 533 | } |
---|
[0ff23cb] | 534 | pimg->label += l; |
---|
| 535 | /* skip the dot if there */ |
---|
| 536 | if (*pimg->label) pimg->label++; |
---|
[76bbb7c9] | 537 | } |
---|
| 538 | |
---|
| 539 | if (result == img_LINE && pimg->pending) { |
---|
| 540 | *p = pimg->mv; |
---|
[ad9a5cd] | 541 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 542 | pimg->pending = opt; |
---|
| 543 | return img_MOVE; |
---|
| 544 | } |
---|
| 545 | pimg->flags = (int)opt & 0x3f; |
---|
[d69255f] | 546 | break; |
---|
| 547 | } |
---|
| 548 | default: |
---|
| 549 | img_errno = IMG_BADFORMAT; |
---|
| 550 | return img_BAD; |
---|
| 551 | } |
---|
[ad9a5cd] | 552 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
[76bbb7c9] | 553 | pimg->pending = 0; |
---|
[d69255f] | 554 | return result; |
---|
| 555 | } else if (pimg->version > 0) { |
---|
| 556 | static long opt_lookahead = 0; |
---|
[ad9a5cd] | 557 | static img_point pt = { 0.0, 0.0, 0.0 }; |
---|
[a420b49] | 558 | long opt; |
---|
| 559 | again: /* label to goto if we get a cross */ |
---|
[5d7280f8] | 560 | pimg->label[0] = '\0'; |
---|
[ad9a5cd] | 561 | |
---|
[5c3c61a] | 562 | if (pimg->version == 1) { |
---|
[c4d4649] | 563 | if (opt_lookahead) { |
---|
| 564 | opt = opt_lookahead; |
---|
| 565 | opt_lookahead = 0; |
---|
| 566 | } else { |
---|
| 567 | opt = get32(pimg->fh); |
---|
| 568 | } |
---|
[5c3c61a] | 569 | } else { |
---|
| 570 | opt = getc(pimg->fh); |
---|
| 571 | } |
---|
[ad9a5cd] | 572 | |
---|
| 573 | if (feof(pimg->fh)) { |
---|
| 574 | img_errno = IMG_BADFORMAT; |
---|
| 575 | return img_BAD; |
---|
| 576 | } |
---|
| 577 | if (ferror(pimg->fh)) { |
---|
| 578 | img_errno = IMG_READERROR; |
---|
| 579 | return img_BAD; |
---|
| 580 | } |
---|
| 581 | |
---|
[a420b49] | 582 | switch (opt) { |
---|
[437caf3] | 583 | case -1: case 0: |
---|
[a420b49] | 584 | return img_STOP; /* end of data marker */ |
---|
| 585 | case 1: |
---|
[ad9a5cd] | 586 | /* skip coordinates */ |
---|
| 587 | if (fseek(pimg->fh, 12, SEEK_CUR) == -1) { |
---|
| 588 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 589 | return img_BAD; |
---|
| 590 | } |
---|
[a420b49] | 591 | goto again; |
---|
[23f7ea7] | 592 | case 2: case 3: { |
---|
[fe8e80e] | 593 | char *q; |
---|
[3a1f8da] | 594 | int ch; |
---|
[437caf3] | 595 | result = img_LABEL; |
---|
[3a1f8da] | 596 | ch = getc(pimg->fh); |
---|
[d69255f] | 597 | if (ch == EOF) { |
---|
[ad9a5cd] | 598 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 599 | return img_BAD; |
---|
| 600 | } |
---|
[3a1f8da] | 601 | if (ch != '\\') ungetc(ch, pimg->fh); |
---|
[0ff23cb] | 602 | fgets(pimg->label_buf, 257, pimg->fh); |
---|
[ad9a5cd] | 603 | if (feof(pimg->fh)) { |
---|
| 604 | img_errno = IMG_BADFORMAT; |
---|
| 605 | return img_BAD; |
---|
| 606 | } |
---|
| 607 | if (ferror(pimg->fh)) { |
---|
| 608 | img_errno = IMG_READERROR; |
---|
| 609 | return img_BAD; |
---|
| 610 | } |
---|
[0ff23cb] | 611 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[d69255f] | 612 | if (*q != '\n') { |
---|
| 613 | img_errno = IMG_BADFORMAT; |
---|
| 614 | return img_BAD; |
---|
| 615 | } |
---|
[9968ebf] | 616 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 617 | if (q == pimg->label_buf) goto again; |
---|
[fe8e80e] | 618 | *q = '\0'; |
---|
[509e099] | 619 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[437caf3] | 620 | if (opt == 2) goto done; |
---|
| 621 | break; |
---|
[23f7ea7] | 622 | } |
---|
[95c3272] | 623 | case 6: case 7: { |
---|
[ad9a5cd] | 624 | long len; |
---|
[23f7ea7] | 625 | result = img_LABEL; |
---|
[ad9a5cd] | 626 | |
---|
[509e099] | 627 | if (opt == 7) |
---|
| 628 | pimg->flags = getc(pimg->fh); |
---|
| 629 | else |
---|
| 630 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[ad9a5cd] | 631 | |
---|
[23f7ea7] | 632 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 633 | |
---|
| 634 | if (feof(pimg->fh)) { |
---|
| 635 | img_errno = IMG_BADFORMAT; |
---|
| 636 | return img_BAD; |
---|
| 637 | } |
---|
| 638 | if (ferror(pimg->fh)) { |
---|
| 639 | img_errno = IMG_READERROR; |
---|
| 640 | return img_BAD; |
---|
| 641 | } |
---|
| 642 | |
---|
[9968ebf] | 643 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
| 644 | if (len == 0) goto again; |
---|
[dd1d6369] | 645 | if (len >= (long)pimg->buf_len) { |
---|
[58f02ae] | 646 | pimg->label = pimg->label_buf = xosrealloc(pimg->label_buf, |
---|
| 647 | len + 1); |
---|
[0ff23cb] | 648 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 649 | img_errno = IMG_OUTOFMEMORY; |
---|
| 650 | return img_BAD; |
---|
| 651 | } |
---|
| 652 | pimg->buf_len = len + 1; |
---|
| 653 | } |
---|
[0ff23cb] | 654 | if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 655 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 656 | return img_BAD; |
---|
| 657 | } |
---|
[23f7ea7] | 658 | break; |
---|
| 659 | } |
---|
[a420b49] | 660 | case 4: |
---|
| 661 | result = img_MOVE; |
---|
| 662 | break; |
---|
| 663 | case 5: |
---|
| 664 | result = img_LINE; |
---|
| 665 | break; |
---|
| 666 | default: |
---|
[95c3272] | 667 | switch ((int)opt & 0xc0) { |
---|
| 668 | case 0x80: |
---|
| 669 | pimg->flags = (int)opt & 0x3f; |
---|
| 670 | result = img_LINE; |
---|
| 671 | break; |
---|
| 672 | case 0x40: { |
---|
| 673 | char *q; |
---|
| 674 | pimg->flags = (int)opt & 0x3f; |
---|
| 675 | result = img_LABEL; |
---|
[0ff23cb] | 676 | if (!fgets(pimg->label_buf, 257, pimg->fh)) { |
---|
[ad9a5cd] | 677 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 678 | return img_BAD; |
---|
| 679 | } |
---|
[0ff23cb] | 680 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[9968ebf] | 681 | /* Ignore empty-labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 682 | if (q == pimg->label_buf) goto again; |
---|
[d69255f] | 683 | if (*q != '\n') { |
---|
| 684 | img_errno = IMG_BADFORMAT; |
---|
| 685 | return img_BAD; |
---|
| 686 | } |
---|
[95c3272] | 687 | *q = '\0'; |
---|
| 688 | break; |
---|
| 689 | } |
---|
| 690 | case 0xc0: |
---|
| 691 | /* use this for an extra leg or station flag if we need it */ |
---|
| 692 | default: |
---|
[d69255f] | 693 | img_errno = IMG_BADFORMAT; |
---|
[95c3272] | 694 | return img_BAD; |
---|
| 695 | } |
---|
[437caf3] | 696 | break; |
---|
[a420b49] | 697 | } |
---|
[ad9a5cd] | 698 | |
---|
| 699 | if (!read_coord(pimg->fh, &pt)) return img_BAD; |
---|
[76bbb7c9] | 700 | |
---|
[ed0f5b6] | 701 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 702 | if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 703 | goto again; |
---|
| 704 | pimg->label += pimg->survey_len + 1; |
---|
| 705 | } |
---|
[76bbb7c9] | 706 | |
---|
[a420b49] | 707 | done: |
---|
[ad9a5cd] | 708 | *p = pt; |
---|
[c4d4649] | 709 | |
---|
| 710 | if (result == img_MOVE && pimg->version == 1) { |
---|
[d69255f] | 711 | /* peek at next code and see if it's an old-style label */ |
---|
[23f7ea7] | 712 | opt_lookahead = get32(pimg->fh); |
---|
[ad9a5cd] | 713 | |
---|
| 714 | if (feof(pimg->fh)) { |
---|
| 715 | img_errno = IMG_BADFORMAT; |
---|
| 716 | return img_BAD; |
---|
| 717 | } |
---|
| 718 | if (ferror(pimg->fh)) { |
---|
| 719 | img_errno = IMG_READERROR; |
---|
| 720 | return img_BAD; |
---|
| 721 | } |
---|
| 722 | |
---|
[d69255f] | 723 | if (opt_lookahead == 2) return img_read_item(pimg, p); |
---|
[c4d4649] | 724 | } |
---|
[23f7ea7] | 725 | |
---|
[a420b49] | 726 | return result; |
---|
[0da6e60] | 727 | } else if (pimg->version == 0) { |
---|
[a420b49] | 728 | ascii_again: |
---|
[5d7280f8] | 729 | pimg->label[0] = '\0'; |
---|
[a420b49] | 730 | if (feof(pimg->fh)) return img_STOP; |
---|
[76bbb7c9] | 731 | if (pimg->pending) { |
---|
| 732 | pimg->pending = 0; |
---|
[a420b49] | 733 | result = img_LINE; |
---|
| 734 | } else { |
---|
| 735 | /* Stop if nothing found */ |
---|
[23f7ea7] | 736 | if (fscanf(pimg->fh, "%s", tmpbuf) < 1) return img_STOP; |
---|
| 737 | if (strcmp(tmpbuf, "move") == 0) |
---|
[a420b49] | 738 | result = img_MOVE; |
---|
[23f7ea7] | 739 | else if (strcmp(tmpbuf, "draw") == 0) |
---|
[a420b49] | 740 | result = img_LINE; |
---|
[23f7ea7] | 741 | else if (strcmp(tmpbuf, "line") == 0) { |
---|
[bd1913f] | 742 | /* set flag to indicate to process second triplet as LINE */ |
---|
[76bbb7c9] | 743 | pimg->pending = 1; |
---|
[a420b49] | 744 | result = img_MOVE; |
---|
[23f7ea7] | 745 | } else if (strcmp(tmpbuf, "cross") == 0) { |
---|
[d69255f] | 746 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 747 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[0ed0e16] | 748 | return img_BAD; |
---|
[d69255f] | 749 | } |
---|
[a420b49] | 750 | goto ascii_again; |
---|
[23f7ea7] | 751 | } else if (strcmp(tmpbuf, "name") == 0) { |
---|
[0da6e60] | 752 | size_t off = 0; |
---|
[58f02ae] | 753 | int ch = getc(pimg->fh); |
---|
| 754 | if (ch == ' ') ch = getc(pimg->fh); |
---|
| 755 | while (ch != ' ') { |
---|
| 756 | if (ch == '\n' || ch == EOF) { |
---|
| 757 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[ad9a5cd] | 758 | return img_BAD; |
---|
| 759 | } |
---|
[58f02ae] | 760 | if (off == pimg->buf_len) { |
---|
| 761 | pimg->buf_len += pimg->buf_len; |
---|
| 762 | pimg->label_buf = xosrealloc(pimg->label_buf, pimg->buf_len); |
---|
[0da6e60] | 763 | } |
---|
[58f02ae] | 764 | pimg->label_buf[off++] = ch; |
---|
| 765 | ch = getc(pimg->fh); |
---|
[ad9a5cd] | 766 | } |
---|
[58f02ae] | 767 | pimg->label_buf[off] = '\0'; |
---|
| 768 | |
---|
| 769 | pimg->label = pimg->label_buf; |
---|
| 770 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[0da6e60] | 771 | |
---|
[a420b49] | 772 | result = img_LABEL; |
---|
[d69255f] | 773 | } else { |
---|
| 774 | img_errno = IMG_BADFORMAT; |
---|
[a420b49] | 775 | return img_BAD; /* unknown keyword */ |
---|
[d69255f] | 776 | } |
---|
[a420b49] | 777 | } |
---|
[d1b1380] | 778 | |
---|
[d69255f] | 779 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 780 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[fe8e80e] | 781 | return img_BAD; |
---|
[d69255f] | 782 | } |
---|
| 783 | |
---|
[58f02ae] | 784 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 785 | if (strncmp(pimg->label, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 786 | goto ascii_again; |
---|
| 787 | pimg->label += pimg->survey_len + 1; |
---|
| 788 | } |
---|
[76bbb7c9] | 789 | |
---|
[a420b49] | 790 | return result; |
---|
[0da6e60] | 791 | } else { |
---|
| 792 | /* version -1: .pos file */ |
---|
[58f02ae] | 793 | size_t off; |
---|
| 794 | againpos: |
---|
| 795 | off = 0; |
---|
[0da6e60] | 796 | while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) { |
---|
| 797 | int ch; |
---|
| 798 | if (ferror(pimg->fh)) { |
---|
| 799 | img_errno = IMG_READERROR; |
---|
| 800 | return img_BAD; |
---|
| 801 | } |
---|
| 802 | if (feof(pimg->fh)) return img_STOP; |
---|
| 803 | if (pimg->pending) { |
---|
| 804 | img_errno = IMG_BADFORMAT; |
---|
| 805 | return img_BAD; |
---|
| 806 | } |
---|
| 807 | pimg->pending = 1; |
---|
| 808 | /* ignore rest of line */ |
---|
| 809 | do { |
---|
| 810 | ch = getc(pimg->fh); |
---|
| 811 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | pimg->label_buf[0] = '\0'; |
---|
| 815 | while (!feof(pimg->fh)) { |
---|
| 816 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 817 | img_errno = IMG_READERROR; |
---|
| 818 | return img_BAD; |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | off += strlen(pimg->label_buf + off); |
---|
| 822 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 823 | pimg->label_buf[off - 1] = '\0'; |
---|
| 824 | break; |
---|
| 825 | } |
---|
| 826 | pimg->buf_len += pimg->buf_len; |
---|
| 827 | pimg->label_buf = xosrealloc(pimg->label_buf, pimg->buf_len); |
---|
| 828 | } |
---|
| 829 | |
---|
[58f02ae] | 830 | pimg->label = pimg->label_buf; |
---|
| 831 | |
---|
| 832 | if (pimg->label[0] == '\\') pimg->label++; |
---|
| 833 | |
---|
| 834 | if (pimg->survey_len) { |
---|
| 835 | size_t l = pimg->survey_len + 1; |
---|
| 836 | if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos; |
---|
| 837 | pimg->label += l; |
---|
| 838 | } |
---|
| 839 | |
---|
[0da6e60] | 840 | return img_LABEL; |
---|
[a420b49] | 841 | } |
---|
[d1b1380] | 842 | } |
---|
| 843 | |
---|
[ad9a5cd] | 844 | static int |
---|
[d69255f] | 845 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 846 | { |
---|
| 847 | size_t len, n, dot; |
---|
| 848 | |
---|
| 849 | /* find length of common prefix */ |
---|
| 850 | dot = 0; |
---|
[0ff23cb] | 851 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 852 | if (s[len] == '.') dot = len + 1; |
---|
| 853 | } |
---|
| 854 | |
---|
| 855 | ASSERT(len <= pimg->label_len); |
---|
| 856 | n = pimg->label_len - len; |
---|
| 857 | if (len == 0) { |
---|
| 858 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 859 | } else if (n <= 16) { |
---|
| 860 | if (n) putc(n + 15, pimg->fh); |
---|
| 861 | } else if (dot == 0) { |
---|
| 862 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[99cf51a] | 863 | len = 0; |
---|
[d69255f] | 864 | } else { |
---|
[0ff23cb] | 865 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 866 | n = 1; |
---|
| 867 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 868 | if (*p++ == '.') n++; |
---|
| 869 | } |
---|
| 870 | if (n <= 14) { |
---|
| 871 | putc(n, pimg->fh); |
---|
| 872 | len = dot; |
---|
| 873 | } else { |
---|
| 874 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 875 | len = 0; |
---|
| 876 | } |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | n = strlen(s + len); |
---|
| 880 | putc(opt, pimg->fh); |
---|
| 881 | if (n < 0xfe) { |
---|
| 882 | putc(n, pimg->fh); |
---|
| 883 | } else if (n < 0xffff + 0xfe) { |
---|
| 884 | putc(0xfe, pimg->fh); |
---|
| 885 | put16(n - 0xfe, pimg->fh); |
---|
| 886 | } else { |
---|
| 887 | putc(0xff, pimg->fh); |
---|
| 888 | put32(n, pimg->fh); |
---|
| 889 | } |
---|
| 890 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 891 | |
---|
| 892 | n += len; |
---|
| 893 | pimg->label_len = n; |
---|
| 894 | if (n >= pimg->buf_len) { |
---|
[0ff23cb] | 895 | char *p = xosrealloc(pimg->label_buf, n + 1); |
---|
[ad9a5cd] | 896 | if (!p) return 0; /* FIXME: distinguish out of memory... */ |
---|
| 897 | pimg->label_buf = p; |
---|
| 898 | pimg->buf_len = n + 1; |
---|
[d69255f] | 899 | } |
---|
[0ff23cb] | 900 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 901 | |
---|
| 902 | return !ferror(pimg->fh); |
---|
[d69255f] | 903 | } |
---|
| 904 | |
---|
[a420b49] | 905 | void |
---|
[95c3272] | 906 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 907 | double x, double y, double z) |
---|
[a420b49] | 908 | { |
---|
[647407d] | 909 | if (!pimg) return; |
---|
[d69255f] | 910 | if (pimg->version == 3) { |
---|
| 911 | int opt = 0; |
---|
| 912 | switch (code) { |
---|
| 913 | case img_LABEL: |
---|
| 914 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 915 | opt = 0; |
---|
| 916 | break; |
---|
| 917 | case img_MOVE: |
---|
| 918 | opt = 15; |
---|
| 919 | break; |
---|
| 920 | case img_LINE: |
---|
| 921 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 922 | opt = 0; |
---|
| 923 | break; |
---|
| 924 | default: /* ignore for now */ |
---|
| 925 | return; |
---|
| 926 | } |
---|
| 927 | if (opt) putc(opt, pimg->fh); |
---|
| 928 | /* Output in cm */ |
---|
[dd1d6369] | 929 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 930 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 931 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[d69255f] | 932 | } else { |
---|
[23f7ea7] | 933 | size_t len; |
---|
[badeec8] | 934 | INT32_T opt = 0; |
---|
[d69255f] | 935 | ASSERT(pimg->version > 0); |
---|
[a420b49] | 936 | switch (code) { |
---|
[437caf3] | 937 | case img_LABEL: |
---|
| 938 | if (pimg->version == 1) { |
---|
| 939 | /* put a move before each label */ |
---|
[95c3272] | 940 | img_write_item(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[437caf3] | 941 | put32(2, pimg->fh); |
---|
[0ed0e16] | 942 | fputsnl(s, pimg->fh); |
---|
[437caf3] | 943 | return; |
---|
| 944 | } |
---|
[23f7ea7] | 945 | len = strlen(s); |
---|
[af56069] | 946 | if (len > 255 || strchr(s, '\n')) { |
---|
[23f7ea7] | 947 | /* long label - not in early incarnations of v2 format, but few |
---|
| 948 | * 3d files will need these, so better not to force incompatibility |
---|
| 949 | * with a new version I think... */ |
---|
[95c3272] | 950 | putc(7, pimg->fh); |
---|
| 951 | putc(flags, pimg->fh); |
---|
[23f7ea7] | 952 | put32(len, pimg->fh); |
---|
| 953 | fputs(s, pimg->fh); |
---|
| 954 | } else { |
---|
[95c3272] | 955 | putc(0x40 | (flags & 0x3f), pimg->fh); |
---|
[23f7ea7] | 956 | fputsnl(s, pimg->fh); |
---|
| 957 | } |
---|
[437caf3] | 958 | opt = 0; |
---|
[23f7ea7] | 959 | break; |
---|
[a420b49] | 960 | case img_MOVE: |
---|
| 961 | opt = 4; |
---|
| 962 | break; |
---|
| 963 | case img_LINE: |
---|
[5c3c61a] | 964 | if (pimg->version > 1) { |
---|
[d69255f] | 965 | opt = 0x80 | (flags & 0x3f); |
---|
[437caf3] | 966 | break; |
---|
[5c3c61a] | 967 | } |
---|
[437caf3] | 968 | opt = 5; |
---|
[a420b49] | 969 | break; |
---|
[437caf3] | 970 | default: /* ignore for now */ |
---|
| 971 | return; |
---|
[a420b49] | 972 | } |
---|
[437caf3] | 973 | if (pimg->version == 1) { |
---|
| 974 | put32(opt, pimg->fh); |
---|
| 975 | } else { |
---|
| 976 | if (opt) putc(opt, pimg->fh); |
---|
[a420b49] | 977 | } |
---|
[d69255f] | 978 | /* Output in cm */ |
---|
[dd1d6369] | 979 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 980 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 981 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[a420b49] | 982 | } |
---|
[d1b1380] | 983 | } |
---|
| 984 | |
---|
[ad9a5cd] | 985 | int |
---|
[a420b49] | 986 | img_close(img *pimg) |
---|
| 987 | { |
---|
[ad9a5cd] | 988 | int result = 1; |
---|
[dbb4e19] | 989 | if (pimg) { |
---|
| 990 | if (pimg->fh) { |
---|
[76bbb7c9] | 991 | if (pimg->fRead) { |
---|
| 992 | osfree(pimg->survey); |
---|
[a2ad284] | 993 | osfree(pimg->title); |
---|
| 994 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 995 | } else { |
---|
| 996 | /* write end of data marker */ |
---|
[d69255f] | 997 | switch (pimg->version) { |
---|
| 998 | case 1: |
---|
[5c3c61a] | 999 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 1000 | break; |
---|
| 1001 | case 2: |
---|
| 1002 | putc(0, pimg->fh); |
---|
| 1003 | break; |
---|
| 1004 | case 3: |
---|
| 1005 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[437caf3] | 1006 | putc(0, pimg->fh); |
---|
[d69255f] | 1007 | break; |
---|
[5c3c61a] | 1008 | } |
---|
| 1009 | } |
---|
[ad9a5cd] | 1010 | if (ferror(pimg->fh)) result = 0; |
---|
| 1011 | if (fclose(pimg->fh)) result = 0; |
---|
| 1012 | img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 1013 | } |
---|
[0ff23cb] | 1014 | osfree(pimg->label_buf); |
---|
[dbb4e19] | 1015 | osfree(pimg); |
---|
[a420b49] | 1016 | } |
---|
[ad9a5cd] | 1017 | return result; |
---|
[d1b1380] | 1018 | } |
---|