-
Notifications
You must be signed in to change notification settings - Fork 127
/
gbfile.cc
1332 lines (1102 loc) · 27.2 KB
/
gbfile.cc
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
/*
Common GPSBabel file I/O API
Copyright (C) 2006,2007,2008 Olaf Klein, o.b.klein@gpsbabel.org
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QByteArray> // for QByteArray
#include <QChar> // for QChar, operator==, operator!=
#include <QDebug> // for QDebug
#include <QString> // for QString
#include <Qt> // for CaseInsensitive
#include <QtGlobal> // for qPrintable
#include <cassert> // for assert
#include <cctype> // for tolower
#include <cstdarg> // for va_list, va_end, va_copy, va_start
#include <cstdio> // for EOF, ferror, ftell, SEEK_SET, SEEK_CUR, SEEK_END, clearerr, fclose, feof, fflush, fileno, fread, fseek, fwrite, ungetc, vsnprintf, FILE, stdin, stdout
#include <cstring> // for memcpy, strlen, strchr, strcpy, strncat
#include "defs.h"
#include "gbfile.h"
#include "src/core/logging.h"
#if __WIN32__
/* taken from minigzip.c (part of the zlib project) */
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) _setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
#define NO_ZLIB "No zlib support.\n"
/* About the ZLIB_INHIBITED stuff:
*
* If a user goes out of his way to build with ZLIB_INHIBITED set,
* we jettison our use of zlib entirely within this file, replacing
* all calls out to zlib with calls to abort() as that's an internal
* consistency error.
*
*/
/*******************************************************************************/
/* %%% file api wrappers %%% */
/*******************************************************************************/
#if !ZLIB_INHIBITED
/*******************************************************************************/
/* %%% Zlib file api %%% */
/*******************************************************************************/
static gbfile*
gzapi_open(gbfile* self, const char* mode)
{
char openmode[32];
self->gzapi = 1;
/* under non-posix systems files MUST be opened in binary mode */
strcpy(openmode, mode);
if (strchr(mode, 'b') == nullptr) {
strncat(openmode, "b", sizeof(openmode) - strlen(openmode) - 1);
}
if (self->is_pipe) {
FILE* fd;
if (self->mode == 'r') {
fd = stdin;
} else {
fd = stdout;
}
SET_BINARY_MODE(fd);
self->handle.gz = gzdopen(fileno(fd), openmode);
} else {
#if __WIN32__
// On Windows, convert to wchar_t[] and use gzopen_w().
self->handle.gz = gzopen_w((const wchar_t*) self->name.utf16(), openmode);
#else
// On other platforms, convert to native locale (UTF-8 or other 8-bit).
self->handle.gz = gzopen(qPrintable(self->name), openmode);
#endif
}
if (self->handle.gz == nullptr) {
gbFatal("Cannot %s file '%s'!\n",
(self->mode == 'r') ? "open" : "create",
gbLogCStr(self->name));
}
return self;
}
static int
gzapi_close(gbfile* self)
{
return gzclose(self->handle.gz);
}
static int
gzapi_seek(gbfile* self, int32_t offset, int whence)
{
assert(whence != SEEK_END);
if ((whence == SEEK_CUR) && (self->back != -1)) {
offset--;
}
int result = gzseek(self->handle.gz, offset, whence);
self->back = -1;
if (result < 0) {
if (self->is_pipe) {
gbFatal("This format cannot be used in piped commands!\n");
}
gbFatal("online compression not yet supported for this format!\n");
}
return 0;
}
static gbsize_t
gzapi_read(void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
int result = 0;
char* target = (char*) buf;
int count = size * members;
if (self->back != -1) {
*target++ = self->back;
count--;
result++;
self->back = -1;
}
result += gzread(self->handle.gz, target, count);
/* Check for an incomplete READ */
if ((members == 1) && (size > 1) && (result > 0) && (result < (int)size)) {
gbFatal("Unexpected end of file (EOF)!\n");
}
result /= size;
if ((result < 0) || ((gbsize_t)result < members)) {
int errnum;
const char* errtxt = gzerror(self->handle.gz, &errnum);
/* Workaround for zlib bug: buffer error on empty files */
if ((errnum == Z_BUF_ERROR) && (gztell(self->handle.gz) == 0)) {
return (gbsize_t) 0;
}
if ((errnum != Z_STREAM_END) && (errnum != 0))
gbFatal("zlib returned error %d ('%s')!\n",
errnum, errtxt);
}
return (gbsize_t) result;
}
static gbsize_t
gzapi_write(const void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
return gzwrite(self->handle.gz, buf, size * members) / size;
}
static int
gzapi_flush(gbfile* self)
{
return gzflush(self->handle.gz, Z_SYNC_FLUSH);
}
static gbsize_t
gzapi_tell(gbfile* self)
{
gbsize_t result = gztell(self->handle.gz);
if (self->back != -1) {
result--;
}
return result;
}
static int
gzapi_eof(gbfile* self)
{
int res = 0;
if (self->back != -1) {
return res;
}
res = gzeof(self->handle.gz);
if (!res) {
unsigned char test;
int len = gzread(self->handle.gz, &test, 1);
if (len == 1) {
/* No EOF, put the single byte back into stream */
self->back = test;
} else {
/* we are at the end of the file */
if (global_opts.debug_level > 0) {
/* now gzeof() should return 1 */
if (!gzeof(self->handle.gz)) {
gbFatal("zlib gzeof error!\n");
}
}
res = 1;
}
}
return res;
}
static int
gzapi_ungetc(const int c, gbfile* self)
{
if (self->back == -1) {
self->back = c;
} else {
gbFatal("Cannot store more than one byte back!\n");
}
return c;
}
static void
gzapi_clearerr(gbfile* self)
{
gzclearerr(self->handle.gz);
}
static int
gzapi_error(gbfile* self)
{
int errnum;
(void)gzerror(self->handle.gz, &errnum);
return errnum;
}
#endif // #if !ZLIB_INHIBITED
/*******************************************************************************/
/* %%% Standard C file api %%% */
/*******************************************************************************/
static gbfile*
stdapi_open(gbfile* self, const char* mode)
{
self->handle.std = xfopen(self->name, mode);
return self;
}
static int
stdapi_close(gbfile* self)
{
return fclose(self->handle.std);
}
static int
stdapi_seek(gbfile* self, int32_t offset, int whence)
{
gbsize_t pos = 0;
if (whence != SEEK_SET) {
pos = ftell(self->handle.std);
}
int result = fseek(self->handle.std, offset, whence);
if (result != 0) {
switch (whence) {
case SEEK_CUR:
case SEEK_END:
pos = pos + offset;
break;
case SEEK_SET:
pos = offset;
break;
default:
gbFatal("Unknown seek operation (%d) for file %s!\n",
whence, gbLogCStr(self->name));
}
gbFatal("Unable to set file (%s) to position (%llu)!\n",
gbLogCStr(self->name), (long long unsigned) pos);
}
return 0;
}
static gbsize_t
stdapi_read(void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
int error_number;
gbsize_t result = fread(buf, size, members, self->handle.std);
if ((result < members) && (error_number = ferror(self->handle.std))) {
gbFatal("Error %d occurred during read of file '%s'!\n",
error_number, gbLogCStr(self->name));
}
return result;
}
static gbsize_t
stdapi_write(const void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
return fwrite(buf, size, members, self->handle.std);
}
static int
stdapi_flush(gbfile* self)
{
return fflush(self->handle.std);
}
static gbsize_t
stdapi_tell(gbfile* self)
{
return ftell(self->handle.std);
}
static int
stdapi_eof(gbfile* self)
{
return feof(self->handle.std);
}
static int
stdapi_ungetc(const int c, gbfile* self)
{
return ungetc(c, self->handle.std);
}
static void
stdapi_clearerr(gbfile* self)
{
clearerr(self->handle.std);
}
static int
stdapi_error(gbfile* self)
{
return ferror(self->handle.std);
}
/*******************************************************************************/
/* %%% Memory stream (memapi) %%% */
/*******************************************************************************/
static gbfile*
memapi_open(gbfile* self, const char* mode)
{
(void)mode;
self->mempos = 0;
self->memsz = 0;
self->handle.mem = nullptr;
return self;
}
static int
memapi_close(gbfile* self)
{
if (self->handle.mem) {
xfree(self->handle.mem);
}
return 0;
}
static int
memapi_seek(gbfile* self, int32_t offset, int whence)
{
long long pos = (int)self->mempos;
switch (whence) {
case SEEK_CUR:
case SEEK_END:
pos = pos + offset;
break;
case SEEK_SET:
pos = offset;
break;
}
if ((pos < 0) || (pos > self->memlen)) {
return -1;
}
self->mempos = pos;
return 0;
}
static gbsize_t
memapi_read(void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
gbsize_t result = (self->memlen - self->mempos) / size;
if (result > members) {
result = members;
}
gbsize_t count = result * size;
if (count) {
memcpy(buf, self->handle.mem + self->mempos, count);
self->mempos += count;
}
return result;
}
static gbsize_t
memapi_write(const void* buf, const gbsize_t size, const gbsize_t members, gbfile* self)
{
if ((size == 0) && (members == 0)) { /* truncate stream */
self->memlen = self->mempos;
return 0;
}
gbsize_t count = size * members;
if (self->mempos + count > self->memsz) {
self->memsz = ((self->mempos + count + 4095) / 4096) * 4096;
self->handle.mem = (unsigned char*) xrealloc(self->handle.mem, self->memsz);
}
memcpy(self->handle.mem + self->mempos, buf, count);
self->mempos += count;
if (self->mempos > self->memlen) {
self->memlen = self->mempos;
}
return members;
}
static int
memapi_flush(gbfile* self)
{
(void)self;
return 0;
}
static gbsize_t
memapi_tell(gbfile* self)
{
return self->mempos;
}
static int
memapi_eof(gbfile* self)
{
return (self->mempos == self->memlen);
}
static int
memapi_ungetc(const int c, gbfile* self)
{
if (self->mempos == 0) {
return EOF;
} else {
self->mempos--;
self->handle.mem[self->mempos] = (unsigned char) c;
return c;
}
}
static void
memapi_clearerr(gbfile* self)
{
(void)self;
}
static int
memapi_error(gbfile* self)
{
(void)self;
return 0;
}
/* GPSBabel 'file' standard calls */
/*
* gbfopen: (as xfopen)
*/
gbfile*
gbfopen(const QString& filename, const char* mode)
{
auto* file = new gbfile;
file->mode = 'r'; // default
file->binary = (strchr(mode, 'b') != nullptr);
file->back = -1;
file->memapi = filename.isNull();
for (const char* m = mode; *m; m++) {
switch (tolower(*m)) {
case 'r':
file->mode = 'r';
#if !ZLIB_INHIBITED
file->gzapi = 1; /* native or transparent */
#endif
break;
case 'w':
file->mode = 'w';
break;
}
}
if (file->memapi) {
file->gzapi = 0;
file->name = "(Memory stream)";
file->fileclearerr = memapi_clearerr;
file->fileclose = memapi_close;
file->fileeof = memapi_eof;
file->fileerror = memapi_error;
file->fileflush = memapi_flush;
file->fileopen = memapi_open;
file->fileread = memapi_read;
file->fileseek = memapi_seek;
file->filetell = memapi_tell;
file->fileungetc = memapi_ungetc;
file->filewrite = memapi_write;
} else {
file->name = filename;
file->is_pipe = (filename == '-');
/* Do we have a '.gz' extension in the filename ? */
if ((file->name.size() > 3) && (file->name.endsWith(".gz", Qt::CaseInsensitive))) {
#if !ZLIB_INHIBITED
/* force gzipped files on output */
file->gzapi = 1;
#else
gbFatal(NO_ZLIB);
#endif
}
if (file->gzapi) {
#if !ZLIB_INHIBITED
file->fileclearerr = gzapi_clearerr;
file->fileclose = gzapi_close;
file->fileeof = gzapi_eof;
file->fileerror = gzapi_error;
file->fileflush = gzapi_flush;
file->fileopen = gzapi_open;
file->fileread = gzapi_read;
file->fileseek = gzapi_seek;
file->filetell = gzapi_tell;
file->fileungetc = gzapi_ungetc;
file->filewrite = gzapi_write;
#else
/* This is the only runtime test we make */
gbFatal("Zlib was not included in this build.\n");
#endif
} else {
file->fileclearerr = stdapi_clearerr;
file->fileclose = stdapi_close;
file->fileeof = stdapi_eof;
file->fileerror = stdapi_error;
file->fileflush = stdapi_flush;
file->fileopen = stdapi_open;
file->fileread = stdapi_read;
file->fileseek = stdapi_seek;
file->filetell = stdapi_tell;
file->fileungetc = stdapi_ungetc;
file->filewrite = stdapi_write;
}
}
file->fileopen(file, mode);
file->buffsz = 256;
file->buff = (char*) xmalloc(file->buffsz);
return file;
}
/*
* gbfopen_be: as gbfopen, but set the BIG-ENDIAN flag
*/
gbfile*
gbfopen_be(const QString& filename, const char* mode)
{
gbfile* result = gbfopen(filename, mode);
result->big_endian = 1;
return result;
}
/*
* gbfclose: (as fclose)
*/
void
gbfclose(gbfile* file)
{
if (!file) {
return;
}
file->fileclose(file);
xfree(file->buff);
delete file;
}
/*
* gbfgetc: (as fgetc)
*/
int
gbfgetc(gbfile* file)
{
unsigned char c;
/* errors are caught in gbfread */
if (gbfread(&c, 1, 1, file) == 0) {
return EOF;
} else {
return (unsigned int)c;
}
}
/*
* gbfgets: (as fgets)
*/
QString
gbfgets(char* buf, int len, gbfile* file)
{
char* result = buf;
while (--len > 0) {
int c = gbfgetc(file);
if (c == EOF) {
break;
}
*(unsigned char*)buf = (unsigned char)c;
buf++;
if (c == '\r') {
c = gbfgetc(file);
if ((c != '\n') && (c != EOF)) {
gbfungetc(c, file);
}
break;
} else if (c == '\n') {
break;
}
}
*buf = '\0';
QString rv(result);
return rv;
}
/*
* gbfread: (as fread)
*/
gbsize_t
gbfread(void* buf, const gbsize_t size, const gbsize_t members, gbfile* file)
{
if ((size == 0) || (members == 0)) {
return 0;
}
return file->fileread(buf, size, members, file);
}
// This probably makes an unnecessary alloc/copy, but keeps the above (kinda
// goofy) calling signature.
gbsize_t
gbfread(QString& buf, const gbsize_t size,
const gbsize_t members, gbfile* file)
{
QByteArray tmp;
tmp.resize(members * size);
gbsize_t retval = gbfread(tmp.data(), size, members, file);
buf = QString(tmp);
return retval;
}
QByteArray gbfreadbuf(gbsize_t size, gbfile* file) {
QByteArray tmp;
tmp.resize(size);
gbsize_t nbytes = gbfread(tmp.data(), 1, size, file);
if (nbytes != size) {
gbFatal(FatalMsg() << "Attempted to read " << size <<
"bytes, but only " << nbytes << "were available.");
}
return tmp;
}
/*
* gbvfprintf: (as vfprintf)
*/
int gbvfprintf(gbfile* file, const char* format, va_list ap)
{
int len;
for (;;) {
va_list args;
va_copy(args, ap);
len = vsnprintf(file->buff, file->buffsz, format, args);
va_end(args);
/* Unambiguous Success */
if ((len > -1) && (len < file->buffsz)) {
break;
}
/* First case: C99 behaviour. Len is correctly sized.
* add space for null terminator. Next time through the
* loop we're guaranteed success.
*
* Second case: SUS (and Windows) behaviour. We know it
* doesn't fit, but we don't know how big it has to be.
` * double it and try again. We'll loop until we succeed.
*
* Since we keep the I/O buffer in the file handle, we
* quickly reach a steady state on the size of these buffers.
*/
if (len > -1) {
file->buffsz = len + 1;
} else {
file->buffsz *= 2;
}
file->buff = (char*) xrealloc(file->buff, file->buffsz);
}
return gbfwrite(file->buff, 1, len, file);
}
/*
* gbfprintf: (as fprintf)
*/
int
gbfprintf(gbfile* file, const char* format, ...)
{
va_list args;
va_start(args, format);
int result = gbvfprintf(file, format, args);
va_end(args);
return result;
}
/*
* gbfputc: (as fputc)
*/
int
gbfputc(int c, gbfile* file)
{
unsigned char temp = (unsigned int) c;
gbfwrite(&temp, 1, 1, file);
return c;
}
/*
* gbfputs: (as fputs)
*/
// This is a depressing hack, meant to ease the pain from C strings
// to QStrings, which are consistently encoded.
int
gbfputs(const QString& s, gbfile* file)
{
QByteArray qs = s.toUtf8();
int rv = gbfwrite(qs.constData(), 1, qs.size(), file);
return rv;
}
/*
* gbfwrite: (as fwrite)
*/
int
gbfwrite(const void* buf, const gbsize_t size, const gbsize_t members, gbfile* file)
{
unsigned int result = file->filewrite(buf, size, members, file);
if (result != members) {
gbFatal("Could not write %lld bytes to %s (result %d)!\n",
(long long int)(members - result) * size,
gbLogCStr(file->name),
result);
}
return result;
}
/*
* gbfflush: (as fflush)
*/
int
gbfflush(gbfile* file)
{
return file->fileflush(file);
}
/*
* gbfclearerr: (as clearerr)
*/
void
gbfclearerr(gbfile* file)
{
file->fileclearerr(file);
}
/*
* gbferror: (as ferror)
*/
int
gbferror(gbfile* file)
{
return file->fileerror(file);
}
/*
* gbfrewind: (as frewind)
*/
void
gbfrewind(gbfile* file)
{
(void) gbfseek(file, 0, SEEK_SET);
gbfclearerr(file);
}
/*
* gbfseek: (as fseek)
*/
int
gbfseek(gbfile* file, int32_t offset, int whence)
{
return file->fileseek(file, offset, whence);
}
/*
* gbftell: (as ftell)
*/
gbsize_t
gbftell(gbfile* file)
{
gbsize_t result = file->filetell(file);
if ((signed) result == -1)
gbFatal("Could not determine position of file '%s'!\n",
gbLogCStr(file->name));
return result;
}
/*
* gbfeof: (as feof)
*/
int
gbfeof(gbfile* file)
{
return file->fileeof(file);
}
/*
* gbfungetc: (as fungetc)
*/
int
gbfungetc(const int c, gbfile* file)
{
return file->fileungetc(c, file);
}
/* GPSBabel 'file' enhancements */
/*
* gbfgetint32: read a signed 32-bit integer from input stream
*/
int32_t
gbfgetint32(gbfile* file)
{
char buf[4];
if (gbfread(&buf, 1, sizeof(buf), file) != sizeof(buf)) {
gbFatal("Unexpected end of file (%s)!\n", gbLogCStr(file->name));
}
if (file->big_endian) {
return be_read32(buf);
} else {
return le_read32(buf);
}
}
/*
* gbfgetint16: read a signed 16-bit integer from input stream
*/
int16_t
gbfgetint16(gbfile* file)
{
char buf[2];
if (gbfread(&buf, 1, sizeof(buf), file) != sizeof(buf)) {
gbFatal("Unexpected end of file (%s)!\n", gbLogCStr(file->name));
}
if (file->big_endian) {
return be_read16(buf);
} else {
return le_read16(buf);
}
}
/*
* gbfgetdbl: read a double value (8 byte, double precision) from input stream
*/
double
gbfgetdbl(gbfile* file)
{
char buf[8];
if (gbfread(&buf, 1, sizeof(buf), file) != sizeof(buf)) {
gbFatal("Unexpected end of file (%s)!\n", gbLogCStr(file->name));
}
return endian_read_double(buf, ! file->big_endian);
}
/*
* gbfgetflt: read a float value (4 byte, single precision) from input stream
*/
float
gbfgetflt(gbfile* file)
{
char buf[4];
if (gbfread(&buf, 1, sizeof(buf), file) != sizeof(buf)) {
gbFatal("Unexpected end of file (%s)!\n", gbLogCStr(file->name));
}
return endian_read_float(buf, ! file->big_endian);
}
/*
* gbfgetcstr: Reads a string from file until either a '\0' or eof.
* The result is a temporary allocated entity: use it or free it!
*/
char*
gbfgetcstr_old(gbfile* file)
{
int len = 0;
char* str = file->buff;
for (;;) {
int c = gbfgetc(file);
if (c == 0) {
break;
}
if (c == EOF) {
gbFatal("Unexpected end of file (%s)!\n", gbLogCStr(file->name));
}