ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
extraUtilReader.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include "extra.h"
23#include "misc/vec/vec.h"
24
25#if (__GNUC__ >= 8)
26 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
27#endif
28
30
34
35#define EXTRA_BUFFER_SIZE 4*1048576 // 1M - size of the data chunk stored in memory
36#define EXTRA_OFFSET_SIZE 4096 // 4K - load new data when less than this is left
37
38#define EXTRA_MINIMUM(a,b) (((a) < (b))? (a) : (b))
39
41{
42 // the input file
43 char * pFileName; // the input file name
44 FILE * pFile; // the input file pointer
45 int nFileSize; // the total number of bytes in the file
46 int nFileRead; // the number of bytes currently read from file
47 // info about processing different types of input chars
48 char pCharMap[256]; // the character map
49 // temporary storage for data
50 char * pBuffer; // the buffer
51 int nBufferSize; // the size of the buffer
52 char * pBufferCur; // the current reading position
53 char * pBufferEnd; // the first position not used by currently loaded data
54 char * pBufferStop; // the position where loading new data will be done
55 // tokens given to the user
56 Vec_Ptr_t * vTokens; // the vector of tokens returned to the user
57 Vec_Int_t * vLines; // the vector of line numbers for each token
58 int nLineCounter; // the counter of lines processed
59 // status of the parser
60 int fStop; // this flag goes high when the end of file is reached
61};
62
63// character types
64typedef enum {
65 EXTRA_CHAR_COMMENT, // a character that begins the comment
66 EXTRA_CHAR_NORMAL, // a regular character
67 EXTRA_CHAR_STOP, // a character that delimits a series of tokens
68 EXTRA_CHAR_CLEAN // a character that should be cleaned
70
71// the static functions
72static void * Extra_FileReaderGetTokens_int( Extra_FileReader_t * p );
73static void Extra_FileReaderReload( Extra_FileReader_t * p );
74
78
91 char * pCharsComment, char * pCharsStop, char * pCharsClean )
92{
94 FILE * pFile;
95 char * pChar;
96 int nCharsToRead;
97 int RetValue;
98 // check if the file can be opened
99 pFile = fopen( pFileName, "rb" );
100 if ( pFile == NULL )
101 {
102 printf( "Extra_FileReaderAlloc(): Cannot open input file \"%s\".\n", pFileName );
103 return NULL;
104 }
105 // start the file reader
107 memset( p, 0, sizeof(Extra_FileReader_t) );
108 p->pFileName = pFileName;
109 p->pFile = pFile;
110 // set the character map
111 memset( p->pCharMap, EXTRA_CHAR_NORMAL, 256 );
112 for ( pChar = pCharsComment; *pChar; pChar++ )
113 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_COMMENT;
114 for ( pChar = pCharsStop; *pChar; pChar++ )
115 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_STOP;
116 for ( pChar = pCharsClean; *pChar; pChar++ )
117 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_CLEAN;
118 // get the file size, in bytes
119 fseek( pFile, 0, SEEK_END );
120 p->nFileSize = ftell( pFile );
121 rewind( pFile );
122 // allocate the buffer
123 p->pBuffer = ABC_ALLOC( char, EXTRA_BUFFER_SIZE+1 );
124 p->nBufferSize = EXTRA_BUFFER_SIZE;
125 p->pBufferCur = p->pBuffer;
126 // determine how many chars to read
127 nCharsToRead = EXTRA_MINIMUM(p->nFileSize, EXTRA_BUFFER_SIZE);
128 // load the first part into the buffer
129 RetValue = fread( p->pBuffer, nCharsToRead, 1, p->pFile );
130 p->nFileRead = nCharsToRead;
131 // set the ponters to the end and the stopping point
132 p->pBufferEnd = p->pBuffer + nCharsToRead;
133 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE;
134 // start the arrays
135 p->vTokens = Vec_PtrAlloc( 100 );
136 p->vLines = Vec_IntAlloc( 100 );
137 p->nLineCounter = 1; // 1-based line counting
138 return p;
139}
140
153{
154 if ( p->pFile )
155 fclose( p->pFile );
156 ABC_FREE( p->pBuffer );
157 Vec_PtrFree( p->vTokens );
158 Vec_IntFree( p->vLines );
159 ABC_FREE( p );
160}
161
174{
175 return p->pFileName;
176}
177
190{
191 return p->nFileSize;
192}
193
206{
207 return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
208}
209
222{
223 assert( iToken >= 0 && iToken < p->vTokens->nSize );
224 return p->vLines->pArray[iToken];
225}
226
227
240{
241 Vec_Ptr_t * vTokens;
242 while ( (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens_int( p )) )
243 if ( vTokens->nSize > 0 )
244 break;
245 return vTokens;
246}
247
259void * Extra_FileReaderGetTokens_int( Extra_FileReader_t * p )
260{
261 char * pChar;
262 int fTokenStarted, MapValue;
263 if ( p->fStop )
264 return NULL;
265 // reset the token info
266 p->vTokens->nSize = 0;
267 p->vLines->nSize = 0;
268 fTokenStarted = 0;
269 // check if the new data should to be loaded
270 if ( p->pBufferCur > p->pBufferStop )
271 Extra_FileReaderReload( p );
272
273// printf( "%d\n", p->pBufferEnd - p->pBufferCur );
274
275 // process the string starting from the current position
276 for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
277 {
278 // count the lines
279 if ( *pChar == '\n' )
280 p->nLineCounter++;
281 // switch depending on the character
282 MapValue = p->pCharMap[(int)*pChar];
283
284// printf( "Char value = %d. Map value = %d.\n", *pChar, MapValue );
285
286
287 switch ( MapValue )
288 {
290 if ( *pChar != '/' || *(pChar+1) == '/' )
291 { // dealing with the need to have // as a comment
292 // if the token was being written, stop it
293 if ( fTokenStarted )
294 fTokenStarted = 0;
295 // eraze the comment till the end of line
296 while ( *pChar != '\n' )
297 {
298 *pChar++ = 0;
299 if ( pChar == p->pBufferEnd )
300 { // this failure is due to the fact the comment continued
301 // through EXTRA_OFFSET_SIZE chars till the end of the buffer
302 printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
303 return NULL;
304 }
305 }
306 pChar--;
307 break;
308 }
309 // otherwise it is a normal character
311 if ( !fTokenStarted )
312 {
313 Vec_PtrPush( p->vTokens, pChar );
314 Vec_IntPush( p->vLines, p->nLineCounter );
315 fTokenStarted = 1;
316 }
317 break;
318 case EXTRA_CHAR_STOP:
319 if ( fTokenStarted )
320 fTokenStarted = 0;
321 *pChar = 0;
322 // prepare before leaving
323 p->pBufferCur = pChar + 1;
324 return p->vTokens;
325 case EXTRA_CHAR_CLEAN:
326 if ( fTokenStarted )
327 fTokenStarted = 0;
328 *pChar = 0;
329 break;
330 default:
331 assert( 0 );
332 }
333 }
334 // the file is finished or the last part continued
335 // through EXTRA_OFFSET_SIZE chars till the end of the buffer
336 if ( p->pBufferStop == p->pBufferEnd ) // end of file
337 {
338 *pChar = 0;
339 p->fStop = 1;
340 return p->vTokens;
341 }
342 printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
343/*
344 {
345 int i;
346 for ( i = 0; i < p->vTokens->nSize; i++ )
347 printf( "%s ", p->vTokens->pArray[i] );
348 printf( "\n" );
349 }
350*/
351 return NULL;
352}
353
365void Extra_FileReaderReload( Extra_FileReader_t * p )
366{
367 int nCharsUsed, nCharsToRead;
368 int RetValue;
369 assert( !p->fStop );
370 assert( p->pBufferCur > p->pBufferStop );
371 assert( p->pBufferCur < p->pBufferEnd );
372 // figure out how many chars are still not processed
373 nCharsUsed = p->pBufferEnd - p->pBufferCur;
374 // move the remaining data to the beginning of the buffer
375 memmove( p->pBuffer, p->pBufferCur, (size_t)nCharsUsed );
376 p->pBufferCur = p->pBuffer;
377 // determine how many chars we will read
378 nCharsToRead = EXTRA_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
379 // read the chars
380 RetValue = fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
381 p->nFileRead += nCharsToRead;
382 // set the ponters to the end and the stopping point
383 p->pBufferEnd = p->pBuffer + nCharsUsed + nCharsToRead;
384 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE;
385}
386
390
391
393
#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
char * Extra_FileReaderGetFileName(Extra_FileReader_t *p)
void * Extra_FileReaderGetTokens(Extra_FileReader_t *p)
Extra_FileReader_t * Extra_FileReaderAlloc(char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean)
FUNCTION DEFINITIONS ///.
#define EXTRA_OFFSET_SIZE
int Extra_FileReaderGetLineNumber(Extra_FileReader_t *p, int iToken)
int Extra_FileReaderGetCurPosition(Extra_FileReader_t *p)
Extra_CharType_t
@ EXTRA_CHAR_STOP
@ EXTRA_CHAR_CLEAN
@ EXTRA_CHAR_COMMENT
@ EXTRA_CHAR_NORMAL
#define EXTRA_MINIMUM(a, b)
int Extra_FileReaderGetFileSize(Extra_FileReader_t *p)
void Extra_FileReaderFree(Extra_FileReader_t *p)
#define EXTRA_BUFFER_SIZE
DECLARATIONS ///.
struct Extra_FileReader_t_ Extra_FileReader_t
Definition extra.h:135
#define assert(ex)
Definition util_old.h:213
char * memset()
char * memmove()
VOID_HACK rewind()
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
#define SEEK_END
Definition zconf.h:392