GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vector/dglib/examples/view.c
Go to the documentation of this file.
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 
19 /* best view tabstop=4
20  */
21 
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <time.h>
29 #include <errno.h>
30 #include <ctype.h>
31 
32 #include "../type.h"
33 #include "../graph.h"
34 
35 #include "opt.h"
36 
37 
38 extern int errno;
39 
40 #define _EDGESET_OFFSET(pg,pl) ((int)(pl) - (int)(pg)->pEdgeBuffer)
41 
42 static int _print_node(dglGraph_s * pgraph, dglInt32_t * pnode, void *pvarg)
43 {
44  FILE *f = (FILE *) pvarg;
45  dglInt32_t *pedgeset;
46  dglInt32_t *pedge;
47  dglInt32_t *ptonode;
48  dglInt32_t *pnattr;
49  int iAttr, cAttr;
50  int role;
51  int i;
52  dglEdgesetTraverser_s edgeaT;
53 
54  role = 0;
55  if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_HEAD) {
56  role |= 1;
57  }
58  if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_TAIL) {
59  role |= 2;
60  }
61 
62  fprintf(f, "HEAD %-8ld - %-s",
63  dglNodeGet_Id(pgraph, pnode),
64  (role > 2) ? "'H/T'" : (role == 2) ? "'T '" : (role ==
65  1) ? "'H '" :
66  "'A '");
67 
68  if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
69  pnattr = dglNodeGet_Attr(pgraph, pnode);
70  fprintf(f, " - HEAD ATTR [");
71  for (iAttr = 0; iAttr < cAttr; iAttr++) {
72  if (iAttr && !(iAttr % 4))
73  fprintf(f, " ");
74  fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
75  }
76  fprintf(f, "]\n");
77  }
78  else {
79  fprintf(f, "\n");
80  }
81 
82  if (role & 1) {
83  pedgeset = dglNodeGet_OutEdgeset(pgraph, pnode);
84 
85  dglEdgeset_T_Initialize(&edgeaT, pgraph, pedgeset);
86  for (i = 0, pedge = dglEdgeset_T_First(&edgeaT);
87  pedge; i++, pedge = dglEdgeset_T_Next(&edgeaT)
88  ) {
89  ptonode = dglEdgeGet_Tail(pgraph, pedge);
90 
91  if (ptonode) {
92  role = 0;
93  if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_HEAD) {
94  role |= 1;
95  }
96  if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_TAIL) {
97  role |= 2;
98  }
99 
100  fprintf(f,
101  "EDGE #%-8d: TAIL %-8ld - %-s - COST %-8ld - ID %-8ld",
102  i, dglNodeGet_Id(pgraph, ptonode),
103  (role > 2) ? "'H/T'" : (role ==
104  2) ? "'T '" : (role ==
105  1) ? "'H '" :
106  "'A '", dglEdgeGet_Cost(pgraph, pedge),
107  dglEdgeGet_Id(pgraph, pedge)
108  );
109 
110  if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
111  pnattr = dglNodeGet_Attr(pgraph, ptonode);
112  fprintf(f, " - TAIL ATTR [");
113  for (iAttr = 0; iAttr < cAttr; iAttr++) {
114  if (iAttr && !(iAttr % 4))
115  fprintf(f, " ");
116  fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
117  }
118  fprintf(f, "]");
119  }
120 
121  if ((cAttr = dglGet_EdgeAttrSize(pgraph)) > 0) {
122  pnattr = dglEdgeGet_Attr(pgraph, pedge);
123  fprintf(f, " - EDGE ATTR [");
124  for (iAttr = 0; iAttr < cAttr; iAttr++) {
125  if (iAttr && !(iAttr % 4))
126  fprintf(f, " ");
127  fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
128  }
129  fprintf(f, "]\n");
130  }
131  else {
132  fprintf(f, "\n");
133  }
134  }
135  }
136  dglEdgeset_T_Release(&edgeaT);
137  }
138  return 0;
139 }
140 
141 int main(int argc, char **argv)
142 {
143  dglGraph_s graph;
144  int fd;
145  int nret;
146 
147  /* program options
148  */
149  char *pszFilein;
150 
151  GNO_BEGIN /* short long default variable help */
152  GNO_OPTION("g", "graph", NULL, &pszFilein, "Graph file to view")
153  GNO_END if (GNO_PARSE(argc, argv) < 0)
154  {
155  return 1;
156  }
157  /*
158  * options parsed
159  */
160 
161  if (pszFilein == NULL) {
162  GNO_HELP("Incomplete parameters");
163  return 1;
164  }
165 
166  fd = open(pszFilein, O_RDONLY);
167  if (fd < 0) {
168  perror("open");
169  return 1;
170  }
171 
172  nret = dglRead(&graph, fd);
173 
174  if (nret < 0) {
175  fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
176  return 1;
177  }
178 
179  close(fd);
180 
181  /* print the header
182  */
183  fprintf(stdout, "Version: %d\n", graph.Version);
184  fprintf(stdout, "Byte Order: %s\n",
185  (graph.Endian ==
186  DGL_ENDIAN_LITTLE) ? "Little Endian" : "Big Endian");
187  fprintf(stdout, "Node Attribute Size: %ld\n", graph.NodeAttrSize);
188  fprintf(stdout, "Edge Attribute Size: %ld\n", graph.EdgeAttrSize);
189  fprintf(stdout,
190  "Counters: %ld Edges - %ld Nodes: %ld HEAD / %ld TAIL / %ld ALONE\n",
191  graph.cEdge, graph.cNode, graph.cHead, graph.cTail, graph.cAlone);
192  fprintf(stdout, "Opaque Settings:\n");
193  fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
194  graph.aOpaqueSet[0], graph.aOpaqueSet[1],
195  graph.aOpaqueSet[2], graph.aOpaqueSet[3]);
196  fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
197  graph.aOpaqueSet[4], graph.aOpaqueSet[5],
198  graph.aOpaqueSet[6], graph.aOpaqueSet[7]);
199  fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
200  graph.aOpaqueSet[8], graph.aOpaqueSet[9],
201  graph.aOpaqueSet[10], graph.aOpaqueSet[11]);
202  fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
203  graph.aOpaqueSet[12], graph.aOpaqueSet[13],
204  graph.aOpaqueSet[14], graph.aOpaqueSet[15]);
205  fprintf(stdout, "Total Cost: %lld\n", graph.nnCost);
206  fprintf(stdout, "--\n");
207 
208 
209  {
210  dglInt32_t *pnode;
211  dglNodeTraverser_s traverser;
212 
213  dglNode_T_Initialize(&traverser, &graph);
214  for (pnode = dglNode_T_First(&traverser); pnode;
215  pnode = dglNode_T_Next(&traverser)) {
216  _print_node(&graph, pnode, stdout);
217  }
218  dglNode_T_Release(&traverser);
219  }
220 
221  printf("\n");
222  dglRelease(&graph);
223  return 0;
224 
225 }