ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
verStream.c
Go to the documentation of this file.
1
20
21#include "ver.h"
22
24
25
29
30#define VER_BUFFER_SIZE 1048576 // 1M - size of the data chunk stored in memory
31#define VER_OFFSET_SIZE 65536 // 64K - load new data when less than this is left
32#define VER_WORD_SIZE 65536 // 64K - the largest token that can be returned
33
34#define VER_MINIMUM(a,b) (((a) < (b))? (a) : (b))
35
37{
38 // the input file
39 char * pFileName; // the input file name
40 FILE * pFile; // the input file pointer
41 iword nFileSize; // the total number of bytes in the file
42 iword nFileRead; // the number of bytes currently read from file
43 iword nLineCounter; // the counter of lines processed
44 // temporary storage for data
45 iword nBufferSize; // the size of the buffer
46 char * pBuffer; // the buffer
47 char * pBufferCur; // the current reading position
48 char * pBufferEnd; // the first position not used by currently loaded data
49 char * pBufferStop; // the position where loading new data will be done
50 // tokens given to the user
51 char pChars[VER_WORD_SIZE+5]; // temporary storage for a word (plus end-of-string and two parentheses)
52 int nChars; // the total number of characters in the word
53 // status of the parser
54 int fStop; // this flag goes high when the end of file is reached
55};
56
57static void Ver_StreamReload( Ver_Stream_t * p );
58
62
74Ver_Stream_t * Ver_StreamAlloc( char * pFileName )
75{
77 FILE * pFile;
78 int nCharsToRead;
79 int RetValue;
80 // check if the file can be opened
81 pFile = fopen( pFileName, "rb" );
82 if ( pFile == NULL )
83 {
84 printf( "Ver_StreamAlloc(): Cannot open input file \"%s\".\n", pFileName );
85 return NULL;
86 }
87 // start the file reader
88 p = ABC_ALLOC( Ver_Stream_t, 1 );
89 memset( p, 0, sizeof(Ver_Stream_t) );
90 p->pFileName = pFileName;
91 p->pFile = pFile;
92 // get the file size, in bytes
93 fseek( pFile, 0, SEEK_END );
94 p->nFileSize = ftell( pFile );
95 rewind( pFile );
96 // allocate the buffer
97 p->pBuffer = ABC_ALLOC( char, VER_BUFFER_SIZE+1 );
98 p->nBufferSize = VER_BUFFER_SIZE;
99 p->pBufferCur = p->pBuffer;
100 // determine how many chars to read
101 nCharsToRead = VER_MINIMUM(p->nFileSize, VER_BUFFER_SIZE);
102 // load the first part into the buffer
103 RetValue = fread( p->pBuffer, nCharsToRead, 1, p->pFile );
104 p->nFileRead = nCharsToRead;
105 // set the ponters to the end and the stopping point
106 p->pBufferEnd = p->pBuffer + nCharsToRead;
107 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + VER_BUFFER_SIZE - VER_OFFSET_SIZE;
108 // start the arrays
109 p->nLineCounter = 1; // 1-based line counting
110 return p;
111}
112
124void Ver_StreamReload( Ver_Stream_t * p )
125{
126 int nCharsUsed, nCharsToRead;
127 int RetValue;
128 assert( !p->fStop );
129 assert( p->pBufferCur > p->pBufferStop );
130 assert( p->pBufferCur < p->pBufferEnd );
131 // figure out how many chars are still not processed
132 nCharsUsed = p->pBufferEnd - p->pBufferCur;
133 // move the remaining data to the beginning of the buffer
134 memmove( p->pBuffer, p->pBufferCur, (size_t)nCharsUsed );
135 p->pBufferCur = p->pBuffer;
136 // determine how many chars we will read
137 nCharsToRead = VER_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
138 // read the chars
139 RetValue = fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
140 p->nFileRead += nCharsToRead;
141 // set the ponters to the end and the stopping point
142 p->pBufferEnd = p->pBuffer + nCharsUsed + nCharsToRead;
143 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + VER_BUFFER_SIZE - VER_OFFSET_SIZE;
144}
145
158{
159 if ( p->pFile )
160 fclose( p->pFile );
161 ABC_FREE( p->pBuffer );
162 ABC_FREE( p );
163}
164
177{
178 return p->pFileName;
179}
180
193{
194 return p->nFileSize;
195}
196
209{
210 return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
211}
212
225{
226 return p->nLineCounter;
227}
228
229
230
243{
244 return !p->fStop;
245}
246
259{
260 assert( !p->fStop );
261 return *p->pBufferCur;
262}
263
276{
277 assert( !p->fStop );
278 // check if the new data should to be loaded
279 if ( p->pBufferCur > p->pBufferStop )
280 Ver_StreamReload( p );
281 // check if there are symbols left
282 if ( p->pBufferCur == p->pBufferEnd ) // end of file
283 {
284 p->fStop = 1;
285 return -1;
286 }
287 // count the lines
288 if ( *p->pBufferCur == '\n' )
289 p->nLineCounter++;
290 return *p->pBufferCur++;
291}
292
304void Ver_StreamSkipChars( Ver_Stream_t * p, char * pCharsToSkip )
305{
306 char * pChar, * pTemp;
307 assert( !p->fStop );
308 assert( pCharsToSkip != NULL );
309 // check if the new data should to be loaded
310 if ( p->pBufferCur > p->pBufferStop )
311 Ver_StreamReload( p );
312 // skip the symbols
313 for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
314 {
315 // skip symbols as long as they are in the list
316 for ( pTemp = pCharsToSkip; *pTemp; pTemp++ )
317 if ( *pChar == *pTemp )
318 break;
319 if ( *pTemp == 0 ) // pChar is not found in the list
320 {
321 p->pBufferCur = pChar;
322 return;
323 }
324 // count the lines
325 if ( *pChar == '\n' )
326 p->nLineCounter++;
327 }
328 // the file is finished or the last part continued
329 // through VER_OFFSET_SIZE chars till the end of the buffer
330 if ( p->pBufferStop == p->pBufferEnd ) // end of file
331 {
332 p->fStop = 1;
333 return;
334 }
335 printf( "Ver_StreamSkipSymbol() failed to parse the file \"%s\".\n", p->pFileName );
336}
337
349void Ver_StreamSkipToChars( Ver_Stream_t * p, char * pCharsToStop )
350{
351 char * pChar, * pTemp;
352 assert( !p->fStop );
353 assert( pCharsToStop != NULL );
354 // check if the new data should to be loaded
355 if ( p->pBufferCur > p->pBufferStop )
356 Ver_StreamReload( p );
357 // skip the symbols
358 for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
359 {
360 // skip symbols as long as they are NOT in the list
361 for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
362 if ( *pChar == *pTemp )
363 break;
364 if ( *pTemp == 0 ) // pChar is not found in the list
365 {
366 // count the lines
367 if ( *pChar == '\n' )
368 p->nLineCounter++;
369 continue;
370 }
371 // the symbol is found - move position and return
372 p->pBufferCur = pChar;
373 return;
374 }
375 // the file is finished or the last part continued
376 // through VER_OFFSET_SIZE chars till the end of the buffer
377 if ( p->pBufferStop == p->pBufferEnd ) // end of file
378 {
379 p->fStop = 1;
380 return;
381 }
382 printf( "Ver_StreamSkipToSymbol() failed to parse the file \"%s\".\n", p->pFileName );
383}
384
397char * Ver_StreamGetWord( Ver_Stream_t * p, char * pCharsToStop )
398{
399 char * pChar, * pTemp;
400 if ( p->fStop )
401 return NULL;
402 assert( pCharsToStop != NULL );
403 // check if the new data should to be loaded
404 if ( p->pBufferCur > p->pBufferStop )
405 Ver_StreamReload( p );
406 // skip the symbols
407 p->nChars = 0;
408 for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
409 {
410 // skip symbols as long as they are NOT in the list
411 for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
412 if ( *pChar == *pTemp )
413 break;
414 if ( *pTemp == 0 ) // pChar is not found in the list
415 {
416 p->pChars[p->nChars++] = *pChar;
417 if ( p->nChars == VER_WORD_SIZE )
418 {
419 printf( "Ver_StreamGetWord(): The buffer size is exceeded.\n" );
420 return NULL;
421 }
422 // count the lines
423 if ( *pChar == '\n' )
424 p->nLineCounter++;
425 continue;
426 }
427 // the symbol is found - move the position, set the word end, return the word
428 p->pBufferCur = pChar;
429 p->pChars[p->nChars] = 0;
430 return p->pChars;
431 }
432 // the file is finished or the last part continued
433 // through VER_OFFSET_SIZE chars till the end of the buffer
434 if ( p->pBufferStop == p->pBufferEnd ) // end of file
435 {
436 p->fStop = 1;
437 p->pChars[p->nChars] = 0;
438 return p->pChars;
439 }
440 printf( "Ver_StreamGetWord() failed to parse the file \"%s\".\n", p->pFileName );
441 return NULL;
442}
443
456{
457 if ( !strncmp(p->pBufferCur+1, "z_g_", 4) || !strncmp(p->pBufferCur+1, "co_g", 3) )
458 while ( p->pBufferCur[0] != '(' )
459 p->pBufferCur++;
460}
461
465
466
468
ABC_INT64_T iword
Definition abc_global.h:244
#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
Cube * p
Definition exorList.c:222
char * pBuffer
Definition verStream.c:46
char pChars[VER_WORD_SIZE+5]
Definition verStream.c:51
FILE * pFile
Definition verStream.c:40
char * pBufferStop
Definition verStream.c:49
iword nFileSize
Definition verStream.c:41
iword nFileRead
Definition verStream.c:42
char * pBufferEnd
Definition verStream.c:48
char * pFileName
Definition verStream.c:39
iword nLineCounter
Definition verStream.c:43
char * pBufferCur
Definition verStream.c:47
iword nBufferSize
Definition verStream.c:45
#define assert(ex)
Definition util_old.h:213
int strncmp()
char * memset()
char * memmove()
VOID_HACK rewind()
void Ver_StreamMove(Ver_Stream_t *p)
Definition verStream.c:455
int Ver_StreamGetCurPosition(Ver_Stream_t *p)
Definition verStream.c:208
char * Ver_StreamGetFileName(Ver_Stream_t *p)
Definition verStream.c:176
int Ver_StreamIsOkey(Ver_Stream_t *p)
Definition verStream.c:242
#define VER_MINIMUM(a, b)
Definition verStream.c:34
char * Ver_StreamGetWord(Ver_Stream_t *p, char *pCharsToStop)
Definition verStream.c:397
#define VER_WORD_SIZE
Definition verStream.c:32
#define VER_OFFSET_SIZE
Definition verStream.c:31
char Ver_StreamPopChar(Ver_Stream_t *p)
Definition verStream.c:275
void Ver_StreamSkipChars(Ver_Stream_t *p, char *pCharsToSkip)
Definition verStream.c:304
Ver_Stream_t * Ver_StreamAlloc(char *pFileName)
FUNCTION DEFINITIONS ///.
Definition verStream.c:74
void Ver_StreamSkipToChars(Ver_Stream_t *p, char *pCharsToStop)
Definition verStream.c:349
char Ver_StreamScanChar(Ver_Stream_t *p)
Definition verStream.c:258
#define VER_BUFFER_SIZE
DECLARATIONS ///.
Definition verStream.c:30
void Ver_StreamFree(Ver_Stream_t *p)
Definition verStream.c:157
int Ver_StreamGetLineNumber(Ver_Stream_t *p)
Definition verStream.c:224
int Ver_StreamGetFileSize(Ver_Stream_t *p)
Definition verStream.c:192
struct Ver_Stream_t_ Ver_Stream_t
Definition ver.h:46
#define SEEK_END
Definition zconf.h:392