source: git/src/img.c @ ca0fe47

v1.0.12
Last change on this file since ca0fe47 was 4c07c51, checked in by Olly Betts <olly@…>, 23 years ago

Renamed ASSERT to SVXASSERT to avoid clash with Allegro which also defines
it. Renamed ASSERT2 to SVXASSERT2 to match.

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

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