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