info.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #ifndef lint
  57. static char *RCSid =
  58. "$XConsortium: info.c /main/3 1996/06/19 17:13:13 drk $";
  59. #endif
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <ctype.h>
  63. #include <string.h>
  64. #include "general.h"
  65. /* ______________________________________________________________________ */
  66. /* Print a summary of each tag use in the instance. Things like depth in
  67. * the tree, number of children, parent, attributes.
  68. */
  69. /* Do the actual printing. Print the info about the node. If null,
  70. * print a header for the columns.
  71. * Arguments:
  72. * Pointer to element structure of the node to print.
  73. */
  74. static void
  75. print_summ(
  76. Element_t *e
  77. )
  78. {
  79. int i, n, dsize;
  80. char *hfmt="%-18.18s %4s %5s %4s %4s %s\n";
  81. char *fmt ="%-18.18s %4d %5d %4d %4d %s\n";
  82. if (e == NULL) {
  83. fprintf(outfp, hfmt, "Element", "Att", "Data", "Chd", "Dep", "Parent");
  84. return;
  85. }
  86. for (i=0,n=0; i<e->ncont; i++) if (IsContElem(e,i)) n++;
  87. for (i=0,dsize=0; i<e->ncont; i++)
  88. if (IsContElem(e,i)) dsize += strlen(e->cont[i].ch.data);
  89. fprintf(outfp, fmt, e->gi, e->natts, dsize, n, e->depth,
  90. e->parent ? e->parent->gi : "-");
  91. for (i=0; i<e->natts; i++) {
  92. fprintf(outfp, "%45d: %s = %s\n", i, e->atts[i].name,
  93. e->atts[i].sval ? e->atts[i].sval : "empty");
  94. }
  95. }
  96. /* Descend the tree, calling processing routine.
  97. * Arguments:
  98. * Pointer to element structure at top of tree to traverse.
  99. */
  100. void
  101. PrintElemSummary(
  102. Element_t *e
  103. )
  104. {
  105. print_summ(0);
  106. DescendTree(e, print_summ, 0, 0, 0);
  107. }
  108. /* ______________________________________________________________________ */
  109. /* Print the context of each tag in the instance (i.e. the tag with its
  110. * ancestors).
  111. */
  112. /* Do the actual printing. Print the context of the node.
  113. * Arguments:
  114. * Pointer to element structure of the node to print.
  115. */
  116. static void
  117. print_context(
  118. Element_t *e
  119. )
  120. {
  121. char buf[LINESIZE];
  122. fprintf(outfp, "%-22s %s\n", e->gi, FindContext(e, 10, buf));
  123. }
  124. /* Descend the tree, calling processing routine.
  125. * Arguments:
  126. * Pointer to element structure at top of tree to traverse.
  127. */
  128. void
  129. PrintContext(
  130. Element_t *e
  131. )
  132. {
  133. fprintf(outfp, "%-22s %s\n", "Element", "Context");
  134. fprintf(outfp, "%-22s %s\n", "---------------", "-----------");
  135. DescendTree(e, print_context, 0, 0, 0);
  136. putc(NL, outfp);
  137. }
  138. /* ______________________________________________________________________ */
  139. /* Print tree of the instance. GI's are printed indented by their depth
  140. * in the tree.
  141. */
  142. /* Do the actual printing. Print the element name, indented the right amount.
  143. * Arguments:
  144. * Pointer to element structure of the node to print.
  145. */
  146. static void
  147. print_indent(
  148. Element_t *e
  149. )
  150. {
  151. int i, ne, nd;
  152. for(i=0; i<e->depth; i++) fputs(". ", outfp);
  153. for(i=0,ne=0; i<e->ncont; i++) if (IsContElem(e,i)) ne++;
  154. for(i=0,nd=0; i<e->ncont; i++) if IsContData(e,i) nd++;
  155. fprintf(outfp, "%s (%d,%d)\n", e->gi, ne, nd);
  156. }
  157. /* Descend the tree, calling processing routine.
  158. * Arguments:
  159. * Pointer to element structure at top of tree to traverse.
  160. */
  161. void
  162. PrintElemTree(
  163. Element_t *e
  164. )
  165. {
  166. DescendTree(e, print_indent, 0, 0, 0);
  167. putc(NL, outfp);
  168. }
  169. /* ______________________________________________________________________ */
  170. /* Print some statistics about the instance.
  171. */
  172. /* Accumulate the totals for the statistics.
  173. * Arguments:
  174. * Pointer to element structure of the node to print.
  175. * Pointer to the total number of elements.
  176. * Pointer to the total amount of content data.
  177. * Pointer to the maximum depth of tree.
  178. */
  179. static void
  180. acc_tots(
  181. Element_t *e,
  182. int *tot_el,
  183. int *tot_data,
  184. int *max_depth
  185. )
  186. {
  187. int i;
  188. for(i=0; i<e->necont; i++)
  189. acc_tots(e->econt[i], tot_el, tot_data, max_depth);
  190. for (i=0; i<e->necont; i++) (*tot_el)++;
  191. for (i=0; i<e->ndcont; i++) (*tot_data) += strlen(e->dcont[i]);
  192. if (e->depth > (*max_depth)) *max_depth = e->depth;
  193. }
  194. /* Descend the tree (recursively), collecting the statistics.
  195. * Arguments:
  196. * Pointer to element structure of the node to print.
  197. * Pointer to the total number of elements.
  198. * Pointer to the total amount of content data.
  199. * Pointer to the maximum depth of tree.
  200. */
  201. static void
  202. elem_usage(
  203. Element_t *e,
  204. char *name,
  205. int *n_used,
  206. int *nchars
  207. )
  208. {
  209. int i;
  210. if (!strcmp(name, e->gi)) {
  211. (*n_used)++;
  212. for (i=0; i<e->ncont; i++)
  213. if (IsContData(e,i)) (*nchars) += strlen(ContData(e,i));
  214. }
  215. for(i=0; i<e->necont; i++)
  216. elem_usage(e->econt[i], name, n_used, nchars);
  217. }
  218. /* Descend the tree, calling processing routine.
  219. * Arguments:
  220. * Pointer to element structure at top of tree to traverse.
  221. */
  222. void
  223. PrintStats(
  224. Element_t *top
  225. )
  226. {
  227. int i, n;
  228. int dif_el=0, tot_el=0, tot_data=0, nchars, max_depth=0;
  229. float pct;
  230. fprintf(outfp, "%-22s %s %s\n", "Element name", "Occurrences", "Character Content");
  231. fprintf(outfp, "%-22s %s %s\n", "---------------", "-----------", "-----------------");
  232. acc_tots(top, &tot_el, &tot_data, &max_depth);
  233. for (i=0; i<nUsedElem; i++) {
  234. n = 0;
  235. nchars = 0;
  236. elem_usage(top, UsedElem[i], &n, &nchars);
  237. if (n > 0) {
  238. pct = 100.0 * (float)n / (float)tot_el;
  239. fprintf(outfp, "%-22s %4d %4.1f%% %6d %4d\n", UsedElem[i],
  240. n, pct, nchars, (nchars/n));
  241. dif_el++;
  242. }
  243. }
  244. fprintf(outfp, "\nTotal of %d elements used, %d different ones.\n",
  245. tot_el, dif_el);
  246. fprintf(outfp, "Total character data: %d.\n", tot_data);
  247. fprintf(outfp, "Maximum element depth: %d.\n", max_depth);
  248. putc(NL, outfp);
  249. }
  250. /* ______________________________________________________________________ */
  251. /* Print list of: ID, GI, input file, line number, separated by colons.
  252. * This is better for other programs to manipulate (like for keeping a
  253. * database of IDs in documents) than humans to read.
  254. */
  255. void
  256. PrintIDList()
  257. {
  258. ID_t *id;
  259. Element_t *ep;
  260. for (id=IDList; id; id=id->next) {
  261. ep = id->elem;
  262. fprintf(outfp, "%s:%s:%s:%d\n", id->id, ep->gi,
  263. ep->infile?ep->infile:"-", ep->lineno);
  264. }
  265. }
  266. /* ______________________________________________________________________ */