cr_from_a.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 /*
00020  * Source best viewed with tabstop=4
00021  */
00022 
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #include <unistd.h>
00028 #include <stdlib.h>
00029 #include <fcntl.h>
00030 #include <time.h>
00031 #include <errno.h>
00032 
00033 #include "../type.h"
00034 #include "../graph.h"
00035 
00036 #include "opt.h"
00037 
00038 extern int errno;
00039 
00040 
00041 
00042 int main( int argc , char ** argv )
00043 {
00044         FILE *                  fp;
00045         dglGraph_s      graph;
00046         char                    sz[ 1024 ];
00047         char                    c;
00048         int                             nret , fd;
00049         int                     version , attrsize;
00050         dglInt32_t              nodeid , from , to , cost , user , xyz[3];
00051         dglInt32_t              opaqueset[ 16 ] = {
00052                 360000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00053         };
00054 
00055         /* program options
00056          */
00057         char    *       pszFilein;
00058         char    *       pszFileout;
00059  
00060         GNO_BEGIN/* short  long        default     variable        help */
00061         GNO_OPTION( "f",        "file",         NULL ,          & pszFilein ,   "Input Graph definition file" )
00062         GNO_OPTION( "g",        "graph",        NULL ,          & pszFileout ,  "Output Graph file" )
00063         GNO_END
00064  
00065 
00066         if ( GNO_PARSE( argc , argv ) < 0 )
00067         {
00068                 return 1;
00069         }
00070         /*
00071          * options parsed
00072          */
00073 
00074         if ( pszFilein == NULL )
00075         {
00076                 GNO_HELP( "Incomplete parameters" );
00077                 return 1;
00078         }
00079 
00080         if ( (fp = fopen( pszFilein , "r" )) == NULL )
00081         {
00082                 perror( "fopen" ); return 1;
00083         }
00084 
00085 reread_first_line:
00086         if( fgets( sz , sizeof( sz ) , fp ) == NULL )
00087         {
00088                 fprintf( stderr , "unexpected EOF\n" ); return 1;
00089         }
00090 
00091         if ( sz[0] == '#' || strlen( sz ) == 0 ) goto reread_first_line;
00092 
00093         sscanf( sz , "%d %d" , & version , &attrsize );
00094 
00095         /*
00096          * initialize the graph
00097          */
00098         dglInitialize (
00099                                         & graph ,                               /* graph context to initialize */
00100                                         version ,                               /* version */
00101                                         sizeof(xyz) ,                   /* node attributes size */
00102                                         0,                                              /* edge attributes size */
00103                                         opaqueset                               /* opaque graph parameters */
00104                                         );
00105 
00106         /*
00107          * generate edge cost prioritizing
00108          */
00109         dglSet_Options( & graph, DGL_GO_EdgePrioritize_COST );
00110 
00111 
00112         /* add arcs and X/Y/Z node attributes
00113          */
00114         while( fgets( sz , sizeof( sz ) , fp ) != NULL )
00115         {
00116                 if ( sz[0] == '#' ) continue;
00117 
00118                 if ( strlen( sz ) == 0 ) continue;
00119 
00120                 if ( sz[0] == 'A' ) /* add a edge */
00121                 {
00122                         sscanf( sz , "%c %ld %ld %ld %ld" , &c , &from , &to , &cost , &user );
00123 
00124                         nret = dglAddEdge       (
00125                                                                 & graph ,
00126                                                                 from ,
00127                                                                 to ,
00128                                                                 cost ,
00129                                                                 user
00130                                                                 );
00131 
00132 
00133                         if ( nret < 0 )
00134                         {
00135                                 fprintf( stderr , "dglAddArc error: %s\n" , dglStrerror( & graph ) );
00136                                 return 1;
00137                         }
00138                 }
00139                 else if ( sz[0] == 'V' ) /* add a node */
00140                 {
00141                         sscanf( sz , "%c %ld" , &c , &nodeid );
00142 
00143                         printf( "add node: %ld\n" , nodeid );
00144 
00145                         nret = dglAddNode( & graph , nodeid , NULL, 0 );
00146 
00147                         if ( nret < 0 )
00148                         {
00149                                 fprintf( stderr , "dglAddNode error: %s\n" , dglStrerror( & graph ) );
00150                                 return 1;
00151                         }
00152                 }
00153                 else if ( sz[0] == 'N' ) /* set attributes for a (already inserted) node */
00154                 {
00155                         sscanf( sz , "%c %ld %ld %ld %ld" , &c , &nodeid , &xyz[0] , &xyz[1] , &xyz[2] );
00156 
00157                         dglNodeSet_Attr(
00158                                                         & graph ,
00159                                                         dglGetNode( & graph, nodeid ),
00160                                                         xyz
00161                                                         );
00162                 }
00163         }
00164         fclose( fp );
00165 
00166 
00167 #if 0 /* show edges */
00168         {
00169                 dglEdgeTraverser_s t;
00170                 dglInt32_t * pEdge;
00171                 nret = dglEdge_T_Initialize(&t, &graph, dglGet_EdgePrioritizer(&graph));
00172                 if ( nret < 0 ) {
00173                         fprintf( stderr , "\ndglEdge_T_Initialize error: %s\n" , dglStrerror(&graph) );
00174                         return 1;
00175                 }
00176                 for( pEdge = dglEdge_T_First(&t); pEdge; pEdge = dglEdge_T_Next(&t) ) {
00177                         printf( "edge: id=%ld cost=%ld\n",
00178                                                 dglEdgeGet_Id(&graph, pEdge),
00179                                                 dglEdgeGet_Cost(&graph, pEdge) );
00180                 }
00181                 dglEdge_T_Release(&t);
00182         }
00183 #endif
00184 
00185         /*
00186          * flatten the graph (make it serializable)
00187          */
00188         nret = dglFlatten( & graph );
00189 
00190         if ( nret < 0 )
00191         {
00192                 fprintf( stderr , "dglFlatten error: %s\n" , dglStrerror( & graph ) );
00193                 return 1;
00194         }
00195 
00196         /*
00197          * store the graph
00198          */
00199         if ( (fd = open( pszFileout , O_WRONLY | O_CREAT , 0666 )) < 0 )
00200         {
00201                 perror( "open" ); return 1;
00202         }
00203 
00204         nret = dglWrite( & graph , fd );
00205 
00206         if ( nret < 0 )
00207         {
00208                 fprintf( stderr , "dglWrite error: %s\n" , dglStrerror( & graph ) );
00209                 return 1;
00210         }
00211 
00212         close( fd );
00213 
00214         /*
00215          * finish
00216          */
00217         dglRelease( & graph );
00218 
00219         return 0;
00220 }

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