New source grs1disp.c
[yaz-moved-to-github.git] / retrieval / d1_map.c
index c7faa42..d3c79e5 100644 (file)
@@ -1,79 +1,21 @@
 /*
- * Copyright (c) 1995-1999, Index Data.
+ * Copyright (c) 1995-2002, Index Data.
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
- * $Log: d1_map.c,v $
- * Revision 1.16  1999-10-21 12:06:29  adam
- * Retrieval module no longer uses ctype.h - functions.
- *
- * Revision 1.15  1999/08/27 09:40:32  adam
- * Renamed logf function to yaz_log. Removed VC++ project files.
- *
- * Revision 1.14  1998/10/13 16:09:50  adam
- * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
- * Added support for multiple attribute set references and tagset references
- * from an abstract syntax file.
- * Fixed many bad logs-calls in routines that read the various
- * specifications regarding data1 (*.abs,*.att,...) and made the messages
- * consistent whenever possible.
- * Added extra 'lineno' argument to function readconf_line.
- *
- * Revision 1.13  1998/02/11 11:53:35  adam
- * Changed code so that it compiles as C++.
- *
- * Revision 1.12  1997/11/18 09:51:09  adam
- * Removed element num_children from data1_node. Minor changes in
- * data1 to Explain.
- *
- * Revision 1.11  1997/09/17 12:10:36  adam
- * YAZ version 1.4.
- *
- * Revision 1.10  1997/09/05 09:50:56  adam
- * Removed global data1_tabpath - uses data1_get_tabpath() instead.
- *
- * Revision 1.9  1996/06/10 08:56:02  quinn
- * Work on Summary.
- *
- * Revision 1.8  1996/05/01  12:45:31  quinn
- * Support use of local tag names in abs file.
- *
- * Revision 1.7  1995/12/13  13:44:31  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/12  14:11:31  quinn
- * More work on the large-record problem.
- *
- * Revision 1.4  1995/12/11  15:22:37  quinn
- * Added last_child field to the node.
- * Rewrote schema-mapping.
- *
- * Revision 1.3  1995/11/01  16:34:56  quinn
- * Making data1 look for tables in data1_tabpath
- *
- * Revision 1.2  1995/11/01  13:54:46  quinn
- * Minor adjustments
- *
- * Revision 1.1  1995/11/01  11:56:08  quinn
- * Added Retrieval (data management) functions en masse.
- *
- *
+ * $Id: d1_map.c,v 1.25 2002-09-24 08:05:41 adam Exp $
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <oid.h>
-#include <log.h>
-#include <readconf.h>
-
-#include <tpath.h>
-#include <data1.h>
-#include <d1_map.h>
+#include <yaz/oid.h>
+#include <yaz/log.h>
+#include <yaz/readconf.h>
+#include <yaz/tpath.h>
+#include <yaz/data1.h>
+#include <yaz/d1_map.h>
 
 data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
 {
@@ -86,7 +28,7 @@ data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
     data1_mapunit **mapp;
     int local_numeric = 0;
 
-    if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
+    if (!(f = data1_path_fopen(dh, file, "r")))
     {
        yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
        return 0;
@@ -170,7 +112,7 @@ data1_maptab *data1_read_maptab (data1_handle dh, const char *file)
 
                if (ep)
                    ep++;
-               if ((np = sscanf(path, "(%d,%[^)]):%[^/]", &type, valstr,
+               if ((np = sscanf(path, "(%d,%511[^)]):%511[^/]", &type, valstr,
                    parm)) < 2)
                {
                    yaz_log(LOG_WARN, "%s:%d: Syntax error in map "
@@ -274,6 +216,27 @@ static int tagmatch(data1_node *n, data1_maptag *t)
     return 1;
 }
 
+static data1_node *dup_child (data1_handle dh, data1_node *n,
+                              data1_node **last, NMEM mem,
+                              data1_node *parent)
+{
+    data1_node *first = 0;
+    data1_node **m = &first;
+
+    for (; n; n = n->next)
+    {
+        *last = *m = (data1_node *) nmem_malloc (mem, sizeof(**m));
+        memcpy (*m, n, sizeof(**m));
+        
+        (*m)->parent = parent;
+        (*m)->root = parent->root;
+        (*m)->child = dup_child(dh, n->child, &(*m)->last_child, mem, *m);
+        m = &(*m)->next;
+    }
+    *m = 0;
+    return first;
+}
+
 static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
                        data1_node *res, NMEM mem)
 {
@@ -301,31 +264,19 @@ static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
                    {
                        if (!cur || mt->new_field || !tagmatch(cur, mt))
                        {
-                           cur = data1_mk_node (dh, mem);
-                           cur->which = DATA1N_tag;
-                           cur->u.tag.element = 0;
+                           cur = data1_mk_node2 (dh, mem, DATA1N_tag, pn);
                            cur->u.tag.tag = mt->value.string;
-                           cur->u.tag.node_selected = 0;
-                           cur->parent = pn;
-                           cur->root = pn->root;
-                           if (!pn->child)
-                               pn->child = cur;
-                           if (pn->last_child)
-                               pn->last_child->next = cur;
-                           pn->last_child = cur;
                        }
                        
                        if (mt->next)
                            pn = cur;
                        else if (!m->no_data)
                        {
-                           cur->child = c->child;
-                           cur->last_child = c->last_child;
-                           c->child = 0;
-                           c->last_child = 0;
+                            cur->child =
+                                dup_child (dh, c->child,
+                                           &cur->last_child, mem, cur);
                        }
                    }
-                   break;
                }
            }
            if (map_children(dh, c, map, res, mem) < 0)
@@ -342,7 +293,7 @@ static int map_children(data1_handle dh, data1_node *n, data1_maptab *map,
 data1_node *data1_map_record (data1_handle dh, data1_node *n,
                              data1_maptab *map, NMEM m)
 {
-    data1_node *res = data1_mk_node(dh, m);
+    data1_node *res1, *res = data1_mk_node2 (dh, m, DATA1N_root, 0);
 
     res->which = DATA1N_root;
     res->u.root.type = map->target_absyn_name;
@@ -351,10 +302,17 @@ data1_node *data1_map_record (data1_handle dh, data1_node *n,
        yaz_log(LOG_WARN, "%s: Failed to load target absyn '%s'",
                map->name, map->target_absyn_name);
     }
-    res->parent = 0;
-    res->root = res;
+    if (data1_is_xmlmode(dh))
+    {
+        n = n->child;
+        if (!n)
+            return 0;
+        res1 = data1_mk_tag (dh, m, map->target_absyn_name, 0, res);
+    }
+    else
+        res1 = res;
 
-    if (map_children(dh, n, map, res, m) < 0)
+    if (map_children(dh, n, map, res1, m) < 0)
     {
        data1_free_tree(dh, res);
        return 0;