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