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