source: git/src/img.c @ 58f02ae

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 58f02ae was 58f02ae, checked in by Olly Betts <olly@…>, 24 years ago

Added tests for 3dtopos; fixed some bugs in img.

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

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