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