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