xsatCnfReader.c 5.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/**CFile****************************************************************

  FileName    [xsatCnfReader.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [xSAT - A SAT solver written in C.
               Read the license file for more info.]

  Synopsis    [CNF DIMACS file format parser.]

  Author      [Bruno Schmitt <boschmitt@inf.ufrgs.br>]

  Affiliation [UC Berkeley / UFRGS]

  Date        [Ver. 1.0. Started - November 10, 2016.]

  Revision    []

***********************************************************************/

////////////////////////////////////////////////////////////////////////
///                          INCLUDES                                ///
////////////////////////////////////////////////////////////////////////
#include <ctype.h>

#include "misc/util/abc_global.h"
#include "misc/vec/vecInt.h"

#include "xsatSolver.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Read the file into the internal buffer.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * xSAT_FileRead( FILE * pFile )
{
    int nFileSize;
    char * pBuffer;
    int RetValue;
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );
    nFileSize = ftell( pFile );
    // move the file current reading position to the beginning
    rewind( pFile );
    // load the contents of the file into memory
    pBuffer = ABC_ALLOC( char, nFileSize + 3 );
    RetValue = fread( pBuffer, nFileSize, 1, pFile );
    // terminate the string with '\0'
    pBuffer[ nFileSize + 0] = '\n';
    pBuffer[ nFileSize + 1] = '\0';
    return pBuffer;
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static void skipLine( char ** pIn )
{
    while ( 1 )
    {
        if (**pIn == 0)
            return;
        if (**pIn == '\n')
        {
            (*pIn)++;
            return;
        }
        (*pIn)++;
    }
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static int xSAT_ReadInt( char ** pIn )
{
    int val = 0;
    int neg = 0;

    for(; isspace(**pIn); (*pIn)++);
    if ( **pIn == '-' )
        neg = 1,
        (*pIn)++;
    else if ( **pIn == '+' )
        (*pIn)++;
    if ( !isdigit(**pIn) )
        fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **pIn),
        exit(1);
    while ( isdigit(**pIn) )
        val = val*10 + (**pIn - '0'),
        (*pIn)++;
    return neg ? -val : val;
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static void xSAT_ReadClause( char ** pIn, xSAT_Solver_t * p, Vec_Int_t * vLits )
{
    int token, var, sign;

    Vec_IntClear( vLits );
    while ( 1 )
    {
        token = xSAT_ReadInt( pIn );
        if ( token == 0 )
            break;
        var = abs(token) - 1;
        sign = (token > 0);
        Vec_IntPush( vLits, xSAT_Var2Lit( var, !sign ) );
    }
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static int xSAT_ParseDimacs( char * pText, xSAT_Solver_t ** pS )
{
    xSAT_Solver_t * p = NULL;
    Vec_Int_t * vLits = NULL;
    char * pIn = pText;
    int nVars, nClas;
    while ( 1 )
    {
        for(; isspace(*pIn); pIn++);
        if ( *pIn == 0 )
            break;
        else if ( *pIn == 'c' )
            skipLine( &pIn );
        else if ( *pIn == 'p' )
        {
            pIn++;
            for(; isspace(*pIn); pIn++);
            for(; !isspace(*pIn); pIn++);

            nVars = xSAT_ReadInt( &pIn );
            nClas = xSAT_ReadInt( &pIn );
            skipLine( &pIn );

            /* start the solver */
            p = xSAT_SolverCreate();
            /* allocate the vector */
            vLits = Vec_IntAlloc( nVars );
        }
        else
        {
            if ( p == NULL )
            {
                printf( "There is no parameter line.\n" );
                exit(1);
            }
            xSAT_ReadClause( &pIn, p, vLits );
            if ( !xSAT_SolverAddClause( p, vLits ) )
            {
                Vec_IntPrint(vLits);
                return 0;
            }
        }
    }
    Vec_IntFree( vLits );
    *pS = p;
    return xSAT_SolverSimplify( p );
}

/**Function*************************************************************

  Synopsis    [Starts the solver and reads the DIMAC file.]

  Description [Returns FALSE upon immediate conflict.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
int xSAT_SolverParseDimacs( FILE * pFile, xSAT_Solver_t ** p )
{
    char * pText;
    int  Value;
    pText = xSAT_FileRead( pFile );
    Value = xSAT_ParseDimacs( pText, p );
    ABC_FREE( pText );
    return Value;
}

ABC_NAMESPACE_IMPL_END

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////