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