GRASS Programmer's Manual  6.4.2(2012)
vector/dglib/examples/view.c
Go to the documentation of this file.
00001 /* LIBDGL -- a Directed Graph Library implementation
00002  * Copyright (C) 2002 Roberto Micarelli
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00017  */
00018 
00019 /* best view tabstop=4
00020  */
00021 
00022 #include <stdio.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <unistd.h>
00026 #include <stdlib.h>
00027 #include <fcntl.h>
00028 #include <time.h>
00029 #include <errno.h>
00030 #include <ctype.h>
00031 
00032 #include "../type.h"
00033 #include "../graph.h"
00034 
00035 #include "opt.h"
00036 
00037 
00038 extern int errno;
00039 
00040 #define _EDGESET_OFFSET(pg,pl) ((int)(pl) - (int)(pg)->pEdgeBuffer)
00041 
00042 static int _print_node(dglGraph_s * pgraph, dglInt32_t * pnode, void *pvarg)
00043 {
00044     FILE *f = (FILE *) pvarg;
00045     dglInt32_t *pedgeset;
00046     dglInt32_t *pedge;
00047     dglInt32_t *ptonode;
00048     dglInt32_t *pnattr;
00049     int iAttr, cAttr;
00050     int role;
00051     int i;
00052     dglEdgesetTraverser_s edgeaT;
00053 
00054     role = 0;
00055     if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_HEAD) {
00056         role |= 1;
00057     }
00058     if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_TAIL) {
00059         role |= 2;
00060     }
00061 
00062     fprintf(f, "HEAD %-8ld - %-s",
00063             dglNodeGet_Id(pgraph, pnode),
00064             (role > 2) ? "'H/T'" : (role == 2) ? "'T  '" : (role ==
00065                                                             1) ? "'H  '" :
00066             "'A  '");
00067 
00068     if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
00069         pnattr = dglNodeGet_Attr(pgraph, pnode);
00070         fprintf(f, " - HEAD ATTR [");
00071         for (iAttr = 0; iAttr < cAttr; iAttr++) {
00072             if (iAttr && !(iAttr % 4))
00073                 fprintf(f, " ");
00074             fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
00075         }
00076         fprintf(f, "]\n");
00077     }
00078     else {
00079         fprintf(f, "\n");
00080     }
00081 
00082     if (role & 1) {
00083         pedgeset = dglNodeGet_OutEdgeset(pgraph, pnode);
00084 
00085         dglEdgeset_T_Initialize(&edgeaT, pgraph, pedgeset);
00086         for (i = 0, pedge = dglEdgeset_T_First(&edgeaT);
00087              pedge; i++, pedge = dglEdgeset_T_Next(&edgeaT)
00088             ) {
00089             ptonode = dglEdgeGet_Tail(pgraph, pedge);
00090 
00091             if (ptonode) {
00092                 role = 0;
00093                 if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_HEAD) {
00094                     role |= 1;
00095                 }
00096                 if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_TAIL) {
00097                     role |= 2;
00098                 }
00099 
00100                 fprintf(f,
00101                         "EDGE #%-8d: TAIL %-8ld - %-s - COST %-8ld - ID %-8ld",
00102                         i, dglNodeGet_Id(pgraph, ptonode),
00103                         (role > 2) ? "'H/T'" : (role ==
00104                                                 2) ? "'T  '" : (role ==
00105                                                                 1) ? "'H  '" :
00106                         "'A  '", dglEdgeGet_Cost(pgraph, pedge),
00107                         dglEdgeGet_Id(pgraph, pedge)
00108                     );
00109 
00110                 if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) {
00111                     pnattr = dglNodeGet_Attr(pgraph, ptonode);
00112                     fprintf(f, " - TAIL ATTR [");
00113                     for (iAttr = 0; iAttr < cAttr; iAttr++) {
00114                         if (iAttr && !(iAttr % 4))
00115                             fprintf(f, " ");
00116                         fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
00117                     }
00118                     fprintf(f, "]");
00119                 }
00120 
00121                 if ((cAttr = dglGet_EdgeAttrSize(pgraph)) > 0) {
00122                     pnattr = dglEdgeGet_Attr(pgraph, pedge);
00123                     fprintf(f, " - EDGE ATTR [");
00124                     for (iAttr = 0; iAttr < cAttr; iAttr++) {
00125                         if (iAttr && !(iAttr % 4))
00126                             fprintf(f, " ");
00127                         fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]);
00128                     }
00129                     fprintf(f, "]\n");
00130                 }
00131                 else {
00132                     fprintf(f, "\n");
00133                 }
00134             }
00135         }
00136         dglEdgeset_T_Release(&edgeaT);
00137     }
00138     return 0;
00139 }
00140 
00141 int main(int argc, char **argv)
00142 {
00143     dglGraph_s graph;
00144     int fd;
00145     int nret;
00146 
00147     /* program options
00148      */
00149     char *pszFilein;
00150 
00151     GNO_BEGIN                   /* short  long        default     variable        help */
00152         GNO_OPTION("g", "graph", NULL, &pszFilein, "Graph file to view")
00153     GNO_END if (GNO_PARSE(argc, argv) < 0)
00154     {
00155         return 1;
00156     }
00157     /*
00158      * options parsed
00159      */
00160 
00161     if (pszFilein == NULL) {
00162         GNO_HELP("Incomplete parameters");
00163         return 1;
00164     }
00165 
00166     fd = open(pszFilein, O_RDONLY);
00167     if (fd < 0) {
00168         perror("open");
00169         return 1;
00170     }
00171 
00172     nret = dglRead(&graph, fd);
00173 
00174     if (nret < 0) {
00175         fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
00176         return 1;
00177     }
00178 
00179     close(fd);
00180 
00181     /* print the header
00182      */
00183     fprintf(stdout, "Version: %d\n", graph.Version);
00184     fprintf(stdout, "Byte Order: %s\n",
00185             (graph.Endian ==
00186              DGL_ENDIAN_LITTLE) ? "Little Endian" : "Big Endian");
00187     fprintf(stdout, "Node Attribute Size:  %ld\n", graph.NodeAttrSize);
00188     fprintf(stdout, "Edge Attribute Size:  %ld\n", graph.EdgeAttrSize);
00189     fprintf(stdout,
00190             "Counters:  %ld Edges - %ld Nodes: %ld HEAD / %ld TAIL / %ld ALONE\n",
00191             graph.cEdge, graph.cNode, graph.cHead, graph.cTail, graph.cAlone);
00192     fprintf(stdout, "Opaque Settings:\n");
00193     fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
00194             graph.aOpaqueSet[0], graph.aOpaqueSet[1],
00195             graph.aOpaqueSet[2], graph.aOpaqueSet[3]);
00196     fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
00197             graph.aOpaqueSet[4], graph.aOpaqueSet[5],
00198             graph.aOpaqueSet[6], graph.aOpaqueSet[7]);
00199     fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
00200             graph.aOpaqueSet[8], graph.aOpaqueSet[9],
00201             graph.aOpaqueSet[10], graph.aOpaqueSet[11]);
00202     fprintf(stdout, "%10ld %10ld %10ld %10ld\n",
00203             graph.aOpaqueSet[12], graph.aOpaqueSet[13],
00204             graph.aOpaqueSet[14], graph.aOpaqueSet[15]);
00205     fprintf(stdout, "Total Cost: %lld\n", graph.nnCost);
00206     fprintf(stdout, "--\n");
00207 
00208 
00209     {
00210         dglInt32_t *pnode;
00211         dglNodeTraverser_s traverser;
00212 
00213         dglNode_T_Initialize(&traverser, &graph);
00214         for (pnode = dglNode_T_First(&traverser); pnode;
00215              pnode = dglNode_T_Next(&traverser)) {
00216             _print_node(&graph, pnode, stdout);
00217         }
00218         dglNode_T_Release(&traverser);
00219     }
00220 
00221     printf("\n");
00222     dglRelease(&graph);
00223     return 0;
00224 
00225 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines