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