parse.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<regex.h>
00025 #include<fcntl.h>
00026 #include<stdlib.h>
00027 #include<string.h>
00028 #include<sys/types.h>
00029 #include<sys/stat.h>
00030 #include<unistd.h>
00031 
00032 #include "opt.h"
00033 #include "../type.h"
00034 #include "../graph.h"
00035 
00036 static void _regmtostring( char * pszOut , int cszOut , char * pszIn , regmatch_t * pregm )
00037 {
00038     int i , iout;
00039     for ( iout = 0 , i = pregm->rm_so ; i < pregm->rm_eo && iout < cszOut-1 ; i ++ ) {
00040         if ( i >= 0 ) pszOut[iout++] = pszIn[i];
00041     }
00042     pszOut[iout] = 0;
00043 }
00044 
00045 static int _sztoattr( unsigned char * pbNodeAttr, int cbNodeAttr, char * szw ) {
00046         int i , ib;
00047         for( ib = 0 , i = 0 ; szw[i] && ib < cbNodeAttr ; i ++ ) {
00048                 if ( szw[i] == ' ' ) continue;
00049                 pbNodeAttr[ib] =        (szw[i]>='0'&&szw[i]<='9')?(szw[i]-'0')*16:
00050                                                         (szw[i]>='A'&&szw[i]<='F')?(10+(szw[i]-'A'))*16:
00051                                                         (szw[i]>='a'&&szw[i]<='f')?(10+(szw[i]-'a'))*16:
00052                                                         0;
00053                 i++;
00054                 if ( szw[i] ) {
00055                         pbNodeAttr[ib] +=       (szw[i]>='0'&&szw[i]<='9')?(szw[i]-'0'):
00056                                                                 (szw[i]>='A'&&szw[i]<='F')?(10+(szw[i]-'A')):
00057                                                                 (szw[i]>='a'&&szw[i]<='f')?(10+(szw[i]-'a')):
00058                                                                 0;
00059                 }
00060                 ib++;
00061         }
00062         return ib;
00063 }
00064 
00065 int main( int argc , char ** argv ) {
00066         FILE * fp;
00067         char sz[1024];
00068         char szw[1024];
00069         int nret;
00070         regmatch_t aregm[64];
00071         dglInt32_t nVersion;
00072         dglInt32_t nNodeAttrSize;
00073         dglInt32_t nEdgeAttrSize;
00074         dglInt32_t anOpaque[16];
00075         int i , fd , cOut;
00076 
00077         regex_t reVersion;
00078         regex_t reByteOrder;
00079         regex_t reNodeAttrSize;
00080         regex_t reEdgeAttrSize;
00081         regex_t reCounters;
00082         regex_t reOpaque;
00083         regex_t reNodeFrom;
00084         regex_t reNodeAttr;
00085         regex_t reEdge;
00086         regex_t reToNodeAttr;
00087         regex_t reEdgeAttr;
00088 
00089 
00090         dglInt32_t      nNodeFrom , nNodeTo , nUser , nCost;
00091 
00092         int fInOpaque;
00093         int fInBody;
00094 
00095         unsigned char * pbNodeAttr , * pbEdgeAttr , * pbToNodeAttr;
00096 
00097         struct stat statdata;
00098 
00099         dglGraph_s graphOut;
00100 
00101         /* program options
00102          */
00103         char    *       pszFilein;
00104         char    *       pszGraphout;
00105  
00106         GNO_BEGIN/* short  long        default     variable        help */
00107         GNO_OPTION( "i",        "input",        NULL ,          & pszFilein ,   "Input text file" )
00108         GNO_OPTION( "o",        "output",       NULL ,          & pszGraphout , "Output graph file" )
00109         GNO_END
00110  
00111 
00112         if ( GNO_PARSE( argc , argv ) < 0 )
00113         {
00114                 return 1;
00115         }
00116         /*
00117          * options parsed
00118          */
00119 
00120         if ( pszFilein == NULL )
00121         {
00122                 GNO_HELP("... usage");
00123                 return 1;
00124         }
00125 
00126         /*
00127          * compile header expressions
00128          */
00129         printf( "Compile header expressions..."); fflush(stdout);
00130         i = 0;
00131         if ( regcomp( &reVersion,"^Version:[ ]+([0-9]+)" , REG_EXTENDED ) != 0) goto regc_error;
00132         i++;
00133         if ( regcomp( &reByteOrder,"^Byte Order:[ ]+(.+)" , REG_EXTENDED ) != 0) goto regc_error;
00134         i++;
00135         if ( regcomp( &reNodeAttrSize,"^Node Attribute Size:[ ]+([0-9]+)" , REG_EXTENDED ) != 0) goto regc_error;
00136         i++;
00137         if ( regcomp( &reEdgeAttrSize,"^Edge Attribute Size:[ ]+([0-9]+)" , REG_EXTENDED ) != 0) goto regc_error;
00138         i++;
00139         if ( regcomp( &reCounters,"^Counters:[ ]+.*" , REG_EXTENDED ) != 0) goto regc_error;
00140         i++;
00141         if ( regcomp( &reOpaque,"^Opaque Settings:" , REG_EXTENDED ) != 0) goto regc_error;
00142         i++;
00143         printf( "done.\n" );
00144 
00145         /*
00146          * compile body expressions
00147          */
00148 
00149         printf( "Compile body expressions..."); fflush(stdout);
00150         if ( regcomp( &reNodeFrom, "^HEAD ([0-9]+)[ ]*- [HT/']+" , REG_EXTENDED ) != 0) goto regc_error;
00151         i++;
00152         if ( regcomp( &reNodeAttr, ".*HEAD ATTR [[]([0-9a-fA-F ]+)]" , REG_EXTENDED ) != 0) goto regc_error;
00153         i++;
00154 
00155         if (regcomp(&reEdge, "^EDGE #([0-9]+)[ ]*: TAIL ([0-9]+)[ ]*- [HT/']+[ ]+- COST ([0-9]+)[ ]*- ID ([0-9]+)", REG_EXTENDED) != 0) goto regc_error;
00156         i++;
00157         if (regcomp(&reToNodeAttr, ".*TAIL ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED) != 0) goto regc_error;
00158         i++;
00159         if (regcomp(&reEdgeAttr,   ".*EDGE ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED) != 0) goto regc_error;
00160         i++;
00161 
00162         printf( "done.\n" );
00163 
00164         goto regc_ok;
00165 
00166 
00167 
00168 
00169 regc_error:
00170         fprintf( stderr, "regex compilation error %d\n", i );
00171         exit(1);
00172 
00173 
00174 
00175 
00176 
00177 regc_ok:
00178 
00179         if ( (fp = fopen( pszFilein , "r" )) == NULL )
00180         {
00181                 perror( "fopen" ); return 1;
00182         }
00183 
00184         fstat( fileno(fp), &statdata );
00185 
00186         fInOpaque = 0;
00187         fInBody = 0;
00188 
00189         nNodeAttrSize = 0;
00190         nEdgeAttrSize = 0;
00191         pbNodeAttr = NULL;
00192         pbToNodeAttr = NULL;
00193         pbEdgeAttr = NULL;
00194 
00195         cOut = 0;
00196 
00197         while( fgets( sz , sizeof( sz ) , fp ) )
00198         {
00199 #ifndef VERBOSE
00200                 if ( !(cOut++ % 512) || ftell(fp) == statdata.st_size )
00201                         printf( "Parse input file ... status: %ld/%ld\r", ftell(fp), statdata.st_size ); fflush(stdout);
00202 #endif
00203 
00204 #ifdef VERYVERBOSE
00205         printf( "<<<%s>>>\n", sz );
00206 #endif
00207                 if ( fInOpaque == 0 && fInBody == 0 ) {
00208                         if ( regexec( &reVersion, sz, 64, aregm, 0 ) == 0 ) {
00209                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00210                                 nVersion = atoi( szw );
00211 #ifdef VERYVERBOSE
00212         printf( "-- version %d\n", nVersion );
00213 #endif
00214                         }
00215                         else if ( regexec( &reByteOrder, sz, 64, aregm, 0 ) == 0 ) {
00216                         }
00217                         else if ( regexec( &reNodeAttrSize, sz, 64, aregm, 0 ) == 0 ) {
00218                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00219                                 nNodeAttrSize = atoi( szw );
00220                                 if ( nNodeAttrSize ) {
00221                                         pbNodeAttr = (unsigned char *) malloc( nNodeAttrSize );
00222                                         if ( pbNodeAttr == NULL ) {
00223                                                 fprintf( stderr , "Memory Exhausted\n" ); exit(1);
00224                                         }
00225                                         pbToNodeAttr = (unsigned char *) malloc( nNodeAttrSize );
00226                                         if ( pbToNodeAttr == NULL ) {
00227                                                 fprintf( stderr , "Memory Exhausted\n" ); exit(1);
00228                                         }
00229                                 }
00230 #ifdef VERYVERBOSE
00231                                 printf( "-- node attr size %d\n", nNodeAttrSize );
00232 #endif
00233                         }
00234                         else if ( regexec( &reEdgeAttrSize, sz, 64, aregm, 0 ) == 0 ) {
00235                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00236                                 nEdgeAttrSize = atoi( szw );
00237                                 if ( nEdgeAttrSize > 0 ) {
00238                                         pbEdgeAttr = (unsigned char *) malloc( nEdgeAttrSize );
00239                                         if ( pbEdgeAttr == NULL ) {
00240                                                 fprintf( stderr , "Memory Exhausted\n" ); exit(1);
00241                                         }
00242                                 }
00243 #ifdef VERYVERBOSE
00244                                 printf( "-- edge attr size %d\n", nEdgeAttrSize );
00245 #endif
00246                         }
00247                         else if ( regexec( &reOpaque, sz, 64, aregm, 0 ) == 0 ) {
00248 #ifdef VERYVERBOSE
00249         printf( "-- opaque...\n" );
00250 #endif
00251                                 fInOpaque = 1;
00252                         }
00253                         else if ( strncmp( sz , "--" , 2) == 0 ) {
00254                                 nret = dglInitialize(
00255                                                         & graphOut,
00256                                                         nVersion,
00257                                                         nNodeAttrSize,
00258                                                         nEdgeAttrSize,
00259                                                         anOpaque
00260                                                         );
00261                                 if ( nret < 0 ) {
00262                                         fprintf( stderr, "dglInitialize error %s\n", dglStrerror( & graphOut ) );
00263                                         exit(1);
00264                                 }
00265 #ifdef VERBOSE
00266                                 printf( "Initialize: Version=%ld NodeAttr=%ld EdgeAttr=%ld\n",
00267                                                         nVersion, nNodeAttrSize, nEdgeAttrSize );
00268 #endif
00269                                 fInBody = 1;
00270                         }
00271                 }
00272                 else if ( fInOpaque > 0 && fInBody == 0 ) {
00273                         if ( fInOpaque == 1 ) {
00274                                 sscanf( sz , "%ld %ld %ld %ld",
00275                                         & anOpaque[0],
00276                                         & anOpaque[1],
00277                                         & anOpaque[2],
00278                                         & anOpaque[3] );
00279                                 fInOpaque ++;
00280 #ifdef VERYVERBOSE
00281         printf("opaque 1: %ld %ld %ld %ld\n",
00282                                         anOpaque[0],
00283                                         anOpaque[1],
00284                                         anOpaque[2],
00285                                         anOpaque[3] );
00286 #endif
00287 
00288                         }
00289                         else if ( fInOpaque == 2 ) {
00290                                 sscanf( sz , "%ld %ld %ld %ld",
00291                                         & anOpaque[4],
00292                                         & anOpaque[5],
00293                                         & anOpaque[6],
00294                                         & anOpaque[7] );
00295 #ifdef VERYVERBOSE
00296         printf("opaque 2: %ld %ld %ld %ld\n",
00297                                         anOpaque[4],
00298                                         anOpaque[5],
00299                                         anOpaque[6],
00300                                         anOpaque[7] );
00301 #endif
00302                                 fInOpaque ++;
00303                         }
00304                         else if ( fInOpaque == 3 ) {
00305                                 sscanf( sz , "%ld %ld %ld %ld",
00306                                         & anOpaque[8],
00307                                         & anOpaque[9],
00308                                         & anOpaque[10],
00309                                         & anOpaque[11] );
00310 #ifdef VERYVERBOSE
00311         printf("opaque 3: %ld %ld %ld %ld\n",
00312                                         anOpaque[8],
00313                                         anOpaque[9],
00314                                         anOpaque[10],
00315                                         anOpaque[11] );
00316 #endif
00317                                 fInOpaque ++;
00318                         }
00319                         else if ( fInOpaque == 4 ) {
00320                                 sscanf( sz , "%ld %ld %ld %ld",
00321                                         & anOpaque[12],
00322                                         & anOpaque[13],
00323                                         & anOpaque[14],
00324                                         & anOpaque[15] );
00325 #ifdef VERYVERBOSE
00326         printf("opaque 4: %ld %ld %ld %ld\n",
00327                                         anOpaque[12],
00328                                         anOpaque[13],
00329                                         anOpaque[14],
00330                                         anOpaque[15] );
00331 #endif
00332                                 fInOpaque = 0;
00333                         }
00334                 }
00335                 else if ( fInBody == 1 ) {
00336                         if ( regexec( &reNodeFrom, sz, 64, aregm, 0 ) == 0 ) {
00337                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00338 #ifdef VERYVERBOSE
00339                                 printf( "node from snippet = %s\n", szw);
00340 #endif
00341                                 nNodeFrom = atol(szw);
00342                                 if ( nNodeAttrSize > 0 ) {
00343                                         if ( regexec( &reNodeAttr, sz, 64, aregm, 0 ) == 0 ) {
00344                                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00345                                                 if ( _sztoattr( pbNodeAttr, nNodeAttrSize, szw ) != nNodeAttrSize ) {
00346                                                         fprintf( stderr, "node attr size mismatch\n" );
00347                                                 }
00348 #ifdef VERYVERBOSE
00349                                                 {
00350                                                                 int k;
00351                                                                 for(k=0;k<nNodeAttrSize;k++) {
00352                                                                 printf("%02x", pbNodeAttr[k]);
00353                                                                 }
00354                                                                 printf("\n");
00355                                                 }
00356 #endif
00357                                         }
00358                                 }
00359                         }
00360                         else if ( regexec( &reEdge, sz, 64, aregm, 0 ) == 0 ) {
00361                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[2] );
00362                                 nNodeTo = atol(szw);
00363                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[3] );
00364                                 nCost = atol(szw);
00365                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[4] );
00366                                 nUser = atol(szw);
00367                                 if ( nEdgeAttrSize > 0 ) {
00368                                         if ( regexec( &reEdgeAttr, sz, 64, aregm, 0 ) == 0 ) {
00369                                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00370                                                 if ( _sztoattr( pbEdgeAttr, nEdgeAttrSize, szw ) != nEdgeAttrSize ) {
00371                                                         fprintf( stderr, "edge attr size mismatch\n" );
00372                                                 }
00373 #ifdef VERYVERBOSE
00374                                                 {
00375                                                                 int k;
00376                                                                 for(k=0;k<nEdgeAttrSize;k++) {
00377                                                                 printf("%02x", pbEdgeAttr[k]);
00378                                                                 }
00379                                                                 printf("\n");
00380                                                 }
00381 #endif
00382                                         }
00383                                 }
00384                                 if ( nNodeAttrSize > 0 ) {
00385                                         if ( regexec( &reToNodeAttr, sz, 64, aregm, 0 ) == 0 ) {
00386                                                 _regmtostring( szw , sizeof(szw) , sz , & aregm[1] );
00387                                                 if ( _sztoattr( pbToNodeAttr, nNodeAttrSize, szw ) != nNodeAttrSize ) {
00388                                                         fprintf( stderr, "to node attr size mismatch\n" );
00389                                                 }
00390 #ifdef VERYVERBOSE
00391                                                 {
00392                                                                 int k;
00393                                                                 for(k=0;k<nNodeAttrSize;k++) {
00394                                                                 printf("%02x", pbToNodeAttr[k]);
00395                                                                 }
00396                                                                 printf("\n");
00397                                                 }
00398 #endif
00399                                         }
00400                                 }
00401                                 nret = dglAddEdgeX(
00402                                                                 & graphOut,
00403                                                                 nNodeFrom,
00404                                                                 nNodeTo,
00405                                                                 nCost,
00406                                                                 nUser,
00407                                                                 pbNodeAttr,
00408                                                                 pbToNodeAttr,
00409                                                                 pbEdgeAttr,
00410                                                                 0 );
00411 
00412                                 if ( nret < 0 ) {
00413                                         fprintf( stderr, "dglAddEdge error %s\n", dglStrerror( & graphOut ) );
00414                                         exit(1);
00415                                 }
00416 #ifdef VERBOSE
00417                                 printf( "AddEdge: from=%ld to=%ld cost=%ld user=%ld\n", nNodeFrom, nNodeTo, nCost, nUser);
00418 #endif
00419                         }
00420                 }
00421         }
00422 #ifndef VERBOSE
00423         printf( "\ndone.\n" );
00424 #endif
00425 
00426         fclose( fp );
00427 
00428         regfree( & reVersion );
00429         regfree( & reByteOrder );
00430         regfree( & reNodeAttrSize );
00431         regfree( & reEdgeAttrSize );
00432         regfree( & reCounters );
00433         regfree( & reOpaque );
00434         regfree( & reNodeFrom );
00435         regfree( & reNodeAttr );
00436         regfree( & reEdge );
00437         regfree( & reToNodeAttr );
00438         regfree( & reEdgeAttr );
00439 
00440         if ( pbNodeAttr ) free( pbNodeAttr );
00441         if ( pbToNodeAttr ) free( pbToNodeAttr );
00442         if ( pbEdgeAttr ) free( pbEdgeAttr );
00443 
00444 
00445         printf( "Flatten..." ); fflush(stdout);
00446         nret = dglFlatten( & graphOut );
00447         if ( nret < 0 ) {
00448                 fprintf( stderr, "dglFlatten error %s\n", dglStrerror( &graphOut ) );
00449                 exit(1);
00450         }
00451         printf( "done.\n" );
00452 
00453         if ( pszGraphout ) {
00454                 fd = open( pszGraphout , O_WRONLY | O_CREAT | O_TRUNC , 0666 );
00455                 if ( fd < 0 ) {
00456                         perror( "open" ); exit(1);
00457                 }
00458 
00459                 printf( "Write <%s>..." , pszGraphout ); fflush(stdout);
00460                 nret = dglWrite( & graphOut , fd );
00461                 if ( nret < 0 ) {
00462                         fprintf( stderr, "dglWrite error %s\n", dglStrerror( &graphOut ) );
00463                         exit(1);
00464                 }
00465                 printf( "done.\n" );
00466                 close(fd);
00467         }
00468 
00469         printf( "Release..." ); fflush(stdout);
00470         dglRelease( &graphOut );
00471         printf( "done.\n" );
00472 
00473         return 0;
00474 }

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