source: git/src/img.c @ 7011db8

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

Trim of any prefix given by --survey.

Removed references to img_CROSS and commented it out in the header.

print*: reduced memory usage by storing a "label" with coordinates and
using it for crosses too, rather than storing separate cross and label
elements.

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

  • Property mode set to 100644
File size: 19.7 KB
RevLine 
[d1b1380]1/* > img.c
2 * Routines for reading and writing Survex ".3d" image files
[bd1913f]3 * Copyright (C) 1993-2001 Olly Betts
[846746e]4 *
[89231c4]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.
[846746e]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
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]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
[d1b1380]18 */
19
[a420b49]20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
[d1b1380]24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <time.h>
28#include <ctype.h>
29
[72af530]30#include "img.h"
31#ifdef IMG_HOSTED
[d69255f]32# include "debug.h"
[d1b1380]33# include "filelist.h"
[d69255f]34# include "filename.h"
35# include "message.h"
36# include "useful.h"
[a420b49]37# define TIMENA msg(/*Date and time not available.*/108)
38# define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107)
[d1b1380]39#else
[72af530]40# define INT32_T long
[d1b1380]41# define TIMENA "Time not available."
42# define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z"
43# define EXT_SVX_3D "3d"
44# define xosmalloc(L) malloc((L))
[72af530]45# define xosrealloc(L,S) realloc((L),(S))
[d1b1380]46# define osfree(P) free((P))
47# define ossizeof(T) sizeof(T)
[72af530]48/* in IMG_HOSTED mode, this tests if a filename refers to a directory */
[d1b1380]49# define fDirectory(X) 0
50/* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */
51/* path isn't used in img.c, but EXT is */
52# define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) fopen(FNM,MODE)
53# define fFalse 0
54# define fTrue 1
55# define bool int
56/* dummy do {...} while(0) hack to permit stuff like
57 * if (s) fputsnl(s,fh); else exit(1);
58 * to work as intended
59 */
[23f7ea7]60# define fputsnl(S, FH) do {fputs((S), (FH)); putc('\n', (FH));} while(0)
[d69255f]61# define ASSERT(X)
[d1b1380]62
[a420b49]63static long
64get32(FILE *fh)
65{
66   long w;
67   w = getc(fh);
68   w |= (long)getc(fh) << 8l;
69   w |= (long)getc(fh) << 16l;
70   w |= (long)getc(fh) << 24l;
71   return w;
[d1b1380]72}
73
[a420b49]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);
[d1b1380]81}
[5c3c61a]82
83#endif
84
[d69255f]85unsigned int img_output_version = 3;
[5c3c61a]86
[23f7ea7]87#define TMPBUFLEN 256
88static char tmpbuf[TMPBUFLEN];
[5c3c61a]89
[72af530]90#ifdef IMG_HOSTED
[5c3c61a]91static enum {
92   IMG_NONE = 0,
[759fb47]93   IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24,
[5c3c61a]94   IMG_OUTOFMEMORY  = /*Out of memory %.0s*/38,
[759fb47]95   IMG_DIRECTORY    = /*Filename `%s' refers to directory*/44,
96   IMG_CANTOPENOUT  = /*Failed to open output file `%s'*/47,
97   IMG_BADFORMAT    = /*Bad 3d image file `%s'*/106
[5c3c61a]98} img_errno = IMG_NONE;
99#else
100static img_errcode img_errno = IMG_NONE;
[c0563da]101#endif
[d1b1380]102
[5c3c61a]103/* Read a line from a stream to a buffer. Any eol chars are removed
104 * from the file and the length of the string excluding '\0' is returned */
[a420b49]105static int
106getline(char *buf, size_t len, FILE *fh)
107{
[eb18f4d]108   size_t i = 0;
[a420b49]109   int ch;
[d1b1380]110
[a420b49]111   ch = getc(fh);
[647407d]112   while (ch != '\n' && ch != '\r' && ch != EOF && i < len - 1) {
[a420b49]113      buf[i++] = ch;
114      ch = getc(fh);
115   }
[647407d]116   if (ch == '\n' || ch == '\r') {
117      /* remove any further eol chars (for DOS text files) */
[a420b49]118      do {
119         ch = getc(fh);
120      } while (ch == '\n' || ch == '\r');
121      ungetc(ch, fh); /* we don't want it, so put it back */
122   }
123   buf[i] = '\0';
[5c3c61a]124   return i;
[d1b1380]125}
[c0563da]126
[72af530]127#ifndef IMG_HOSTED
[a420b49]128img_errcode
129img_error(void)
130{
131   return img_errno;
[d1b1380]132}
133#else
[a420b49]134int
135img_error(void)
136{
137   return (int)img_errno;
[d1b1380]138}
139#endif
140
[a420b49]141img *
[76bbb7c9]142img_open_survey(const char *fnm, char *title_buf, char *date_buf,
143                const char *survey)
[a420b49]144{
145   img *pimg;
146
147   if (fDirectory(fnm)) {
148      img_errno = IMG_DIRECTORY;
149      return NULL;
150   }
[d1b1380]151
[a420b49]152   pimg = (img *)xosmalloc(ossizeof(img));
153   if (pimg == NULL) {
154      img_errno = IMG_OUTOFMEMORY;
155      return NULL;
156   }
[d1b1380]157
[23f7ea7]158   pimg->buf_len = 257;
[0ff23cb]159   pimg->label_buf = xosmalloc(pimg->buf_len);
160   if (!pimg->label_buf) {
[23f7ea7]161      osfree(pimg);
162      img_errno = IMG_OUTOFMEMORY;
163      return NULL;
164   }
165
[a420b49]166   pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", NULL);
167   if (pimg->fh == NULL) {
168      osfree(pimg);
169      img_errno = IMG_FILENOTFOUND;
170      return NULL;
171   }
[d1b1380]172
[23f7ea7]173   getline(tmpbuf, TMPBUFLEN, pimg->fh); /* id string */
174   if (strcmp(tmpbuf, "Survex 3D Image File") != 0) {
[a420b49]175      fclose(pimg->fh);
176      osfree(pimg);
177      img_errno = IMG_BADFORMAT;
178      return NULL;
179   }
[d1b1380]180
[23f7ea7]181   getline(tmpbuf, TMPBUFLEN, pimg->fh); /* file format version */
182   pimg->version = (tolower(*tmpbuf) == 'b'); /* binary file iff B/b prefix */
[a420b49]183   /* knock off the 'B' or 'b' if it's there */
[23f7ea7]184   if (strcmp(tmpbuf + pimg->version, "v0.01") == 0) {
[5c3c61a]185      pimg->flags = 0;
[d69255f]186   } else if (pimg->version == 0 && tmpbuf[0] == 'v' &&
187              (tmpbuf[1] >= '2' || tmpbuf[1] <= '3') &&
188              tmpbuf[2] == '\0') {
189      pimg->version = tmpbuf[1] - '0';
[5c3c61a]190   } else {
[a420b49]191      fclose(pimg->fh);
192      osfree(pimg);
[647407d]193      img_errno = IMG_BADFORMAT;
[a420b49]194      return NULL;
195   }
[fe8e80e]196   getline((title_buf ? title_buf : tmpbuf), TMPBUFLEN, pimg->fh);
[95c3272]197   getline((date_buf ? date_buf : tmpbuf), TMPBUFLEN, pimg->fh);
[a420b49]198   pimg->fRead = fTrue; /* reading from this file */
199   img_errno = IMG_NONE;
[f2588ca]200   pimg->start = ftell(pimg->fh);
[d69255f]201
202   /* for version 3 we use label to store the prefix for reuse */
203   pimg->label_len = 0;
204
[76bbb7c9]205   pimg->survey = NULL;
206   pimg->survey_len = 0;
207
208   if (survey) {
209      size_t len = strlen(survey);
210      if (len) {
211         if (survey[len - 1] == '.') len--;
212         if (len) {
213            pimg->survey = osmalloc(len + 2);
214            memcpy(pimg->survey, survey, len);
215            pimg->survey[len] = '.';
216            pimg->survey[len + 1] = '\0';
217         }
218      }
219      pimg->survey_len = len;
220   }
221
222   /* [version 0] not in the middle of a 'LINE' command
223    * [version 3] not in the middle of turning a LINE into a MOVE */
224   pimg->pending = 0;
225
[a420b49]226   return pimg;
[d1b1380]227}
228
[f2588ca]229void
230img_rewind(img *pimg)
231{
232   fseek(pimg->fh, pimg->start, SEEK_SET);
[76bbb7c9]233
234   /* [version 0] not in the middle of a 'LINE' command
235    * [version 3] not in the middle of turning a LINE into a MOVE */
236   pimg->pending = 0;
[d69255f]237
238   img_errno = IMG_NONE;
239   
240   /* for version 3 we use label to store the prefix for reuse */
241   pimg->label_len = 0;
[f2588ca]242}
243
[a420b49]244img *
[fe8e80e]245img_open_write(const char *fnm, char *title_buf, bool fBinary)
[a420b49]246{
247   time_t tm;
248   img *pimg;
[d1b1380]249
[d69255f]250   fBinary = fBinary;
251
[a420b49]252   if (fDirectory(fnm)) {
253      img_errno = IMG_DIRECTORY;
254      return NULL;
255   }
[d1b1380]256
[a420b49]257   pimg = (img *)xosmalloc(ossizeof(img));
258   if (pimg == NULL) {
259      img_errno = IMG_OUTOFMEMORY;
260      return NULL;
261   }
[d1b1380]262
[fe8e80e]263   pimg->buf_len = 257;
[0ff23cb]264   pimg->label_buf = xosmalloc(pimg->buf_len);
265   if (!pimg->label_buf) {
[fe8e80e]266      osfree(pimg);
267      img_errno = IMG_OUTOFMEMORY;
268      return NULL;
269   }
270
[a420b49]271   pimg->fh = fopen(fnm, "wb");
272   if (!pimg->fh) {
273      osfree(pimg);
274      img_errno = IMG_CANTOPENOUT;
275      return NULL;
276   }
[d1b1380]277
[a420b49]278   /* Output image file header */
279   fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */
[5c3c61a]280   if (img_output_version < 2) {
[d69255f]281      pimg->version = 1;
282      fputs("Bv0.01\n", pimg->fh); /* binary file format version number */
[5c3c61a]283   } else {
[d69255f]284      pimg->version = (img_output_version > 2) ? 3 : 2;
[5c3c61a]285      fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */
286   }
[fe8e80e]287   fputsnl(title_buf, pimg->fh);
[a420b49]288   tm = time(NULL);
289   if (tm == (time_t)-1) {
290      fputsnl(TIMENA, pimg->fh);
291   } else {
292      /* output current date and time in format specified */
[23f7ea7]293      strftime(tmpbuf, TMPBUFLEN, TIMEFMT, localtime(&tm));
294      fputsnl(tmpbuf, pimg->fh);
[a420b49]295   }
296   pimg->fRead = fFalse; /* writing to this file */
297   img_errno = IMG_NONE;
[d69255f]298
299   /* for version 3 we use label to store the prefix for reuse */
[0ff23cb]300   pimg->label_buf[0] = '\0';
[d69255f]301   pimg->label_len = 0;
302
[a420b49]303   return pimg;
[d1b1380]304}
305
[a420b49]306int
[23f7ea7]307img_read_item(img *pimg, img_point *p)
[a420b49]308{
309   int result;
[95c3272]310   pimg->flags = 0;
[0ff23cb]311   pimg->label = pimg->label_buf;
[d69255f]312
313   if (pimg->version == 3) {
314      int opt;
[76bbb7c9]315      if (pimg->pending >= 0x80) {
316         *p = pimg->mv;
317         pimg->flags = (int)(pimg->pending) & 0x3f;
318         pimg->pending = 0;
[0ff23cb]319         return img_LINE; // FIXME
[76bbb7c9]320      }
[d69255f]321      again3: /* label to goto if we get a prefix */
322      opt = getc(pimg->fh);
323      switch (opt >> 6) {
324       case 0:
325         if (opt == 0) {
326            if (!pimg->label_len) return img_STOP; /* end of data marker */
327            pimg->label_len = 0;
328            goto again3;
329         }
330         if (opt < 15) {
331            /* 1-14 mean trim that many levels from current prefix */
332            int c;
333            /* zero prefix using "0" */
334            if (pimg->label_len <= 16) {
335               img_errno = IMG_BADFORMAT;
336               return img_BAD;
337            }
338            c = pimg->label_len - 16;
339            opt &= 0x07;
[0ff23cb]340            while (pimg->label_buf[c] != '.' || --opt > 0) {
[d69255f]341               if (--c < 0) {
342                  /* zero prefix using "0" */
343                  img_errno = IMG_BADFORMAT;
344                  return img_BAD;
345               }
346            }
347            c++;
[0ff23cb]348            pimg->label_buf[c] = '\0';
[d69255f]349            pimg->label_len = c;
350            goto again3;
351         }
352         if (opt == 15) {
353            result = img_MOVE;
354            break;
355         }
356         /* 16-31 mean remove (n - 15) characters from the prefix */
357         pimg->label_len -= (opt - 15);
358         /* zero prefix using 0 */
359         if (pimg->label_len <= 0) {
360            img_errno = IMG_BADFORMAT;
361            return img_BAD;
362         }
363         goto again3;
364       case 1: case 2: {
365         char *q;
366         size_t len;
367
368         len = getc(pimg->fh);
369         if (len == 0xfe) {
370            len += get16(pimg->fh);
371         } else if (len == 0xff) {
372            len = get32(pimg->fh);
373            if (len < 0xfe + 0xffff) {
374               img_errno = IMG_BADFORMAT;
375               return img_BAD;
376            }
377         }
378
[0ff23cb]379         q = pimg->label_buf + pimg->label_len;
[d69255f]380         pimg->label_len += len;
381         if (pimg->label_len >= pimg->buf_len) {
[0ff23cb]382            pimg->label_buf = xosrealloc(pimg->label_buf, pimg->label_len + 1);
383            if (!pimg->label_buf) {
[d69255f]384               img_errno = IMG_OUTOFMEMORY;
385               return img_BAD;
386            }
387            pimg->buf_len = pimg->label_len + 1;
388         }
389         if (len && fread(q, len, 1, pimg->fh) != 1) {
390            img_errno = IMG_BADFORMAT;
391            return img_BAD;
392         }
393         q[len] = '\0';
[76bbb7c9]394
395         result = opt & 0x40 ? img_LABEL : img_LINE;
396
397         if (pimg->survey_len) {
398            size_t l = pimg->survey_len;
[0ff23cb]399            const char *s = pimg->label_buf;
[76bbb7c9]400            if (result == img_LINE) {
401               if (strncmp(pimg->survey, s, l) != 0 ||
402                   !(s[l] == '.' || s[l] == '\0')) {
403                  pimg->mv.x = get32(pimg->fh) / 100.0;
404                  pimg->mv.y = get32(pimg->fh) / 100.0;
405                  pimg->mv.z = get32(pimg->fh) / 100.0;
406                  pimg->pending = 15;
407                  goto again3;
408               }
409            } else {
410               if (strncmp(pimg->survey, s, l + 1) != 0) {
411                  fseek(pimg->fh, 12, SEEK_CUR);
412                  goto again3;
413               }
414            }
[0ff23cb]415            pimg->label += l;
416            /* skip the dot if there */
417            if (*pimg->label) pimg->label++;
[76bbb7c9]418         }
419
420         if (result == img_LINE && pimg->pending) {
421            *p = pimg->mv;
422            pimg->mv.x = get32(pimg->fh) / 100.0;
423            pimg->mv.y = get32(pimg->fh) / 100.0;
424            pimg->mv.z = get32(pimg->fh) / 100.0;
425            pimg->pending = opt;
426            return img_MOVE;
427         }
428         pimg->flags = (int)opt & 0x3f;
[d69255f]429         break;
430       }
431       default:
432         img_errno = IMG_BADFORMAT;
433         return img_BAD;
434      }
435      p->x = get32(pimg->fh) / 100.0;
436      p->y = get32(pimg->fh) / 100.0;
437      p->z = get32(pimg->fh) / 100.0;
[76bbb7c9]438      pimg->pending = 0;
[d69255f]439      return result;
440   } else if (pimg->version > 0) {
441      static long opt_lookahead = 0;
442      static double x = 0.0, y = 0.0, z = 0.0;
[a420b49]443      long opt;
444      again: /* label to goto if we get a cross */
[5c3c61a]445      if (pimg->version == 1) {
[c4d4649]446         if (opt_lookahead) {
447            opt = opt_lookahead;
448            opt_lookahead = 0;
449         } else {
450            opt = get32(pimg->fh);
451         }
[5c3c61a]452      } else {
453         opt = getc(pimg->fh);
454      }
[a420b49]455      switch (opt) {
[437caf3]456       case -1: case 0:
[a420b49]457         return img_STOP; /* end of data marker */
458       case 1:
459         (void)get32(pimg->fh);
460         (void)get32(pimg->fh);
461         (void)get32(pimg->fh);
462         goto again;
[23f7ea7]463       case 2: case 3: {
[fe8e80e]464         char *q;
[3a1f8da]465         int ch;
[437caf3]466         result = img_LABEL;
[3a1f8da]467         ch = getc(pimg->fh);
[d69255f]468         if (ch == EOF) {
469            img_errno = IMG_BADFORMAT;
470            return img_BAD;
471         }
[3a1f8da]472         if (ch != '\\') ungetc(ch, pimg->fh);
[0ff23cb]473         fgets(pimg->label_buf, 257, pimg->fh);
474         q = pimg->label_buf + strlen(pimg->label_buf) - 1;
[d69255f]475         if (*q != '\n') {
476            img_errno = IMG_BADFORMAT;
477            return img_BAD;
478         }
[9968ebf]479         /* Ignore empty labels in some .3d files (caused by a bug) */
[0ff23cb]480         if (q == pimg->label_buf) goto again;
[fe8e80e]481         *q = '\0';
[509e099]482         pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
[437caf3]483         if (opt == 2) goto done;
484         break;
[23f7ea7]485       }
[95c3272]486       case 6: case 7: {
[23f7ea7]487         size_t len;
488         result = img_LABEL;
[509e099]489         if (opt == 7)
490            pimg->flags = getc(pimg->fh);
491         else
492            pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */
[23f7ea7]493         len = get32(pimg->fh);
[9968ebf]494         /* Ignore empty labels in some .3d files (caused by a bug) */
495         if (len == 0) goto again;
[23f7ea7]496         if (len >= pimg->buf_len) {
[0ff23cb]497            pimg->label_buf = xosrealloc(pimg->label_buf, len + 1);
498            if (!pimg->label_buf) {
[23f7ea7]499               img_errno = IMG_OUTOFMEMORY;
500               return img_BAD;
501            }
502            pimg->buf_len = len + 1;
503         }
[0ff23cb]504         if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) {
[d69255f]505            img_errno = IMG_BADFORMAT;
506            return img_BAD;
507         }
[23f7ea7]508         break;
509       }
[a420b49]510       case 4:
511         result = img_MOVE;
512         break;
513       case 5:
514         result = img_LINE;
515         break;
516       default:
[95c3272]517         switch ((int)opt & 0xc0) {
518          case 0x80:
519            pimg->flags = (int)opt & 0x3f;
520            result = img_LINE;
521            break;
522          case 0x40: {
523            char *q;
524            pimg->flags = (int)opt & 0x3f;
525            result = img_LABEL;
[0ff23cb]526            if (!fgets(pimg->label_buf, 257, pimg->fh)) {
[d69255f]527               img_errno = IMG_BADFORMAT;
528               return img_BAD;
529            }
[0ff23cb]530            q = pimg->label_buf + strlen(pimg->label_buf) - 1;
[9968ebf]531            /* Ignore empty-labels in some .3d files (caused by a bug) */
[0ff23cb]532            if (q == pimg->label_buf) goto again;
[d69255f]533            if (*q != '\n') {
534               img_errno = IMG_BADFORMAT;
535               return img_BAD;
536            }
[95c3272]537            *q = '\0';
538            break;
539          }
540          case 0xc0:
541            /* use this for an extra leg or station flag if we need it */
542          default:
[d69255f]543            img_errno = IMG_BADFORMAT;
[95c3272]544            return img_BAD;
545         }
[437caf3]546         break;
[a420b49]547      }
[a9f5117]548      x = get32(pimg->fh) / 100.0;
549      y = get32(pimg->fh) / 100.0;
550      z = get32(pimg->fh) / 100.0;
[76bbb7c9]551
[0ff23cb]552      if (result == img_LABEL) {
553         if (pimg->survey_len &&
554          (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0))
555            goto again;
556         pimg->label += pimg->survey_len + 1;
557      }
[76bbb7c9]558
[a420b49]559      done:
[0ed0e16]560      p->x = x;
561      p->y = y;
562      p->z = z;
[c4d4649]563
564      if (result == img_MOVE && pimg->version == 1) {
[d69255f]565         /* peek at next code and see if it's an old-style label */
[23f7ea7]566         opt_lookahead = get32(pimg->fh);
[d69255f]567         if (opt_lookahead == 2) return img_read_item(pimg, p);
[c4d4649]568      }
[23f7ea7]569
[a420b49]570      return result;
571   } else {
572      ascii_again:
573      if (feof(pimg->fh)) return img_STOP;
[76bbb7c9]574      if (pimg->pending) {
575         pimg->pending = 0;
[a420b49]576         result = img_LINE;
577      } else {
578         /* Stop if nothing found */
[23f7ea7]579         if (fscanf(pimg->fh, "%s", tmpbuf) < 1) return img_STOP;
580         if (strcmp(tmpbuf, "move") == 0)
[a420b49]581            result = img_MOVE;
[23f7ea7]582         else if (strcmp(tmpbuf, "draw") == 0)
[a420b49]583            result = img_LINE;
[23f7ea7]584         else if (strcmp(tmpbuf, "line") == 0) {
[bd1913f]585            /* set flag to indicate to process second triplet as LINE */
[76bbb7c9]586            pimg->pending = 1;
[a420b49]587            result = img_MOVE;
[23f7ea7]588         } else if (strcmp(tmpbuf, "cross") == 0) {
[d69255f]589            if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
590               img_errno = IMG_BADFORMAT;             
[0ed0e16]591               return img_BAD;
[d69255f]592            }
[a420b49]593            goto ascii_again;
[23f7ea7]594         } else if (strcmp(tmpbuf, "name") == 0) {
[3a1f8da]595            int ch;
596            ch = getc(pimg->fh);
[d69255f]597            if (ch == EOF) {
598               img_errno = IMG_BADFORMAT;
599               return img_BAD;
600            }
[3a1f8da]601            if (ch != '\\') ungetc(ch, pimg->fh);
[0ff23cb]602            if (fscanf(pimg->fh, "%256s", pimg->label_buf) < 1 ||
603                strlen(pimg->label_buf) == 256) {
[d69255f]604               img_errno = IMG_BADFORMAT;
[4ee5fbf]605               return img_BAD;
606            }
[a420b49]607            result = img_LABEL;
[d69255f]608         } else {
609            img_errno = IMG_BADFORMAT;
[a420b49]610            return img_BAD; /* unknown keyword */
[d69255f]611         }
[a420b49]612      }
[d1b1380]613
[d69255f]614      if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) {
615         img_errno = IMG_BADFORMAT;
[fe8e80e]616         return img_BAD;
[d69255f]617      }
618
[0ff23cb]619      if (result == img_LABEL) {
620         if (pimg->survey_len &&
621          (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0))
622            goto ascii_again;
623         pimg->label += pimg->survey_len + 1;
624      }
[76bbb7c9]625
[a420b49]626      return result;
627   }
[d1b1380]628}
629
[d69255f]630static void
631write_v3label(img *pimg, int opt, const char *s)
632{
633   size_t len, n, dot;
634
635   /* find length of common prefix */
636   dot = 0;
[0ff23cb]637   for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) {
[d69255f]638      if (s[len] == '.') dot = len + 1;
639   }
640
641   ASSERT(len <= pimg->label_len);
642   n = pimg->label_len - len;
643   if (len == 0) {
644      if (pimg->label_len) putc(0, pimg->fh);
645   } else if (n <= 16) {
646      if (n) putc(n + 15, pimg->fh);
647   } else if (dot == 0) {
648      if (pimg->label_len) putc(0, pimg->fh);
649      len = 0;     
650   } else {
[0ff23cb]651      const char *p = pimg->label_buf + dot;
[d69255f]652      n = 1;
653      for (len = pimg->label_len - dot - 17; len; len--) {
654         if (*p++ == '.') n++;
655      }
656      if (n <= 14) {
657         putc(n, pimg->fh);
658         len = dot;
659      } else {
660         if (pimg->label_len) putc(0, pimg->fh);
661         len = 0;
662      }
663   }
664
665   n = strlen(s + len);
666   putc(opt, pimg->fh);
667   if (n < 0xfe) {
668      putc(n, pimg->fh);
669   } else if (n < 0xffff + 0xfe) {
670      putc(0xfe, pimg->fh);
671      put16(n - 0xfe, pimg->fh);
672   } else {
673      putc(0xff, pimg->fh);
674      put32(n, pimg->fh);
675   }
676   fwrite(s + len, n, 1, pimg->fh);
677
678   n += len;
679   pimg->label_len = n;
680   if (n >= pimg->buf_len) {
[0ff23cb]681      char *p = xosrealloc(pimg->label_buf, n + 1);
[d69255f]682      if (p) {
[0ff23cb]683         pimg->label_buf = p;
[d69255f]684         pimg->buf_len = n + 1;
685      } else {
686         /* can't store this station, so just reset to no prefix */
687         if (pimg->label_len) {
688            /* can't here... putc(0, pimg->fh); */
689            abort();
[0ff23cb]690            pimg->label_buf[0] = '\0';
[d69255f]691            pimg->label_len = 0;
692         }
693      }
694   }
[0ff23cb]695   memcpy(pimg->label_buf + len, s + len, n - len + 1);
[d69255f]696}
697
[a420b49]698void
[95c3272]699img_write_item(img *pimg, int code, int flags, const char *s,
[a9f5117]700               double x, double y, double z)
[a420b49]701{
[647407d]702   if (!pimg) return;
[d69255f]703   if (pimg->version == 3) {
704      int opt = 0;
705      switch (code) {
706       case img_LABEL:
707         write_v3label(pimg, 0x40 | flags, s);
708         opt = 0;
709         break;
710       case img_MOVE:
711         opt = 15;
712         break;
713       case img_LINE:
714         write_v3label(pimg, 0x80 | flags, s ? s : "");
715         opt = 0;
716         break;
717       default: /* ignore for now */
718         return;
719      }
720      if (opt) putc(opt, pimg->fh);
721      /* Output in cm */
722      put32((INT32_T)(x * 100.0), pimg->fh);
723      put32((INT32_T)(y * 100.0), pimg->fh);
724      put32((INT32_T)(z * 100.0), pimg->fh);
725   } else {
[23f7ea7]726      size_t len;
[badeec8]727      INT32_T opt = 0;
[d69255f]728      ASSERT(pimg->version > 0);
[a420b49]729      switch (code) {
[437caf3]730       case img_LABEL:
731         if (pimg->version == 1) {
732            /* put a move before each label */
[95c3272]733            img_write_item(pimg, img_MOVE, 0, NULL, x, y, z);
[437caf3]734            put32(2, pimg->fh);
[0ed0e16]735            fputsnl(s, pimg->fh);
[437caf3]736            return;
737         }
[23f7ea7]738         len = strlen(s);
[af56069]739         if (len > 255 || strchr(s, '\n')) {
[23f7ea7]740            /* long label - not in early incarnations of v2 format, but few
741             * 3d files will need these, so better not to force incompatibility
742             * with a new version I think... */
[95c3272]743            putc(7, pimg->fh);
744            putc(flags, pimg->fh);
[23f7ea7]745            put32(len, pimg->fh);
746            fputs(s, pimg->fh);
747         } else {
[95c3272]748            putc(0x40 | (flags & 0x3f), pimg->fh);
[23f7ea7]749            fputsnl(s, pimg->fh);
750         }
[437caf3]751         opt = 0;
[23f7ea7]752         break;
[a420b49]753       case img_MOVE:
754         opt = 4;
755         break;
756       case img_LINE:
[5c3c61a]757         if (pimg->version > 1) {
[d69255f]758            opt = 0x80 | (flags & 0x3f);
[437caf3]759            break;
[5c3c61a]760         }
[437caf3]761         opt = 5;
[a420b49]762         break;
[437caf3]763       default: /* ignore for now */
764         return;
[a420b49]765      }
[437caf3]766      if (pimg->version == 1) {
767         put32(opt, pimg->fh);
768      } else {
769         if (opt) putc(opt, pimg->fh);
[a420b49]770      }
[d69255f]771      /* Output in cm */
772      put32((INT32_T)(x * 100.0), pimg->fh);
773      put32((INT32_T)(y * 100.0), pimg->fh);
774      put32((INT32_T)(z * 100.0), pimg->fh);
[a420b49]775   }
[d1b1380]776}
777
[a420b49]778void
779img_close(img *pimg)
780{
[dbb4e19]781   if (pimg) {
782      if (pimg->fh) {
[76bbb7c9]783         if (pimg->fRead) {
784            osfree(pimg->survey);
785         } else {
786            /* write end of data marker */
[d69255f]787            switch (pimg->version) {
788             case 1:
[5c3c61a]789               put32((INT32_T)-1, pimg->fh);
[d69255f]790               break;
791             case 2:
792               putc(0, pimg->fh);
793               break;
794             case 3:
795               if (pimg->label_len) putc(0, pimg->fh);
[437caf3]796               putc(0, pimg->fh);
[d69255f]797               break;
[5c3c61a]798            }
799         }
[dbb4e19]800         fclose(pimg->fh);
801      }
[0ff23cb]802      osfree(pimg->label_buf);
[dbb4e19]803      osfree(pimg);
[a420b49]804   }
[d1b1380]805}
Note: See TracBrowser for help on using the repository browser.