/*
- * Copyright (c) 1995-1999, Index Data.
+ * Copyright (c) 1995-2002, Index Data.
* See the file LICENSE for details.
* Sebastian Hammer, Adam Dickmeiss
*
- * $Log: d1_read.c,v $
- * Revision 1.26 1999-07-13 13:23:48 adam
- * Non-recursive version of data1_read_node. data1_read_nodex reads
- * stream of bytes (instead of buffer in memory).
- *
- * Revision 1.25 1999/04/20 09:56:48 adam
- * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
- * Modified all encoders/decoders to reflect this change.
- *
- * Revision 1.24 1998/10/28 15:10:09 adam
- * Added --with-yc option to configure. For the data1_node in data1.h:
- * decreased size of localdata and removed member "line" which wasn't useful.
- *
- * Revision 1.23 1998/03/12 11:28:45 adam
- * Fix: didn't set root member of tagged node in function.
- * data1_add_insert_taggeddata.
- *
- * Revision 1.22 1998/03/05 08:15:32 adam
- * Implemented data1_add_insert_taggeddata utility which is more flexible
- * than data1_insert_taggeddata.
- *
- * Revision 1.21 1998/02/27 14:08:05 adam
- * Added const to some char pointer arguments.
- * Reworked data1_read_node so that it doesn't create a tree with
- * pointers to original "SGML"-buffer.
- *
- * Revision 1.20 1998/02/11 11:53:35 adam
- * Changed code so that it compiles as C++.
- *
- * Revision 1.19 1997/12/09 16:17:09 adam
- * Fix bug regarding variants. Tags with prefix "var" was incorrectly
- * interpreted as "start of variants". Now, only "var" indicates such
- * start.
- * Cleaned up data1_read_node so tag names and variant names are
- * copied and not pointed to by the generated data1 tree. Data nodes
- * still point to old buffer.
- *
- * 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
- * C++ support.
- *
- * Revision 1.13 1996/10/29 13:35:38 adam
- * Implemented data1_set_tabpath and data1_get_tabpath.
- *
- * Revision 1.12 1996/10/11 10:35:38 adam
- * Fixed a bug that caused data1_read_node to core dump when no abstract
- * syntax was defined in a "sgml"-record.
- *
- * Revision 1.11 1996/07/06 19:58:35 quinn
- * System headerfiles gathered in yconfig
- *
- * Revision 1.10 1996/01/19 15:41:47 quinn
- * Fixed uninitialized boolean.
- *
- * Revision 1.9 1996/01/17 14:52:47 adam
- * Changed prototype for reader function parsed to data1_read_record.
- *
- * Revision 1.8 1995/12/15 16:20:41 quinn
- * Added formatted text.
- *
- * Revision 1.7 1995/12/13 13:44:32 quinn
- * Modified Data1-system to use nmem
- *
- * Revision 1.6 1995/12/12 16:37:08 quinn
- * Added destroy element to data1_node.
- *
- * Revision 1.5 1995/12/11 15:22:37 quinn
- * Added last_child field to the node.
- * Rewrote schema-mapping.
- *
- * Revision 1.4 1995/11/13 09:27:36 quinn
- * Fiddling with the variant stuff.
- *
- * Revision 1.3 1995/11/01 16:34:57 quinn
- * Making data1 look for tables in data1_tabpath
- *
- * Revision 1.2 1995/11/01 13:54:48 quinn
- * Minor adjustments
- *
- * Revision 1.1 1995/11/01 11:56:09 quinn
- * Added Retrieval (data management) functions en masse.
- *
- * Revision 1.14 1995/10/30 12:40:55 quinn
- * Fixed a couple of bugs.
- *
- * Revision 1.13 1995/10/25 16:00:47 quinn
- * USMARC support is now almost operational
- *
- * Revision 1.12 1995/10/16 14:02:55 quinn
- * Changes to support element set names and espec1
- *
- * Revision 1.11 1995/10/13 16:05:08 quinn
- * Adding Espec1-processing
- *
- * Revision 1.10 1995/10/11 14:53:44 quinn
- * Work on variants.
- *
- * Revision 1.9 1995/10/06 16:56:50 quinn
- * Fixed ranked result.
- *
- * Revision 1.8 1995/10/06 16:44:13 quinn
- * Work on attribute set mapping, etc.
- *
- * Revision 1.7 1995/10/06 12:58:35 quinn
- * SUTRS support
- *
- * Revision 1.6 1995/10/04 09:29:49 quinn
- * Adjustments to support USGS test data
- *
- * Revision 1.5 1995/10/03 17:56:43 quinn
- * Fixing GRS code.
- *
- * Revision 1.4 1995/10/02 15:53:19 quinn
- * Work
- *
- * Revision 1.3 1995/10/02 14:55:21 quinn
- * *** empty log message ***
- *
- * Revision 1.2 1995/09/14 15:18:13 quinn
- * Work
- *
- * Revision 1.1 1995/09/12 11:24:30 quinn
- * Beginning to add code for structured records.
- *
- *
+ * $Id: d1_read.c,v 1.39 2002-04-15 09:06:30 adam Exp $
*/
#include <assert.h>
-#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
-#include <xmalloc.h>
-#include <log.h>
-#include <data1.h>
+#include <yaz/xmalloc.h>
+#include <yaz/log.h>
+#include <yaz/data1.h>
/*
* get the tag which is the immediate parent of this node (this may mean
return r;
}
+data1_node *data1_mk_node_type (data1_handle dh, NMEM m, int type)
+{
+ data1_node *r;
+
+ r = data1_mk_node(dh, m);
+ r->which = type;
+ switch(type)
+ {
+ case DATA1N_tag:
+ r->u.tag.tag = 0;
+ r->u.tag.element = 0;
+ r->u.tag.no_data_requested = 0;
+ r->u.tag.node_selected = 0;
+ r->u.tag.make_variantlist = 0;
+ r->u.tag.get_bytes = -1;
+#if DATA1_USING_XATTR
+ r->u.tag.attributes = 0;
+#endif
+ break;
+ case DATA1N_root:
+ r->u.root.type = 0;
+ r->u.root.absyn = 0;
+ break;
+ case DATA1N_data:
+ r->u.data.data = 0;
+ r->u.data.len = 0;
+ r->u.data.what = 0;
+ r->u.data.formatted_text = 0;
+ break;
+ default:
+ logf (LOG_WARN, "data_mk_node_type. bad type = %d\n", type);
+ }
+ return r;
+}
+
void data1_free_tree (data1_handle dh, data1_node *t)
{
data1_node *p = t->child, *pn;
int first_flag, int local_allowed)
{
data1_node *partag = get_parent_tag (dh, at);
- data1_node *tagn = data1_mk_node (dh, m);
+ data1_node *tagn = data1_mk_node_type (dh, m, DATA1N_tag);
data1_element *e = NULL;
data1_node *datn;
- tagn->which = DATA1N_tag;
tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
- tagn->u.tag.node_selected = 0;
- tagn->u.tag.make_variantlist = 0;
- tagn->u.tag.no_data_requested = 0;
- tagn->u.tag.get_bytes = -1;
if (partag)
e = partag->u.tag.element;
data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
if (!local_allowed && !tagn->u.tag.element)
return NULL;
- tagn->last_child = tagn->child = datn = data1_mk_node (dh, m);
+ tagn->last_child = tagn->child = datn = data1_mk_node_type (dh, m, DATA1N_data);
tagn->root = root;
datn->parent = tagn;
datn->root = root;
- datn->which = DATA1N_data;
datn->u.data.formatted_text = 0;
tagn->parent = at;
return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
}
+#if DATA1_USING_XATTR
+data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
+ int (*get_byte)(void *fh), void *fh,
+ WRBUF wrbuf, int *ch)
+{
+ data1_xattr *p_first = 0;
+ data1_xattr **pp = &p_first;
+ int c = *ch;
+ for (;;)
+ {
+ data1_xattr *p;
+ int len;
+ while (c && d1_isspace(c))
+ c = (*get_byte)(fh);
+ if (!c || c == '>' || c == '/')
+ break;
+ *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
+ p->next = 0;
+ pp = &p->next;
+ p->value = 0;
+
+ wrbuf_rewind(wrbuf);
+ while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
+ {
+ wrbuf_putc (wrbuf, c);
+ c = (*get_byte)(fh);
+ }
+ wrbuf_putc (wrbuf, '\0');
+ len = wrbuf_len(wrbuf);
+ p->name = (char*) nmem_malloc (m, len);
+ strcpy (p->name, wrbuf_buf(wrbuf));
+ if (c == '=')
+ {
+ c = (*get_byte)(fh);
+ if (c == '"')
+ {
+ c = (*get_byte)(fh);
+ wrbuf_rewind(wrbuf);
+ while (c && c != '"')
+ {
+ wrbuf_putc (wrbuf, c);
+ c = (*get_byte)(fh);
+ }
+ if (c)
+ c = (*get_byte)(fh);
+ }
+ else
+ {
+ wrbuf_rewind(wrbuf);
+ while (c && c != '>' && c != '/')
+ {
+ wrbuf_putc (wrbuf, c);
+ c = (*get_byte)(fh);
+ }
+ }
+ wrbuf_putc (wrbuf, '\0');
+ len = wrbuf_len(wrbuf);
+ p->value = (char*) nmem_malloc (m, len);
+ strcpy (p->value, wrbuf_buf(wrbuf));
+ }
+ }
+ *ch = c;
+ return p_first;
+}
+#endif
+
/*
* Ugh. Sometimes functions just grow and grow on you. This one reads a
* 'node' and its children.
while (1)
{
data1_node *parent = level ? d1_stack[level-1] : 0;
- while (c != '\0' && isspace(c))
+ while (c != '\0' && d1_isspace(c))
{
if (c == '\n')
line++;
if (c == '<') /* beginning of tag */
{
+#if DATA1_USING_XATTR
+ data1_xattr *xattr;
+#endif
char tag[64];
char args[256];
- size_t i;
-
- for (i = 0; (c=(*get_byte)(fh)) && c != '>' && !isspace(c);)
+ int null_tag = 0;
+ int end_tag = 0;
+ size_t i = 0;
+
+ c = (*get_byte)(fh);
+ if (c == '/')
+ {
+ end_tag = 1;
+ c = (*get_byte)(fh);
+ }
+ else if (c == '!') /* tags/comments that we don't deal with yet */
+ {
+ while (c && c != '>')
+ c = (*get_byte)(fh);
+ if (c)
+ c = (*get_byte)(fh);
+ continue;
+ }
+ while (c && c != '>' && c != '/' && !d1_isspace(c))
+ {
if (i < (sizeof(tag)-1))
tag[i++] = c;
+ c = (*get_byte)(fh);
+ }
tag[i] = '\0';
- while (isspace(c))
+#if DATA1_USING_XATTR
+ xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c);
+ args[0] = '\0';
+#else
+ while (d1_isspace(c))
c = (*get_byte)(fh);
- for (i = 0; c && c != '>'; c = (*get_byte)(fh))
+ for (i = 0; c && c != '>' && c != '/'; c = (*get_byte)(fh))
if (i < (sizeof(args)-1))
args[i++] = c;
args[i] = '\0';
+#endif
+ if (c == '/')
+ { /* <tag attrs/> or <tag/> */
+ null_tag = 1;
+ c = (*get_byte)(fh);
+ }
if (c != '>')
{
- logf(LOG_WARN, "d1: %d: Malformed tag", line);
+ yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
return 0;
}
else
c = (*get_byte)(fh);
/* End tag? */
- if (*tag == '/')
+ if (end_tag)
{
- if (tag[1] == '\0')
+ if (*tag == '\0')
--level; /* </> */
else
{ /* </tag> */
{
parent = d1_stack[--i];
if ((parent->which == DATA1N_root &&
- !strcmp(tag+1, parent->u.root.type)) ||
+ !strcmp(tag, parent->u.root.type)) ||
(parent->which == DATA1N_tag &&
- !strcmp(tag+1, parent->u.tag.tag)))
+ !strcmp(tag, parent->u.tag.tag)))
{
level = i;
break;
}
if (i != level)
{
- logf (LOG_WARN, "%d: no begin tag for %s",
- line, tag);
+ yaz_log (LOG_WARN, "%d: no begin tag for %s",
+ line, tag);
break;
}
}
{
if (!(absyn = data1_get_absyn (dh, tag)))
{
- logf(LOG_WARN, "Unable to acquire abstract syntax "
- "for '%s'", tag);
- return 0;
+ yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'", tag);
+ /* It's now OK for a record not to have an absyn */
}
- res = data1_mk_node (dh, m);
- res->which = DATA1N_root;
+ res = data1_mk_node_type (dh, m, DATA1N_root);
res->u.root.type = data1_insert_string (dh, res, m, tag);
res->u.root.absyn = absyn;
res->root = res;
if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
{
- logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
+ yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
continue;
}
if (!(tp =
localtag = 1; /* our parent is a local tag */
elem = data1_getelementbytagname(dh, absyn, e, tag);
- res = data1_mk_node (dh, m);
- res->which = DATA1N_tag;
+ res = data1_mk_node_type (dh, m, DATA1N_tag);
res->u.tag.tag = data1_insert_string (dh, res, m, tag);
res->u.tag.element = elem;
- res->u.tag.node_selected = 0;
- res->u.tag.make_variantlist = 0;
- res->u.tag.no_data_requested = 0;
- res->u.tag.get_bytes = -1;
+#if DATA1_USING_XATTR
+ res->u.tag.attributes = xattr;
+#endif
}
if (parent)
{
else if (parent)
parent->child = res;
d1_stack[level] = res;
- d1_stack[++level] = 0;
+ d1_stack[level+1] = 0;
+ if (level < 250 && !null_tag)
+ ++level;
}
else /* != '<'... this is a body of text */
{
c = (*get_byte)(fh);
continue;
}
- res = data1_mk_node(dh, m);
+ res = data1_mk_node_type (dh, m, DATA1N_data);
res->parent = parent;
- res->which = DATA1N_data;
res->u.data.what = DATA1I_text;
res->u.data.formatted_text = 0;
res->root = parent->root;
wrbuf_rewind(wrbuf);
- while (c != '<')
+ while (c && c != '<')
{
wrbuf_putc (wrbuf, c);
c = (*get_byte)(fh);
{
if (*src == '\n')
line++;
- if (isspace (*src))
+ if (d1_isspace (*src))
prev_char = ' ';
else
{
WRBUF wrbuf = wrbuf_alloc();
data1_node *node;
- node = data1_read_nodex(dh, m, getc_mem, buf, wrbuf);
+ node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
wrbuf_free (wrbuf, 1);
return node;
}