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