[0da6e60] | 1 | /* img.c |
---|
[d1b1380] | 2 | * Routines for reading and writing Survex ".3d" image files |
---|
[452e200] | 3 | * Copyright (C) 1993-2004,2005,2006,2010 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 |
---|
[0e75725] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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> |
---|
[898bc69] | 29 | #include <locale.h> |
---|
[d1b1380] | 30 | |
---|
[72af530] | 31 | #include "img.h" |
---|
[7d27e36] | 32 | |
---|
[c61aa79] | 33 | #define LATEST_IMG_VERSION 6 |
---|
| 34 | |
---|
[72af530] | 35 | #ifdef IMG_HOSTED |
---|
[d69255f] | 36 | # include "debug.h" |
---|
[d1b1380] | 37 | # include "filelist.h" |
---|
[d69255f] | 38 | # include "filename.h" |
---|
| 39 | # include "message.h" |
---|
| 40 | # include "useful.h" |
---|
[a420b49] | 41 | # define TIMENA msg(/*Date and time not available.*/108) |
---|
| 42 | # define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107) |
---|
[8c45eea] | 43 | # define INT32_T int32_t |
---|
[d1b1380] | 44 | #else |
---|
[0e75725] | 45 | # define INT32_T int |
---|
[d1b1380] | 46 | # define TIMENA "Time not available." |
---|
| 47 | # define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z" |
---|
| 48 | # define EXT_SVX_3D "3d" |
---|
[be97baf] | 49 | # define EXT_SVX_POS "pos" |
---|
| 50 | # define FNM_SEP_EXT '.' |
---|
| 51 | # define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
[d1b1380] | 52 | # define xosmalloc(L) malloc((L)) |
---|
[72af530] | 53 | # define xosrealloc(L,S) realloc((L),(S)) |
---|
[d1b1380] | 54 | # define osfree(P) free((P)) |
---|
| 55 | # define ossizeof(T) sizeof(T) |
---|
[72af530] | 56 | /* in IMG_HOSTED mode, this tests if a filename refers to a directory */ |
---|
[d1b1380] | 57 | # define fDirectory(X) 0 |
---|
| 58 | /* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */ |
---|
| 59 | /* path isn't used in img.c, but EXT is */ |
---|
| 60 | # define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) fopen(FNM,MODE) |
---|
| 61 | # define fFalse 0 |
---|
| 62 | # define fTrue 1 |
---|
| 63 | # define bool int |
---|
| 64 | /* dummy do {...} while(0) hack to permit stuff like |
---|
| 65 | * if (s) fputsnl(s,fh); else exit(1); |
---|
| 66 | * to work as intended |
---|
| 67 | */ |
---|
[23f7ea7] | 68 | # define fputsnl(S, FH) do {fputs((S), (FH)); putc('\n', (FH));} while(0) |
---|
[4c07c51] | 69 | # define SVX_ASSERT(X) |
---|
[d1b1380] | 70 | |
---|
[0e75725] | 71 | static INT32_T |
---|
[a420b49] | 72 | get32(FILE *fh) |
---|
| 73 | { |
---|
[0e75725] | 74 | INT32_T w = getc(fh); |
---|
| 75 | w |= (INT32_T)getc(fh) << 8l; |
---|
| 76 | w |= (INT32_T)getc(fh) << 16l; |
---|
| 77 | w |= (INT32_T)getc(fh) << 24l; |
---|
[a420b49] | 78 | return w; |
---|
[d1b1380] | 79 | } |
---|
| 80 | |
---|
[a420b49] | 81 | static void |
---|
| 82 | put32(long w, FILE *fh) |
---|
| 83 | { |
---|
| 84 | putc((char)(w), fh); |
---|
| 85 | putc((char)(w >> 8l), fh); |
---|
| 86 | putc((char)(w >> 16l), fh); |
---|
| 87 | putc((char)(w >> 24l), fh); |
---|
[d1b1380] | 88 | } |
---|
[5c3c61a] | 89 | |
---|
[be97baf] | 90 | static short |
---|
| 91 | get16(FILE *fh) |
---|
| 92 | { |
---|
| 93 | short w = getc(fh); |
---|
| 94 | w |= (long)getc(fh) << 8l; |
---|
| 95 | return w; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static void |
---|
| 99 | put16(short w, FILE *fh) |
---|
| 100 | { |
---|
| 101 | putc((char)(w), fh); |
---|
| 102 | putc((char)(w >> 8l), fh); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | static char * |
---|
| 106 | baseleaf_from_fnm(const char *fnm) |
---|
| 107 | { |
---|
| 108 | const char *p; |
---|
| 109 | char *q; |
---|
| 110 | size_t len; |
---|
| 111 | |
---|
| 112 | p = fnm; |
---|
| 113 | q = strrchr(p, '/'); |
---|
| 114 | if (q) p = q + 1; |
---|
| 115 | q = strrchr(p, '\\'); |
---|
| 116 | if (q) p = q + 1; |
---|
| 117 | |
---|
| 118 | q = strrchr(p, FNM_SEP_EXT); |
---|
| 119 | if (q) len = (const char *)q - p; else len = strlen(p); |
---|
| 120 | |
---|
| 121 | q = xosmalloc(len + 1); |
---|
| 122 | if (!q) return NULL; |
---|
| 123 | memcpy(q, p, len); |
---|
| 124 | q[len] = '\0'; |
---|
| 125 | return q; |
---|
| 126 | } |
---|
[5c3c61a] | 127 | #endif |
---|
| 128 | |
---|
[be97baf] | 129 | #include <math.h> |
---|
[dd1d6369] | 130 | #ifdef HAVE_ROUND |
---|
| 131 | extern double round(double); /* prototype is often missing... */ |
---|
| 132 | # define my_round round |
---|
| 133 | #else |
---|
| 134 | static double |
---|
| 135 | my_round(double x) { |
---|
| 136 | if (x >= 0.0) return floor(x + 0.5); |
---|
| 137 | return ceil(x - 0.5); |
---|
| 138 | } |
---|
| 139 | #endif |
---|
| 140 | |
---|
[ad4eaf3f] | 141 | /* portable case insensitive string compare */ |
---|
| 142 | #if defined(strcasecmp) || defined(HAVE_STRCASECMP) |
---|
| 143 | # define my_strcasecmp strcasecmp |
---|
| 144 | #else |
---|
| 145 | /* What about top bit set chars? */ |
---|
| 146 | int my_strcasecmp(const char *s1, const char *s2) { |
---|
[be97baf] | 147 | register int c1, c2; |
---|
[ad4eaf3f] | 148 | do { |
---|
| 149 | c1 = *s1++; |
---|
| 150 | c2 = *s2++; |
---|
| 151 | } while (c1 && toupper(c1) == toupper(c2)); |
---|
| 152 | /* now calculate real difference */ |
---|
| 153 | return c1 - c2; |
---|
| 154 | } |
---|
| 155 | #endif |
---|
| 156 | |
---|
[c61aa79] | 157 | unsigned int img_output_version = LATEST_IMG_VERSION; |
---|
[5c3c61a] | 158 | |
---|
[72af530] | 159 | #ifdef IMG_HOSTED |
---|
[5c3c61a] | 160 | static enum { |
---|
| 161 | IMG_NONE = 0, |
---|
[759fb47] | 162 | IMG_FILENOTFOUND = /*Couldn't open data file `%s'*/24, |
---|
[5c3c61a] | 163 | IMG_OUTOFMEMORY = /*Out of memory %.0s*/38, |
---|
[759fb47] | 164 | IMG_DIRECTORY = /*Filename `%s' refers to directory*/44, |
---|
| 165 | IMG_CANTOPENOUT = /*Failed to open output file `%s'*/47, |
---|
[ad9a5cd] | 166 | IMG_BADFORMAT = /*Bad 3d image file `%s'*/106, |
---|
| 167 | IMG_READERROR = /*Error reading from file `%s'*/109, |
---|
[6c1c4f3] | 168 | IMG_WRITEERROR = /*Error writing to file `%s'*/110, |
---|
| 169 | IMG_TOONEW = /*File `%s' has a newer format than this program can understand*/114 |
---|
[5c3c61a] | 170 | } img_errno = IMG_NONE; |
---|
| 171 | #else |
---|
| 172 | static img_errcode img_errno = IMG_NONE; |
---|
[c0563da] | 173 | #endif |
---|
[d1b1380] | 174 | |
---|
[7d27e36] | 175 | #define FILEID "Survex 3D Image File" |
---|
| 176 | |
---|
[ad4eaf3f] | 177 | #define EXT_PLT "plt" |
---|
[d16db18] | 178 | #define EXT_PLF "plf" |
---|
[f1c7c9c7] | 179 | #define EXT_XYZ "xyz" |
---|
[ad4eaf3f] | 180 | |
---|
[0da6e60] | 181 | /* Attempt to string paste to ensure we are passed a literal string */ |
---|
| 182 | #define LITLEN(S) (sizeof(S"") - 1) |
---|
| 183 | |
---|
[db9e5bf] | 184 | /* Fake "version numbers" for non-3d formats we can read. */ |
---|
| 185 | #define VERSION_CMAP_SHOT -4 |
---|
| 186 | #define VERSION_CMAP_STATION -3 |
---|
| 187 | #define VERSION_COMPASS_PLT -2 |
---|
| 188 | #define VERSION_SURVEX_POS -1 |
---|
| 189 | |
---|
[7d27e36] | 190 | static char * |
---|
| 191 | my_strdup(const char *str) |
---|
[a420b49] | 192 | { |
---|
[7d27e36] | 193 | char *p; |
---|
| 194 | size_t len = strlen(str) + 1; |
---|
| 195 | p = xosmalloc(len); |
---|
| 196 | if (p) memcpy(p, str, len); |
---|
| 197 | return p; |
---|
[d1b1380] | 198 | } |
---|
[c0563da] | 199 | |
---|
[a2ad284] | 200 | static char * |
---|
| 201 | getline_alloc(FILE *fh) |
---|
| 202 | { |
---|
| 203 | int ch; |
---|
| 204 | size_t i = 0; |
---|
| 205 | size_t len = 16; |
---|
| 206 | char *buf = xosmalloc(len); |
---|
| 207 | if (!buf) return NULL; |
---|
| 208 | |
---|
| 209 | ch = getc(fh); |
---|
| 210 | while (ch != '\n' && ch != '\r' && ch != EOF) { |
---|
| 211 | buf[i++] = ch; |
---|
| 212 | if (i == len - 1) { |
---|
| 213 | char *p; |
---|
| 214 | len += len; |
---|
| 215 | p = xosrealloc(buf, len); |
---|
| 216 | if (!p) { |
---|
| 217 | osfree(buf); |
---|
| 218 | return NULL; |
---|
| 219 | } |
---|
| 220 | buf = p; |
---|
| 221 | } |
---|
| 222 | ch = getc(fh); |
---|
| 223 | } |
---|
| 224 | if (ch == '\n' || ch == '\r') { |
---|
[4ebc557] | 225 | int otherone = ch ^ ('\n' ^ '\r'); |
---|
| 226 | ch = getc(fh); |
---|
| 227 | /* if it's not the other eol character, put it back */ |
---|
| 228 | if (ch != otherone) ungetc(ch, fh); |
---|
[a2ad284] | 229 | } |
---|
[421b7d2] | 230 | buf[i++] = '\0'; |
---|
[a2ad284] | 231 | return buf; |
---|
| 232 | } |
---|
| 233 | |
---|
[72af530] | 234 | #ifndef IMG_HOSTED |
---|
[a420b49] | 235 | img_errcode |
---|
| 236 | img_error(void) |
---|
| 237 | { |
---|
| 238 | return img_errno; |
---|
[d1b1380] | 239 | } |
---|
| 240 | #else |
---|
[a420b49] | 241 | int |
---|
| 242 | img_error(void) |
---|
| 243 | { |
---|
| 244 | return (int)img_errno; |
---|
[d1b1380] | 245 | } |
---|
| 246 | #endif |
---|
| 247 | |
---|
[d4d3655] | 248 | static bool |
---|
| 249 | check_label_space(img *pimg, size_t len) |
---|
| 250 | { |
---|
| 251 | if (len > pimg->buf_len) { |
---|
| 252 | char *b = xosrealloc(pimg->label_buf, len); |
---|
| 253 | if (!b) return fFalse; |
---|
| 254 | pimg->label = (pimg->label - pimg->label_buf) + b; |
---|
| 255 | pimg->label_buf = b; |
---|
| 256 | pimg->buf_len = len; |
---|
| 257 | } |
---|
| 258 | return fTrue; |
---|
| 259 | } |
---|
| 260 | |
---|
[d16db18] | 261 | #define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\ |
---|
| 262 | (F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\ |
---|
| 263 | my_strcasecmp((F) + (L) - LITLEN(E), E) == 0) |
---|
| 264 | |
---|
[a420b49] | 265 | img * |
---|
[a2ad284] | 266 | img_open_survey(const char *fnm, const char *survey) |
---|
[a420b49] | 267 | { |
---|
| 268 | img *pimg; |
---|
[0da6e60] | 269 | size_t len; |
---|
[27b8b59] | 270 | char buf[LITLEN(FILEID) + 9]; |
---|
[7d27e36] | 271 | int ch; |
---|
[a420b49] | 272 | |
---|
| 273 | if (fDirectory(fnm)) { |
---|
| 274 | img_errno = IMG_DIRECTORY; |
---|
| 275 | return NULL; |
---|
| 276 | } |
---|
[d1b1380] | 277 | |
---|
[a420b49] | 278 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 279 | if (pimg == NULL) { |
---|
| 280 | img_errno = IMG_OUTOFMEMORY; |
---|
| 281 | return NULL; |
---|
| 282 | } |
---|
[d1b1380] | 283 | |
---|
[23f7ea7] | 284 | pimg->buf_len = 257; |
---|
[0ff23cb] | 285 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 286 | if (!pimg->label_buf) { |
---|
[23f7ea7] | 287 | osfree(pimg); |
---|
| 288 | img_errno = IMG_OUTOFMEMORY; |
---|
| 289 | return NULL; |
---|
| 290 | } |
---|
| 291 | |
---|
[cc9eb738] | 292 | pimg->fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", &(pimg->filename_opened)); |
---|
[a420b49] | 293 | if (pimg->fh == NULL) { |
---|
[ce1e5ac] | 294 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 295 | osfree(pimg); |
---|
[a420b49] | 296 | img_errno = IMG_FILENOTFOUND; |
---|
| 297 | return NULL; |
---|
| 298 | } |
---|
[d1b1380] | 299 | |
---|
[0da6e60] | 300 | pimg->fRead = fTrue; /* reading from this file */ |
---|
| 301 | img_errno = IMG_NONE; |
---|
| 302 | |
---|
| 303 | pimg->flags = 0; |
---|
| 304 | |
---|
[b5a3219] | 305 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[db9e5bf] | 306 | /* for VERSION_COMPASS_PLT, 0 value indicates we haven't |
---|
| 307 | * entered a survey yet */ |
---|
| 308 | /* for VERSION_CMAP_SHOT, we store the last station here |
---|
| 309 | * to detect whether we MOVE or LINE */ |
---|
[0da6e60] | 310 | pimg->label_len = 0; |
---|
[f1c7c9c7] | 311 | pimg->label_buf[0] = '\0'; |
---|
[0da6e60] | 312 | |
---|
| 313 | pimg->survey = NULL; |
---|
| 314 | pimg->survey_len = 0; |
---|
[ee1ec59] | 315 | pimg->separator = '.'; |
---|
[b5a3219] | 316 | pimg->date1 = 0; |
---|
| 317 | pimg->date2 = 0; |
---|
[eef68f9] | 318 | pimg->is_extended_elevation = 0; |
---|
[0da6e60] | 319 | |
---|
[62a841c] | 320 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
| 321 | |
---|
[c637fad] | 322 | pimg->title = pimg->datestamp = NULL; |
---|
[0da6e60] | 323 | if (survey) { |
---|
| 324 | len = strlen(survey); |
---|
| 325 | if (len) { |
---|
| 326 | if (survey[len - 1] == '.') len--; |
---|
| 327 | if (len) { |
---|
| 328 | char *p; |
---|
[be97baf] | 329 | pimg->survey = xosmalloc(len + 2); |
---|
| 330 | if (!pimg->survey) { |
---|
| 331 | img_errno = IMG_OUTOFMEMORY; |
---|
| 332 | goto error; |
---|
| 333 | } |
---|
[0da6e60] | 334 | memcpy(pimg->survey, survey, len); |
---|
| 335 | /* Set title to leaf survey name */ |
---|
| 336 | pimg->survey[len] = '\0'; |
---|
| 337 | p = strchr(pimg->survey, '.'); |
---|
| 338 | if (p) p++; else p = pimg->survey; |
---|
[7d27e36] | 339 | pimg->title = my_strdup(p); |
---|
| 340 | if (!pimg->title) { |
---|
| 341 | img_errno = IMG_OUTOFMEMORY; |
---|
| 342 | goto error; |
---|
| 343 | } |
---|
[0da6e60] | 344 | pimg->survey[len] = '.'; |
---|
| 345 | pimg->survey[len + 1] = '\0'; |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | pimg->survey_len = len; |
---|
| 349 | } |
---|
| 350 | |
---|
[db9e5bf] | 351 | /* [VERSION_COMPASS_PLT, VERSION_CMAP_STATION, VERSION_CMAP_SHOT] pending |
---|
| 352 | * IMG_LINE or IMG_MOVE - both have 4 added. |
---|
| 353 | * [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one |
---|
[0da6e60] | 354 | * [version 0] not in the middle of a 'LINE' command |
---|
[b5a3219] | 355 | * [version >= 3] not in the middle of turning a LINE into a MOVE |
---|
[0da6e60] | 356 | */ |
---|
| 357 | pimg->pending = 0; |
---|
| 358 | |
---|
| 359 | len = strlen(fnm); |
---|
[d16db18] | 360 | if (has_ext(fnm, len, EXT_SVX_POS)) { |
---|
[f830e0d] | 361 | pos_file: |
---|
[db9e5bf] | 362 | pimg->version = VERSION_SURVEX_POS; |
---|
[984f49e] | 363 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[7d27e36] | 364 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 365 | if (!pimg->datestamp) { |
---|
| 366 | img_errno = IMG_OUTOFMEMORY; |
---|
| 367 | goto error; |
---|
| 368 | } |
---|
[58f02ae] | 369 | pimg->start = 0; |
---|
[0da6e60] | 370 | return pimg; |
---|
| 371 | } |
---|
[5757725] | 372 | |
---|
[d16db18] | 373 | if (has_ext(fnm, len, EXT_PLT) || has_ext(fnm, len, EXT_PLF)) { |
---|
[4ebc557] | 374 | long fpos; |
---|
[27bd06b] | 375 | plt_file: |
---|
[db9e5bf] | 376 | pimg->version = VERSION_COMPASS_PLT; |
---|
[ee1ec59] | 377 | /* Spaces aren't legal in Compass station names, but dots are, so |
---|
| 378 | * use space as the level separator */ |
---|
| 379 | pimg->separator = ' '; |
---|
[4ebc557] | 380 | pimg->start = 0; |
---|
[984f49e] | 381 | if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm); |
---|
[ad4eaf3f] | 382 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 383 | if (!pimg->datestamp) { |
---|
| 384 | img_errno = IMG_OUTOFMEMORY; |
---|
| 385 | goto error; |
---|
| 386 | } |
---|
[4ebc557] | 387 | while (1) { |
---|
[b4fe9fb] | 388 | ch = getc(pimg->fh); |
---|
[4ebc557] | 389 | switch (ch) { |
---|
| 390 | case '\x1a': |
---|
| 391 | fseek(pimg->fh, -1, SEEK_CUR); |
---|
| 392 | /* FALL THRU */ |
---|
| 393 | case EOF: |
---|
| 394 | pimg->start = ftell(pimg->fh); |
---|
| 395 | return pimg; |
---|
| 396 | case 'N': { |
---|
| 397 | char *line, *q; |
---|
| 398 | fpos = ftell(pimg->fh) - 1; |
---|
| 399 | if (!pimg->survey) { |
---|
| 400 | /* FIXME : if there's only one survey in the file, it'd be nice |
---|
| 401 | * to use its description as the title here... |
---|
| 402 | */ |
---|
[0d922fc] | 403 | ungetc('N', pimg->fh); |
---|
[4ebc557] | 404 | pimg->start = fpos; |
---|
[984f49e] | 405 | return pimg; |
---|
| 406 | } |
---|
[4ebc557] | 407 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 408 | if (!line) { |
---|
| 409 | img_errno = IMG_OUTOFMEMORY; |
---|
| 410 | goto error; |
---|
| 411 | } |
---|
[0d922fc] | 412 | len = 0; |
---|
[4ebc557] | 413 | while (line[len] > 32) ++len; |
---|
| 414 | if (pimg->survey_len != len || |
---|
| 415 | memcmp(line, pimg->survey, len) != 0) { |
---|
[ad4eaf3f] | 416 | osfree(line); |
---|
[4ebc557] | 417 | continue; |
---|
[ad4eaf3f] | 418 | } |
---|
[4ebc557] | 419 | q = strchr(line + len, 'C'); |
---|
| 420 | if (q && q[1]) { |
---|
| 421 | osfree(pimg->title); |
---|
[f1c7c9c7] | 422 | pimg->title = my_strdup(q + 1); |
---|
[4ebc557] | 423 | } else if (!pimg->title) { |
---|
[f1c7c9c7] | 424 | pimg->title = my_strdup(pimg->label); |
---|
[4ebc557] | 425 | } |
---|
| 426 | osfree(line); |
---|
| 427 | if (!pimg->title) { |
---|
| 428 | img_errno = IMG_OUTOFMEMORY; |
---|
| 429 | goto error; |
---|
| 430 | } |
---|
| 431 | if (!pimg->start) pimg->start = fpos; |
---|
[27bd06b] | 432 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
[4ebc557] | 433 | return pimg; |
---|
| 434 | } |
---|
| 435 | case 'M': case 'D': |
---|
| 436 | pimg->start = ftell(pimg->fh) - 1; |
---|
| 437 | break; |
---|
[984f49e] | 438 | } |
---|
[4ebc557] | 439 | while (ch != '\n' && ch != '\r') { |
---|
| 440 | ch = getc(pimg->fh); |
---|
[984f49e] | 441 | } |
---|
| 442 | } |
---|
[ad4eaf3f] | 443 | } |
---|
[0da6e60] | 444 | |
---|
[d16db18] | 445 | if (has_ext(fnm, len, EXT_XYZ)) { |
---|
[f1c7c9c7] | 446 | char *line; |
---|
| 447 | xyz_file: |
---|
[ee1ec59] | 448 | /* Spaces aren't legal in CMAP station names, but dots are, so |
---|
[6d4c4b9] | 449 | * use space as the level separator. */ |
---|
[ee1ec59] | 450 | pimg->separator = ' '; |
---|
[f1c7c9c7] | 451 | line = getline_alloc(pimg->fh); |
---|
| 452 | if (!line) { |
---|
| 453 | img_errno = IMG_OUTOFMEMORY; |
---|
| 454 | goto error; |
---|
| 455 | } |
---|
| 456 | /* FIXME: reparse date? */ |
---|
| 457 | len = strlen(line); |
---|
| 458 | if (len > 59) line[59] = '\0'; |
---|
| 459 | if (len > 45) { |
---|
| 460 | pimg->datestamp = my_strdup(line + 45); |
---|
| 461 | } else { |
---|
| 462 | pimg->datestamp = my_strdup(TIMENA); |
---|
| 463 | } |
---|
| 464 | if (strncmp(line, " Cave Survey Data Processed by CMAP ", |
---|
| 465 | LITLEN(" Cave Survey Data Processed by CMAP ")) == 0) { |
---|
| 466 | len = 0; |
---|
| 467 | } else { |
---|
| 468 | if (len > 45) { |
---|
| 469 | line[45] = '\0'; |
---|
| 470 | len = 45; |
---|
| 471 | } |
---|
| 472 | while (len > 2 && line[len - 1] == ' ') --len; |
---|
| 473 | if (len > 2) { |
---|
| 474 | line[len] = '\0'; |
---|
| 475 | pimg->title = my_strdup(line + 2); |
---|
| 476 | } |
---|
| 477 | } |
---|
| 478 | if (len <= 2) pimg->title = baseleaf_from_fnm(fnm); |
---|
| 479 | osfree(line); |
---|
| 480 | if (!pimg->datestamp || !pimg->title) { |
---|
| 481 | img_errno = IMG_OUTOFMEMORY; |
---|
| 482 | goto error; |
---|
| 483 | } |
---|
| 484 | line = getline_alloc(pimg->fh); |
---|
| 485 | if (!line) { |
---|
| 486 | img_errno = IMG_OUTOFMEMORY; |
---|
| 487 | goto error; |
---|
| 488 | } |
---|
| 489 | if (line[0] != ' ' || (line[1] != 'S' && line[1] != 'O')) { |
---|
| 490 | img_errno = IMG_BADFORMAT; |
---|
| 491 | goto error; |
---|
| 492 | } |
---|
| 493 | if (line[1] == 'S') { |
---|
[db9e5bf] | 494 | pimg->version = VERSION_CMAP_STATION; |
---|
[f1c7c9c7] | 495 | } else { |
---|
[db9e5bf] | 496 | pimg->version = VERSION_CMAP_SHOT; |
---|
[f1c7c9c7] | 497 | } |
---|
| 498 | osfree(line); |
---|
| 499 | line = getline_alloc(pimg->fh); |
---|
| 500 | if (!line) { |
---|
| 501 | img_errno = IMG_OUTOFMEMORY; |
---|
| 502 | goto error; |
---|
| 503 | } |
---|
| 504 | if (line[0] != ' ' || line[1] != '-') { |
---|
| 505 | img_errno = IMG_BADFORMAT; |
---|
| 506 | goto error; |
---|
| 507 | } |
---|
| 508 | osfree(line); |
---|
| 509 | pimg->start = ftell(pimg->fh); |
---|
| 510 | return pimg; |
---|
| 511 | } |
---|
| 512 | |
---|
[7d27e36] | 513 | if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 || |
---|
[d16db18] | 514 | memcmp(buf, FILEID"\n", LITLEN(FILEID) + 1) != 0) { |
---|
[27b8b59] | 515 | if (fread(buf + LITLEN(FILEID) + 1, 8, 1, pimg->fh) == 1 && |
---|
| 516 | memcmp(buf, FILEID"\r\nv0.01\r\n", LITLEN(FILEID) + 9) == 0) { |
---|
| 517 | /* v0 3d file with DOS EOLs */ |
---|
| 518 | pimg->version = 0; |
---|
| 519 | goto v03d; |
---|
| 520 | } |
---|
[d16db18] | 521 | rewind(pimg->fh); |
---|
[f1c7c9c7] | 522 | if (buf[1] == ' ') { |
---|
| 523 | if (buf[0] == ' ') { |
---|
| 524 | /* Looks like a CMAP .xyz file ... */ |
---|
| 525 | goto xyz_file; |
---|
| 526 | } else if (strchr("ZSNF", buf[0])) { |
---|
| 527 | /* Looks like a Compass .plt file ... */ |
---|
| 528 | /* Almost certainly it'll start "Z " */ |
---|
| 529 | goto plt_file; |
---|
| 530 | } |
---|
[27bd06b] | 531 | } |
---|
[f830e0d] | 532 | if (buf[0] == '(') { |
---|
| 533 | /* Looks like a Survex .pos file ... */ |
---|
| 534 | goto pos_file; |
---|
| 535 | } |
---|
[a420b49] | 536 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 537 | goto error; |
---|
[a420b49] | 538 | } |
---|
[d1b1380] | 539 | |
---|
[7d27e36] | 540 | /* check file format version */ |
---|
| 541 | ch = getc(pimg->fh); |
---|
| 542 | pimg->version = 0; |
---|
| 543 | if (tolower(ch) == 'b') { |
---|
| 544 | /* binary file iff B/b prefix */ |
---|
| 545 | pimg->version = 1; |
---|
| 546 | ch = getc(pimg->fh); |
---|
| 547 | } |
---|
| 548 | if (ch != 'v') { |
---|
| 549 | img_errno = IMG_BADFORMAT; |
---|
| 550 | goto error; |
---|
| 551 | } |
---|
| 552 | ch = getc(pimg->fh); |
---|
| 553 | if (ch == '0') { |
---|
| 554 | if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) { |
---|
| 555 | img_errno = IMG_BADFORMAT; |
---|
| 556 | goto error; |
---|
| 557 | } |
---|
[0da6e60] | 558 | /* nothing special to do */ |
---|
[7d27e36] | 559 | } else if (pimg->version == 0) { |
---|
[c61aa79] | 560 | if (ch < '2' || ch > '0' + LATEST_IMG_VERSION || getc(pimg->fh) != '\n') { |
---|
[6c1c4f3] | 561 | img_errno = IMG_TOONEW; |
---|
[58f02ae] | 562 | goto error; |
---|
[6c1c4f3] | 563 | } |
---|
[7d27e36] | 564 | pimg->version = ch - '0'; |
---|
[5c3c61a] | 565 | } else { |
---|
[647407d] | 566 | img_errno = IMG_BADFORMAT; |
---|
[58f02ae] | 567 | goto error; |
---|
[a420b49] | 568 | } |
---|
[421b7d2] | 569 | |
---|
[27b8b59] | 570 | v03d: |
---|
[4d3dfdf] | 571 | if (!pimg->title) |
---|
| 572 | pimg->title = getline_alloc(pimg->fh); |
---|
| 573 | else |
---|
| 574 | osfree(getline_alloc(pimg->fh)); |
---|
[a2ad284] | 575 | pimg->datestamp = getline_alloc(pimg->fh); |
---|
| 576 | if (!pimg->title || !pimg->datestamp) { |
---|
[58f02ae] | 577 | img_errno = IMG_OUTOFMEMORY; |
---|
| 578 | error: |
---|
[a2ad284] | 579 | osfree(pimg->title); |
---|
[984f49e] | 580 | osfree(pimg->datestamp); |
---|
[452e200] | 581 | osfree(pimg->filename_opened); |
---|
[a2ad284] | 582 | fclose(pimg->fh); |
---|
| 583 | osfree(pimg); |
---|
| 584 | return NULL; |
---|
| 585 | } |
---|
| 586 | |
---|
[58f02ae] | 587 | pimg->start = ftell(pimg->fh); |
---|
[421b7d2] | 588 | |
---|
[eef68f9] | 589 | len = strlen(pimg->title); |
---|
| 590 | if (len > 11 && strcmp(pimg->title + len - 11, " (extended)") == 0) { |
---|
| 591 | pimg->title[len - 11] = '\0'; |
---|
| 592 | pimg->is_extended_elevation = 1; |
---|
| 593 | } |
---|
| 594 | |
---|
[a420b49] | 595 | return pimg; |
---|
[d1b1380] | 596 | } |
---|
| 597 | |
---|
[9f3e5df] | 598 | int |
---|
[f2588ca] | 599 | img_rewind(img *pimg) |
---|
| 600 | { |
---|
[9f3e5df] | 601 | if (!pimg->fRead) { |
---|
| 602 | img_errno = IMG_WRITEERROR; |
---|
| 603 | return 0; |
---|
| 604 | } |
---|
| 605 | if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) { |
---|
| 606 | img_errno = IMG_READERROR; |
---|
| 607 | return 0; |
---|
| 608 | } |
---|
[58f02ae] | 609 | clearerr(pimg->fh); |
---|
[db9e5bf] | 610 | /* [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one |
---|
[58f02ae] | 611 | * [version 0] not in the middle of a 'LINE' command |
---|
[b5a3219] | 612 | * [version >= 3] not in the middle of turning a LINE into a MOVE */ |
---|
[4ebc557] | 613 | pimg->pending = 0; |
---|
[d69255f] | 614 | |
---|
| 615 | img_errno = IMG_NONE; |
---|
[99cf51a] | 616 | |
---|
[b5a3219] | 617 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[db9e5bf] | 618 | /* for VERSION_COMPASS_PLT, 0 value indicates we haven't entered a survey |
---|
| 619 | * yet */ |
---|
| 620 | /* for VERSION_CMAP_SHOT, we store the last station here to detect whether |
---|
[f1c7c9c7] | 621 | * we MOVE or LINE */ |
---|
[d69255f] | 622 | pimg->label_len = 0; |
---|
[9f3e5df] | 623 | return 1; |
---|
[f2588ca] | 624 | } |
---|
| 625 | |
---|
[a420b49] | 626 | img * |
---|
[fe8e80e] | 627 | img_open_write(const char *fnm, char *title_buf, bool fBinary) |
---|
[a420b49] | 628 | { |
---|
| 629 | time_t tm; |
---|
| 630 | img *pimg; |
---|
[d1b1380] | 631 | |
---|
[d69255f] | 632 | fBinary = fBinary; |
---|
| 633 | |
---|
[a420b49] | 634 | if (fDirectory(fnm)) { |
---|
| 635 | img_errno = IMG_DIRECTORY; |
---|
| 636 | return NULL; |
---|
| 637 | } |
---|
[d1b1380] | 638 | |
---|
[a420b49] | 639 | pimg = (img *)xosmalloc(ossizeof(img)); |
---|
| 640 | if (pimg == NULL) { |
---|
| 641 | img_errno = IMG_OUTOFMEMORY; |
---|
| 642 | return NULL; |
---|
| 643 | } |
---|
[d1b1380] | 644 | |
---|
[fe8e80e] | 645 | pimg->buf_len = 257; |
---|
[0ff23cb] | 646 | pimg->label_buf = xosmalloc(pimg->buf_len); |
---|
| 647 | if (!pimg->label_buf) { |
---|
[fe8e80e] | 648 | osfree(pimg); |
---|
| 649 | img_errno = IMG_OUTOFMEMORY; |
---|
| 650 | return NULL; |
---|
| 651 | } |
---|
| 652 | |
---|
[a420b49] | 653 | pimg->fh = fopen(fnm, "wb"); |
---|
| 654 | if (!pimg->fh) { |
---|
[ce1e5ac] | 655 | osfree(pimg->label_buf); |
---|
[47e4a2e] | 656 | osfree(pimg); |
---|
[a420b49] | 657 | img_errno = IMG_CANTOPENOUT; |
---|
| 658 | return NULL; |
---|
| 659 | } |
---|
[d1b1380] | 660 | |
---|
[452e200] | 661 | pimg->filename_opened = NULL; |
---|
| 662 | |
---|
[a420b49] | 663 | /* Output image file header */ |
---|
| 664 | fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */ |
---|
[5c3c61a] | 665 | if (img_output_version < 2) { |
---|
[d69255f] | 666 | pimg->version = 1; |
---|
| 667 | fputs("Bv0.01\n", pimg->fh); /* binary file format version number */ |
---|
[5c3c61a] | 668 | } else { |
---|
[c61aa79] | 669 | pimg->version = (img_output_version > LATEST_IMG_VERSION) ? LATEST_IMG_VERSION : img_output_version; |
---|
[5c3c61a] | 670 | fprintf(pimg->fh, "v%d\n", pimg->version); /* file format version no. */ |
---|
| 671 | } |
---|
[fe8e80e] | 672 | fputsnl(title_buf, pimg->fh); |
---|
[a420b49] | 673 | tm = time(NULL); |
---|
| 674 | if (tm == (time_t)-1) { |
---|
| 675 | fputsnl(TIMENA, pimg->fh); |
---|
| 676 | } else { |
---|
[7d27e36] | 677 | char date[256]; |
---|
[a420b49] | 678 | /* output current date and time in format specified */ |
---|
[7d27e36] | 679 | strftime(date, 256, TIMEFMT, localtime(&tm)); |
---|
| 680 | fputsnl(date, pimg->fh); |
---|
[a420b49] | 681 | } |
---|
[ee05463] | 682 | #if 0 |
---|
| 683 | if (img_output_version >= 5) { |
---|
| 684 | static const unsigned char codelengths[32] = { |
---|
| 685 | 4, 8, 8, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 686 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
---|
| 687 | }; |
---|
| 688 | fwrite(codelengths, 32, 1, pimg->fh); |
---|
| 689 | } |
---|
| 690 | #endif |
---|
[a420b49] | 691 | pimg->fRead = fFalse; /* writing to this file */ |
---|
| 692 | img_errno = IMG_NONE; |
---|
[d69255f] | 693 | |
---|
[b5a3219] | 694 | /* for version >= 3 we use label_buf to store the prefix for reuse */ |
---|
[0ff23cb] | 695 | pimg->label_buf[0] = '\0'; |
---|
[d69255f] | 696 | pimg->label_len = 0; |
---|
| 697 | |
---|
[b5a3219] | 698 | pimg->date1 = 0; |
---|
| 699 | pimg->date2 = 0; |
---|
| 700 | |
---|
[ee05463] | 701 | pimg->olddate1 = 0; |
---|
| 702 | pimg->olddate2 = 0; |
---|
| 703 | |
---|
[62a841c] | 704 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
[ee7af72] | 705 | |
---|
[c61aa79] | 706 | pimg->n_legs = 0; |
---|
| 707 | pimg->length = 0.0; |
---|
| 708 | pimg->E = pimg->H = pimg->V = 0.0; |
---|
| 709 | |
---|
[ad9a5cd] | 710 | /* Don't check for write errors now - let img_close() report them... */ |
---|
[a420b49] | 711 | return pimg; |
---|
[d1b1380] | 712 | } |
---|
| 713 | |
---|
[f1c7c9c7] | 714 | static void |
---|
| 715 | read_xyz_station_coords(img_point *pt, const char *line) |
---|
| 716 | { |
---|
| 717 | char num[12]; |
---|
| 718 | memcpy(num, line + 6, 9); |
---|
| 719 | num[9] = '\0'; |
---|
| 720 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 721 | memcpy(num, line + 15, 9); |
---|
| 722 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 723 | memcpy(num, line + 24, 8); |
---|
| 724 | num[8] = '\0'; |
---|
| 725 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | static void |
---|
| 729 | read_xyz_shot_coords(img_point *pt, const char *line) |
---|
| 730 | { |
---|
| 731 | char num[12]; |
---|
| 732 | memcpy(num, line + 40, 10); |
---|
| 733 | num[10] = '\0'; |
---|
| 734 | pt->x = atof(num) / METRES_PER_FOOT; |
---|
| 735 | memcpy(num, line + 50, 10); |
---|
| 736 | pt->y = atof(num) / METRES_PER_FOOT; |
---|
| 737 | memcpy(num, line + 60, 9); |
---|
| 738 | num[9] = '\0'; |
---|
| 739 | pt->z = atof(num) / METRES_PER_FOOT; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | static void |
---|
| 743 | subtract_xyz_shot_deltas(img_point *pt, const char *line) |
---|
| 744 | { |
---|
| 745 | char num[12]; |
---|
| 746 | memcpy(num, line + 15, 9); |
---|
| 747 | num[9] = '\0'; |
---|
| 748 | pt->x -= atof(num) / METRES_PER_FOOT; |
---|
| 749 | memcpy(num, line + 24, 8); |
---|
| 750 | num[8] = '\0'; |
---|
| 751 | pt->y -= atof(num) / METRES_PER_FOOT; |
---|
| 752 | memcpy(num, line + 32, 8); |
---|
| 753 | pt->z -= atof(num) / METRES_PER_FOOT; |
---|
| 754 | } |
---|
| 755 | |
---|
[ad9a5cd] | 756 | static int |
---|
| 757 | read_coord(FILE *fh, img_point *pt) |
---|
| 758 | { |
---|
[4c07c51] | 759 | SVX_ASSERT(fh); |
---|
| 760 | SVX_ASSERT(pt); |
---|
[ad9a5cd] | 761 | pt->x = get32(fh) / 100.0; |
---|
| 762 | pt->y = get32(fh) / 100.0; |
---|
| 763 | pt->z = get32(fh) / 100.0; |
---|
| 764 | if (ferror(fh) || feof(fh)) { |
---|
| 765 | img_errno = feof(fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 766 | return 0; |
---|
[421b7d2] | 767 | } |
---|
[ad9a5cd] | 768 | return 1; |
---|
| 769 | } |
---|
| 770 | |
---|
[4d3dfdf] | 771 | static int |
---|
| 772 | skip_coord(FILE *fh) |
---|
| 773 | { |
---|
| 774 | return (fseek(fh, 12, SEEK_CUR) == 0); |
---|
| 775 | } |
---|
| 776 | |
---|
[ee05463] | 777 | static int |
---|
| 778 | read_v3label(img *pimg) |
---|
| 779 | { |
---|
| 780 | char *q; |
---|
| 781 | long len = getc(pimg->fh); |
---|
| 782 | if (len == EOF) { |
---|
| 783 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 784 | return img_BAD; |
---|
| 785 | } |
---|
| 786 | if (len == 0xfe) { |
---|
| 787 | len += get16(pimg->fh); |
---|
| 788 | if (feof(pimg->fh)) { |
---|
| 789 | img_errno = IMG_BADFORMAT; |
---|
| 790 | return img_BAD; |
---|
| 791 | } |
---|
| 792 | if (ferror(pimg->fh)) { |
---|
| 793 | img_errno = IMG_READERROR; |
---|
| 794 | return img_BAD; |
---|
| 795 | } |
---|
| 796 | } else if (len == 0xff) { |
---|
| 797 | len = get32(pimg->fh); |
---|
| 798 | if (ferror(pimg->fh)) { |
---|
| 799 | img_errno = IMG_READERROR; |
---|
| 800 | return img_BAD; |
---|
| 801 | } |
---|
| 802 | if (feof(pimg->fh) || len < 0xfe + 0xffff) { |
---|
| 803 | img_errno = IMG_BADFORMAT; |
---|
| 804 | return img_BAD; |
---|
| 805 | } |
---|
| 806 | } |
---|
| 807 | |
---|
| 808 | if (!check_label_space(pimg, pimg->label_len + len + 1)) { |
---|
| 809 | img_errno = IMG_OUTOFMEMORY; |
---|
| 810 | return img_BAD; |
---|
| 811 | } |
---|
| 812 | q = pimg->label_buf + pimg->label_len; |
---|
| 813 | pimg->label_len += len; |
---|
| 814 | if (len && fread(q, len, 1, pimg->fh) != 1) { |
---|
| 815 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 816 | return img_BAD; |
---|
| 817 | } |
---|
| 818 | q[len] = '\0'; |
---|
| 819 | return 0; |
---|
| 820 | } |
---|
| 821 | |
---|
[898bc69] | 822 | static int img_read_item_ascii(img *pimg, img_point *p); |
---|
| 823 | |
---|
[a420b49] | 824 | int |
---|
[23f7ea7] | 825 | img_read_item(img *pimg, img_point *p) |
---|
[a420b49] | 826 | { |
---|
| 827 | int result; |
---|
[95c3272] | 828 | pimg->flags = 0; |
---|
[d69255f] | 829 | |
---|
[b5a3219] | 830 | if (pimg->version >= 3) { |
---|
[d69255f] | 831 | int opt; |
---|
[ee05463] | 832 | pimg->l = pimg->r = pimg->u = pimg->d = -1.0; |
---|
[ded184e2] | 833 | if (pimg->pending == 256) { |
---|
| 834 | pimg->pending = 0; |
---|
| 835 | return img_XSECT_END; |
---|
| 836 | } |
---|
[76bbb7c9] | 837 | if (pimg->pending >= 0x80) { |
---|
| 838 | *p = pimg->mv; |
---|
| 839 | pimg->flags = (int)(pimg->pending) & 0x3f; |
---|
| 840 | pimg->pending = 0; |
---|
[421b7d2] | 841 | return img_LINE; |
---|
[76bbb7c9] | 842 | } |
---|
[ee05463] | 843 | again3: /* label to goto if we get a prefix, date, or lrud */ |
---|
[4d3dfdf] | 844 | pimg->label = pimg->label_buf; |
---|
[d69255f] | 845 | opt = getc(pimg->fh); |
---|
[ad9a5cd] | 846 | if (opt == EOF) { |
---|
| 847 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 848 | return img_BAD; |
---|
| 849 | } |
---|
[d69255f] | 850 | switch (opt >> 6) { |
---|
| 851 | case 0: |
---|
| 852 | if (opt == 0) { |
---|
| 853 | if (!pimg->label_len) return img_STOP; /* end of data marker */ |
---|
| 854 | pimg->label_len = 0; |
---|
| 855 | goto again3; |
---|
| 856 | } |
---|
| 857 | if (opt < 15) { |
---|
| 858 | /* 1-14 mean trim that many levels from current prefix */ |
---|
| 859 | int c; |
---|
[64d37a3] | 860 | if (pimg->label_len <= 17) { |
---|
[4d3dfdf] | 861 | /* zero prefix using "0" */ |
---|
[d69255f] | 862 | img_errno = IMG_BADFORMAT; |
---|
| 863 | return img_BAD; |
---|
| 864 | } |
---|
[64d37a3] | 865 | /* extra - 1 because label_len points to one past the end */ |
---|
| 866 | c = pimg->label_len - 17 - 1; |
---|
[0ff23cb] | 867 | while (pimg->label_buf[c] != '.' || --opt > 0) { |
---|
[d69255f] | 868 | if (--c < 0) { |
---|
| 869 | /* zero prefix using "0" */ |
---|
| 870 | img_errno = IMG_BADFORMAT; |
---|
| 871 | return img_BAD; |
---|
| 872 | } |
---|
| 873 | } |
---|
| 874 | c++; |
---|
| 875 | pimg->label_len = c; |
---|
| 876 | goto again3; |
---|
| 877 | } |
---|
| 878 | if (opt == 15) { |
---|
| 879 | result = img_MOVE; |
---|
| 880 | break; |
---|
| 881 | } |
---|
[32f05df] | 882 | if (opt >= 0x20) { |
---|
| 883 | switch (opt) { |
---|
[ee05463] | 884 | case 0x20: /* Single date */ |
---|
[32f05df] | 885 | pimg->date1 = get32(pimg->fh); |
---|
| 886 | pimg->date2 = pimg->date1; |
---|
| 887 | break; |
---|
[ee05463] | 888 | case 0x21: /* Date range */ |
---|
[32f05df] | 889 | pimg->date1 = get32(pimg->fh); |
---|
| 890 | pimg->date2 = get32(pimg->fh); |
---|
| 891 | break; |
---|
[c61aa79] | 892 | case 0x22: /* Error info */ |
---|
| 893 | pimg->n_legs = get32(pimg->fh); |
---|
| 894 | pimg->length = get32(pimg->fh) / 100.0; |
---|
| 895 | pimg->E = get32(pimg->fh) / 100.0; |
---|
| 896 | pimg->H = get32(pimg->fh) / 100.0; |
---|
| 897 | pimg->V = get32(pimg->fh) / 100.0; |
---|
| 898 | return img_ERROR_INFO; |
---|
[ee05463] | 899 | case 0x30: case 0x31: /* LRUD */ |
---|
| 900 | case 0x32: case 0x33: /* Big LRUD! */ |
---|
| 901 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
| 902 | pimg->flags = (int)opt & 0x01; |
---|
[d7d7fa7] | 903 | if (opt < 0x32) { |
---|
| 904 | pimg->l = get16(pimg->fh) / 100.0; |
---|
| 905 | pimg->r = get16(pimg->fh) / 100.0; |
---|
| 906 | pimg->u = get16(pimg->fh) / 100.0; |
---|
| 907 | pimg->d = get16(pimg->fh) / 100.0; |
---|
| 908 | } else { |
---|
| 909 | pimg->l = get32(pimg->fh) / 100.0; |
---|
| 910 | pimg->r = get32(pimg->fh) / 100.0; |
---|
| 911 | pimg->u = get32(pimg->fh) / 100.0; |
---|
| 912 | pimg->d = get32(pimg->fh) / 100.0; |
---|
| 913 | } |
---|
| 914 | if (pimg->survey_len) { |
---|
| 915 | size_t l = pimg->survey_len; |
---|
| 916 | const char *s = pimg->label_buf; |
---|
| 917 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
| 918 | return img_XSECT_END; |
---|
| 919 | } |
---|
| 920 | pimg->label += l; |
---|
| 921 | /* skip the dot if there */ |
---|
| 922 | if (*pimg->label) pimg->label++; |
---|
| 923 | } |
---|
| 924 | /* If this is the last cross-section in this passage, set |
---|
| 925 | * pending so we return img_XSECT_END next time. */ |
---|
| 926 | if (pimg->flags & 0x01) { |
---|
| 927 | pimg->pending = 256; |
---|
| 928 | pimg->flags &= ~0x01; |
---|
| 929 | } |
---|
[ee05463] | 930 | return img_XSECT; |
---|
[c61aa79] | 931 | default: /* 0x23 - 0x2f and 0x34 - 0x3f are currently unallocated. */ |
---|
[32f05df] | 932 | img_errno = IMG_BADFORMAT; |
---|
| 933 | return img_BAD; |
---|
| 934 | } |
---|
| 935 | goto again3; |
---|
| 936 | } |
---|
[d69255f] | 937 | /* 16-31 mean remove (n - 15) characters from the prefix */ |
---|
| 938 | /* zero prefix using 0 */ |
---|
[ad4eaf3f] | 939 | if (pimg->label_len <= (size_t)(opt - 15)) { |
---|
[d69255f] | 940 | img_errno = IMG_BADFORMAT; |
---|
| 941 | return img_BAD; |
---|
| 942 | } |
---|
[d245d71] | 943 | pimg->label_len -= (opt - 15); |
---|
[d69255f] | 944 | goto again3; |
---|
[ee05463] | 945 | case 1: |
---|
| 946 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
| 947 | |
---|
| 948 | result = img_LABEL; |
---|
| 949 | |
---|
| 950 | if (pimg->survey_len) { |
---|
| 951 | size_t l = pimg->survey_len; |
---|
| 952 | const char *s = pimg->label_buf; |
---|
| 953 | if (strncmp(pimg->survey, s, l + 1) != 0) { |
---|
| 954 | if (!skip_coord(pimg->fh)) return img_BAD; |
---|
| 955 | pimg->pending = 0; |
---|
| 956 | goto again3; |
---|
[d69255f] | 957 | } |
---|
[ee05463] | 958 | pimg->label += l; |
---|
| 959 | /* skip the dot if there */ |
---|
| 960 | if (*pimg->label) pimg->label++; |
---|
[d69255f] | 961 | } |
---|
| 962 | |
---|
[ee05463] | 963 | pimg->flags = (int)opt & 0x3f; |
---|
| 964 | break; |
---|
| 965 | case 2: |
---|
| 966 | if (read_v3label(pimg) == img_BAD) return img_BAD; |
---|
[76bbb7c9] | 967 | |
---|
[ee05463] | 968 | result = img_LINE; |
---|
[76bbb7c9] | 969 | |
---|
| 970 | if (pimg->survey_len) { |
---|
| 971 | size_t l = pimg->survey_len; |
---|
[0ff23cb] | 972 | const char *s = pimg->label_buf; |
---|
[ee05463] | 973 | if (strncmp(pimg->survey, s, l) != 0 || |
---|
| 974 | !(s[l] == '.' || s[l] == '\0')) { |
---|
| 975 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
| 976 | pimg->pending = 15; |
---|
| 977 | goto again3; |
---|
[76bbb7c9] | 978 | } |
---|
[0ff23cb] | 979 | pimg->label += l; |
---|
| 980 | /* skip the dot if there */ |
---|
| 981 | if (*pimg->label) pimg->label++; |
---|
[76bbb7c9] | 982 | } |
---|
| 983 | |
---|
[ee05463] | 984 | if (pimg->pending) { |
---|
[76bbb7c9] | 985 | *p = pimg->mv; |
---|
[ad9a5cd] | 986 | if (!read_coord(pimg->fh, &(pimg->mv))) return img_BAD; |
---|
[76bbb7c9] | 987 | pimg->pending = opt; |
---|
| 988 | return img_MOVE; |
---|
| 989 | } |
---|
| 990 | pimg->flags = (int)opt & 0x3f; |
---|
[d69255f] | 991 | break; |
---|
| 992 | default: |
---|
| 993 | img_errno = IMG_BADFORMAT; |
---|
| 994 | return img_BAD; |
---|
| 995 | } |
---|
[ad9a5cd] | 996 | if (!read_coord(pimg->fh, p)) return img_BAD; |
---|
[76bbb7c9] | 997 | pimg->pending = 0; |
---|
[d69255f] | 998 | return result; |
---|
[4d3dfdf] | 999 | } |
---|
| 1000 | |
---|
| 1001 | pimg->label = pimg->label_buf; |
---|
| 1002 | |
---|
| 1003 | if (pimg->version > 0) { |
---|
[d69255f] | 1004 | static long opt_lookahead = 0; |
---|
[ad9a5cd] | 1005 | static img_point pt = { 0.0, 0.0, 0.0 }; |
---|
[a420b49] | 1006 | long opt; |
---|
| 1007 | again: /* label to goto if we get a cross */ |
---|
[5d7280f8] | 1008 | pimg->label[0] = '\0'; |
---|
[ad9a5cd] | 1009 | |
---|
[5c3c61a] | 1010 | if (pimg->version == 1) { |
---|
[c4d4649] | 1011 | if (opt_lookahead) { |
---|
| 1012 | opt = opt_lookahead; |
---|
| 1013 | opt_lookahead = 0; |
---|
| 1014 | } else { |
---|
| 1015 | opt = get32(pimg->fh); |
---|
| 1016 | } |
---|
[5c3c61a] | 1017 | } else { |
---|
[421b7d2] | 1018 | opt = getc(pimg->fh); |
---|
[5c3c61a] | 1019 | } |
---|
[ad9a5cd] | 1020 | |
---|
| 1021 | if (feof(pimg->fh)) { |
---|
| 1022 | img_errno = IMG_BADFORMAT; |
---|
| 1023 | return img_BAD; |
---|
| 1024 | } |
---|
| 1025 | if (ferror(pimg->fh)) { |
---|
| 1026 | img_errno = IMG_READERROR; |
---|
| 1027 | return img_BAD; |
---|
| 1028 | } |
---|
| 1029 | |
---|
[a420b49] | 1030 | switch (opt) { |
---|
[437caf3] | 1031 | case -1: case 0: |
---|
[a420b49] | 1032 | return img_STOP; /* end of data marker */ |
---|
| 1033 | case 1: |
---|
[ad9a5cd] | 1034 | /* skip coordinates */ |
---|
[4d3dfdf] | 1035 | if (!skip_coord(pimg->fh)) { |
---|
[ad9a5cd] | 1036 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
| 1037 | return img_BAD; |
---|
| 1038 | } |
---|
[a420b49] | 1039 | goto again; |
---|
[23f7ea7] | 1040 | case 2: case 3: { |
---|
[fe8e80e] | 1041 | char *q; |
---|
[3a1f8da] | 1042 | int ch; |
---|
[437caf3] | 1043 | result = img_LABEL; |
---|
[3a1f8da] | 1044 | ch = getc(pimg->fh); |
---|
[d69255f] | 1045 | if (ch == EOF) { |
---|
[ad9a5cd] | 1046 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 1047 | return img_BAD; |
---|
| 1048 | } |
---|
[3a1f8da] | 1049 | if (ch != '\\') ungetc(ch, pimg->fh); |
---|
[0ff23cb] | 1050 | fgets(pimg->label_buf, 257, pimg->fh); |
---|
[421b7d2] | 1051 | if (feof(pimg->fh)) { |
---|
[ad9a5cd] | 1052 | img_errno = IMG_BADFORMAT; |
---|
| 1053 | return img_BAD; |
---|
| 1054 | } |
---|
[421b7d2] | 1055 | if (ferror(pimg->fh)) { |
---|
[ad9a5cd] | 1056 | img_errno = IMG_READERROR; |
---|
| 1057 | return img_BAD; |
---|
| 1058 | } |
---|
[0ff23cb] | 1059 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[d69255f] | 1060 | if (*q != '\n') { |
---|
| 1061 | img_errno = IMG_BADFORMAT; |
---|
| 1062 | return img_BAD; |
---|
| 1063 | } |
---|
[9968ebf] | 1064 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 1065 | if (q == pimg->label_buf) goto again; |
---|
[fe8e80e] | 1066 | *q = '\0'; |
---|
[509e099] | 1067 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[437caf3] | 1068 | if (opt == 2) goto done; |
---|
| 1069 | break; |
---|
[23f7ea7] | 1070 | } |
---|
[95c3272] | 1071 | case 6: case 7: { |
---|
[ad9a5cd] | 1072 | long len; |
---|
[23f7ea7] | 1073 | result = img_LABEL; |
---|
[ad9a5cd] | 1074 | |
---|
[509e099] | 1075 | if (opt == 7) |
---|
| 1076 | pimg->flags = getc(pimg->fh); |
---|
| 1077 | else |
---|
| 1078 | pimg->flags = img_SFLAG_UNDERGROUND; /* no flags given... */ |
---|
[ad9a5cd] | 1079 | |
---|
[23f7ea7] | 1080 | len = get32(pimg->fh); |
---|
[ad9a5cd] | 1081 | |
---|
| 1082 | if (feof(pimg->fh)) { |
---|
| 1083 | img_errno = IMG_BADFORMAT; |
---|
| 1084 | return img_BAD; |
---|
| 1085 | } |
---|
| 1086 | if (ferror(pimg->fh)) { |
---|
| 1087 | img_errno = IMG_READERROR; |
---|
| 1088 | return img_BAD; |
---|
| 1089 | } |
---|
| 1090 | |
---|
[9968ebf] | 1091 | /* Ignore empty labels in some .3d files (caused by a bug) */ |
---|
| 1092 | if (len == 0) goto again; |
---|
[d4d3655] | 1093 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1094 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1095 | return img_BAD; |
---|
[23f7ea7] | 1096 | } |
---|
[0ff23cb] | 1097 | if (fread(pimg->label_buf, len, 1, pimg->fh) != 1) { |
---|
[ad9a5cd] | 1098 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 1099 | return img_BAD; |
---|
| 1100 | } |
---|
[c3e5ba5] | 1101 | pimg->label_buf[len] = '\0'; |
---|
[23f7ea7] | 1102 | break; |
---|
| 1103 | } |
---|
[a420b49] | 1104 | case 4: |
---|
| 1105 | result = img_MOVE; |
---|
| 1106 | break; |
---|
| 1107 | case 5: |
---|
| 1108 | result = img_LINE; |
---|
| 1109 | break; |
---|
| 1110 | default: |
---|
[95c3272] | 1111 | switch ((int)opt & 0xc0) { |
---|
| 1112 | case 0x80: |
---|
| 1113 | pimg->flags = (int)opt & 0x3f; |
---|
| 1114 | result = img_LINE; |
---|
| 1115 | break; |
---|
| 1116 | case 0x40: { |
---|
| 1117 | char *q; |
---|
| 1118 | pimg->flags = (int)opt & 0x3f; |
---|
| 1119 | result = img_LABEL; |
---|
[0ff23cb] | 1120 | if (!fgets(pimg->label_buf, 257, pimg->fh)) { |
---|
[ad9a5cd] | 1121 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[d69255f] | 1122 | return img_BAD; |
---|
| 1123 | } |
---|
[0ff23cb] | 1124 | q = pimg->label_buf + strlen(pimg->label_buf) - 1; |
---|
[9968ebf] | 1125 | /* Ignore empty-labels in some .3d files (caused by a bug) */ |
---|
[0ff23cb] | 1126 | if (q == pimg->label_buf) goto again; |
---|
[d69255f] | 1127 | if (*q != '\n') { |
---|
| 1128 | img_errno = IMG_BADFORMAT; |
---|
| 1129 | return img_BAD; |
---|
| 1130 | } |
---|
[95c3272] | 1131 | *q = '\0'; |
---|
| 1132 | break; |
---|
| 1133 | } |
---|
| 1134 | case 0xc0: |
---|
| 1135 | /* use this for an extra leg or station flag if we need it */ |
---|
| 1136 | default: |
---|
[d69255f] | 1137 | img_errno = IMG_BADFORMAT; |
---|
[95c3272] | 1138 | return img_BAD; |
---|
| 1139 | } |
---|
[437caf3] | 1140 | break; |
---|
[a420b49] | 1141 | } |
---|
[ad9a5cd] | 1142 | |
---|
| 1143 | if (!read_coord(pimg->fh, &pt)) return img_BAD; |
---|
[76bbb7c9] | 1144 | |
---|
[ed0f5b6] | 1145 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 1146 | if (strncmp(pimg->label_buf, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 1147 | goto again; |
---|
| 1148 | pimg->label += pimg->survey_len + 1; |
---|
| 1149 | } |
---|
[76bbb7c9] | 1150 | |
---|
[a420b49] | 1151 | done: |
---|
[ad9a5cd] | 1152 | *p = pt; |
---|
[c4d4649] | 1153 | |
---|
| 1154 | if (result == img_MOVE && pimg->version == 1) { |
---|
[d69255f] | 1155 | /* peek at next code and see if it's an old-style label */ |
---|
[23f7ea7] | 1156 | opt_lookahead = get32(pimg->fh); |
---|
[ad9a5cd] | 1157 | |
---|
| 1158 | if (feof(pimg->fh)) { |
---|
| 1159 | img_errno = IMG_BADFORMAT; |
---|
| 1160 | return img_BAD; |
---|
| 1161 | } |
---|
| 1162 | if (ferror(pimg->fh)) { |
---|
| 1163 | img_errno = IMG_READERROR; |
---|
| 1164 | return img_BAD; |
---|
| 1165 | } |
---|
| 1166 | |
---|
[d69255f] | 1167 | if (opt_lookahead == 2) return img_read_item(pimg, p); |
---|
[c4d4649] | 1168 | } |
---|
[23f7ea7] | 1169 | |
---|
[a420b49] | 1170 | return result; |
---|
[898bc69] | 1171 | } else { |
---|
| 1172 | /* We need to set the default locale for fscanf() to work on |
---|
| 1173 | * numbers with "." as decimal point. */ |
---|
| 1174 | char * current_locale = my_strdup(setlocale(LC_NUMERIC, NULL)); |
---|
| 1175 | setlocale(LC_NUMERIC, "C"); |
---|
| 1176 | result = img_read_item_ascii(pimg, p); |
---|
| 1177 | setlocale(LC_NUMERIC, current_locale); |
---|
| 1178 | free(current_locale); |
---|
| 1179 | return result; |
---|
| 1180 | } |
---|
| 1181 | } |
---|
[eef68f9] | 1182 | |
---|
[898bc69] | 1183 | /* Handle all for ASCII formats so we can easily fix up the locale |
---|
| 1184 | * so fscanf(), etc work on numbers with "." as decimal point. */ |
---|
| 1185 | static int |
---|
| 1186 | img_read_item_ascii(img *pimg, img_point *p) |
---|
| 1187 | { |
---|
| 1188 | int result; |
---|
| 1189 | if (pimg->version == 0) { |
---|
[a420b49] | 1190 | ascii_again: |
---|
[5d7280f8] | 1191 | pimg->label[0] = '\0'; |
---|
[a420b49] | 1192 | if (feof(pimg->fh)) return img_STOP; |
---|
[76bbb7c9] | 1193 | if (pimg->pending) { |
---|
| 1194 | pimg->pending = 0; |
---|
[a420b49] | 1195 | result = img_LINE; |
---|
| 1196 | } else { |
---|
[7d27e36] | 1197 | char cmd[7]; |
---|
[a420b49] | 1198 | /* Stop if nothing found */ |
---|
[7d27e36] | 1199 | if (fscanf(pimg->fh, "%6s", cmd) < 1) return img_STOP; |
---|
| 1200 | if (strcmp(cmd, "move") == 0) |
---|
[a420b49] | 1201 | result = img_MOVE; |
---|
[7d27e36] | 1202 | else if (strcmp(cmd, "draw") == 0) |
---|
[a420b49] | 1203 | result = img_LINE; |
---|
[7d27e36] | 1204 | else if (strcmp(cmd, "line") == 0) { |
---|
[bd1913f] | 1205 | /* set flag to indicate to process second triplet as LINE */ |
---|
[76bbb7c9] | 1206 | pimg->pending = 1; |
---|
[a420b49] | 1207 | result = img_MOVE; |
---|
[7d27e36] | 1208 | } else if (strcmp(cmd, "cross") == 0) { |
---|
[d69255f] | 1209 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1210 | img_errno = feof(pimg->fh) ? IMG_BADFORMAT : IMG_READERROR; |
---|
[0ed0e16] | 1211 | return img_BAD; |
---|
[d69255f] | 1212 | } |
---|
[a420b49] | 1213 | goto ascii_again; |
---|
[7d27e36] | 1214 | } else if (strcmp(cmd, "name") == 0) { |
---|
[0da6e60] | 1215 | size_t off = 0; |
---|
[58f02ae] | 1216 | int ch = getc(pimg->fh); |
---|
| 1217 | if (ch == ' ') ch = getc(pimg->fh); |
---|
| 1218 | while (ch != ' ') { |
---|
| 1219 | if (ch == '\n' || ch == EOF) { |
---|
| 1220 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[ad9a5cd] | 1221 | return img_BAD; |
---|
| 1222 | } |
---|
[58f02ae] | 1223 | if (off == pimg->buf_len) { |
---|
[d4d3655] | 1224 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1225 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1226 | return img_BAD; |
---|
| 1227 | } |
---|
[0da6e60] | 1228 | } |
---|
[58f02ae] | 1229 | pimg->label_buf[off++] = ch; |
---|
| 1230 | ch = getc(pimg->fh); |
---|
[ad9a5cd] | 1231 | } |
---|
[58f02ae] | 1232 | pimg->label_buf[off] = '\0'; |
---|
| 1233 | |
---|
| 1234 | pimg->label = pimg->label_buf; |
---|
| 1235 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[0da6e60] | 1236 | |
---|
[1182e94] | 1237 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
| 1238 | |
---|
[a420b49] | 1239 | result = img_LABEL; |
---|
[d69255f] | 1240 | } else { |
---|
| 1241 | img_errno = IMG_BADFORMAT; |
---|
[a420b49] | 1242 | return img_BAD; /* unknown keyword */ |
---|
[d69255f] | 1243 | } |
---|
[a420b49] | 1244 | } |
---|
[d1b1380] | 1245 | |
---|
[d69255f] | 1246 | if (fscanf(pimg->fh, "%lf%lf%lf", &p->x, &p->y, &p->z) < 3) { |
---|
[ad9a5cd] | 1247 | img_errno = ferror(pimg->fh) ? IMG_READERROR : IMG_BADFORMAT; |
---|
[fe8e80e] | 1248 | return img_BAD; |
---|
[d69255f] | 1249 | } |
---|
| 1250 | |
---|
[58f02ae] | 1251 | if (result == img_LABEL && pimg->survey_len) { |
---|
| 1252 | if (strncmp(pimg->label, pimg->survey, pimg->survey_len + 1) != 0) |
---|
[0ff23cb] | 1253 | goto ascii_again; |
---|
| 1254 | pimg->label += pimg->survey_len + 1; |
---|
| 1255 | } |
---|
[76bbb7c9] | 1256 | |
---|
[a420b49] | 1257 | return result; |
---|
[db9e5bf] | 1258 | } else if (pimg->version == VERSION_SURVEX_POS) { |
---|
| 1259 | /* Survex .pos file */ |
---|
[58f02ae] | 1260 | size_t off; |
---|
[5eea574] | 1261 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[58f02ae] | 1262 | againpos: |
---|
| 1263 | off = 0; |
---|
[0da6e60] | 1264 | while (fscanf(pimg->fh, "(%lf,%lf,%lf ) ", &p->x, &p->y, &p->z) != 3) { |
---|
| 1265 | int ch; |
---|
| 1266 | if (ferror(pimg->fh)) { |
---|
| 1267 | img_errno = IMG_READERROR; |
---|
| 1268 | return img_BAD; |
---|
| 1269 | } |
---|
| 1270 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1271 | if (pimg->pending) { |
---|
| 1272 | img_errno = IMG_BADFORMAT; |
---|
| 1273 | return img_BAD; |
---|
| 1274 | } |
---|
| 1275 | pimg->pending = 1; |
---|
| 1276 | /* ignore rest of line */ |
---|
| 1277 | do { |
---|
| 1278 | ch = getc(pimg->fh); |
---|
| 1279 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1280 | } |
---|
| 1281 | |
---|
| 1282 | pimg->label_buf[0] = '\0'; |
---|
| 1283 | while (!feof(pimg->fh)) { |
---|
| 1284 | if (!fgets(pimg->label_buf + off, pimg->buf_len - off, pimg->fh)) { |
---|
| 1285 | img_errno = IMG_READERROR; |
---|
| 1286 | return img_BAD; |
---|
| 1287 | } |
---|
| 1288 | |
---|
| 1289 | off += strlen(pimg->label_buf + off); |
---|
| 1290 | if (off && pimg->label_buf[off - 1] == '\n') { |
---|
| 1291 | pimg->label_buf[off - 1] = '\0'; |
---|
| 1292 | break; |
---|
| 1293 | } |
---|
[d4d3655] | 1294 | if (!check_label_space(pimg, pimg->buf_len * 2)) { |
---|
[9b56f4d] | 1295 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1296 | return img_BAD; |
---|
| 1297 | } |
---|
[0da6e60] | 1298 | } |
---|
| 1299 | |
---|
[58f02ae] | 1300 | pimg->label = pimg->label_buf; |
---|
| 1301 | |
---|
[421b7d2] | 1302 | if (pimg->label[0] == '\\') pimg->label++; |
---|
[58f02ae] | 1303 | |
---|
| 1304 | if (pimg->survey_len) { |
---|
| 1305 | size_t l = pimg->survey_len + 1; |
---|
| 1306 | if (strncmp(pimg->survey, pimg->label, l) != 0) goto againpos; |
---|
| 1307 | pimg->label += l; |
---|
| 1308 | } |
---|
| 1309 | |
---|
[0da6e60] | 1310 | return img_LABEL; |
---|
[db9e5bf] | 1311 | } else if (pimg->version == VERSION_COMPASS_PLT) { |
---|
| 1312 | /* Compass .plt file */ |
---|
[4ebc557] | 1313 | if (pimg->pending > 0) { |
---|
| 1314 | /* -1 signals we've entered the first survey we want to |
---|
| 1315 | * read, and need to fudge lots if the first action is 'D'... |
---|
| 1316 | */ |
---|
| 1317 | /* pending MOVE or LINE */ |
---|
[984f49e] | 1318 | int r = pimg->pending - 4; |
---|
[ad4eaf3f] | 1319 | pimg->pending = 0; |
---|
| 1320 | pimg->flags = 0; |
---|
[f710a3f] | 1321 | pimg->label[pimg->label_len] = '\0'; |
---|
[ad4eaf3f] | 1322 | return r; |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | while (1) { |
---|
[4ebc557] | 1326 | char *line; |
---|
[ad4eaf3f] | 1327 | char *q; |
---|
| 1328 | size_t len = 0; |
---|
[4ebc557] | 1329 | int ch = getc(pimg->fh); |
---|
| 1330 | |
---|
| 1331 | switch (ch) { |
---|
| 1332 | case '\x1a': case EOF: /* Don't insist on ^Z at end of file */ |
---|
| 1333 | return img_STOP; |
---|
[27bd06b] | 1334 | case 'X': case 'F': case 'S': |
---|
| 1335 | /* bounding boX (marks end of survey), Feature survey, or |
---|
| 1336 | * new Section - skip to next survey */ |
---|
| 1337 | if (pimg->survey) return img_STOP; |
---|
| 1338 | skip_to_N: |
---|
| 1339 | while (1) { |
---|
| 1340 | do { |
---|
| 1341 | ch = getc(pimg->fh); |
---|
| 1342 | } while (ch != '\n' && ch != '\r' && ch != EOF); |
---|
| 1343 | while (ch == '\n' || ch == '\r') ch = getc(pimg->fh); |
---|
| 1344 | if (ch == 'N') break; |
---|
| 1345 | if (ch == '\x1a' || ch == EOF) return img_STOP; |
---|
| 1346 | } |
---|
| 1347 | /* FALLTHRU */ |
---|
[4ebc557] | 1348 | case 'N': |
---|
| 1349 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1350 | if (!line) { |
---|
| 1351 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1352 | return img_BAD; |
---|
| 1353 | } |
---|
[ee1ec59] | 1354 | while (line[len] > 32) ++len; |
---|
[ef59f5e] | 1355 | if (pimg->label_len == 0) pimg->pending = -1; |
---|
[4ebc557] | 1356 | if (!check_label_space(pimg, len + 1)) { |
---|
| 1357 | osfree(line); |
---|
| 1358 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1359 | return img_BAD; |
---|
| 1360 | } |
---|
| 1361 | pimg->label_len = len; |
---|
| 1362 | pimg->label = pimg->label_buf; |
---|
| 1363 | memcpy(pimg->label, line, len); |
---|
| 1364 | pimg->label[len] = '\0'; |
---|
| 1365 | osfree(line); |
---|
| 1366 | break; |
---|
| 1367 | case 'M': case 'D': { |
---|
[ad4eaf3f] | 1368 | /* Move or Draw */ |
---|
[4ebc557] | 1369 | long fpos = -1; |
---|
[27bd06b] | 1370 | if (pimg->survey && pimg->label_len == 0) { |
---|
[4ebc557] | 1371 | /* We're only holding onto this line in case the first line |
---|
[27bd06b] | 1372 | * of the 'N' is a 'D', so skip it for now... |
---|
[4ebc557] | 1373 | */ |
---|
[27bd06b] | 1374 | goto skip_to_N; |
---|
[4ebc557] | 1375 | } |
---|
| 1376 | if (ch == 'D' && pimg->pending == -1) { |
---|
[ef59f5e] | 1377 | if (pimg->survey) { |
---|
| 1378 | fpos = ftell(pimg->fh) - 1; |
---|
| 1379 | fseek(pimg->fh, pimg->start, SEEK_SET); |
---|
| 1380 | ch = getc(pimg->fh); |
---|
| 1381 | pimg->pending = 0; |
---|
| 1382 | } else { |
---|
| 1383 | /* If a file actually has a 'D' before any 'M', then |
---|
| 1384 | * pretend the 'D' is an 'M' - one of the examples |
---|
| 1385 | * in the docs was like this! */ |
---|
| 1386 | ch = 'M'; |
---|
| 1387 | } |
---|
[4ebc557] | 1388 | } |
---|
| 1389 | line = getline_alloc(pimg->fh); |
---|
[f1c7c9c7] | 1390 | if (!line) { |
---|
| 1391 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1392 | return img_BAD; |
---|
| 1393 | } |
---|
[898bc69] | 1394 | /* Compass stores coordinates as North, East, Up = (y,x,z)! */ |
---|
[88c780d] | 1395 | if (sscanf(line, "%lf%lf%lf", &p->y, &p->x, &p->z) != 3) { |
---|
[ad4eaf3f] | 1396 | osfree(line); |
---|
| 1397 | if (ferror(pimg->fh)) { |
---|
| 1398 | img_errno = IMG_READERROR; |
---|
| 1399 | } else { |
---|
| 1400 | img_errno = IMG_BADFORMAT; |
---|
| 1401 | } |
---|
| 1402 | return img_BAD; |
---|
| 1403 | } |
---|
[a9ab65a] | 1404 | p->x *= METRES_PER_FOOT; |
---|
| 1405 | p->y *= METRES_PER_FOOT; |
---|
| 1406 | p->z *= METRES_PER_FOOT; |
---|
[ad4eaf3f] | 1407 | q = strchr(line, 'S'); |
---|
| 1408 | if (!q) { |
---|
| 1409 | osfree(line); |
---|
[5757725] | 1410 | img_errno = IMG_BADFORMAT; |
---|
[ad4eaf3f] | 1411 | return img_BAD; |
---|
| 1412 | } |
---|
| 1413 | ++q; |
---|
| 1414 | len = 0; |
---|
[ee1ec59] | 1415 | while (q[len] > ' ') ++len; |
---|
[ad4eaf3f] | 1416 | q[len] = '\0'; |
---|
[ee1ec59] | 1417 | len += 2; /* ' ' and '\0' */ |
---|
[d4d3655] | 1418 | if (!check_label_space(pimg, pimg->label_len + len)) { |
---|
| 1419 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1420 | return img_BAD; |
---|
[ad4eaf3f] | 1421 | } |
---|
| 1422 | pimg->label = pimg->label_buf; |
---|
[be97baf] | 1423 | if (pimg->label_len) { |
---|
| 1424 | pimg->label[pimg->label_len] = ' '; |
---|
| 1425 | memcpy(pimg->label + pimg->label_len + 1, q, len - 1); |
---|
| 1426 | } else { |
---|
| 1427 | memcpy(pimg->label, q, len - 1); |
---|
| 1428 | } |
---|
[ee7af72] | 1429 | q += len - 1; |
---|
| 1430 | /* Now read LRUD. Technically, this is optional but virtually |
---|
[ee05463] | 1431 | * all PLT files have it (with dummy negative values if no LRUD |
---|
| 1432 | * was measured) and some versions of Compass can't read PLT |
---|
| 1433 | * files without it! |
---|
[ee7af72] | 1434 | */ |
---|
| 1435 | while (*q && *q <= ' ') q++; |
---|
| 1436 | if (*q == 'P') { |
---|
| 1437 | if (sscanf(q + 1, "%lf%lf%lf%lf", |
---|
| 1438 | &pimg->l, &pimg->r, &pimg->u, &pimg->d) != 4) { |
---|
| 1439 | osfree(line); |
---|
| 1440 | if (ferror(pimg->fh)) { |
---|
| 1441 | img_errno = IMG_READERROR; |
---|
| 1442 | } else { |
---|
| 1443 | img_errno = IMG_BADFORMAT; |
---|
| 1444 | } |
---|
| 1445 | return img_BAD; |
---|
| 1446 | } |
---|
| 1447 | pimg->l *= METRES_PER_FOOT; |
---|
| 1448 | pimg->r *= METRES_PER_FOOT; |
---|
| 1449 | pimg->u *= METRES_PER_FOOT; |
---|
| 1450 | pimg->d *= METRES_PER_FOOT; |
---|
| 1451 | } else { |
---|
| 1452 | pimg->l = pimg->r = pimg->u = pimg->d = -1; |
---|
| 1453 | } |
---|
[ad4eaf3f] | 1454 | osfree(line); |
---|
| 1455 | pimg->flags = img_SFLAG_UNDERGROUND; /* default flags */ |
---|
[0d922fc] | 1456 | if (fpos != -1) { |
---|
| 1457 | fseek(pimg->fh, fpos, SEEK_SET); |
---|
| 1458 | } else { |
---|
| 1459 | pimg->pending = (ch == 'M' ? img_MOVE : img_LINE) + 4; |
---|
| 1460 | } |
---|
[ad4eaf3f] | 1461 | return img_LABEL; |
---|
[4ebc557] | 1462 | } |
---|
[ad4eaf3f] | 1463 | default: |
---|
| 1464 | img_errno = IMG_BADFORMAT; |
---|
| 1465 | return img_BAD; |
---|
| 1466 | } |
---|
| 1467 | } |
---|
[f1c7c9c7] | 1468 | } else { |
---|
[db9e5bf] | 1469 | /* CMAP .xyz file */ |
---|
[f1c7c9c7] | 1470 | char *line = NULL; |
---|
| 1471 | char *q; |
---|
| 1472 | size_t len; |
---|
| 1473 | |
---|
| 1474 | if (pimg->pending) { |
---|
| 1475 | /* pending MOVE or LINE or LABEL or STOP */ |
---|
| 1476 | int r = pimg->pending - 4; |
---|
[c3e5ba5] | 1477 | /* Set label to empty - don't use "" as we adjust label relative |
---|
| 1478 | * to label_buf when label_buf is reallocated. */ |
---|
| 1479 | pimg->label = pimg->label_buf + strlen(pimg->label_buf); |
---|
[f1c7c9c7] | 1480 | pimg->flags = 0; |
---|
| 1481 | if (r == img_LABEL) { |
---|
| 1482 | /* nasty magic */ |
---|
| 1483 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1484 | subtract_xyz_shot_deltas(p, pimg->label_buf + 16); |
---|
| 1485 | pimg->pending = img_STOP + 4; |
---|
| 1486 | return img_MOVE; |
---|
| 1487 | } |
---|
[6d4c4b9] | 1488 | |
---|
[f1c7c9c7] | 1489 | pimg->pending = 0; |
---|
| 1490 | |
---|
| 1491 | if (r == img_STOP) { |
---|
| 1492 | /* nasty magic */ |
---|
| 1493 | read_xyz_shot_coords(p, pimg->label_buf + 16); |
---|
| 1494 | return img_LINE; |
---|
| 1495 | } |
---|
[ee05463] | 1496 | |
---|
[f1c7c9c7] | 1497 | return r; |
---|
| 1498 | } |
---|
| 1499 | |
---|
| 1500 | pimg->label = pimg->label_buf; |
---|
| 1501 | do { |
---|
| 1502 | osfree(line); |
---|
| 1503 | if (feof(pimg->fh)) return img_STOP; |
---|
| 1504 | line = getline_alloc(pimg->fh); |
---|
| 1505 | if (!line) { |
---|
| 1506 | img_errno = IMG_OUTOFMEMORY; |
---|
| 1507 | return img_BAD; |
---|
| 1508 | } |
---|
| 1509 | } while (line[0] == ' ' || line[0] == '\0'); |
---|
| 1510 | if (line[0] == '\x1a') return img_STOP; |
---|
| 1511 | |
---|
| 1512 | len = strlen(line); |
---|
[db9e5bf] | 1513 | if (pimg->version == VERSION_CMAP_STATION) { |
---|
[f1c7c9c7] | 1514 | /* station variant */ |
---|
| 1515 | if (len < 37) { |
---|
| 1516 | osfree(line); |
---|
| 1517 | img_errno = IMG_BADFORMAT; |
---|
| 1518 | return img_BAD; |
---|
| 1519 | } |
---|
| 1520 | memcpy(pimg->label, line, 6); |
---|
| 1521 | q = memchr(pimg->label, ' ', 6); |
---|
| 1522 | if (!q) q = pimg->label + 6; |
---|
| 1523 | *q = '\0'; |
---|
| 1524 | |
---|
| 1525 | read_xyz_station_coords(p, line); |
---|
[6d4c4b9] | 1526 | |
---|
[f1c7c9c7] | 1527 | /* FIXME: look at prev for lines (line + 32, 5) */ |
---|
| 1528 | /* FIXME: duplicate stations... */ |
---|
| 1529 | return img_LABEL; |
---|
| 1530 | } else { |
---|
| 1531 | /* Shot variant */ |
---|
| 1532 | char old[8], new[8]; |
---|
| 1533 | if (len < 61) { |
---|
| 1534 | osfree(line); |
---|
| 1535 | img_errno = IMG_BADFORMAT; |
---|
| 1536 | return img_BAD; |
---|
| 1537 | } |
---|
[6d4c4b9] | 1538 | |
---|
[f1c7c9c7] | 1539 | memcpy(old, line, 7); |
---|
| 1540 | q = memchr(old, ' ', 7); |
---|
| 1541 | if (!q) q = old + 7; |
---|
[6d4c4b9] | 1542 | *q = '\0'; |
---|
[ee1ec59] | 1543 | |
---|
[f1c7c9c7] | 1544 | memcpy(new, line + 7, 7); |
---|
| 1545 | q = memchr(new, ' ', 7); |
---|
| 1546 | if (!q) q = new + 7; |
---|
[eef68f9] | 1547 | *q = '\0'; |
---|
[ee1ec59] | 1548 | |
---|
[f1c7c9c7] | 1549 | pimg->flags = img_SFLAG_UNDERGROUND; |
---|
| 1550 | |
---|
| 1551 | if (strcmp(old, new) == 0) { |
---|
| 1552 | pimg->pending = img_MOVE + 4; |
---|
| 1553 | read_xyz_shot_coords(p, line); |
---|
| 1554 | strcpy(pimg->label, new); |
---|
| 1555 | osfree(line); |
---|
| 1556 | return img_LABEL; |
---|
| 1557 | } |
---|
[6d4c4b9] | 1558 | |
---|
[f1c7c9c7] | 1559 | if (strcmp(old, pimg->label) == 0) { |
---|
| 1560 | pimg->pending = img_LINE + 4; |
---|
| 1561 | read_xyz_shot_coords(p, line); |
---|
| 1562 | strcpy(pimg->label, new); |
---|
| 1563 | osfree(line); |
---|
| 1564 | return img_LABEL; |
---|
| 1565 | } |
---|
[6d4c4b9] | 1566 | |
---|
[f1c7c9c7] | 1567 | pimg->pending = img_LABEL + 4; |
---|
| 1568 | read_xyz_shot_coords(p, line); |
---|
| 1569 | strcpy(pimg->label, new); |
---|
| 1570 | memcpy(pimg->label + 16, line, 70); |
---|
| 1571 | |
---|
| 1572 | osfree(line); |
---|
| 1573 | return img_LABEL; |
---|
| 1574 | } |
---|
[a420b49] | 1575 | } |
---|
[d1b1380] | 1576 | } |
---|
| 1577 | |
---|
[ad9a5cd] | 1578 | static int |
---|
[d69255f] | 1579 | write_v3label(img *pimg, int opt, const char *s) |
---|
| 1580 | { |
---|
| 1581 | size_t len, n, dot; |
---|
| 1582 | |
---|
| 1583 | /* find length of common prefix */ |
---|
| 1584 | dot = 0; |
---|
[0ff23cb] | 1585 | for (len = 0; s[len] == pimg->label_buf[len] && s[len] != '\0'; len++) { |
---|
[d69255f] | 1586 | if (s[len] == '.') dot = len + 1; |
---|
| 1587 | } |
---|
| 1588 | |
---|
[4c07c51] | 1589 | SVX_ASSERT(len <= pimg->label_len); |
---|
[d69255f] | 1590 | n = pimg->label_len - len; |
---|
| 1591 | if (len == 0) { |
---|
| 1592 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1593 | } else if (n <= 16) { |
---|
| 1594 | if (n) putc(n + 15, pimg->fh); |
---|
| 1595 | } else if (dot == 0) { |
---|
| 1596 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[99cf51a] | 1597 | len = 0; |
---|
[d69255f] | 1598 | } else { |
---|
[0ff23cb] | 1599 | const char *p = pimg->label_buf + dot; |
---|
[d69255f] | 1600 | n = 1; |
---|
| 1601 | for (len = pimg->label_len - dot - 17; len; len--) { |
---|
| 1602 | if (*p++ == '.') n++; |
---|
| 1603 | } |
---|
| 1604 | if (n <= 14) { |
---|
| 1605 | putc(n, pimg->fh); |
---|
| 1606 | len = dot; |
---|
| 1607 | } else { |
---|
| 1608 | if (pimg->label_len) putc(0, pimg->fh); |
---|
| 1609 | len = 0; |
---|
| 1610 | } |
---|
| 1611 | } |
---|
| 1612 | |
---|
| 1613 | n = strlen(s + len); |
---|
| 1614 | putc(opt, pimg->fh); |
---|
| 1615 | if (n < 0xfe) { |
---|
| 1616 | putc(n, pimg->fh); |
---|
| 1617 | } else if (n < 0xffff + 0xfe) { |
---|
| 1618 | putc(0xfe, pimg->fh); |
---|
[63dc4eb] | 1619 | put16((short)(n - 0xfe), pimg->fh); |
---|
[d69255f] | 1620 | } else { |
---|
| 1621 | putc(0xff, pimg->fh); |
---|
| 1622 | put32(n, pimg->fh); |
---|
| 1623 | } |
---|
| 1624 | fwrite(s + len, n, 1, pimg->fh); |
---|
| 1625 | |
---|
| 1626 | n += len; |
---|
| 1627 | pimg->label_len = n; |
---|
[d4d3655] | 1628 | if (!check_label_space(pimg, n + 1)) |
---|
| 1629 | return 0; /* FIXME: distinguish out of memory... */ |
---|
[0ff23cb] | 1630 | memcpy(pimg->label_buf + len, s + len, n - len + 1); |
---|
[ad9a5cd] | 1631 | |
---|
| 1632 | return !ferror(pimg->fh); |
---|
[d69255f] | 1633 | } |
---|
| 1634 | |
---|
[d4650b3] | 1635 | static void |
---|
| 1636 | img_write_item_date(img *pimg) |
---|
| 1637 | { |
---|
| 1638 | if (pimg->date1 != pimg->olddate1 || |
---|
| 1639 | pimg->date2 != pimg->olddate2) { |
---|
| 1640 | /* Only write dates when they've changed. */ |
---|
| 1641 | if (pimg->date1 == pimg->date2) { |
---|
| 1642 | putc(0x20, pimg->fh); |
---|
| 1643 | put32(pimg->date1, pimg->fh); |
---|
| 1644 | } else { |
---|
| 1645 | putc(0x21, pimg->fh); |
---|
| 1646 | put32(pimg->date1, pimg->fh); |
---|
| 1647 | put32(pimg->date2, pimg->fh); |
---|
| 1648 | } |
---|
| 1649 | pimg->olddate1 = pimg->date1; |
---|
| 1650 | pimg->olddate2 = pimg->date2; |
---|
| 1651 | } |
---|
| 1652 | } |
---|
| 1653 | |
---|
[a420b49] | 1654 | void |
---|
[95c3272] | 1655 | img_write_item(img *pimg, int code, int flags, const char *s, |
---|
[a9f5117] | 1656 | double x, double y, double z) |
---|
[a420b49] | 1657 | { |
---|
[647407d] | 1658 | if (!pimg) return; |
---|
[b5a3219] | 1659 | if (pimg->version >= 3) { |
---|
[d69255f] | 1660 | int opt = 0; |
---|
| 1661 | switch (code) { |
---|
| 1662 | case img_LABEL: |
---|
| 1663 | write_v3label(pimg, 0x40 | flags, s); |
---|
| 1664 | opt = 0; |
---|
| 1665 | break; |
---|
[ee05463] | 1666 | case img_XSECT: { |
---|
[d4650b3] | 1667 | INT32_T l, r, u, d, max_dim; |
---|
| 1668 | /* Need at least version 5 for img_XSECT. */ |
---|
| 1669 | if (pimg->version < 5) break; |
---|
| 1670 | img_write_item_date(pimg); |
---|
| 1671 | l = (INT32_T)my_round(pimg->l * 100.0); |
---|
| 1672 | r = (INT32_T)my_round(pimg->r * 100.0); |
---|
| 1673 | u = (INT32_T)my_round(pimg->u * 100.0); |
---|
| 1674 | d = (INT32_T)my_round(pimg->d * 100.0); |
---|
[ee05463] | 1675 | if (l < 0) l = -1; |
---|
| 1676 | if (r < 0) r = -1; |
---|
| 1677 | if (u < 0) u = -1; |
---|
| 1678 | if (d < 0) d = -1; |
---|
[d4650b3] | 1679 | max_dim = max(max(l, r), max(u, d)); |
---|
[ee05463] | 1680 | flags &= 1; |
---|
| 1681 | if (max_dim >= 32768) flags |= 2; |
---|
| 1682 | write_v3label(pimg, 0x30 | flags, s); |
---|
| 1683 | if (flags & 2) { |
---|
| 1684 | /* Big passage! Need to use 4 bytes. */ |
---|
| 1685 | put32(l, pimg->fh); |
---|
| 1686 | put32(r, pimg->fh); |
---|
| 1687 | put32(u, pimg->fh); |
---|
| 1688 | put32(d, pimg->fh); |
---|
| 1689 | } else { |
---|
| 1690 | put16(l, pimg->fh); |
---|
| 1691 | put16(r, pimg->fh); |
---|
| 1692 | put16(u, pimg->fh); |
---|
| 1693 | put16(d, pimg->fh); |
---|
| 1694 | } |
---|
| 1695 | return; |
---|
| 1696 | } |
---|
[d69255f] | 1697 | case img_MOVE: |
---|
| 1698 | opt = 15; |
---|
| 1699 | break; |
---|
| 1700 | case img_LINE: |
---|
[ee05463] | 1701 | if (pimg->version >= 4) { |
---|
[d4650b3] | 1702 | img_write_item_date(pimg); |
---|
[32f05df] | 1703 | } |
---|
[d69255f] | 1704 | write_v3label(pimg, 0x80 | flags, s ? s : ""); |
---|
| 1705 | opt = 0; |
---|
| 1706 | break; |
---|
| 1707 | default: /* ignore for now */ |
---|
| 1708 | return; |
---|
| 1709 | } |
---|
| 1710 | if (opt) putc(opt, pimg->fh); |
---|
| 1711 | /* Output in cm */ |
---|
[dd1d6369] | 1712 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1713 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1714 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[d69255f] | 1715 | } else { |
---|
[23f7ea7] | 1716 | size_t len; |
---|
[badeec8] | 1717 | INT32_T opt = 0; |
---|
[4c07c51] | 1718 | SVX_ASSERT(pimg->version > 0); |
---|
[a420b49] | 1719 | switch (code) { |
---|
[437caf3] | 1720 | case img_LABEL: |
---|
| 1721 | if (pimg->version == 1) { |
---|
| 1722 | /* put a move before each label */ |
---|
[95c3272] | 1723 | img_write_item(pimg, img_MOVE, 0, NULL, x, y, z); |
---|
[437caf3] | 1724 | put32(2, pimg->fh); |
---|
[0ed0e16] | 1725 | fputsnl(s, pimg->fh); |
---|
[437caf3] | 1726 | return; |
---|
| 1727 | } |
---|
[23f7ea7] | 1728 | len = strlen(s); |
---|
[af56069] | 1729 | if (len > 255 || strchr(s, '\n')) { |
---|
[23f7ea7] | 1730 | /* long label - not in early incarnations of v2 format, but few |
---|
| 1731 | * 3d files will need these, so better not to force incompatibility |
---|
| 1732 | * with a new version I think... */ |
---|
[95c3272] | 1733 | putc(7, pimg->fh); |
---|
| 1734 | putc(flags, pimg->fh); |
---|
[23f7ea7] | 1735 | put32(len, pimg->fh); |
---|
| 1736 | fputs(s, pimg->fh); |
---|
| 1737 | } else { |
---|
[95c3272] | 1738 | putc(0x40 | (flags & 0x3f), pimg->fh); |
---|
[23f7ea7] | 1739 | fputsnl(s, pimg->fh); |
---|
| 1740 | } |
---|
[437caf3] | 1741 | opt = 0; |
---|
[23f7ea7] | 1742 | break; |
---|
[a420b49] | 1743 | case img_MOVE: |
---|
| 1744 | opt = 4; |
---|
| 1745 | break; |
---|
| 1746 | case img_LINE: |
---|
[421b7d2] | 1747 | if (pimg->version > 1) { |
---|
[d69255f] | 1748 | opt = 0x80 | (flags & 0x3f); |
---|
[437caf3] | 1749 | break; |
---|
[421b7d2] | 1750 | } |
---|
[437caf3] | 1751 | opt = 5; |
---|
[a420b49] | 1752 | break; |
---|
[437caf3] | 1753 | default: /* ignore for now */ |
---|
| 1754 | return; |
---|
[a420b49] | 1755 | } |
---|
[437caf3] | 1756 | if (pimg->version == 1) { |
---|
| 1757 | put32(opt, pimg->fh); |
---|
| 1758 | } else { |
---|
| 1759 | if (opt) putc(opt, pimg->fh); |
---|
[a420b49] | 1760 | } |
---|
[d69255f] | 1761 | /* Output in cm */ |
---|
[dd1d6369] | 1762 | put32((INT32_T)my_round(x * 100.0), pimg->fh); |
---|
| 1763 | put32((INT32_T)my_round(y * 100.0), pimg->fh); |
---|
| 1764 | put32((INT32_T)my_round(z * 100.0), pimg->fh); |
---|
[a420b49] | 1765 | } |
---|
[d1b1380] | 1766 | } |
---|
| 1767 | |
---|
[c61aa79] | 1768 | /* Write error information for the current traverse |
---|
| 1769 | * n_legs is the number of legs in the traverse |
---|
| 1770 | * length is the traverse length (in m) |
---|
| 1771 | * E is the ratio of the observed misclosure to the theoretical one |
---|
| 1772 | * H is the ratio of the observed horizontal misclosure to the theoretical one |
---|
| 1773 | * V is the ratio of the observed vertical misclosure to the theoretical one |
---|
| 1774 | */ |
---|
| 1775 | void |
---|
| 1776 | img_write_errors(img *pimg, int n_legs, double length, |
---|
| 1777 | double E, double H, double V) |
---|
| 1778 | { |
---|
| 1779 | putc(0x22, pimg->fh); |
---|
| 1780 | put32(n_legs, pimg->fh); |
---|
| 1781 | put32((INT32_T)my_round(length * 100.0), pimg->fh); |
---|
| 1782 | put32((INT32_T)my_round(E * 100.0), pimg->fh); |
---|
| 1783 | put32((INT32_T)my_round(H * 100.0), pimg->fh); |
---|
| 1784 | put32((INT32_T)my_round(V * 100.0), pimg->fh); |
---|
| 1785 | } |
---|
| 1786 | |
---|
[ad9a5cd] | 1787 | int |
---|
[a420b49] | 1788 | img_close(img *pimg) |
---|
| 1789 | { |
---|
[ad9a5cd] | 1790 | int result = 1; |
---|
[dbb4e19] | 1791 | if (pimg) { |
---|
| 1792 | if (pimg->fh) { |
---|
[76bbb7c9] | 1793 | if (pimg->fRead) { |
---|
| 1794 | osfree(pimg->survey); |
---|
[a2ad284] | 1795 | osfree(pimg->title); |
---|
| 1796 | osfree(pimg->datestamp); |
---|
[76bbb7c9] | 1797 | } else { |
---|
| 1798 | /* write end of data marker */ |
---|
[d69255f] | 1799 | switch (pimg->version) { |
---|
| 1800 | case 1: |
---|
[5c3c61a] | 1801 | put32((INT32_T)-1, pimg->fh); |
---|
[d69255f] | 1802 | break; |
---|
| 1803 | case 2: |
---|
| 1804 | putc(0, pimg->fh); |
---|
| 1805 | break; |
---|
[ee05463] | 1806 | default: |
---|
[d69255f] | 1807 | if (pimg->label_len) putc(0, pimg->fh); |
---|
[437caf3] | 1808 | putc(0, pimg->fh); |
---|
[d69255f] | 1809 | break; |
---|
[5c3c61a] | 1810 | } |
---|
| 1811 | } |
---|
[ad9a5cd] | 1812 | if (ferror(pimg->fh)) result = 0; |
---|
| 1813 | if (fclose(pimg->fh)) result = 0; |
---|
[0d922fc] | 1814 | if (!result) img_errno = pimg->fRead ? IMG_READERROR : IMG_WRITEERROR; |
---|
[dbb4e19] | 1815 | } |
---|
[0ff23cb] | 1816 | osfree(pimg->label_buf); |
---|
[452e200] | 1817 | osfree(pimg->filename_opened); |
---|
[dbb4e19] | 1818 | osfree(pimg); |
---|
[a420b49] | 1819 | } |
---|
[ad9a5cd] | 1820 | return result; |
---|
[d1b1380] | 1821 | } |
---|