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