cf0c6fc726f874df63a3b1bb62a9d19a2edc5675
[idzebra-moved-to-github.git] / data1 / d1_prtree.c
1 /* $Id: d1_prtree.c,v 1.10 2007-01-15 15:10:14 adam Exp $
2    Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <yaz/log.h>
24 #include <idzebra/data1.h>
25
26 static void pr_string (FILE *out, const char *str, int len)
27 {
28     int i;
29     for (i = 0; i<len; i++)
30     {
31         int c = str[i];
32         if (c < 32 || c >126)
33             fprintf (out, "\\x%02x", c & 255);
34         else
35             fputc (c, out);
36     }
37 }
38
39 static void pr_tree (data1_handle dh, data1_node *n, FILE *out, int level)
40 {
41     fprintf (out, "%*s", level, "");
42     switch (n->which)
43     {
44     case DATA1N_root:
45         fprintf (out, "root abstract syntax=%s\n", n->u.root.type);
46         break;
47     case DATA1N_tag:
48         fprintf (out, "tag type=%s sel=%d\n", n->u.tag.tag,
49                  n->u.tag.node_selected);
50         if (n->u.tag.attributes)
51         {
52             data1_xattr *xattr = n->u.tag.attributes;
53             fprintf (out, "%*s attr", level, "");
54             for (; xattr; xattr = xattr->next)
55                 fprintf (out, " %s=%s ", xattr->name, xattr->value);
56             fprintf (out, "\n");
57         }
58         break;
59     case DATA1N_data:
60     case DATA1N_comment:
61         if (n->which == DATA1N_data)
62             fprintf (out, "data type=");
63         else
64             fprintf (out, "comment type=");
65         switch (n->u.data.what)
66         {
67         case DATA1I_inctxt:
68             fprintf (out, "inctxt\n");
69             break;
70         case DATA1I_incbin:
71             fprintf (out, "incbin\n");
72             break;
73         case DATA1I_text:
74             fprintf (out, "text '");
75             pr_string (out, n->u.data.data, n->u.data.len);
76             fprintf (out, "'\n");
77             break;
78         case DATA1I_num:
79             fprintf (out, "num '");
80             pr_string (out, n->u.data.data, n->u.data.len);
81             fprintf (out, "'\n");
82             break;
83         case DATA1I_oid:
84             fprintf (out, "oid '");
85             pr_string (out, n->u.data.data, n->u.data.len);
86             fprintf (out, "'\n");
87             break;
88         case DATA1I_xmltext:
89             fprintf (out, "xml text '");
90             pr_string (out, n->u.data.data, n->u.data.len);
91             fprintf (out, "'\n");
92             break;
93         default:
94             fprintf (out, "unknown(%d)\n", n->u.data.what);
95             break;
96         }
97         break;
98     case DATA1N_preprocess:
99         fprintf (out, "preprocess target=%s\n", n->u.preprocess.target);
100         if (n->u.preprocess.attributes)
101         {
102             data1_xattr *xattr = n->u.preprocess.attributes;
103             fprintf (out, "%*s attr", level, "");
104             for (; xattr; xattr = xattr->next)
105                 fprintf (out, " %s=%s ", xattr->name, xattr->value);
106             fprintf (out, "\n");
107         }
108         break;
109     case DATA1N_variant:
110         fprintf (out, "variant\n");
111 #if 0
112         if (n->u.variant.type->name)
113             fprintf (out, " class=%s type=%d value=%s\n",
114                      n->u.variant.type->name, n->u.variant.type->type,
115                      n->u.variant.value);
116 #endif
117         break;
118     default:
119         fprintf (out, "unknown(%d)\n", n->which);
120     }
121     if (n->child)
122         pr_tree (dh, n->child, out, level+4);
123     if (n->next)
124         pr_tree (dh, n->next, out, level);
125     else
126     {
127         if (n->parent && n->parent->last_child != n)
128             fprintf(out, "%*sWARNING: last_child=%p != %p\n", level, "",
129                     n->parent->last_child, n);
130     }
131 }
132
133
134 void data1_pr_tree (data1_handle dh, data1_node *n, FILE *out)
135 {
136     pr_tree (dh, n, out, 0);
137 }
138 /*
139  * Local variables:
140  * c-basic-offset: 4
141  * indent-tabs-mode: nil
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */
145