00001 
00021 #include "msatInt.h"
00022 
00026 
00027 static char * Msat_FileRead( FILE * pFile );
00028 
00032 
00044 char * Msat_FileRead( FILE * pFile )
00045 {
00046     int nFileSize;
00047     char * pBuffer;
00048     
00049     fseek( pFile, 0, SEEK_END );  
00050     nFileSize = ftell( pFile );  
00051     
00052     rewind( pFile ); 
00053     
00054     pBuffer = ALLOC( char, nFileSize + 3 );
00055     fread( pBuffer, nFileSize, 1, pFile );
00056     
00057     pBuffer[ nFileSize + 0] = '\n';
00058     pBuffer[ nFileSize + 1] = '\0';
00059     return pBuffer;
00060 }
00061 
00073 static void Msat_ReadWhitespace( char ** pIn ) 
00074 {
00075     while ((**pIn >= 9 && **pIn <= 13) || **pIn == 32)
00076         (*pIn)++; 
00077 }
00078 
00090 static void Msat_ReadNotWhitespace( char ** pIn ) 
00091 {
00092     while ( !((**pIn >= 9 && **pIn <= 13) || **pIn == 32) )
00093         (*pIn)++; 
00094 }
00095 
00107 static void skipLine( char ** pIn ) 
00108 {
00109     while ( 1 )
00110     {
00111         if (**pIn == 0) 
00112             return;
00113         if (**pIn == '\n') 
00114         { 
00115             (*pIn)++; 
00116             return; 
00117         }
00118         (*pIn)++; 
00119     } 
00120 }
00121 
00133 static int Msat_ReadInt( char ** pIn ) 
00134 {
00135     int     val = 0;
00136     bool    neg = 0;
00137 
00138     Msat_ReadWhitespace( pIn );
00139     if ( **pIn == '-' ) 
00140         neg = 1, 
00141         (*pIn)++;
00142     else if ( **pIn == '+' ) 
00143         (*pIn)++;
00144     if ( **pIn < '0' || **pIn > '9' ) 
00145         fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **pIn), 
00146         exit(1);
00147     while ( **pIn >= '0' && **pIn <= '9' )
00148         val = val*10 + (**pIn - '0'),
00149         (*pIn)++;
00150     return neg ? -val : val; 
00151 }
00152 
00164 static void Msat_ReadClause( char ** pIn, Msat_Solver_t * p, Msat_IntVec_t * pLits ) 
00165 {
00166     int nVars = Msat_SolverReadVarNum( p );
00167     int parsed_lit, var, sign;
00168 
00169     Msat_IntVecClear( pLits );
00170     while ( 1 )
00171     {
00172         parsed_lit = Msat_ReadInt(pIn);
00173         if ( parsed_lit == 0 ) 
00174             break;
00175         var = abs(parsed_lit) - 1;
00176         sign = (parsed_lit > 0);
00177         if ( var >= nVars )
00178         {
00179             printf( "Variable %d is larger than the number of allocated variables (%d).\n", var+1, nVars );
00180             exit(1);
00181         }
00182         Msat_IntVecPush( pLits, MSAT_VAR2LIT(var, !sign) );
00183     }
00184 }
00185  
00197 static bool Msat_ReadDimacs( char * pText, Msat_Solver_t ** pS, bool fVerbose ) 
00198 {
00199     Msat_Solver_t * p;
00200     Msat_IntVec_t * pLits;
00201     char * pIn = pText;
00202     int nVars, nClas;
00203     while ( 1 )
00204     {
00205         Msat_ReadWhitespace( &pIn );
00206         if ( *pIn == 0 )
00207             break;
00208         else if ( *pIn == 'c' )
00209             skipLine( &pIn );
00210         else if ( *pIn == 'p' )
00211         {
00212             pIn++;
00213             Msat_ReadWhitespace( &pIn );
00214             Msat_ReadNotWhitespace( &pIn );
00215 
00216             nVars = Msat_ReadInt( &pIn );
00217             nClas = Msat_ReadInt( &pIn );
00218             skipLine( &pIn );
00219             
00220             p = Msat_SolverAlloc( nVars, 1, 1, 1, 1, 0 ); 
00221             Msat_SolverClean( p, nVars );
00222             Msat_SolverSetVerbosity( p, fVerbose );
00223             
00224             pLits = Msat_IntVecAlloc( nVars );
00225         }
00226         else
00227         {
00228             if ( p == NULL )
00229             {
00230                 printf( "There is no parameter line.\n" );
00231                 exit(1);
00232             }
00233             Msat_ReadClause( &pIn, p, pLits );
00234             if ( !Msat_SolverAddClause( p, pLits ) )
00235                 return 0;
00236         }
00237     }
00238     Msat_IntVecFree( pLits );
00239     *pS = p;
00240     return Msat_SolverSimplifyDB( p );
00241 }
00242 
00254 bool Msat_SolverParseDimacs( FILE * pFile, Msat_Solver_t ** p, int fVerbose )
00255 {
00256     char * pText;
00257     bool Value;
00258     pText = Msat_FileRead( pFile );
00259     Value = Msat_ReadDimacs( pText, p, fVerbose );
00260     free( pText );
00261     return Value;
00262 }
00263 
00267 
00268