Spelling fixes
[oweals/cde.git] / cde / programs / dtdocbook / instant / info.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *  Copyright 1993 Open Software Foundation, Inc., Cambridge, Massachusetts.
25  *  All rights reserved.
26  */
27 /*
28  * Copyright (c) 1994  
29  * Open Software Foundation, Inc. 
30  *  
31  * Permission is hereby granted to use, copy, modify and freely distribute 
32  * the software in this file and its documentation for any purpose without 
33  * fee, provided that the above copyright notice appears in all copies and 
34  * that both the copyright notice and this permission notice appear in 
35  * supporting documentation.  Further, provided that the name of Open 
36  * Software Foundation, Inc. ("OSF") not be used in advertising or 
37  * publicity pertaining to distribution of the software without prior 
38  * written permission from OSF.  OSF makes no representations about the 
39  * suitability of this software for any purpose.  It is provided "as is" 
40  * without express or implied warranty. 
41  */
42 /* ________________________________________________________________________
43  *
44  *  Functions for printing information about an instance in the 'instant'
45  *  program.  Most of these are fairly short and simple.
46  *
47  *  Entry points for this module:
48  *      PrintElemSummary(elem)  print summary info of each element
49  *      PrintContext(elem)      print context of each element
50  *      PrintElemTree(elem)     print tree of document
51  *      PrintStats(elem)        print statistics about doc tree
52  *      PrintIDList(elem)       print list of IDs and element context
53  *  Most Print*() functions start at subtree pointed to by 'elem'.
54  * ________________________________________________________________________
55  */
56
57 #ifndef lint
58 static char *RCSid =
59   "$XConsortium: info.c /main/3 1996/06/19 17:13:13 drk $";
60 #endif
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <ctype.h>
65 #include <string.h>
66
67 #include "general.h"
68
69 /* ______________________________________________________________________ */
70 /*  Print a summary of each tag use in the instance.  Things like depth in
71  *  the tree, number of children, parent, attributes.
72  */
73
74 /*  Do the actual printing.  Print the info about the node.  If null,
75  *  print a header for the columns.
76  *  Arguments:
77  *      Pointer to element structure of the node to print.
78  */
79 static void
80 print_summ(
81     Element_t   *e
82 )
83 {
84     int i, n, dsize;
85     char *hfmt="%-18.18s %4s %5s %4s %4s %s\n";
86     char *fmt ="%-18.18s %4d %5d %4d %4d %s\n";
87
88     if (e == NULL) {
89         fprintf(outfp, hfmt, "Element", "Att", "Data", "Chd", "Dep", "Parent");
90         return;
91     }
92     for (i=0,n=0; i<e->ncont; i++) if (IsContElem(e,i)) n++;
93     for (i=0,dsize=0; i<e->ncont; i++)
94         if (IsContElem(e,i)) dsize += strlen(e->cont[i].ch.data);
95     fprintf(outfp, fmt, e->gi, e->natts, dsize, n, e->depth,
96         e->parent ? e->parent->gi : "-");
97
98     for (i=0; i<e->natts; i++) {
99         fprintf(outfp, "%45d: %s = %s\n", i, e->atts[i].name,
100             e->atts[i].sval ? e->atts[i].sval : "empty");
101     }
102 }
103
104 /*  Descend the tree, calling processing routine.
105  *  Arguments:
106  *      Pointer to element structure at top of tree to traverse.
107  */
108 void
109 PrintElemSummary(
110     Element_t   *e
111 )
112 {
113     print_summ(0);
114     DescendTree(e, print_summ, 0, 0, 0);
115 }
116
117 /* ______________________________________________________________________ */
118 /*  Print the context of each tag in the instance (i.e. the tag with its
119  *  ancestors).
120  */
121
122 /*  Do the actual printing.  Print the context of the node.
123  *  Arguments:
124  *      Pointer to element structure of the node to print.
125  */
126 static void
127 print_context(
128     Element_t   *e
129 )
130 {
131     char buf[LINESIZE];
132
133     fprintf(outfp, "%-22s %s\n", e->gi, FindContext(e, 10, buf));
134 }
135
136 /*  Descend the tree, calling processing routine.
137  *  Arguments:
138  *      Pointer to element structure at top of tree to traverse.
139  */
140 void
141 PrintContext(
142     Element_t   *e
143 )
144 {
145     fprintf(outfp, "%-22s %s\n", "Element", "Context");
146     fprintf(outfp, "%-22s %s\n", "---------------", "-----------");
147     DescendTree(e, print_context, 0, 0, 0);
148
149     putc(NL, outfp);
150 }
151
152 /* ______________________________________________________________________ */
153 /*  Print tree of the instance.  GI's are printed indented by their depth
154  *  in the tree.
155  */
156
157 /*  Do the actual printing.  Print the element name, indented the right amount.
158  *  Arguments:
159  *      Pointer to element structure of the node to print.
160  */
161 static void
162 print_indent(
163     Element_t   *e
164 )
165 {
166     int         i, ne, nd;
167     for(i=0; i<e->depth; i++) fputs(".  ", outfp);
168     for(i=0,ne=0; i<e->ncont; i++) if (IsContElem(e,i)) ne++;
169     for(i=0,nd=0; i<e->ncont; i++) if IsContData(e,i) nd++;
170     fprintf(outfp, "%s  (%d,%d)\n", e->gi, ne, nd);
171 }
172
173 /*  Descend the tree, calling processing routine.
174  *  Arguments:
175  *      Pointer to element structure at top of tree to traverse.
176  */
177 void
178 PrintElemTree(
179     Element_t   *e
180 )
181 {
182     DescendTree(e, print_indent, 0, 0, 0);
183     putc(NL, outfp);
184 }
185
186 /* ______________________________________________________________________ */
187 /*  Print some statistics about the instance.
188  */
189
190 /*  Accumulate the totals for the statistics.
191  *  Arguments:
192  *      Pointer to element structure of the node to print.
193  *      Pointer to the total number of elements.
194  *      Pointer to the total amount of content data.
195  *      Pointer to the maximum depth of tree.
196  */
197 static void
198 acc_tots(
199     Element_t   *e,
200     int         *tot_el,
201     int         *tot_data,
202     int         *max_depth
203 )
204 {
205     int         i;
206     for(i=0; i<e->necont; i++)
207         acc_tots(e->econt[i], tot_el, tot_data, max_depth);
208     for (i=0; i<e->necont; i++) (*tot_el)++;
209     for (i=0; i<e->ndcont; i++) (*tot_data) += strlen(e->dcont[i]);
210     if (e->depth > (*max_depth)) *max_depth = e->depth;
211 }
212
213 /*  Descend the tree (recursively), collecting the statistics.
214  *  Arguments:
215  *      Pointer to element structure of the node to print.
216  *      Pointer to the total number of elements.
217  *      Pointer to the total amount of content data.
218  *      Pointer to the maximum depth of tree.
219  */
220 static void
221 elem_usage(
222     Element_t   *e,
223     char        *name,
224     int         *n_used,
225     int         *nchars
226 )
227 {
228     int         i;
229     if (!strcmp(name, e->gi)) {
230         (*n_used)++;
231         for (i=0; i<e->ncont; i++)
232             if (IsContData(e,i)) (*nchars) += strlen(ContData(e,i));
233     }
234     for(i=0; i<e->necont; i++)
235         elem_usage(e->econt[i], name, n_used, nchars);
236 }
237
238 /*  Descend the tree, calling processing routine.
239  *  Arguments:
240  *      Pointer to element structure at top of tree to traverse.
241  */
242 void
243 PrintStats(
244     Element_t   *top
245 )
246 {
247     int         i, n;
248     int         dif_el=0, tot_el=0, tot_data=0, nchars, max_depth=0;
249     float       pct;
250
251     fprintf(outfp, "%-22s %s   %s\n", "Element name",    "Occurrences", "Character Content");
252     fprintf(outfp, "%-22s %s   %s\n", "---------------", "-----------", "-----------------");
253
254     acc_tots(top, &tot_el, &tot_data, &max_depth);
255
256     for (i=0; i<nUsedElem; i++) {
257         n = 0;
258         nchars = 0;
259         elem_usage(top, UsedElem[i], &n, &nchars);
260         if (n > 0) {
261             pct = 100.0 * (float)n / (float)tot_el;
262             fprintf(outfp, "%-22s %4d  %4.1f%%   %6d  %4d\n", UsedElem[i],
263                 n, pct, nchars, (nchars/n));
264             dif_el++;
265         }
266     }
267
268     fprintf(outfp, "\nTotal of %d elements used, %d different ones.\n",
269         tot_el, dif_el);
270     fprintf(outfp, "Total character data: %d.\n", tot_data);
271     fprintf(outfp, "Maximum element depth: %d.\n", max_depth);
272     putc(NL, outfp);
273 }
274
275 /* ______________________________________________________________________ */
276 /* Print list of: ID, GI, input file, line number, separated by colons.
277  * This is better for other programs to manipulate (like for keeping a
278  * database of IDs in documents) than humans to read.
279  */
280
281 void
282 PrintIDList()
283 {
284     ID_t        *id;
285     Element_t   *ep;
286
287     for (id=IDList; id; id=id->next) {
288         ep = id->elem;
289         fprintf(outfp, "%s:%s:%s:%d\n", id->id, ep->gi,
290                 ep->infile?ep->infile:"-", ep->lineno);
291     }
292 }
293
294 /* ______________________________________________________________________ */
295