source: git/src/img.c @ 82be646

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 82be646 was a2ad284, checked in by Olly Betts <olly@…>, 24 years ago

Enhanced img to read arbitrarily long title and datestamp.

git-svn-id: file:///home/survex-svn/survex/trunk@1305 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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