From d4cb7e14b54e4f4510fd73d1b3e219eaaffbe081 Mon Sep 17 00:00:00 2001 From: tlatorre Date: Tue, 25 Jun 2019 14:56:35 -0500 Subject: add ability to read gzipped zdab files --- src/Makefile | 2 +- src/zebra.c | 33 +++++++++++++++++++++------------ src/zebra.h | 3 ++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Makefile b/src/Makefile index a0eef06..99cff5e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ release_hdr := $(shell sh -c './mkreleasehdr.sh') CFLAGS=-fdiagnostics-color -O2 -Wall -g -DSWAP_BYTES -LDLIBS=-fdiagnostics-color -lm -lgsl -lgslcblas -lnlopt_cxx -lstdc++ +LDLIBS=-fdiagnostics-color -lm -lgsl -lgslcblas -lnlopt_cxx -lstdc++ -lz PREFIX?=$(HOME)/local INSTALL_BIN=$(PREFIX)/bin diff --git a/src/zebra.c b/src/zebra.c index 690294b..1289318 100644 --- a/src/zebra.c +++ b/src/zebra.c @@ -21,16 +21,25 @@ #include /* for memmove() */ #include /* for uint8_t, etc. */ #include /* for strerror(), etc. */ +#include char zebra_err[256]; +/* Returns a pointer to a new zebra file object. + * + * Returns the pointer to the zebraFile object on success, or NULL on error. */ zebraFile *zebra_open(const char *filename) { - /* Returns a pointer to a new zebra file object. + /* We use gzopen() here even though the file may not be gzipped. According + * to the docs: + * + * gzopen can be used to read a file which is not in gzip format; in + * this case gzread will directly read from the file without + * decompression. When reading, this will be detected automatically + * by looking for the magic two-byte gzip header. * - * Returns the pointer to the zebraFile object on success, or NULL on - * error. */ - FILE *f = fopen(filename, "r"); + * See https://zlib.net/manual.html. */ + gzFile f = gzopen(filename, "r"); if (!f) { sprintf(zebra_err, "failed to open '%s': %s", filename, strerror(errno)); @@ -41,7 +50,7 @@ zebraFile *zebra_open(const char *filename) if (!z) { sprintf(zebra_err, "failed to allocate space for zebraFile struct: %s", strerror(errno)); - fclose(f); + gzclose(f); return NULL; } @@ -64,12 +73,12 @@ static int read_next_physical_record(zebraFile *z) size_t nbytes; uint32_t s0, s1, s2, s3, nwphr, nfast; - nbytes = fread(buf,4,8,z->f); + nbytes = gzread(z->f,buf,4*8); if (nbytes == 0) return 1; - if (nbytes != 8) { - sprintf(zebra_err, "expected %i words but only read %zu", 8, nbytes); + if (nbytes != 8*4) { + sprintf(zebra_err, "expected %i bytes but only read %zu", 8*4, nbytes); return -1; } @@ -109,12 +118,12 @@ static int read_next_physical_record(zebraFile *z) z->buf = realloc(z->buf, z->buf_size+words*4); - nbytes = fread(z->buf+z->buf_size, 4, words, z->f); + nbytes = gzread(z->f, z->buf+z->buf_size, 4*words); z->buf_size += words*4; - if (nbytes != words) { - sprintf(zebra_err, "expected %i words but only read %zu", words, nbytes); + if (nbytes != words*4) { + sprintf(zebra_err, "expected %i bytes but only read %zu", words*4, nbytes); return -1; } @@ -383,7 +392,7 @@ int zebra_get_bank(zebraFile *z, zebraBank *b, uint32_t link) void zebra_close(zebraFile *z) { - fclose(z->f); + gzclose(z->f); if (z->tab) free(z->tab); if (z->buf) free(z->buf); free(z); diff --git a/src/zebra.h b/src/zebra.h index 321befa..16b120f 100644 --- a/src/zebra.h +++ b/src/zebra.h @@ -54,6 +54,7 @@ #include /* for FILE */ #include /* for size_t */ #include /* for uint8_t, etc. */ +#include /* for gzFile */ /* Maximum number of links in a bank. * @@ -131,7 +132,7 @@ typedef struct zebraBank { } zebraBank; typedef struct zebraFile { - FILE *f; + gzFile f; /* Size of the current logical record in bytes. */ size_t lr_size; /* Buffer used to read in the zebra file. */ -- cgit