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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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         {
00057                 role |= 1;
00058         }
00059         if ( dglNodeGet_Status(pgraph,pnode) & DGL_NS_TAIL )
00060         {
00061                 role |= 2;
00062         }
00063 
00064         fprintf( f , "HEAD %-8ld - %-s",
00065                         dglNodeGet_Id(pgraph,pnode),
00066                         (role>2)?"'H/T'":(role==2)?"'T  '":(role==1)?"'H  '":"'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) ) fprintf( f , " " );
00073                         fprintf( f , "%02x" , ((unsigned char*)pnattr)[iAttr] );
00074                 }
00075                 fprintf( f , "]\n" );
00076         }
00077         else {
00078                 fprintf( f , "\n" );
00079         }
00080 
00081         if ( role & 1 ) {
00082                 pedgeset = dglNodeGet_OutEdgeset(pgraph, pnode);
00083 
00084                 dglEdgeset_T_Initialize(&edgeaT, pgraph, pedgeset);
00085                 for     (
00086                         i = 0, pedge = dglEdgeset_T_First( &edgeaT ) ;
00087                         pedge ;
00088                         i++  , pedge = dglEdgeset_T_Next ( &edgeaT )
00089                         )
00090                 {
00091                         ptonode = dglEdgeGet_Tail(pgraph,pedge);
00092 
00093                         if ( ptonode ) {
00094                                 role = 0;
00095                                 if ( dglNodeGet_Status(pgraph,ptonode) & DGL_NS_HEAD )
00096                                 {
00097                                         role |= 1;
00098                                 }
00099                                 if ( dglNodeGet_Status(pgraph,ptonode) & DGL_NS_TAIL )
00100                                 {
00101                                         role |= 2;
00102                                 }
00103 
00104                                 fprintf( f , "EDGE #%-8d: TAIL %-8ld - %-s - COST %-8ld - ID %-8ld",
00105                                          i ,
00106                                          dglNodeGet_Id(pgraph,ptonode) ,
00107                                          (role>2)?"'H/T'":(role==2)?"'T  '":(role==1)?"'H  '":"'A  '",
00108                                          dglEdgeGet_Cost(pgraph,pedge) ,
00109                                          dglEdgeGet_Id(pgraph,pedge)
00110                                          );
00111 
00112                                 if ( (cAttr = dglGet_NodeAttrSize(pgraph)) > 0 ) {
00113                                         pnattr = dglNodeGet_Attr(pgraph,ptonode);
00114                                         fprintf( f , " - TAIL ATTR [" );
00115                                         for ( iAttr = 0 ; iAttr < cAttr ; iAttr ++ ) {
00116                                                 if ( iAttr && !(iAttr%4) ) fprintf( f , " " );
00117                                                 fprintf( f , "%02x" , ((unsigned char*)pnattr)[iAttr] );
00118                                         }
00119                                         fprintf( f , "]" );
00120                                 }
00121 
00122                                 if ( (cAttr = dglGet_EdgeAttrSize(pgraph)) > 0 ) {
00123                                         pnattr = dglEdgeGet_Attr(pgraph,pedge);
00124                                         fprintf( f , " - EDGE ATTR [" );
00125                                         for ( iAttr = 0 ; iAttr < cAttr ; iAttr ++ ) {
00126                                                 if ( iAttr && !(iAttr%4) ) 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
00154  
00155 
00156         if ( GNO_PARSE( argc , argv ) < 0 )
00157         {
00158                 return 1;
00159         }
00160         /*
00161          * options parsed
00162          */
00163 
00164         if ( pszFilein == NULL )
00165         {
00166                 GNO_HELP( "Incomplete parameters" );
00167                 return 1;
00168         }
00169 
00170         fd = open( pszFilein , O_RDONLY );
00171         if ( fd < 0 )
00172         {       
00173                 perror( "open" ); return 1;
00174         }
00175 
00176         nret = dglRead( & graph , fd );
00177 
00178         if ( nret < 0 )
00179         {
00180                 fprintf( stderr , "dglRead error: %s\n" , dglStrerror( & graph ) );
00181                 return 1;
00182         }
00183 
00184         close( fd );
00185 
00186         /* print the header
00187          */
00188         fprintf( stdout , "Version: %d\n" ,
00189                          graph.Version );
00190         fprintf( stdout , "Byte Order: %s\n" ,
00191                          (graph.Endian == DGL_ENDIAN_LITTLE)?"Little Endian":"Big Endian" );
00192         fprintf( stdout , "Node Attribute Size:  %ld\n" ,
00193                          graph.NodeAttrSize );
00194         fprintf( stdout , "Edge Attribute Size:  %ld\n" ,
00195                          graph.EdgeAttrSize );
00196         fprintf( stdout , "Counters:  %ld Edges - %ld Nodes: %ld HEAD / %ld TAIL / %ld ALONE\n" ,
00197                          graph.cEdge , graph.cNode ,
00198                          graph.cHead , graph.cTail , graph.cAlone );
00199         fprintf( stdout , "Opaque Settings:\n" );
00200         fprintf( stdout , "%10ld %10ld %10ld %10ld\n",
00201                          graph.aOpaqueSet[ 0 ], graph.aOpaqueSet[ 1 ],
00202                          graph.aOpaqueSet[ 2 ], graph.aOpaqueSet[ 3 ] );
00203         fprintf( stdout , "%10ld %10ld %10ld %10ld\n",
00204                          graph.aOpaqueSet[ 4 ], graph.aOpaqueSet[ 5 ],
00205                          graph.aOpaqueSet[ 6 ], graph.aOpaqueSet[ 7 ] );
00206         fprintf( stdout , "%10ld %10ld %10ld %10ld\n",
00207                          graph.aOpaqueSet[ 8 ], graph.aOpaqueSet[ 9 ],
00208                          graph.aOpaqueSet[ 10 ], graph.aOpaqueSet[ 11 ] );
00209         fprintf( stdout , "%10ld %10ld %10ld %10ld\n",
00210                          graph.aOpaqueSet[ 12 ], graph.aOpaqueSet[ 13 ],
00211                          graph.aOpaqueSet[ 14 ], graph.aOpaqueSet[ 15 ] );
00212         fprintf( stdout , "Total Cost: %lld\n", graph.nnCost );
00213         fprintf( stdout , "--\n" );
00214 
00215 
00216         {
00217                 dglInt32_t * pnode;
00218                 dglNodeTraverser_s traverser;
00219                 dglNode_T_Initialize( &traverser, &graph );
00220                 for (pnode = dglNode_T_First(&traverser) ; pnode ; pnode = dglNode_T_Next(&traverser)) {
00221                         _print_node( &graph, pnode, stdout );
00222                 }
00223                 dglNode_T_Release   ( & traverser );
00224         }
00225 
00226         printf( "\n" );
00227         dglRelease( & graph );
00228         return 0;
00229 
00230 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5