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