ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
xsatCnfReader.c
Go to the documentation of this file.
1
21
25#include <ctype.h>
26
28#include "misc/vec/vecInt.h"
29
30#include "xsatSolver.h"
31
33
37
49char * xSAT_FileRead( FILE * pFile )
50{
51 int nFileSize;
52 char * pBuffer;
53 int RetValue;
54 // get the file size, in bytes
55 fseek( pFile, 0, SEEK_END );
56 nFileSize = ftell( pFile );
57 // move the file current reading position to the beginning
58 rewind( pFile );
59 // load the contents of the file into memory
60 pBuffer = ABC_ALLOC( char, nFileSize + 3 );
61 RetValue = fread( pBuffer, nFileSize, 1, pFile );
62 // terminate the string with '\0'
63 pBuffer[ nFileSize + 0] = '\n';
64 pBuffer[ nFileSize + 1] = '\0';
65 return pBuffer;
66}
67
79static void skipLine( char ** pIn )
80{
81 while ( 1 )
82 {
83 if (**pIn == 0)
84 return;
85 if (**pIn == '\n')
86 {
87 (*pIn)++;
88 return;
89 }
90 (*pIn)++;
91 }
92}
93
105static int xSAT_ReadInt( char ** pIn )
106{
107 int val = 0;
108 int neg = 0;
109
110 for(; isspace(**pIn); (*pIn)++);
111 if ( **pIn == '-' )
112 neg = 1,
113 (*pIn)++;
114 else if ( **pIn == '+' )
115 (*pIn)++;
116 if ( !isdigit(**pIn) )
117 fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **pIn),
118 exit(1);
119 while ( isdigit(**pIn) )
120 val = val*10 + (**pIn - '0'),
121 (*pIn)++;
122 return neg ? -val : val;
123}
124
136static void xSAT_ReadClause( char ** pIn, xSAT_Solver_t * p, Vec_Int_t * vLits )
137{
138 int token, var, sign;
139
140 Vec_IntClear( vLits );
141 while ( 1 )
142 {
143 token = xSAT_ReadInt( pIn );
144 if ( token == 0 )
145 break;
146 var = abs(token) - 1;
147 sign = (token > 0);
148 Vec_IntPush( vLits, xSAT_Var2Lit( var, !sign ) );
149 }
150}
151
163static int xSAT_ParseDimacs( char * pText, xSAT_Solver_t ** pS )
164{
165 xSAT_Solver_t * p = NULL;
166 Vec_Int_t * vLits = NULL;
167 char * pIn = pText;
168 int nVars, nClas;
169 while ( 1 )
170 {
171 for(; isspace(*pIn); pIn++);
172 if ( *pIn == 0 )
173 break;
174 else if ( *pIn == 'c' )
175 skipLine( &pIn );
176 else if ( *pIn == 'p' )
177 {
178 pIn++;
179 for(; isspace(*pIn); pIn++);
180 for(; !isspace(*pIn); pIn++);
181
182 nVars = xSAT_ReadInt( &pIn );
183 nClas = xSAT_ReadInt( &pIn );
184 skipLine( &pIn );
185
186 /* start the solver */
188 /* allocate the vector */
189 vLits = Vec_IntAlloc( nVars );
190 }
191 else
192 {
193 if ( p == NULL )
194 {
195 printf( "There is no parameter line.\n" );
196 exit(1);
197 }
198 xSAT_ReadClause( &pIn, p, vLits );
199 if ( !xSAT_SolverAddClause( p, vLits ) )
200 {
201 Vec_IntPrint(vLits);
202 return 0;
203 }
204 }
205 }
206 Vec_IntFree( vLits );
207 *pS = p;
208 return xSAT_SolverSimplify( p );
209}
210
223{
224 char * pText;
225 int Value;
226 pText = xSAT_FileRead( pFile );
227 Value = xSAT_ParseDimacs( pText, p );
228 ABC_FREE( pText );
229 return Value;
230}
231
233
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
bool sign(Lit p)
Definition SolverTypes.h:63
unsigned short var
Definition giaNewBdd.h:35
VOID_HACK rewind()
VOID_HACK exit()
int xSAT_SolverParseDimacs(FILE *pFile, xSAT_Solver_t **p)
FUNCTION DECLARATIONS ///.
ABC_NAMESPACE_IMPL_START char * xSAT_FileRead(FILE *pFile)
INCLUDES ///.
int xSAT_SolverAddClause(xSAT_Solver_t *, Vec_Int_t *)
xSAT_Solver_t * xSAT_SolverCreate()
int xSAT_SolverSimplify(xSAT_Solver_t *)
struct xSAT_Solver_t_ xSAT_Solver_t
Definition xsat.h:36
#define SEEK_END
Definition zconf.h:392