Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / src / grs1disp.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file grs1disp.c
8  * \brief Implements display of GRS-1 records
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15
16 #include <yaz/proto.h>
17 #include <yaz/oid_db.h>
18
19 static void display_variant(WRBUF w, Z_Variant *v, int level)
20 {
21     int i;
22
23     for (i = 0; i < v->num_triples; i++)
24     {
25         wrbuf_printf(w, "%*sclass=" ODR_INT_PRINTF ",type=" ODR_INT_PRINTF,
26                      level * 4, "", *v->triples[i]->zclass,
27                      *v->triples[i]->type);
28         if (v->triples[i]->which == Z_Triple_internationalString)
29             wrbuf_printf(w, ",value=%s\n",
30                          v->triples[i]->value.internationalString);
31         else
32             wrbuf_printf(w, "\n");
33     }
34 }
35
36 static void display_grs1(WRBUF w, Z_GenericRecord *r, int level)
37 {
38     int i;
39
40     if (!r)
41     {
42         return;
43     }
44     for (i = 0; i < r->num_elements; i++)
45     {
46         Z_TaggedElement *t;
47
48         wrbuf_printf(w, "%*s", level * 4, "");
49         t = r->elements[i];
50         wrbuf_printf(w, "(");
51         if (t->tagType)
52             wrbuf_printf(w, ODR_INT_PRINTF, *t->tagType);
53         else
54             wrbuf_printf(w, "?,");
55         if (t->tagValue->which == Z_StringOrNumeric_numeric)
56             wrbuf_printf(w, ODR_INT_PRINTF ") ", *t->tagValue->u.numeric);
57         else
58             wrbuf_printf(w, "%s) ", t->tagValue->u.string);
59         if (t->content->which == Z_ElementData_subtree)
60         {
61             if (!t->content->u.subtree)
62                 printf (" (no subtree)\n");
63             else
64             {
65                 wrbuf_printf(w, "\n");
66                 display_grs1(w, t->content->u.subtree, level+1);
67             }
68         }
69         else if (t->content->which == Z_ElementData_string)
70         {
71             wrbuf_puts(w, t->content->u.string);
72             wrbuf_puts(w, "\n");
73         }
74         else if (t->content->which == Z_ElementData_numeric)
75         {
76             wrbuf_printf(w, ODR_INT_PRINTF "\n", *t->content->u.numeric);
77         }
78         else if (t->content->which == Z_ElementData_oid)
79         {
80             Odr_oid *ip = t->content->u.oid;
81
82             if (ip)
83             {
84                 char oid_name_str[OID_STR_MAX];
85                 oid_class oclass;
86                 const char *oid_name 
87                     = yaz_oid_to_string_buf(ip, &oclass, oid_name_str);
88             
89                 if (oid_name)
90                     wrbuf_printf(w, "OID: %s\n", oid_name);
91             }
92         }
93         else if (t->content->which == Z_ElementData_noDataRequested)
94             wrbuf_printf(w, "[No data requested]\n");
95         else if (t->content->which == Z_ElementData_elementEmpty)
96             wrbuf_printf(w, "[Element empty]\n");
97         else if (t->content->which == Z_ElementData_elementNotThere)
98             wrbuf_printf(w, "[Element not there]\n");
99         else if (t->content->which == Z_ElementData_date)
100             wrbuf_printf(w, "Date: %s\n", t->content->u.date);
101         else if (t->content->which == Z_ElementData_ext)
102         {
103             printf ("External\n");
104             /* we cannot print externals here. Srry */
105         } 
106         else
107             wrbuf_printf(w, "? type = %d\n",t->content->which);
108         if (t->appliedVariant)
109             display_variant(w, t->appliedVariant, level+1);
110         if (t->metaData && t->metaData->supportedVariants)
111         {
112             int c;
113
114             wrbuf_printf(w, "%*s---- variant list\n", (level+1)*4, "");
115             for (c = 0; c < t->metaData->num_supportedVariants; c++)
116             {
117                 wrbuf_printf(w, "%*svariant #%d\n", (level+1)*4, "", c);
118                 display_variant(w, t->metaData->supportedVariants[c], level+2);
119             }
120         }
121     }
122 }
123
124 void yaz_display_grs1(WRBUF wrbuf, Z_GenericRecord *r, int flags)
125 {
126     display_grs1(wrbuf, r, 0);
127 }
128
129 /*
130  * Local variables:
131  * c-basic-offset: 4
132  * c-file-style: "Stroustrup"
133  * indent-tabs-mode: nil
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */
137