source: git/src/img.c @ a93236ab

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

Fixed a few warnings from Norcroft C.

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