source: git/src/img.c @ f558923

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

Improved handling of Compass PLT files and CMAP xyz files - no longer change
dots to spaces in survey and station names - instead use a space as the
separator between survey name and station name.

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

  • Property mode set to 100644
File size: 37.2 KB
Line 
1/* img.c
2 * Routines for reading and writing Survex ".3d" image files
3 * Copyright (C) 1993-2002 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 SVX_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
97/* portable case insensitive string compare */
98#if defined(strcasecmp) || defined(HAVE_STRCASECMP)
99# define my_strcasecmp strcasecmp
100#else
101/* What about top bit set chars? */
102int my_strcasecmp(const char *s1, const char *s2) {
103   register c1, c2;
104   do {
105      c1 = *s1++;
106      c2 = *s2++;
107   } while (c1 && toupper(c1) == toupper(c2));
108   /* now calculate real difference */
109   return c1 - c2;
110}
111#endif
112
113unsigned int img_output_version = 3;
114
115#ifdef IMG_HOSTED
116static enum {
117   IMG_NONE = 0,
118   IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24,
119   IMG_OUTOFMEMORY  = /*Out of memory %.0s*/38,
120   IMG_DIRECTORY    = /*Filename `%s' refers to directory*/44,
121   IMG_CANTOPENOUT  = /*Failed to open output file `%s'*/47,
122   IMG_BADFORMAT    = /*Bad 3d image file `%s'*/106,
123   IMG_READERROR    = /*Error reading from file `%s'*/109,
124   IMG_WRITEERROR   = /*Error writing to file `%s'*/110,
125   IMG_TOONEW       = /*File `%s' has a newer format than this program can understand*/114
126} img_errno = IMG_NONE;
127#else
128static img_errcode img_errno = IMG_NONE;
129#endif
130
131#define FILEID "Survex 3D Image File"
132
133#define EXT_PLT "plt"
134#define EXT_PLF "plf"
135#define EXT_XYZ "xyz"
136
137/* Attempt to string paste to ensure we are passed a literal string */
138#define LITLEN(S) (sizeof(S"") - 1)
139
140static char *
141my_strdup(const char *str)
142{
143   char *p;
144   size_t len = strlen(str) + 1;
145   p = xosmalloc(len);
146   if (p) memcpy(p, str, len);
147   return p;
148}
149
150static char *
151getline_alloc(FILE *fh)
152{
153   int ch;
154   size_t i = 0;
155   size_t len = 16;
156   char *buf = xosmalloc(len);
157   if (!buf) return NULL;
158
159   ch = getc(fh);
160   while (ch != '\n' && ch != '\r' && ch != EOF) {
161      buf[i++] = ch;
162      if (i == len - 1) {
163         char *p;
164         len += len;
165         p = xosrealloc(buf, len);
166         if (!p) {
167            osfree(buf);
168            return NULL;
169         }
170         buf = p;
171      }
172      ch = getc(fh);
173   }
174   if (ch == '\n' || ch == '\r') {
175      int otherone = ch ^ ('\n' ^ '\r');
176      ch = getc(fh);
177      /* if it's not the other eol character, put it back */
178      if (ch != otherone) ungetc(ch, fh);
179   }
180   buf[i++] = '\0';
181   return buf;
182}
183
184#ifndef IMG_HOSTED
185img_errcode
186img_error(void)
187{
188   return img_errno;
189}
190#else
191int
192img_error(void)
193{
194   return (int)img_errno;
195}
196#endif
197
198static bool
199check_label_space(img *pimg, size_t len)
200{
201   if (len > pimg->buf_len) {
202      char *b = xosrealloc(pimg->label_buf, len);
203      if (!b) return fFalse;
204      pimg->label = (pimg->label - pimg->label_buf) + b;
205      pimg->label_buf = b;
206      pimg->buf_len = len;
207   }
208   return fTrue;
209}
210
211#define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
212                        (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
213                        my_strcasecmp((F) + (L) - LITLEN(E), E) == 0)
214
215img *
216img_open_survey(const char *fnm, const char *survey)
217{
218   img *pimg;
219   size_t len;
220   char buf[LITLEN(FILEID) + 1];
221   int ch;
222
223   if (fDirectory(fnm)) {
224      img_errno = IMG_DIRECTORY;
225      return NULL;
226   }
227
228   pimg = (img *)xosmalloc(ossizeof(img));
229   if (pimg == NULL) {
230      img_errno = IMG_OUTOFMEMORY;
231      return NULL;
232   }
233
234   pimg->buf_len = 257;
235   pimg->label_buf = xosmalloc(pimg->buf_len);
236   if (!pimg->label_buf) {
237      osfree(pimg);
238      img_errno = IMG_OUTOFMEMORY;
239      return NULL;
240   }
241
242   pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL);
243   if (pimg->fh == NULL) {
244      osfree(pimg->label_buf);
245      osfree(pimg);
246      img_errno = IMG_FILENOTFOUND;
247      return NULL;
248   }
249
250   pimg->fRead = fTrue; /* reading from this file */
251   img_errno = IMG_NONE;
252
253   pimg->flags = 0;
254
255   /* for version 3 we use label_buf to store the prefix for reuse */
256   /* for version -2, 0 value indicates we haven't entered a survey yet */
257   /* for version -4, we store the last station here to detect whether
258    * we MOVE or LINE */
259   pimg->label_len = 0;
260   pimg->label_buf[0] = '\0';
261
262   pimg->survey = NULL;
263   pimg->survey_len = 0;
264   pimg->separator = '.';
265
266   pimg->title = pimg->datestamp = NULL;
267   if (survey) {
268      len = strlen(survey);
269      if (len) {
270         if (survey[len - 1] == '.') len--;
271         if (len) {
272            char *p;
273            pimg->survey = osmalloc(len + 2);
274            memcpy(pimg->survey, survey, len);
275            /* Set title to leaf survey name */
276            pimg->survey[len] = '\0';
277            p = strchr(pimg->survey, '.');
278            if (p) p++; else p = pimg->survey;
279            pimg->title = my_strdup(p);
280            if (!pimg->title) {
281               img_errno = IMG_OUTOFMEMORY;
282               goto error;
283            }
284            pimg->survey[len] = '.';
285            pimg->survey[len + 1] = '\0';
286         }
287      }
288      pimg->survey_len = len;
289   }
290
291   /* [version -2, -3, -4] pending IMG_LINE or IMG_MOVE - both have 4 added
292    * [version -1] already skipped heading line, or there wasn't one
293    * [version 0] not in the middle of a 'LINE' command
294    * [version 3] not in the middle of turning a LINE into a MOVE
295    */
296   pimg->pending = 0;
297
298   len = strlen(fnm);
299   if (has_ext(fnm, len, EXT_SVX_POS)) {
300pos_file:
301      pimg->version = -1;
302      if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm);
303      pimg->datestamp = my_strdup(TIMENA);
304      if (!pimg->datestamp) {
305         img_errno = IMG_OUTOFMEMORY;
306         goto error;
307      }
308      pimg->start = 0;
309      return pimg;
310   }
311
312   if (has_ext(fnm, len, EXT_PLT) || has_ext(fnm, len, EXT_PLF)) {
313      long fpos;
314plt_file:
315      pimg->version = -2;
316      /* Spaces aren't legal in Compass station names, but dots are, so
317       * use space as the level separator */
318      pimg->separator = ' ';
319      pimg->start = 0;
320      if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm);
321      pimg->datestamp = my_strdup(TIMENA);
322      if (!pimg->datestamp) {
323         img_errno = IMG_OUTOFMEMORY;
324         goto error;
325      }
326      while (1) {
327         int ch = getc(pimg->fh);
328         switch (ch) {
329          case '\x1a':
330            fseek(pimg->fh, -1, SEEK_CUR);
331            /* FALL THRU */
332          case EOF:
333            pimg->start = ftell(pimg->fh);
334            return pimg;
335          case 'N': {
336            char *line, *q;
337            fpos = ftell(pimg->fh) - 1;
338            if (!pimg->survey) {
339               /* FIXME : if there's only one survey in the file, it'd be nice
340                * to use its description as the title here...
341                */
342               ungetc('N', pimg->fh);
343               pimg->start = fpos;
344               return pimg;
345            }
346            line = getline_alloc(pimg->fh);
347            if (!line) {
348               img_errno = IMG_OUTOFMEMORY;
349               goto error;
350            }
351            len = 0;
352            while (line[len] > 32) ++len;
353            if (pimg->survey_len != len ||
354                memcmp(line, pimg->survey, len) != 0) {
355               osfree(line);
356               continue;
357            }
358            q = strchr(line + len, 'C');
359            if (q && q[1]) {
360                osfree(pimg->title);
361                pimg->title = my_strdup(q + 1);
362            } else if (!pimg->title) {
363                pimg->title = my_strdup(pimg->label);
364            }
365            osfree(line);
366            if (!pimg->title) {
367                img_errno = IMG_OUTOFMEMORY;
368                goto error;
369            }
370            if (!pimg->start) pimg->start = fpos;
371            fseek(pimg->fh, pimg->start, SEEK_SET);
372            return pimg;
373          }
374          case 'M': case 'D':
375            pimg->start = ftell(pimg->fh) - 1;
376            break;
377         }
378         while (ch != '\n' && ch != '\r') {
379            ch = getc(pimg->fh);
380         }
381      }
382   }
383
384   if (has_ext(fnm, len, EXT_XYZ)) {
385      char *line;
386xyz_file:
387      /* Spaces aren't legal in CMAP station names, but dots are, so
388       * use space as the level separator */
389      pimg->separator = ' ';
390      line = getline_alloc(pimg->fh);
391      if (!line) {
392         img_errno = IMG_OUTOFMEMORY;
393         goto error;
394      }
395      /* FIXME: reparse date? */
396      len = strlen(line);
397      if (len > 59) line[59] = '\0';
398      if (len > 45) {
399         pimg->datestamp = my_strdup(line + 45);
400      } else {
401         pimg->datestamp = my_strdup(TIMENA);
402      }
403      if (strncmp(line, "  Cave Survey Data Processed by CMAP ",
404                  LITLEN("  Cave Survey Data Processed by CMAP ")) == 0) {
405         len = 0;
406      } else {
407         if (len > 45) {
408            line[45] = '\0';
409            len = 45;
410         }
411         while (len > 2 && line[len - 1] == ' ') --len;
412         if (len > 2) {
413            line[len] = '\0';
414            pimg->title = my_strdup(line + 2);
415         }
416      }
417      if (len <= 2) pimg->title = baseleaf_from_fnm(fnm);
418      osfree(line);
419      if (!pimg->datestamp || !pimg->title) {
420         img_errno = IMG_OUTOFMEMORY;
421         goto error;
422      }
423      line = getline_alloc(pimg->fh);
424      if (!line) {
425         img_errno = IMG_OUTOFMEMORY;
426         goto error;
427      }
428      if (line[0] != ' ' || (line[1] != 'S' && line[1] != 'O')) {
429         img_errno = IMG_BADFORMAT;
430         goto error;
431      }
432      if (line[1] == 'S') {
433         pimg->version = -3; /* Station format */
434      } else {
435         pimg->version = -4; /* Shot format */
436      }
437      osfree(line);
438      line = getline_alloc(pimg->fh);
439      if (!line) {
440         img_errno = IMG_OUTOFMEMORY;
441         goto error;
442      }
443      if (line[0] != ' ' || line[1] != '-') {
444         img_errno = IMG_BADFORMAT;
445         goto error;
446      }
447      osfree(line);
448      pimg->start = ftell(pimg->fh);
449      return pimg;
450   }
451
452   if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 ||
453       memcmp(buf, FILEID"\n", LITLEN(FILEID) + 1) != 0) {
454      rewind(pimg->fh);
455      if (buf[1] == ' ') {
456         if (buf[0] == ' ') {
457            /* Looks like a CMAP .xyz file ... */
458            goto xyz_file;
459         } else if (strchr("ZSNF", buf[0])) {
460            /* Looks like a Compass .plt file ... */
461            /* Almost certainly it'll start "Z " */
462            goto plt_file;
463         }
464      }
465      if (buf[0] == '(') {
466         /* Looks like a Survex .pos file ... */
467         goto pos_file;
468      }
469      img_errno = IMG_BADFORMAT;
470      goto error;
471   }
472
473   /* check file format version */
474   ch = getc(pimg->fh);
475   pimg->version = 0;
476   if (tolower(ch) == 'b') {
477      /* binary file iff B/b prefix */
478      pimg->version = 1;
479      ch = getc(pimg->fh);
480   }
481   if (ch != 'v') {
482      img_errno = IMG_BADFORMAT;
483      goto error;
484   }
485   ch = getc(pimg->fh);
486   if (ch == '0') {
487      if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) {
488         img_errno = IMG_BADFORMAT;
489         goto error;
490      }
491      /* nothing special to do */
492   } else if (pimg->version == 0) {
493      if (ch < '2' || ch > '3' || getc(pimg->fh) != '\n') {
494         img_errno = IMG_TOONEW;
495         goto error;
496      }
497      pimg->version = ch - '0';
498   } else {
499      img_errno = IMG_BADFORMAT;
500      goto error;
501   }
502
503   if (!pimg->title)
504       pimg->title = getline_alloc(pimg->fh);
505   else
506       osfree(getline_alloc(pimg->fh));
507   pimg->datestamp = getline_alloc(pimg->fh);
508   if (!pimg->title || !pimg->datestamp) {
509      img_errno = IMG_OUTOFMEMORY;
510      error:
511      osfree(pimg->title);
512      osfree(pimg->datestamp);
513      fclose(pimg->fh);
514      osfree(pimg);
515      return NULL;
516   }
517
518   pimg->start = ftell(pimg->fh);
519
520   return pimg;
521}
522
523int
524img_rewind(img *pimg)
525{
526   if (!pimg->fRead) {
527      img_errno = IMG_WRITEERROR;
528      return 0;
529   }
530   if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) {
531      img_errno = IMG_READERROR;
532      return 0;
533   }
534   clearerr(pimg->fh);
535   /* [version -1] already skipped heading line, or there wasn't one
536    * [version 0] not in the middle of a 'LINE' command
537    * [version 3] not in the middle of turning a LINE into a MOVE */
538   pimg->pending = 0;
539
540   img_errno = IMG_NONE;
541
542   /* for version 3 we use label_buf to store the prefix for reuse */
543   /* for version -2, 0 value indicates we haven't entered a survey yet */
544   /* for version -4, we store the last station here to detect whether
545    * we MOVE or LINE */
546   pimg->label_len = 0;
547   return 1;
548}
549
550img *
551img_open_write(const char *fnm, char *title_buf, bool fBinary)
552{
553   time_t tm;
554   img *pimg;
555
556   fBinary = fBinary;
557
558   if (fDirectory(fnm)) {
559      img_errno = IMG_DIRECTORY;
560      return NULL;
561   }
562
563   pimg = (img *)xosmalloc(ossizeof(img));
564   if (pimg == NULL) {
565      img_errno = IMG_OUTOFMEMORY;
566      return NULL;
567   }
568
569   pimg->buf_len = 257;
570   pimg->label_buf = xosmalloc(pimg->buf_len);
571   if (!pimg->label_buf) {
572      osfree(pimg);
573      img_errno = IMG_OUTOFMEMORY;
574      return NULL;
575   }
576
577   pimg->fh = fopen(fnm, "wb");
578   if (!pimg->fh) {
579      osfree(pimg->label_buf);
580      osfree(pimg);
581      img_errno = IMG_CANTOPENOUT;
582      return NULL;
583   }
584
585   /* Output image file header */
586   fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */
587   if (img_output_version < 2) {
588      pimg->version = 1;
589      fputs("Bv0.01\n", pimg->fh); /* binary file format version number */
590   } else {
591      pimg->version = (img_output_version > 2) ? 3 : 2;
592      fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */
593   }
594   fputsnl(title_buf, pimg->fh);
595   tm = time(NULL);
596   if (tm == (time_t)-1) {
597      fputsnl(TIMENA, pimg->fh);
598   } else {
599      char date[256];
600      /* output current date and time in format specified */
601      strftime(date, 256, TIMEFMT, localtime(&tm));
602      fputsnl(date, pimg->fh);
603   }
604   pimg->fRead = fFalse; /* writing to this file */
605   img_errno = IMG_NONE;
606
607   /* for version 3 we use label_buf to store the prefix for reuse */
608   pimg->label_buf[0] = '\0';
609   pimg->label_len = 0;
610
611   /* Don't check for write errors now - let img_close() report them... */
612   return pimg;
613}
614
615static void
616read_xyz_station_coords(img_point *pt, const char *line)
617{
618   char num[12];
619   memcpy(num, line + 6, 9);
620   num[9] = '\0';
621   pt->x = atof(num) / METRES_PER_FOOT;
622   memcpy(num, line + 15, 9);
623   pt->y = atof(num) / METRES_PER_FOOT;
624   memcpy(num, line + 24, 8);
625   num[8] = '\0';
626   pt->z = atof(num) / METRES_PER_FOOT;
627}
628
629static void
630read_xyz_shot_coords(img_point *pt, const char *line)
631{
632   char num[12];
633   memcpy(num, line + 40, 10);
634   num[10] = '\0';
635   pt->x = atof(num) / METRES_PER_FOOT;
636   memcpy(num, line + 50, 10);
637   pt->y = atof(num) / METRES_PER_FOOT;
638   memcpy(num, line + 60, 9);
639   num[9] = '\0';
640   pt->z = atof(num) / METRES_PER_FOOT;
641}
642
643static void
644subtract_xyz_shot_deltas(img_point *pt, const char *line)
645{
646   char num[12];
647   memcpy(num, line + 15, 9);
648   num[9] = '\0';
649   pt->x -= atof(num) / METRES_PER_FOOT;
650   memcpy(num, line + 24, 8);
651   num[8] = '\0';
652   pt->y -= atof(num) / METRES_PER_FOOT;
653   memcpy(num, line + 32, 8);
654   pt->z -= atof(num) / METRES_PER_FOOT;
655}
656
657static int
658read_coord(FILE *fh, img_point *pt)
659{
660   SVX_ASSERT(fh);
661   SVX_ASSERT(pt);
662   pt->x = get32(fh) / 100.0;
663   pt->y = get32(fh) / 100.0;
664   pt->z = get32(fh) / 100.0;
665   if (ferror(fh) || feof(fh)) {
666      img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR;
667      return 0;
668   }
669   return 1;
670}
671
672static int
673skip_coord(FILE *fh)
674{
675    return (fseek(fh, 12, SEEK_CUR) == 0);
676}
677
678int
679img_read_item(img *pimg, img_point *p)
680{
681   int result;
682   pimg->flags = 0;
683
684   if (pimg->version == 3) {
685      int opt;
686      if (pimg->pending >= 0x80) {
687         *p = pimg->mv;
688         pimg->flags = (int)(pimg->pending) & 0x3f;
689         pimg->pending = 0;
690         return img_LINE;
691      }
692      again3: /* label to goto if we get a prefix */
693      pimg->label = pimg->label_buf;
694      opt = getc(pimg->fh);
695      if (opt == EOF) {
696         img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
697         return img_BAD;
698      }
699      switch (opt >> 6) {
700       case 0:
701         if (opt == 0) {
702            if (!pimg->label_len) return img_STOP; /* end of data marker */
703            pimg->label_len = 0;
704            goto again3;
705         }
706         if (opt < 15) {
707            /* 1-14 mean trim that many levels from current prefix */
708            int c;
709            if (pimg->label_len <= 16) {
710               /* zero prefix using "0" */
711               img_errno = IMG_BADFORMAT;
712               return img_BAD;
713            }
714            c = pimg->label_len - 16 - 1;
715            while (pimg->label_buf[c] != '.' || --opt > 0) {
716               if (--c < 0) {
717                  /* zero prefix using "0" */
718                  img_errno = IMG_BADFORMAT;
719                  return img_BAD;
720               }
721            }
722            c++;
723            pimg->label_len = c;
724            goto again3;
725         }
726         if (opt == 15) {
727            result = img_MOVE;
728            break;
729         }
730         /* 16-31 mean remove (n - 15) characters from the prefix */
731         /* zero prefix using 0 */
732         if (pimg->label_len <= (size_t)(opt - 15)) {
733            img_errno = IMG_BADFORMAT;
734            return img_BAD;
735         }
736         pimg->label_len -= (opt - 15);
737         goto again3;
738       case 1: case 2: {
739         char *q;
740         long len = getc(pimg->fh);
741         if (len == EOF) {
742            img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
743            return img_BAD;
744         }
745         if (len == 0xfe) {
746            len += get16(pimg->fh);
747            if (feof(pimg->fh)) {
748               img_errno = IMG_BADFORMAT;
749               return img_BAD;
750            }
751            if (ferror(pimg->fh)) {
752               img_errno = IMG_READERROR;
753               return img_BAD;
754            }
755         } else if (len == 0xff) {
756            len = get32(pimg->fh);
757            if (ferror(pimg->fh)) {
758               img_errno = IMG_READERROR;
759               return img_BAD;
760            }
761            if (feof(pimg->fh) || len < 0xfe + 0xffff) {
762               img_errno = IMG_BADFORMAT;
763               return img_BAD;
764            }
765         }
766
767         if (!check_label_space(pimg, pimg->label_len + len + 1)) {
768            img_errno = IMG_OUTOFMEMORY;
769            return img_BAD;
770         }
771         q = pimg->label_buf + pimg->label_len;
772         pimg->label_len += len;
773         if (len && fread(q, len, 1, pimg->fh) != 1) {
774            img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
775            return img_BAD;
776         }
777         q[len] = '\0';
778
779         result = opt & 0x40 ? img_LABEL : img_LINE;
780
781         if (pimg->survey_len) {
782            size_t l = pimg->survey_len;
783            const char *s = pimg->label_buf;
784            if (result == img_LINE) {
785               if (strncmp(pimg->survey, s, l) != 0 ||
786                   !(s[l] == '.' || s[l] == '\0')) {
787                  if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD;
788                  pimg->pending = 15;
789                  goto again3;
790               }
791            } else {
792               if (strncmp(pimg->survey, s, l + 1) != 0) {
793                  if (!skip_coord(pimg->fh)) return img_BAD;
794                  pimg->pending = 0;
795                  goto again3;
796               }
797            }
798            pimg->label += l;
799            /* skip the dot if there */
800            if (*pimg->label) pimg->label++;
801         }
802
803         if (result == img_LINE && pimg->pending) {
804            *p = pimg->mv;
805            if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD;
806            pimg->pending = opt;
807            return img_MOVE;
808         }
809         pimg->flags = (int)opt & 0x3f;
810         break;
811       }
812       default:
813         img_errno = IMG_BADFORMAT;
814         return img_BAD;
815      }
816      if (!read_coord(pimg->fh, p)) return img_BAD;
817      pimg->pending = 0;
818      return result;
819   }
820
821   pimg->label = pimg->label_buf;
822
823   if (pimg->version > 0) {
824      static long opt_lookahead = 0;
825      static img_point pt = { 0.0, 0.0, 0.0 };
826      long opt;
827      again: /* label to goto if we get a cross */
828      pimg->label[0] = '\0';
829
830      if (pimg->version == 1) {
831         if (opt_lookahead) {
832            opt = opt_lookahead;
833            opt_lookahead = 0;
834         } else {
835            opt = get32(pimg->fh);
836         }
837      } else {
838         opt = getc(pimg->fh);
839      }
840
841      if (feof(pimg->fh)) {
842         img_errno = IMG_BADFORMAT;
843         return img_BAD;
844      }
845      if (ferror(pimg->fh)) {
846         img_errno = IMG_READERROR;
847         return img_BAD;
848      }
849
850      switch (opt) {
851       case -1: case 0:
852         return img_STOP; /* end of data marker */
853       case 1:
854         /* skip coordinates */
855         if (!skip_coord(pimg->fh)) {
856            img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
857            return img_BAD;
858         }
859         goto again;
860       case 2: case 3: {
861         char *q;
862         int ch;
863         result = img_LABEL;
864         ch = getc(pimg->fh);
865         if (ch == EOF) {
866            img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
867            return img_BAD;
868         }
869         if (ch != '\\') ungetc(ch, pimg->fh);
870         fgets(pimg->label_buf, 257, pimg->fh);
871         if (feof(pimg->fh)) {
872            img_errno = IMG_BADFORMAT;
873            return img_BAD;
874         }
875         if (ferror(pimg->fh)) {
876            img_errno = IMG_READERROR;
877            return img_BAD;
878         }
879         q = pimg->label_buf + strlen(pimg->label_buf) - 1;
880         if (*q != '\n') {
881            img_errno = IMG_BADFORMAT;
882            return img_BAD;
883         }
884         /* Ignore empty labels in some .3d files (caused by a bug) */
885         if (q == pimg->label_buf) goto again;
886         *q = '\0';
887         pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
888         if (opt == 2) goto done;
889         break;
890       }
891       case 6: case 7: {
892         long len;
893         result = img_LABEL;
894
895         if (opt == 7)
896            pimg->flags = getc(pimg->fh);
897         else
898            pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
899
900         len = get32(pimg->fh);
901
902         if (feof(pimg->fh)) {
903            img_errno = IMG_BADFORMAT;
904            return img_BAD;
905         }
906         if (ferror(pimg->fh)) {
907            img_errno = IMG_READERROR;
908            return img_BAD;
909         }
910
911         /* Ignore empty labels in some .3d files (caused by a bug) */
912         if (len == 0) goto again;
913         if (!check_label_space(pimg, len + 1)) {
914            img_errno = IMG_OUTOFMEMORY;
915            return img_BAD;
916         }
917         if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) {
918            img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
919            return img_BAD;
920         }
921         pimg->label_buf[len] = '\0';
922         break;
923       }
924       case 4:
925         result = img_MOVE;
926         break;
927       case 5:
928         result = img_LINE;
929         break;
930       default:
931         switch ((int)opt & 0xc0) {
932          case 0x80:
933            pimg->flags = (int)opt & 0x3f;
934            result = img_LINE;
935            break;
936          case 0x40: {
937            char *q;
938            pimg->flags = (int)opt & 0x3f;
939            result = img_LABEL;
940            if (!fgets(pimg->label_buf, 257, pimg->fh)) {
941               img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
942               return img_BAD;
943            }
944            q = pimg->label_buf + strlen(pimg->label_buf) - 1;
945            /* Ignore empty-labels in some .3d files (caused by a bug) */
946            if (q == pimg->label_buf) goto again;
947            if (*q != '\n') {
948               img_errno = IMG_BADFORMAT;
949               return img_BAD;
950            }
951            *q = '\0';
952            break;
953          }
954          case 0xc0:
955            /* use this for an extra leg or station flag if we need it */
956          default:
957            img_errno = IMG_BADFORMAT;
958            return img_BAD;
959         }
960         break;
961      }
962
963      if (!read_coord(pimg->fh, &pt)) return img_BAD;
964
965      if (result == img_LABEL && pimg->survey_len) {
966         if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0)
967            goto again;
968         pimg->label += pimg->survey_len + 1;
969      }
970
971      done:
972      *p = pt;
973
974      if (result == img_MOVE && pimg->version == 1) {
975         /* peek at next code and see if it's an old-style label */
976         opt_lookahead = get32(pimg->fh);
977
978         if (feof(pimg->fh)) {
979            img_errno = IMG_BADFORMAT;
980            return img_BAD;
981         }
982         if (ferror(pimg->fh)) {
983            img_errno = IMG_READERROR;
984            return img_BAD;
985         }
986
987         if (opt_lookahead == 2) return img_read_item(pimg, p);
988      }
989
990      return result;
991   } else if (pimg->version == 0) {
992      ascii_again:
993      pimg->label[0] = '\0';
994      if (feof(pimg->fh)) return img_STOP;
995      if (pimg->pending) {
996         pimg->pending = 0;
997         result = img_LINE;
998      } else {
999         char cmd[7];
1000         /* Stop if nothing found */
1001         if (fscanf(pimg->fh, "%6s", cmd) < 1) return img_STOP;
1002         if (strcmp(cmd, "move") == 0)
1003            result = img_MOVE;
1004         else if (strcmp(cmd, "draw") == 0)
1005            result = img_LINE;
1006         else if (strcmp(cmd, "line") == 0) {
1007            /* set flag to indicate to process second triplet as LINE */
1008            pimg->pending = 1;
1009            result = img_MOVE;
1010         } else if (strcmp(cmd, "cross") == 0) {
1011            if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
1012               img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR;
1013               return img_BAD;
1014            }
1015            goto ascii_again;
1016         } else if (strcmp(cmd, "name") == 0) {
1017            size_t off = 0;
1018            int ch = getc(pimg->fh);
1019            if (ch == ' ') ch = getc(pimg->fh);
1020            while (ch != ' ') {
1021               if (ch == '\n' || ch == EOF) {
1022                  img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT;
1023                  return img_BAD;
1024               }
1025               if (off == pimg->buf_len) {
1026                  if (!check_label_space(pimg, pimg->buf_len * 2)) {
1027                     img_errno = IMG_OUTOFMEMORY;
1028                     return img_BAD;
1029                  }
1030               }
1031               pimg->label_buf[off++] = ch;
1032               ch = getc(pimg->fh);
1033            }
1034            pimg->label_buf[off] = '\0';
1035
1036            pimg->label = pimg->label_buf;
1037            if (pimg->label[0] == '\\') pimg->label++;
1038
1039            result = img_LABEL;
1040         } else {
1041            img_errno = IMG_BADFORMAT;
1042            return img_BAD; /* unknown keyword */
1043         }
1044      }
1045
1046      if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
1047         img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT;
1048         return img_BAD;
1049      }
1050
1051      if (result == img_LABEL && pimg->survey_len) {
1052         if (strncmp(pimg->label, pimg->survey, pimg->survey_len + 1) != 0)
1053            goto ascii_again;
1054         pimg->label += pimg->survey_len + 1;
1055      }
1056
1057      return result;
1058   } else if (pimg->version == -1) {
1059      /* version -1: .pos file */
1060      size_t off;
1061      pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */
1062      againpos:
1063      off = 0;
1064      while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) {
1065         int ch;
1066         if (ferror(pimg->fh)) {
1067            img_errno = IMG_READERROR;
1068            return img_BAD;
1069         }
1070         if (feof(pimg->fh)) return img_STOP;
1071         if (pimg->pending) {
1072            img_errno = IMG_BADFORMAT;
1073            return img_BAD;
1074         }
1075         pimg->pending = 1;
1076         /* ignore rest of line */
1077         do {
1078            ch = getc(pimg->fh);
1079         } while (ch != '\n' && ch != '\r' && ch != EOF);
1080      }
1081
1082      pimg->label_buf[0] = '\0';
1083      while (!feof(pimg->fh)) {
1084         if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) {
1085            img_errno = IMG_READERROR;
1086            return img_BAD;
1087         }
1088
1089         off += strlen(pimg->label_buf + off);
1090         if (off && pimg->label_buf[off - 1] == '\n') {
1091            pimg->label_buf[off - 1] = '\0';
1092            break;
1093         }
1094         if (!check_label_space(pimg, pimg->buf_len * 2)) {
1095            img_errno = IMG_OUTOFMEMORY;
1096            return img_BAD;
1097         }
1098      }
1099
1100      pimg->label = pimg->label_buf;
1101
1102      if (pimg->label[0] == '\\') pimg->label++;
1103
1104      if (pimg->survey_len) {
1105         size_t l = pimg->survey_len + 1;
1106         if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos;
1107         pimg->label += l;
1108      }
1109
1110      return img_LABEL;
1111   } else if (pimg->version == -2) {
1112      /* version -2: Compass .plt file */
1113      if (pimg->pending > 0) {
1114         /* -1 signals we've entered the first survey we want to
1115          * read, and need to fudge lots if the first action is 'D'...
1116          */
1117         /* pending MOVE or LINE */
1118         int r = pimg->pending - 4;
1119         pimg->pending = 0;
1120         pimg->flags = 0;
1121         pimg->label[pimg->label_len] = '\0';
1122         return r;
1123      }
1124
1125      while (1) {
1126         char *line;
1127         char *q;
1128         size_t len = 0;
1129         int ch = getc(pimg->fh);
1130
1131         switch (ch) {
1132            case '\x1a': case EOF: /* Don't insist on ^Z at end of file */
1133               return img_STOP;
1134            case 'X': case 'F': case 'S':
1135               /* bounding boX (marks end of survey), Feature survey, or
1136                * new Section - skip to next survey */
1137               if (pimg->survey) return img_STOP;
1138skip_to_N:
1139               while (1) {
1140                  do {
1141                     ch = getc(pimg->fh);
1142                  } while (ch != '\n' && ch != '\r' && ch != EOF);
1143                  while (ch == '\n' || ch == '\r') ch = getc(pimg->fh);
1144                  if (ch == 'N') break;
1145                  if (ch == '\x1a' || ch == EOF) return img_STOP;
1146               }
1147               /* FALLTHRU */
1148            case 'N':
1149               line = getline_alloc(pimg->fh);
1150               if (!line) {
1151                  img_errno = IMG_OUTOFMEMORY;
1152                  return img_BAD;
1153               }
1154               while (line[len] > 32) ++len;
1155               if (pimg->label_len == 0) pimg->pending = -1;
1156               if (!check_label_space(pimg, len + 1)) {
1157                  osfree(line);
1158                  img_errno = IMG_OUTOFMEMORY;
1159                  return img_BAD;
1160               }
1161               pimg->label_len = len;
1162               pimg->label = pimg->label_buf;
1163               memcpy(pimg->label, line, len);
1164               pimg->label[len] = '\0';
1165               osfree(line);
1166               break;
1167            case 'M': case 'D': {
1168               /* Move or Draw */
1169               long fpos = -1;
1170               if (pimg->survey && pimg->label_len == 0) {
1171                  /* We're only holding onto this line in case the first line
1172                   * of the 'N' is a 'D', so skip it for now...
1173                   */
1174                  goto skip_to_N;
1175               }
1176               if (ch == 'D' && pimg->pending == -1) {
1177                  if (pimg->survey) {
1178                     fpos = ftell(pimg->fh) - 1;
1179                     fseek(pimg->fh, pimg->start, SEEK_SET);
1180                     ch = getc(pimg->fh);
1181                     pimg->pending = 0;
1182                  } else {
1183                     /* If a file actually has a 'D' before any 'M', then
1184                      * pretend the 'D' is an 'M' - one of the examples
1185                      * in the docs was like this! */
1186                     ch = 'M';
1187                  }
1188               }
1189               line = getline_alloc(pimg->fh);
1190               if (!line) {
1191                  img_errno = IMG_OUTOFMEMORY;
1192                  return img_BAD;
1193               }
1194               /* Compass store coordinates as North, East, Up = (y,x,z)! */
1195               if (sscanf(line, "%lf%lf%lf", &p->y, &p->x, &p->z) != 3) {
1196                  osfree(line);
1197                  if (ferror(pimg->fh)) {
1198                     img_errno = IMG_READERROR;
1199                  } else {
1200                     img_errno = IMG_BADFORMAT;
1201                  }
1202                  return img_BAD;
1203               }
1204               p->x *= METRES_PER_FOOT;
1205               p->y *= METRES_PER_FOOT;
1206               p->z *= METRES_PER_FOOT;
1207               q = strchr(line, 'S');
1208               if (!q) {
1209                  osfree(line);
1210                  img_errno = IMG_BADFORMAT;
1211                  return img_BAD;
1212               }
1213               ++q;
1214               len = 0;
1215               while (q[len] > ' ') ++len;
1216               q[len] = '\0';
1217               len += 2; /* ' ' and '\0' */
1218               if (!check_label_space(pimg, pimg->label_len + len)) {
1219                  img_errno = IMG_OUTOFMEMORY;
1220                  return img_BAD;
1221               }
1222               pimg->label = pimg->label_buf;
1223               pimg->label[pimg->label_len] = ' ';
1224               memcpy(pimg->label + pimg->label_len + 1, q, len - 1);
1225               osfree(line);
1226               pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */
1227               if (fpos != -1) {
1228                  fseek(pimg->fh, fpos, SEEK_SET);
1229               } else {
1230                  pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4;
1231               }
1232               return img_LABEL;
1233            }
1234            default:
1235               img_errno = IMG_BADFORMAT;
1236               return img_BAD;
1237         }
1238      }
1239   } else {
1240      /* version -3 or -4: CMAP .xyz file */
1241      char *line = NULL;
1242      char *q;
1243      size_t len;
1244
1245      if (pimg->pending) {
1246         /* pending MOVE or LINE or LABEL or STOP */
1247         int r = pimg->pending - 4;
1248         /* Set label to empty - don't use "" as we adjust label relative
1249          * to label_buf when label_buf is reallocated. */
1250         pimg->label = pimg->label_buf + strlen(pimg->label_buf);
1251         pimg->flags = 0;
1252         if (r == img_LABEL) {
1253            /* nasty magic */
1254            read_xyz_shot_coords(p, pimg->label_buf + 16);
1255            subtract_xyz_shot_deltas(p, pimg->label_buf + 16);
1256            pimg->pending = img_STOP + 4;
1257            return img_MOVE;
1258         }
1259       
1260         pimg->pending = 0;
1261
1262         if (r == img_STOP) {
1263            /* nasty magic */
1264            read_xyz_shot_coords(p, pimg->label_buf + 16);
1265            return img_LINE;
1266         }
1267         
1268         return r;
1269      }
1270
1271      pimg->label = pimg->label_buf;
1272      do {
1273         osfree(line);
1274         if (feof(pimg->fh)) return img_STOP;
1275         line = getline_alloc(pimg->fh);
1276         if (!line) {
1277            img_errno = IMG_OUTOFMEMORY;
1278            return img_BAD;
1279         }
1280      } while (line[0] == ' ' || line[0] == '\0');
1281      if (line[0] == '\x1a') return img_STOP;
1282
1283      len = strlen(line);
1284      if (pimg->version == -3) {
1285         /* station variant */
1286         if (len < 37) {
1287            osfree(line);
1288            img_errno = IMG_BADFORMAT;
1289            return img_BAD;
1290         }
1291         memcpy(pimg->label, line, 6);
1292         q = memchr(pimg->label, ' ', 6);
1293         if (!q) q = pimg->label + 6;
1294         *q = '\0';
1295
1296         read_xyz_station_coords(p, line);
1297 
1298         /* FIXME: look at prev for lines (line + 32, 5) */
1299         /* FIXME: duplicate stations... */
1300         return img_LABEL;
1301      } else {
1302         /* Shot variant */
1303         char old[8], new[8];
1304         if (len < 61) {
1305            osfree(line);
1306            img_errno = IMG_BADFORMAT;
1307            return img_BAD;
1308         }
1309 
1310         memcpy(old, line, 7);
1311         q = memchr(old, ' ', 7);
1312         if (!q) q = old + 7;
1313         *q = '\0';
1314
1315         memcpy(new, line + 7, 7);
1316         q = memchr(new, ' ', 7);
1317         if (!q) q = new + 7;
1318         *q = '\0';
1319
1320         pimg->flags = img_SFLAG_UNDERGROUND;
1321
1322         if (strcmp(old, new) == 0) {
1323            pimg->pending = img_MOVE + 4;
1324            read_xyz_shot_coords(p, line);
1325            strcpy(pimg->label, new);
1326            osfree(line);
1327            return img_LABEL;
1328         }
1329 
1330         if (strcmp(old, pimg->label) == 0) {
1331            pimg->pending = img_LINE + 4;
1332            read_xyz_shot_coords(p, line);
1333            strcpy(pimg->label, new);
1334            osfree(line);
1335            return img_LABEL;
1336         }
1337         
1338         pimg->pending = img_LABEL + 4;
1339         read_xyz_shot_coords(p, line);
1340         strcpy(pimg->label, new);
1341         memcpy(pimg->label + 16, line, 70);
1342
1343         osfree(line);
1344         return img_LABEL;
1345      }
1346   }
1347}
1348
1349static int
1350write_v3label(img *pimg, int opt, const char *s)
1351{
1352   size_t len, n, dot;
1353
1354   /* find length of common prefix */
1355   dot = 0;
1356   for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) {
1357      if (s[len] == '.') dot = len + 1;
1358   }
1359
1360   SVX_ASSERT(len <= pimg->label_len);
1361   n = pimg->label_len - len;
1362   if (len == 0) {
1363      if (pimg->label_len) putc(0, pimg->fh);
1364   } else if (n <= 16) {
1365      if (n) putc(n + 15, pimg->fh);
1366   } else if (dot == 0) {
1367      if (pimg->label_len) putc(0, pimg->fh);
1368      len = 0;
1369   } else {
1370      const char *p = pimg->label_buf + dot;
1371      n = 1;
1372      for (len = pimg->label_len - dot - 17; len; len--) {
1373         if (*p++ == '.') n++;
1374      }
1375      if (n <= 14) {
1376         putc(n, pimg->fh);
1377         len = dot;
1378      } else {
1379         if (pimg->label_len) putc(0, pimg->fh);
1380         len = 0;
1381      }
1382   }
1383
1384   n = strlen(s + len);
1385   putc(opt, pimg->fh);
1386   if (n < 0xfe) {
1387      putc(n, pimg->fh);
1388   } else if (n < 0xffff + 0xfe) {
1389      putc(0xfe, pimg->fh);
1390      put16(n - 0xfe, pimg->fh);
1391   } else {
1392      putc(0xff, pimg->fh);
1393      put32(n, pimg->fh);
1394   }
1395   fwrite(s + len, n, 1, pimg->fh);
1396
1397   n += len;
1398   pimg->label_len = n;
1399   if (!check_label_space(pimg, n + 1))
1400      return 0; /* FIXME: distinguish out of memory... */
1401   memcpy(pimg->label_buf + len, s + len, n - len + 1);
1402
1403   return !ferror(pimg->fh);
1404}
1405
1406void
1407img_write_item(img *pimg, int code, int flags, const char *s,
1408               double x, double y, double z)
1409{
1410   if (!pimg) return;
1411   if (pimg->version == 3) {
1412      int opt = 0;
1413      switch (code) {
1414       case img_LABEL:
1415         write_v3label(pimg, 0x40 | flags, s);
1416         opt = 0;
1417         break;
1418       case img_MOVE:
1419         opt = 15;
1420         break;
1421       case img_LINE:
1422         write_v3label(pimg, 0x80 | flags, s ? s : "");
1423         opt = 0;
1424         break;
1425       default: /* ignore for now */
1426         return;
1427      }
1428      if (opt) putc(opt, pimg->fh);
1429      /* Output in cm */
1430      put32((INT32_T)my_round(x * 100.0), pimg->fh);
1431      put32((INT32_T)my_round(y * 100.0), pimg->fh);
1432      put32((INT32_T)my_round(z * 100.0), pimg->fh);
1433   } else {
1434      size_t len;
1435      INT32_T opt = 0;
1436      SVX_ASSERT(pimg->version > 0);
1437      switch (code) {
1438       case img_LABEL:
1439         if (pimg->version == 1) {
1440            /* put a move before each label */
1441            img_write_item(pimg, img_MOVE, 0, NULL, x, y, z);
1442            put32(2, pimg->fh);
1443            fputsnl(s, pimg->fh);
1444            return;
1445         }
1446         len = strlen(s);
1447         if (len > 255 || strchr(s, '\n')) {
1448            /* long label - not in early incarnations of v2 format, but few
1449             * 3d files will need these, so better not to force incompatibility
1450             * with a new version I think... */
1451            putc(7, pimg->fh);
1452            putc(flags, pimg->fh);
1453            put32(len, pimg->fh);
1454            fputs(s, pimg->fh);
1455         } else {
1456            putc(0x40 | (flags & 0x3f), pimg->fh);
1457            fputsnl(s, pimg->fh);
1458         }
1459         opt = 0;
1460         break;
1461       case img_MOVE:
1462         opt = 4;
1463         break;
1464       case img_LINE:
1465         if (pimg->version > 1) {
1466            opt = 0x80 | (flags & 0x3f);
1467            break;
1468         }
1469         opt = 5;
1470         break;
1471       default: /* ignore for now */
1472         return;
1473      }
1474      if (pimg->version == 1) {
1475         put32(opt, pimg->fh);
1476      } else {
1477         if (opt) putc(opt, pimg->fh);
1478      }
1479      /* Output in cm */
1480      put32((INT32_T)my_round(x * 100.0), pimg->fh);
1481      put32((INT32_T)my_round(y * 100.0), pimg->fh);
1482      put32((INT32_T)my_round(z * 100.0), pimg->fh);
1483   }
1484}
1485
1486int
1487img_close(img *pimg)
1488{
1489   int result = 1;
1490   if (pimg) {
1491      if (pimg->fh) {
1492         if (pimg->fRead) {
1493            osfree(pimg->survey);
1494            osfree(pimg->title);
1495            osfree(pimg->datestamp);
1496         } else {
1497            /* write end of data marker */
1498            switch (pimg->version) {
1499             case 1:
1500               put32((INT32_T)-1, pimg->fh);
1501               break;
1502             case 2:
1503               putc(0, pimg->fh);
1504               break;
1505             case 3:
1506               if (pimg->label_len) putc(0, pimg->fh);
1507               putc(0, pimg->fh);
1508               break;
1509            }
1510         }
1511         if (ferror(pimg->fh)) result = 0;
1512         if (fclose(pimg->fh)) result = 0;
1513         if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR;
1514      }
1515      osfree(pimg->label_buf);
1516      osfree(pimg);
1517   }
1518   return result;
1519}
Note: See TracBrowser for help on using the repository browser.