4146d07a9a1608436588c4fb96e0a9e33a83afc5
[idzebra-moved-to-github.git] / data1 / d1_sumout.c
1 /* $Id: d1_sumout.c,v 1.3 2004-09-28 10:15:03 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <yaz/log.h>
28 #include <yaz/proto.h>
29 #include <idzebra/data1.h>
30
31 static int *f_integer(data1_node *c, ODR o)
32 {
33     int *r;
34     char intbuf[64];
35
36     if (!c->child || c->child->which != DATA1N_data ||
37         c->child->u.data.len > 63)
38         return 0;
39     r = (int *)odr_malloc(o, sizeof(*r));
40     sprintf(intbuf, "%.*s", 63, c->child->u.data.data);
41     *r = atoi(intbuf);
42     return r;
43 }
44
45 static char *f_string(data1_node *c, ODR o)
46 {
47     char *r;
48
49     if (!c->child || c->child->which != DATA1N_data)
50         return 0;
51     r = (char *)odr_malloc(o, c->child->u.data.len+1);
52     memcpy(r, c->child->u.data.data, c->child->u.data.len);
53     r[c->child->u.data.len] = '\0';
54     return r;
55 }
56
57 Z_BriefBib *data1_nodetosummary (data1_handle dh, data1_node *n,
58                                  int select, ODR o)
59 {
60     Z_BriefBib *res = (Z_BriefBib *)odr_malloc(o, sizeof(*res));
61     data1_node *c;
62
63     assert(n->which == DATA1N_root);
64     if (strcmp(n->u.root.type, "summary"))
65     {
66         yaz_log(LOG_WARN, "Attempt to convert a non-summary record");
67         return 0;
68     }
69
70     res->title = "[UNKNOWN]";
71     res->author = 0;
72     res->callNumber = 0;
73     res->recordType = 0;
74     res->bibliographicLevel = 0;
75     res->num_format = 0;
76     res->format = 0;
77     res->publicationPlace = 0;
78     res->publicationDate = 0;
79     res->targetSystemKey = 0;
80     res->satisfyingElement = 0;
81     res->rank = 0;
82     res->documentId = 0;
83     res->abstract = 0;
84     res->otherInfo = 0;
85
86     for (c = n->child; c; c = c->next)
87     {
88         if (c->which != DATA1N_tag || !c->u.tag.element)
89         {
90             yaz_log(LOG_WARN, "Malformed element in Summary record");
91             return 0;
92         }
93         if (select && !c->u.tag.node_selected)
94             continue;
95         switch (c->u.tag.element->tag->value.numeric)
96         {
97             case 0: res->title = f_string(c, o); break;
98             case 1: res->author = f_string(c, o); break;
99             case 2: res->callNumber = f_string(c, o); break;
100             case 3: res->recordType = f_string(c, o); break;
101             case 4: res->bibliographicLevel = f_string(c, o); break;
102             case 5: abort();   /* TODO */
103             case 10: res->publicationPlace = f_string(c, o); break;
104             case 11: res->publicationDate = f_string(c, o); break;
105             case 12: res->targetSystemKey = f_string(c, o); break;
106             case 13: res->satisfyingElement = f_string(c, o); break;
107             case 14: res->rank = f_integer(c, o); break;
108             case 15: res->documentId = f_string(c, o); break;
109             case 16: res->abstract = f_string(c, o); break;
110             case 17: abort(); /* TODO */
111             default:
112                 yaz_log(LOG_WARN, "Unknown element in Summary record.");
113         }
114     }
115     return res;
116 }