-
Notifications
You must be signed in to change notification settings - Fork 2
/
img.c
2773 lines (2585 loc) · 69.8 KB
/
img.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* img.c
* Routines for reading and writing Survex ".3d" image files
* Copyright (C) 1993-2004,2005,2006,2010,2011,2013,2014,2017,2018 Olly Betts
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "img.h"
#define TIMENA "?"
#ifdef IMG_HOSTED
# define INT32_T int32_t
# include "debug.h"
# include "filelist.h"
# include "filename.h"
# include "message.h"
# include "useful.h"
# define TIMEFMT msg(/*%a,%Y.%m.%d %H:%M:%S %Z*/107)
#else
# ifdef HAVE_STDINT_H
# include <stdint.h>
# define INT32_T int32_t
# else
# include <limits.h>
# if INT_MAX >= 2147483647
# define INT32_T int
# else
# define INT32_T long
# endif
# endif
# define TIMEFMT "%a,%Y.%m.%d %H:%M:%S %Z"
# define EXT_SVX_3D "3d"
# define EXT_SVX_POS "pos"
# define FNM_SEP_EXT '.'
# define METRES_PER_FOOT 0.3048 /* exact value */
# define xosmalloc(L) malloc((L))
# define xosrealloc(L,S) realloc((L),(S))
# define osfree(P) free((P))
# define osnew(T) (T*)malloc(sizeof(T))
/* in IMG_HOSTED mode, this tests if a filename refers to a directory */
# define fDirectory(X) 0
/* open file FNM with mode MODE, maybe using path PTH and/or extension EXT */
/* path isn't used in img.c, but EXT is */
# define fopenWithPthAndExt(PTH,FNM,EXT,MODE,X) \
((*(X) = NULL), fopen(FNM,MODE))
# ifndef PUTC
# define PUTC(C, FH) putc(C, FH)
# endif
# ifndef GETC
# define GETC(FH) getc(FH)
# endif
# define fputsnl(S, FH) (fputs((S), (FH)) == EOF ? EOF : putc('\n', (FH)))
# define SVX_ASSERT(X)
#ifdef __cplusplus
# include <algorithm>
using std::max;
using std::min;
#else
/* Return max/min of two numbers. */
/* May be defined already (e.g. by Borland C in stdlib.h) */
/* NB Bad news if X or Y has side-effects... */
# ifndef max
# define max(X, Y) ((X) > (Y) ? (X) : (Y))
# endif
# ifndef min
# define min(X, Y) ((X) < (Y) ? (X) : (Y))
# endif
#endif
static INT32_T
get32(FILE *fh)
{
INT32_T w = GETC(fh);
w |= (INT32_T)GETC(fh) << 8l;
w |= (INT32_T)GETC(fh) << 16l;
w |= (INT32_T)GETC(fh) << 24l;
return w;
}
static void
put32(long w, FILE *fh)
{
PUTC((char)(w), fh);
PUTC((char)(w >> 8l), fh);
PUTC((char)(w >> 16l), fh);
PUTC((char)(w >> 24l), fh);
}
static short
get16(FILE *fh)
{
short w = GETC(fh);
w |= (short)GETC(fh) << 8l;
return w;
}
static void
put16(short w, FILE *fh)
{
PUTC((char)(w), fh);
PUTC((char)(w >> 8l), fh);
}
static char *
baseleaf_from_fnm(const char *fnm)
{
const char *p;
const char *q;
char * res;
size_t len;
p = fnm;
q = strrchr(p, '/');
if (q) p = q + 1;
q = strrchr(p, '\\');
if (q) p = q + 1;
q = strrchr(p, FNM_SEP_EXT);
if (q) len = (const char *)q - p; else len = strlen(p);
res = (char *)xosmalloc(len + 1);
if (!res) return NULL;
memcpy(res, p, len);
res[len] = '\0';
return res;
}
#endif
static char * my_strdup(const char *str);
static time_t
mktime_with_tz(struct tm * tm, const char * tz)
{
time_t r;
char * old_tz = getenv("TZ");
#ifdef _MSC_VER
if (old_tz) {
old_tz = my_strdup(old_tz);
if (!old_tz)
return (time_t)-1;
}
if (_putenv_s("TZ", tz) != 0) {
osfree(old_tz);
return (time_t)-1;
}
#elif defined HAVE_SETENV
if (old_tz) {
old_tz = my_strdup(old_tz);
if (!old_tz)
return (time_t)-1;
}
if (setenv("TZ", tz, 1) < 0) {
osfree(old_tz);
return (time_t)-1;
}
#else
char * p;
if (old_tz) {
size_t len = strlen(old_tz) + 1;
p = (char *)xosmalloc(len + 3);
if (!p)
return (time_t)-1;
memcpy(p, "TZ=", 3);
memcpy(p + 3, tz, len);
old_tz = p;
}
p = (char *)xosmalloc(strlen(tz) + 4);
if (!p) {
osfree(old_tz);
return (time_t)-1;
}
memcpy(p, "TZ=", 3);
strcpy(p + 3, tz);
if (putenv(p) != 0) {
osfree(p);
osfree(old_tz);
return (time_t)-1;
}
#define CLEANUP() osfree(p)
#endif
tzset();
r = mktime(tm);
if (old_tz) {
#ifdef _MSC_VER
_putenv_s("TZ", old_tz);
#elif !defined HAVE_SETENV
putenv(old_tz);
#else
setenv("TZ", old_tz, 1);
#endif
osfree(old_tz);
} else {
#ifdef _MSC_VER
_putenv_s("TZ", "");
#elif !defined HAVE_UNSETENV
putenv((char*)"TZ");
#else
unsetenv("TZ");
#endif
}
#ifdef CLEANUP
CLEANUP();
#undef CLEANUP
#endif
return r;
}
static unsigned short
getu16(FILE *fh)
{
return (unsigned short)get16(fh);
}
#include <math.h>
#if !defined HAVE_LROUND && !defined HAVE_DECL_LROUND
/* The autoconf tests are not in use, but C99 and C++11 both added lround(),
* so set HAVE_LROUND and HAVE_DECL_LROUND conservatively based on the language
* standard version the compiler claims to support. */
# if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \
(defined __cplusplus && __cplusplus >= 201103L)
# define HAVE_LROUND 1
# define HAVE_DECL_LROUND 1
# endif
#endif
#ifdef HAVE_LROUND
# if defined HAVE_DECL_LROUND && !HAVE_DECL_LROUND
/* On older systems, the prototype may be missing. */
extern long lround(double);
# endif
# define my_lround lround
#else
static long
my_lround(double x) {
return (x >= 0.0) ? (long)(x + 0.5) : -(long)(0.5 - x);
}
#endif
/* portable case insensitive string compare */
#if defined(strcasecmp) || defined(HAVE_STRCASECMP)
# define my_strcasecmp strcasecmp
#else
static int my_strcasecmp(const char *s1, const char *s2) {
unsigned char c1, c2;
do {
c1 = *s1++;
c2 = *s2++;
} while (c1 && toupper(c1) == toupper(c2));
/* now calculate real difference */
return c1 - c2;
}
#endif
unsigned int img_output_version = IMG_VERSION_MAX;
static img_errcode img_errno = IMG_NONE;
#define FILEID "Survex 3D Image File"
#define EXT_PLT "plt"
#define EXT_PLF "plf"
/* Attempt to string paste to ensure we are passed a literal string */
#define LITLEN(S) (sizeof(S"") - 1)
/* Fake "version numbers" for non-3d formats we can read. */
#define VERSION_CMAP_SHOT -4
#define VERSION_CMAP_STATION -3
#define VERSION_COMPASS_PLT -2
#define VERSION_SURVEX_POS -1
static char *
my_strdup(const char *str)
{
char *p;
size_t len = strlen(str) + 1;
p = (char *)xosmalloc(len);
if (p) memcpy(p, str, len);
return p;
}
#define getline_alloc(FH) getline_alloc_len(FH, NULL)
static char *
getline_alloc_len(FILE *fh, size_t * p_len)
{
int ch;
size_t i = 0;
size_t len = 16;
char *buf = (char *)xosmalloc(len);
if (!buf) return NULL;
ch = GETC(fh);
while (ch != '\n' && ch != '\r' && ch != EOF) {
buf[i++] = ch;
if (i == len - 1) {
char *p;
len += len;
p = (char *)xosrealloc(buf, len);
if (!p) {
osfree(buf);
return NULL;
}
buf = p;
}
ch = GETC(fh);
}
if (ch == '\n' || ch == '\r') {
int otherone = ch ^ ('\n' ^ '\r');
ch = GETC(fh);
/* if it's not the other eol character, put it back */
if (ch != otherone) ungetc(ch, fh);
}
buf[i] = '\0';
if (p_len) *p_len = i;
return buf;
}
img_errcode
img_error(void)
{
return img_errno;
}
static int
check_label_space(img *pimg, size_t len)
{
if (len > pimg->buf_len) {
char *b = (char *)xosrealloc(pimg->label_buf, len);
if (!b) return 0;
pimg->label = (pimg->label - pimg->label_buf) + b;
pimg->label_buf = b;
pimg->buf_len = len;
}
return 1;
}
/* Check if a station name should be included. */
static int
stn_included(img *pimg)
{
if (!pimg->survey_len) return 1;
size_t l = pimg->survey_len;
const char *s = pimg->label_buf;
if (strncmp(pimg->survey, s, l + 1) != 0) {
return 0;
}
pimg->label += l + 1;
return 1;
}
/* Check if a survey name should be included. */
static int
survey_included(img *pimg)
{
if (!pimg->survey_len) return 1;
size_t l = pimg->survey_len;
const char *s = pimg->label_buf;
if (strncmp(pimg->survey, s, l) != 0 ||
!(s[l] == '.' || s[l] == '\0')) {
return 0;
}
pimg->label += l;
/* skip the dot if there */
if (*pimg->label) pimg->label++;
return 1;
}
/* Check if a survey name in a buffer should be included.
*
* For "foreign" formats which just have one level of surveys.
*/
static int
buf_included(img *pimg, const char *buf, size_t len)
{
return pimg->survey_len == len && strncmp(buf, pimg->survey, len) == 0;
}
#define has_ext(F,L,E) ((L) > LITLEN(E) + 1 &&\
(F)[(L) - LITLEN(E) - 1] == FNM_SEP_EXT &&\
my_strcasecmp((F) + (L) - LITLEN(E), E) == 0)
img *
img_open_survey(const char *fnm, const char *survey)
{
img *pimg;
FILE *fh;
char* filename_opened = NULL;
if (fDirectory(fnm)) {
img_errno = IMG_DIRECTORY;
return NULL;
}
fh = fopenWithPthAndExt("", fnm, EXT_SVX_3D, "rb", &filename_opened);
pimg = img_read_stream_survey(fh, fclose, filename_opened, survey);
if (pimg) {
pimg->filename_opened = filename_opened;
} else {
osfree(filename_opened);
}
return pimg;
}
img *
img_read_stream_survey(FILE *stream, int (*close_func)(FILE*),
const char *fnm,
const char *survey)
{
img *pimg;
size_t len;
char buf[LITLEN(FILEID) + 9];
int ch;
if (stream == NULL) {
img_errno = IMG_FILENOTFOUND;
return NULL;
}
pimg = osnew(img);
if (pimg == NULL) {
img_errno = IMG_OUTOFMEMORY;
if (close_func) close_func(stream);
return NULL;
}
pimg->fh = stream;
pimg->close_func = close_func;
pimg->buf_len = 257;
pimg->label_buf = (char *)xosmalloc(pimg->buf_len);
if (!pimg->label_buf) {
if (pimg->close_func) pimg->close_func(pimg->fh);
osfree(pimg);
img_errno = IMG_OUTOFMEMORY;
return NULL;
}
pimg->fRead = 1; /* reading from this file */
img_errno = IMG_NONE;
pimg->flags = 0;
pimg->filename_opened = NULL;
/* for version >= 3 we use label_buf to store the prefix for reuse */
/* for VERSION_COMPASS_PLT, 0 value indicates we haven't
* entered a survey yet */
/* for VERSION_CMAP_SHOT, we store the last station here
* to detect whether we MOVE or LINE */
pimg->label_len = 0;
pimg->label_buf[0] = '\0';
pimg->survey = NULL;
pimg->survey_len = 0;
pimg->separator = '.';
#if IMG_API_VERSION == 0
pimg->date1 = pimg->date2 = 0;
#else /* IMG_API_VERSION == 1 */
pimg->days1 = pimg->days2 = -1;
#endif
pimg->is_extended_elevation = 0;
pimg->style = pimg->oldstyle = img_STYLE_UNKNOWN;
pimg->l = pimg->r = pimg->u = pimg->d = -1.0;
pimg->title = pimg->datestamp = pimg->cs = NULL;
pimg->datestamp_numeric = (time_t)-1;
if (survey) {
len = strlen(survey);
if (len) {
if (survey[len - 1] == '.') len--;
if (len) {
char *p;
pimg->survey = (char *)xosmalloc(len + 2);
if (!pimg->survey) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
memcpy(pimg->survey, survey, len);
/* Set title to leaf survey name */
pimg->survey[len] = '\0';
p = strrchr(pimg->survey, '.');
if (p) p++; else p = pimg->survey;
pimg->title = my_strdup(p);
if (!pimg->title) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
pimg->survey[len] = '.';
pimg->survey[len + 1] = '\0';
}
}
pimg->survey_len = len;
}
/* [VERSION_COMPASS_PLT, VERSION_CMAP_STATION, VERSION_CMAP_SHOT] pending
* IMG_LINE or IMG_MOVE - both have 4 added.
* [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one
* [version 0] not in the middle of a 'LINE' command
* [version >= 3] not in the middle of turning a LINE into a MOVE
*/
pimg->pending = 0;
printf("about to strlen me\n");
printf("file name is %s\n", fnm);
len = strlen(fnm);
if (has_ext(fnm, len, EXT_SVX_POS)) {
pos_file:
pimg->version = VERSION_SURVEX_POS;
if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm);
pimg->datestamp = my_strdup(TIMENA);
if (!pimg->datestamp) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
pimg->start = 0;
return pimg;
}
if (has_ext(fnm, len, EXT_PLT) || has_ext(fnm, len, EXT_PLF)) {
long fpos;
plt_file:
pimg->version = VERSION_COMPASS_PLT;
/* Spaces aren't legal in Compass station names, but dots are, so
* use space as the level separator */
pimg->separator = ' ';
pimg->start = 0;
if (!pimg->survey) pimg->title = baseleaf_from_fnm(fnm);
pimg->datestamp = my_strdup(TIMENA);
if (!pimg->datestamp) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
while (1) {
ch = GETC(pimg->fh);
switch (ch) {
case '\x1a':
fseek(pimg->fh, -1, SEEK_CUR);
/* FALL THRU */
case EOF:
pimg->start = ftell(pimg->fh);
return pimg;
case 'N': {
char *line, *q;
fpos = ftell(pimg->fh) - 1;
if (!pimg->survey) {
/* FIXME : if there's only one survey in the file, it'd be nice
* to use its description as the title here...
*/
ungetc('N', pimg->fh);
pimg->start = fpos;
return pimg;
}
line = getline_alloc(pimg->fh);
if (!line) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
len = 0;
while (line[len] > 32) ++len;
if (!buf_included(pimg, line, len)) {
osfree(line);
continue;
}
q = strchr(line + len, 'C');
if (q && q[1]) {
osfree(pimg->title);
pimg->title = my_strdup(q + 1);
} else if (!pimg->title) {
pimg->title = my_strdup(pimg->label);
}
osfree(line);
if (!pimg->title) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
if (!pimg->start) pimg->start = fpos;
fseek(pimg->fh, pimg->start, SEEK_SET);
return pimg;
}
case 'M': case 'D':
pimg->start = ftell(pimg->fh) - 1;
break;
}
while (ch != '\n' && ch != '\r') {
ch = GETC(pimg->fh);
}
}
}
/* Although these are often referred to as "CMAP .XYZ files", it seems
* that actually, the extension .XYZ isn't used, rather .SHT (shot
* variant, produced by CMAP v16 and later), .UNA (unadjusted) and
* .ADJ (adjusted) extensions are. Since img has long checked for
* .XYZ, we continue to do so in case anyone is relying on it.
*/
if (has_ext(fnm, len, "sht") ||
has_ext(fnm, len, "adj") ||
has_ext(fnm, len, "una") ||
has_ext(fnm, len, "xyz")) {
char *line;
xyz_file:
/* Spaces aren't legal in CMAP station names, but dots are, so
* use space as the level separator. */
pimg->separator = ' ';
line = getline_alloc(pimg->fh);
if (!line) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
/* There doesn't seem to be a spec for what happens after 1999 with cmap
* files, so this code allows for:
* * 21xx -> xx (up to 2150)
* * 21xx -> 1xx (up to 2199)
* * full year being specified instead of 2 digits
*/
len = strlen(line);
if (len > 59) {
/* Don't just truncate at column 59, allow for a > 2 digit year. */
char * p = strstr(line + len, "Page");
if (p) {
while (p > line && p[-1] == ' ')
--p;
*p = '\0';
len = p - line;
} else {
line[59] = '\0';
}
}
if (len > 45) {
/* YY/MM/DD HH:MM */
struct tm tm;
unsigned long v;
char * p;
pimg->datestamp = my_strdup(line + 45);
p = pimg->datestamp;
v = strtoul(p, &p, 10);
if (v <= 50) {
/* In the absence of a spec for cmap files, assume <= 50 means 21st
* century. */
v += 2000;
} else if (v < 200) {
/* Map 100-199 to 21st century. */
v += 1900;
}
if (v == ULONG_MAX || *p++ != '/')
goto bad_cmap_date;
tm.tm_year = v - 1900;
v = strtoul(p, &p, 10);
if (v < 1 || v > 12 || *p++ != '/')
goto bad_cmap_date;
tm.tm_mon = v - 1;
v = strtoul(p, &p, 10);
if (v < 1 || v > 31 || *p++ != ' ')
goto bad_cmap_date;
tm.tm_mday = v;
v = strtoul(p, &p, 10);
if (v >= 24 || *p++ != ':')
goto bad_cmap_date;
tm.tm_hour = v;
v = strtoul(p, &p, 10);
if (v >= 60)
goto bad_cmap_date;
tm.tm_min = v;
if (*p == ':') {
v = strtoul(p + 1, &p, 10);
if (v > 60)
goto bad_cmap_date;
tm.tm_sec = v;
} else {
tm.tm_sec = 0;
}
tm.tm_isdst = 0;
/* We have no indication of what timezone this timestamp is in. It's
* probably local time for whoever processed the data, so just assume
* UTC, which is at least fairly central in the possibilities.
*/
pimg->datestamp_numeric = mktime_with_tz(&tm, "");
} else {
pimg->datestamp = my_strdup(TIMENA);
}
bad_cmap_date:
if (strncmp(line, " Cave Survey Data Processed by CMAP ",
LITLEN(" Cave Survey Data Processed by CMAP ")) == 0) {
len = 0;
} else {
if (len > 45) {
line[45] = '\0';
len = 45;
}
while (len > 2 && line[len - 1] == ' ') --len;
if (len > 2) {
line[len] = '\0';
pimg->title = my_strdup(line + 2);
}
}
if (len <= 2) pimg->title = baseleaf_from_fnm(fnm);
osfree(line);
if (!pimg->datestamp || !pimg->title) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
line = getline_alloc(pimg->fh);
if (!line) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
if (line[0] != ' ' || (line[1] != 'S' && line[1] != 'O')) {
img_errno = IMG_BADFORMAT;
goto error;
}
if (line[1] == 'S') {
pimg->version = VERSION_CMAP_STATION;
} else {
pimg->version = VERSION_CMAP_SHOT;
}
osfree(line);
line = getline_alloc(pimg->fh);
if (!line) {
img_errno = IMG_OUTOFMEMORY;
goto error;
}
if (line[0] != ' ' || line[1] != '-') {
img_errno = IMG_BADFORMAT;
goto error;
}
osfree(line);
pimg->start = ftell(pimg->fh);
return pimg;
}
if (fread(buf, LITLEN(FILEID) + 1, 1, pimg->fh) != 1 ||
memcmp(buf, FILEID"\n", LITLEN(FILEID) + 1) != 0) {
if (fread(buf + LITLEN(FILEID) + 1, 8, 1, pimg->fh) == 1 &&
memcmp(buf, FILEID"\r\nv0.01\r\n", LITLEN(FILEID) + 9) == 0) {
/* v0 3d file with DOS EOLs */
pimg->version = 0;
goto v03d;
}
rewind(pimg->fh);
if (buf[1] == ' ') {
if (buf[0] == ' ') {
/* Looks like a CMAP .xyz file ... */
goto xyz_file;
} else if (strchr("ZSNF", buf[0])) {
/* Looks like a Compass .plt file ... */
/* Almost certainly it'll start "Z " */
goto plt_file;
}
}
if (buf[0] == '(') {
/* Looks like a Survex .pos file ... */
goto pos_file;
}
img_errno = IMG_BADFORMAT;
goto error;
}
/* check file format version */
ch = GETC(pimg->fh);
pimg->version = 0;
if (tolower(ch) == 'b') {
/* binary file iff B/b prefix */
pimg->version = 1;
ch = GETC(pimg->fh);
}
if (ch != 'v') {
img_errno = IMG_BADFORMAT;
goto error;
}
ch = GETC(pimg->fh);
if (ch == '0') {
if (fread(buf, 4, 1, pimg->fh) != 1 || memcmp(buf, ".01\n", 4) != 0) {
img_errno = IMG_BADFORMAT;
goto error;
}
/* nothing special to do */
} else if (pimg->version == 0) {
if (ch < '2' || ch > '0' + IMG_VERSION_MAX || GETC(pimg->fh) != '\n') {
img_errno = IMG_TOONEW;
goto error;
}
pimg->version = ch - '0';
} else {
img_errno = IMG_BADFORMAT;
goto error;
}
v03d:
{
size_t title_len;
char * title = getline_alloc_len(pimg->fh, &title_len);
if (pimg->version == 8 && title) {
/* We sneak in an extra field after a zero byte here, containing the
* specified coordinate system (if any). Older readers will just
* not see it (which is fine), and this trick avoids us having to
* bump the 3d format version.
*/
size_t real_len = strlen(title);
if (real_len != title_len) {
pimg->cs = my_strdup(title + real_len + 1);
}
}
if (!pimg->title) {
pimg->title = title;
} else {
osfree(title);
}
}
pimg->datestamp = getline_alloc(pimg->fh);
if (!pimg->title || !pimg->datestamp) {
img_errno = IMG_OUTOFMEMORY;
error:
osfree(pimg->title);
osfree(pimg->cs);
osfree(pimg->datestamp);
osfree(pimg->filename_opened);
if (pimg->close_func) pimg->close_func(pimg->fh);
osfree(pimg);
return NULL;
}
if (pimg->version >= 8) {
int flags = GETC(pimg->fh);
if (flags & img_FFLAG_EXTENDED) pimg->is_extended_elevation = 1;
} else {
len = strlen(pimg->title);
if (len > 11 && strcmp(pimg->title + len - 11, " (extended)") == 0) {
pimg->title[len - 11] = '\0';
pimg->is_extended_elevation = 1;
}
}
if (pimg->datestamp[0] == '@') {
unsigned long v;
char * p;
errno = 0;
v = strtoul(pimg->datestamp + 1, &p, 10);
if (errno == 0 && *p == '\0')
pimg->datestamp_numeric = v;
/* FIXME: We're assuming here that the C time_t epoch is 1970, which is
* true for Unix-like systems, Mac OS X and Windows, but isn't guaranteed
* by ISO C.
*/
} else {
/* %a,%Y.%m.%d %H:%M:%S %Z */
struct tm tm;
unsigned long v;
char * p = pimg->datestamp;
while (isalpha((unsigned char)*p)) ++p;
if (*p == ',') ++p;
while (isspace((unsigned char)*p)) ++p;
v = strtoul(p, &p, 10);
if (v == ULONG_MAX || *p++ != '.')
goto bad_3d_date;
tm.tm_year = v - 1900;
v = strtoul(p, &p, 10);
if (v < 1 || v > 12 || *p++ != '.')
goto bad_3d_date;
tm.tm_mon = v - 1;
v = strtoul(p, &p, 10);
if (v < 1 || v > 31 || *p++ != ' ')
goto bad_3d_date;
tm.tm_mday = v;
v = strtoul(p, &p, 10);
if (v >= 24 || *p++ != ':')
goto bad_3d_date;
tm.tm_hour = v;
v = strtoul(p, &p, 10);
if (v >= 60 || *p++ != ':')
goto bad_3d_date;
tm.tm_min = v;
v = strtoul(p, &p, 10);
if (v > 60)
goto bad_3d_date;
tm.tm_sec = v;
tm.tm_isdst = 0;
while (isspace((unsigned char)*p)) ++p;
/* p now points to the timezone string.
*
* However, it's likely to be a string like "BST", and such strings can
* be ambiguous (BST could be UTC+1 or UTC+6), so it is impossible to
* reliably convert in all cases. Just pass what we have to tzset() - if
* it doesn't handle it, UTC will be used.
*/
pimg->datestamp_numeric = mktime_with_tz(&tm, p);
}
bad_3d_date:
pimg->start = ftell(pimg->fh);
return pimg;
}
int
img_rewind(img *pimg)
{
if (!pimg->fRead) {
img_errno = IMG_WRITEERROR;
return 0;
}
if (fseek(pimg->fh, pimg->start, SEEK_SET) != 0) {
img_errno = IMG_READERROR;
return 0;
}
clearerr(pimg->fh);
/* [VERSION_SURVEX_POS] already skipped heading line, or there wasn't one
* [version 0] not in the middle of a 'LINE' command
* [version >= 3] not in the middle of turning a LINE into a MOVE */
pimg->pending = 0;
img_errno = IMG_NONE;
/* for version >= 3 we use label_buf to store the prefix for reuse */
/* for VERSION_COMPASS_PLT, 0 value indicates we haven't entered a survey
* yet */
/* for VERSION_CMAP_SHOT, we store the last station here to detect whether
* we MOVE or LINE */
pimg->label_len = 0;
pimg->style = img_STYLE_UNKNOWN;
return 1;
}
img *
img_open_write_cs(const char *fnm, const char *title, const char *cs, int flags)
{
if (fDirectory(fnm)) {
img_errno = IMG_DIRECTORY;
return NULL;
}
return img_write_stream(fopen(fnm, "wb"), fclose, title, cs, flags);
}
img *
img_write_stream(FILE *stream, int (*close_func)(FILE*),
const char *title, const char *cs, int flags)
{
time_t tm;
img *pimg;
if (stream == NULL) {
img_errno = IMG_FILENOTFOUND;
return NULL;
}
pimg = osnew(img);
if (pimg == NULL) {
img_errno = IMG_OUTOFMEMORY;
if (close_func) close_func(stream);
return NULL;
}
pimg->fh = stream;
pimg->close_func = close_func;
pimg->buf_len = 257;
pimg->label_buf = (char *)xosmalloc(pimg->buf_len);
if (!pimg->label_buf) {
if (pimg->close_func) pimg->close_func(pimg->fh);
osfree(pimg);
img_errno = IMG_OUTOFMEMORY;
return NULL;
}
pimg->filename_opened = NULL;
/* Output image file header */
fputs("Survex 3D Image File\n", pimg->fh); /* file identifier string */
if (img_output_version < 2) {
pimg->version = 1;