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