X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=retrieval%2Fd1_read.c;h=38dd68315eaa4adf796fc4ce6f17c7e9dd4d75c1;hp=d4a14bde58623a95a22e20cc9fae17bd4e61b2b6;hb=dd307c65deac044b20c13e99aad2de0d8608a8b7;hpb=7eb2f0de616840d9a340519eac4c271820cd1248 diff --git a/retrieval/d1_read.c b/retrieval/d1_read.c index d4a14bd..38dd683 100644 --- a/retrieval/d1_read.c +++ b/retrieval/d1_read.c @@ -1,10 +1,20 @@ /* - * Copyright (c) 1995, Index Data. + * Copyright (c) 1995-1997, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * * $Log: d1_read.c,v $ - * Revision 1.15 1997-09-05 09:50:57 adam + * Revision 1.18 1997-11-18 09:51:09 adam + * Removed element num_children from data1_node. Minor changes in + * data1 to Explain. + * + * Revision 1.17 1997/11/05 09:20:51 adam + * Minor change. + * + * Revision 1.16 1997/09/17 12:10:37 adam + * YAZ version 1.4. + * + * Revision 1.15 1997/09/05 09:50:57 adam * Removed global data1_tabpath - uses data1_get_tabpath() instead. * * Revision 1.14 1997/05/14 06:54:04 adam @@ -104,33 +114,11 @@ #include #include -static char *data1_tabpath = 0; /* global path for tables */ - -void data1_set_tabpath(const char *p) -{ - if (data1_tabpath) - { - xfree (data1_tabpath); - data1_tabpath = NULL; - } - if (p) - { - data1_tabpath = xmalloc (strlen(p)+1); - strcpy (data1_tabpath, p); - } -} - -const char *data1_get_tabpath (void) -{ - return data1_tabpath; -} - - /* * get the tag which is the immediate parent of this node (this may mean * traversing intermediate things like variants and stuff. */ -data1_node *get_parent_tag(data1_node *n) +data1_node *get_parent_tag (data1_handle dh, data1_node *n) { for (; n && n->which != DATA1N_root; n = n->parent) if (n->which == DATA1N_tag) @@ -138,25 +126,24 @@ data1_node *get_parent_tag(data1_node *n) return 0; } -data1_node *data1_mk_node(NMEM m) +data1_node *data1_mk_node (data1_handle dh, NMEM m) { data1_node *r; r = nmem_malloc(m, sizeof(*r)); r->next = r->child = r->last_child = r->parent = 0; - r->num_children = 0; r->destroy = 0; return r; } -void data1_free_tree(data1_node *t) +void data1_free_tree (data1_handle dh, data1_node *t) { data1_node *p = t->child, *pn; while (p) { pn = p->next; - data1_free_tree(p); + data1_free_tree (dh, p); p = pn; } if (t->destroy) @@ -168,10 +155,11 @@ void data1_free_tree(data1_node *t) * which should be root or tag itself). Returns pointer to the data node, * which can then be modified. */ -data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, - char *tagname, NMEM m) +data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root, + data1_node *at, + char *tagname, NMEM m) { - data1_node *tagn = data1_mk_node(m); + data1_node *tagn = data1_mk_node (dh, m); data1_node *datn; tagn->which = DATA1N_tag; @@ -181,11 +169,11 @@ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, tagn->u.tag.make_variantlist = 0; tagn->u.tag.no_data_requested = 0; tagn->u.tag.get_bytes = -1; - if (!(tagn->u.tag.element = data1_getelementbytagname(root->u.root.absyn, - 0, tagname))) + if (!(tagn->u.tag.element = data1_getelementbytagname (dh, + root->u.root.absyn, + 0, tagname))) return 0; - tagn->child = datn = data1_mk_node(m); - tagn->num_children = 1; + tagn->child = datn = data1_mk_node (dh, m); datn->parent = tagn; datn->root = root; datn->which = DATA1N_data; @@ -193,7 +181,6 @@ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, tagn->next = at->child; tagn->parent = at; at->child = tagn; - at->num_children++; return datn; } @@ -201,8 +188,9 @@ data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at, * Ugh. Sometimes functions just grow and grow on you. This one reads a * 'node' and its children. */ -data1_node *data1_read_node(char **buf, data1_node *parent, int *line, - data1_absyn *absyn, NMEM m) +data1_node *data1_read_node (data1_handle dh, char **buf, + data1_node *parent, int *line, + data1_absyn *absyn, NMEM m) { data1_node *res; @@ -254,10 +242,11 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, { if (!parent) return 0; - if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1, - parent->u.root.type)) || - (parent->which == DATA1N_tag && !strcmp(tag + 1, - parent->u.tag.tag))) + if (!*(tag +1) || + (parent->which == DATA1N_root && + !strcmp(tag + 1,parent->u.root.type)) || + (parent->which == DATA1N_tag && + !strcmp(tag + 1, parent->u.tag.tag))) { *buf = t + 1; return 0; @@ -271,13 +260,13 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, if (!absyn) /* parent node - what are we? */ { - if (!(absyn = data1_get_absyn(tag))) + if (!(absyn = data1_get_absyn (dh, tag))) { logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'", tag); return 0; } - res = data1_mk_node(m); + res = data1_mk_node (dh, m); res->which = DATA1N_root; res->u.root.type = tag; res->u.root.absyn = absyn; @@ -296,8 +285,9 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, logf(LOG_WARN, "Malformed variant triple at '%s'", tag); return 0; } - if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset, - tclass, type))) + if (!(tp = + data1_getvartypebyct(dh, parent->root->u.root.absyn->varset, + tclass, type))) return 0; /* @@ -306,7 +296,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, */ if (parent->which != DATA1N_variant) { - res = data1_mk_node(m); + res = data1_mk_node (dh, m); res->which = DATA1N_variant; res->u.variant.type = 0; res->u.variant.value = 0; @@ -329,7 +319,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, return 0; } - res = data1_mk_node(m); + res = data1_mk_node (dh, m); res->which = DATA1N_variant; res->root = parent->root; res->u.variant.type = tp; @@ -339,7 +329,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, } else /* tag.. acquire our element in the abstract syntax */ { - data1_node *partag = get_parent_tag(parent); + data1_node *partag = get_parent_tag (dh, parent); data1_element *e = 0; int localtag = 0; @@ -362,9 +352,9 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, return 0; } #else - elem = data1_getelementbytagname(absyn, e, tag); + elem = data1_getelementbytagname(dh, absyn, e, tag); #endif - res = data1_mk_node(m); + res = data1_mk_node (dh, m); res->which = DATA1N_tag; res->u.tag.element = elem; res->u.tag.tag = tag; @@ -377,16 +367,13 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, } res->parent = parent; - res->num_children = 0; - pp = &res->child; /* * Read child nodes. */ - while ((*pp = data1_read_node(buf, res, line, absyn, m))) + while ((*pp = data1_read_node(dh, buf, res, line, absyn, m))) { res->last_child = *pp; - res->num_children++; pp = &(*pp)->next; } } @@ -415,7 +402,7 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, } while (isspace(data[len-1])) len--; - res = data1_mk_node(m); + res = data1_mk_node(dh, m); res->parent = parent; res->which = DATA1N_data; res->u.data.what = DATA1I_text; @@ -430,28 +417,29 @@ data1_node *data1_read_node(char **buf, data1_node *parent, int *line, /* * Read a record in the native syntax. */ -data1_node *data1_read_record(int (*rf)(void *, char *, size_t), void *fh, +data1_node *data1_read_record(data1_handle dh, + int (*rf)(void *, char *, size_t), void *fh, NMEM m) { - static char *buf = 0; + int *size; + char **buf = data1_get_read_buf (dh, &size); char *bp; - static int size; int rd = 0, res; int line = 0; - - if (!buf && !(buf = xmalloc(size = 4096))) - abort(); - + + if (!*buf) + *buf = xmalloc(*size = 4096); + for (;;) { - if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2))) + if (rd + 4096 > *size && !(*buf =xrealloc(*buf, *size *= 2))) abort(); - if ((res = (*rf)(fh, buf + rd, 4096)) <= 0) + if ((res = (*rf)(fh, *buf + rd, 4096)) <= 0) { if (!res) { - bp = buf; - return data1_read_node(&bp, 0, &line, 0, m); + bp = *buf; + return data1_read_node(dh, &bp, 0, &line, 0, m); } else return 0;