view.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* LIBDGL -- a Directed Graph Library implementation
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* best view tabstop=4
  19. */
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <time.h>
  27. #include <errno.h>
  28. #include <ctype.h>
  29. #include "../type.h"
  30. #include "../graph.h"
  31. #include "opt.h"
  32. extern int errno;
  33. #define _EDGESET_OFFSET(pg,pl) ((int)(pl) - (int)(pg)->pEdgeBuffer)
  34. static int _print_node(dglGraph_s * pgraph, dglInt32_t * pnode, void *pvarg)
  35. {
  36. FILE *f = (FILE *) pvarg;
  37. dglInt32_t *pedgeset;
  38. dglInt32_t *pedge;
  39. dglInt32_t *ptonode;
  40. dglInt32_t *pnattr;
  41. int iAttr, cAttr;
  42. int role;
  43. int i;
  44. dglEdgesetTraverser_s edgeaT;
  45. role = 0;
  46. if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_HEAD) {
  47. role |= 1;
  48. }
  49. if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_TAIL) {
  50. role |= 2;
  51. }
  52. fprintf(f, "HEAD %-8ld - %-s",
  53. dglNodeGet_Id(pgraph, pnode),
  54. (role > 2) ? "'H/T'" : (role == 2) ? "'T '" : (role ==
  55. 1) ? "'H '" :
  56. "'A '");
  57. if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
  58. pnattr = dglNodeGet_Attr(pgraph, pnode);
  59. fprintf(f, " - HEAD ATTR [");
  60. for (iAttr = 0; iAttr < cAttr; iAttr++) {
  61. if (iAttr && !(iAttr % 4))
  62. fprintf(f, " ");
  63. fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
  64. }
  65. fprintf(f, "]\n");
  66. }
  67. else {
  68. fprintf(f, "\n");
  69. }
  70. if (role & 1) {
  71. pedgeset = dglNodeGet_OutEdgeset(pgraph, pnode);
  72. dglEdgeset_T_Initialize(&edgeaT, pgraph, pedgeset);
  73. for (i = 0, pedge = dglEdgeset_T_First(&edgeaT);
  74. pedge; i++, pedge = dglEdgeset_T_Next(&edgeaT)
  75. ) {
  76. ptonode = dglEdgeGet_Tail(pgraph, pedge);
  77. if (ptonode) {
  78. role = 0;
  79. if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_HEAD) {
  80. role |= 1;
  81. }
  82. if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_TAIL) {
  83. role |= 2;
  84. }
  85. fprintf(f,
  86. "EDGE #%-8d: TAIL %-8ld - %-s - COST %-8ld - ID %-8ld",
  87. i, dglNodeGet_Id(pgraph, ptonode),
  88. (role > 2) ? "'H/T'" : (role ==
  89. 2) ? "'T '" : (role ==
  90. 1) ? "'H '" :
  91. "'A '", dglEdgeGet_Cost(pgraph, pedge),
  92. dglEdgeGet_Id(pgraph, pedge)
  93. );
  94. if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
  95. pnattr = dglNodeGet_Attr(pgraph, ptonode);
  96. fprintf(f, " - TAIL ATTR [");
  97. for (iAttr = 0; iAttr < cAttr; iAttr++) {
  98. if (iAttr && !(iAttr % 4))
  99. fprintf(f, " ");
  100. fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
  101. }
  102. fprintf(f, "]");
  103. }
  104. if ((cAttr = dglGet_EdgeAttrSize(pgraph)) > 0) {
  105. pnattr = dglEdgeGet_Attr(pgraph, pedge);
  106. fprintf(f, " - EDGE ATTR [");
  107. for (iAttr = 0; iAttr < cAttr; iAttr++) {
  108. if (iAttr && !(iAttr % 4))
  109. fprintf(f, " ");
  110. fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
  111. }
  112. fprintf(f, "]\n");
  113. }
  114. else {
  115. fprintf(f, "\n");
  116. }
  117. }
  118. }
  119. dglEdgeset_T_Release(&edgeaT);
  120. }
  121. return 0;
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. dglGraph_s graph;
  126. int fd;
  127. int nret;
  128. /* program options
  129. */
  130. char *pszFilein;
  131. GNO_BEGIN /* short long default variable help */
  132. GNO_OPTION("g", "graph", NULL, &pszFilein, "Graph file to view")
  133. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  134. return 1;
  135. }
  136. /*
  137. * options parsed
  138. */
  139. if (pszFilein == NULL) {
  140. GNO_HELP("Incomplete parameters");
  141. return 1;
  142. }
  143. fd = open(pszFilein, O_RDONLY);
  144. if (fd < 0) {
  145. perror("open");
  146. return 1;
  147. }
  148. nret = dglRead(&graph, fd);
  149. if (nret < 0) {
  150. fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
  151. return 1;
  152. }
  153. close(fd);
  154. /* print the header
  155. */
  156. fprintf(stdout, "Version: %d\n", graph.Version);
  157. fprintf(stdout, "Byte Order: %s\n",
  158. (graph.Endian ==
  159. DGL_ENDIAN_LITTLE) ? "Little Endian" : "Big Endian");
  160. fprintf(stdout, "Node Attribute Size: %ld\n", graph.NodeAttrSize);
  161. fprintf(stdout, "Edge Attribute Size: %ld\n", graph.EdgeAttrSize);
  162. fprintf(stdout,
  163. "Counters: %ld Edges - %ld Nodes: %ld HEAD / %ld TAIL / %ld ALONE\n",
  164. graph.cEdge, graph.cNode, graph.cHead, graph.cTail, graph.cAlone);
  165. fprintf(stdout, "Opaque Settings:\n");
  166. fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
  167. graph.aOpaqueSet[0], graph.aOpaqueSet[1],
  168. graph.aOpaqueSet[2], graph.aOpaqueSet[3]);
  169. fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
  170. graph.aOpaqueSet[4], graph.aOpaqueSet[5],
  171. graph.aOpaqueSet[6], graph.aOpaqueSet[7]);
  172. fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
  173. graph.aOpaqueSet[8], graph.aOpaqueSet[9],
  174. graph.aOpaqueSet[10], graph.aOpaqueSet[11]);
  175. fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
  176. graph.aOpaqueSet[12], graph.aOpaqueSet[13],
  177. graph.aOpaqueSet[14], graph.aOpaqueSet[15]);
  178. fprintf(stdout, "Total Cost: %lld\n", graph.nnCost);
  179. fprintf(stdout, "--\n");
  180. {
  181. dglInt32_t *pnode;
  182. dglNodeTraverser_s traverser;
  183. dglNode_T_Initialize(&traverser, &graph);
  184. for (pnode = dglNode_T_First(&traverser); pnode;
  185. pnode = dglNode_T_Next(&traverser)) {
  186. _print_node(&graph, pnode, stdout);
  187. }
  188. dglNode_T_Release(&traverser);
  189. }
  190. printf("\n");
  191. dglRelease(&graph);
  192. return 0;
  193. }