From 473c3731099ae692d952efad97d60f119518cf48 Mon Sep 17 00:00:00 2001 From: marc Date: Thu, 14 Dec 1995 16:03:08 +0000 Subject: [PATCH] added iterator stuff CVS patchset: 41 CVS date: 1995/12/14 16:03:08 --- lib/header.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/header.h | 7 ++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/header.c b/lib/header.c index f7cbe45..5397281 100644 --- a/lib/header.c +++ b/lib/header.c @@ -38,6 +38,73 @@ struct indexEntry { int_32 count; }; +struct headerIteratorS { + Header h; + int next_index; +}; + +HeaderIterator initIterator(Header h) +{ + HeaderIterator hi = malloc(sizeof(struct headerIteratorS)); + hi->h = h; + hi->next_index = 0; + return hi; +} + +int nextIterator(HeaderIterator iter, + int_32 *tag, int_32 *type, void **p, int_32 *c) +{ + struct headerToken *h = iter->h; + struct indexEntry *index = h->index; + int x = h->entries_used; + int slot = iter->next_index; + char **spp; + char *sp; + + if (slot == h->entries_used) { + return 0; + } + iter->next_index++; + + *tag = ntohl(index[slot].tag); + *type = ntohl(index[slot].type); + *c = ntohl(index[slot].count); + + /* Now look it up */ + switch (*type) { + case INT64_TYPE: + case INT32_TYPE: + case INT16_TYPE: + case INT8_TYPE: + case BIN_TYPE: + case CHAR_TYPE: + *p = h->data + ntohl(index[slot].offset); + break; + case STRING_TYPE: + if (*c == 1) { + /* Special case -- just return a pointer to the string */ + *p = h->data + ntohl(index[slot].offset); + } else { + /* Otherwise, build up an array of char* to return */ + x = index[slot].count; + p = malloc(x * sizeof(char *)); + spp = (char **) p; + sp = h->data + ntohl(index[slot].offset); + while (x--) { + *spp++ = sp; + sp = strchr(sp, 0); + sp++; + } + } + break; + default: + fprintf(stderr, "Data type %d not supprted\n", (int) *type); + exit(1); + } + + return 1; +} + /********************************************************************/ unsigned int sizeofHeader(Header h) diff --git a/lib/header.h b/lib/header.h index 40aeb05..4f690fd 100644 --- a/lib/header.h +++ b/lib/header.h @@ -18,7 +18,7 @@ typedef unsigned int uint_32; #else typedef long long int int_64; -typedef long int int_32; +typedef int int_32; typedef short int int_16; typedef char int_8; @@ -26,6 +26,7 @@ typedef unsigned int uint_32; #endif typedef struct headerToken *Header; +typedef struct headerIteratorS *HeaderIterator; /* read and write a header from a file */ Header readHeader(int fd); @@ -49,6 +50,10 @@ void dumpHeader(Header h, FILE *f, int flags); int getEntry(Header h, int_32 tag, int_32 *type, void **p, int_32 *c); int addEntry(Header h, int_32 tag, int_32 type, void *p, int_32 c); +HeaderIterator initIterator(Header h); +int nextIterator(HeaderIterator iter, + int_32 *tag, int_32 *type, void **p, int_32 *c); + /* Entry Types */ #define NULL_TYPE 0 -- 2.7.4