abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
verStream.c File Reference
#include "ver.h"

Go to the source code of this file.

Data Structures

struct  Ver_Stream_t_
 

Macros

#define VER_BUFFER_SIZE   1048576
 DECLARATIONS ///. More...
 
#define VER_OFFSET_SIZE   65536
 
#define VER_WORD_SIZE   65536
 
#define VER_MINIMUM(a, b)   (((a) < (b))? (a) : (b))
 

Functions

static void Ver_StreamReload (Ver_Stream_t *p)
 
Ver_Stream_tVer_StreamAlloc (char *pFileName)
 FUNCTION DEFINITIONS ///. More...
 
void Ver_StreamFree (Ver_Stream_t *p)
 
char * Ver_StreamGetFileName (Ver_Stream_t *p)
 
int Ver_StreamGetFileSize (Ver_Stream_t *p)
 
int Ver_StreamGetCurPosition (Ver_Stream_t *p)
 
int Ver_StreamGetLineNumber (Ver_Stream_t *p)
 
int Ver_StreamIsOkey (Ver_Stream_t *p)
 
char Ver_StreamScanChar (Ver_Stream_t *p)
 
char Ver_StreamPopChar (Ver_Stream_t *p)
 
void Ver_StreamSkipChars (Ver_Stream_t *p, char *pCharsToSkip)
 
void Ver_StreamSkipToChars (Ver_Stream_t *p, char *pCharsToStop)
 
char * Ver_StreamGetWord (Ver_Stream_t *p, char *pCharsToStop)
 

Macro Definition Documentation

#define VER_BUFFER_SIZE   1048576

DECLARATIONS ///.

CFile****************************************************************

FileName [verStream.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [Verilog parser.]

Synopsis [Input file stream, which knows nothing about Verilog.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - August 19, 2006.]

Revision [

Id:
verStream.c,v 1.00 2006/08/19 00:00:00 alanmi Exp

]

Definition at line 30 of file verStream.c.

#define VER_MINIMUM (   a,
 
)    (((a) < (b))? (a) : (b))

Definition at line 34 of file verStream.c.

#define VER_OFFSET_SIZE   65536

Definition at line 31 of file verStream.c.

#define VER_WORD_SIZE   65536

Definition at line 32 of file verStream.c.

Function Documentation

Ver_Stream_t* Ver_StreamAlloc ( char *  pFileName)

FUNCTION DEFINITIONS ///.

Function*************************************************************

Synopsis [Starts the file reader for the given file.]

Description []

SideEffects []

SeeAlso []

Definition at line 74 of file verStream.c.

75 {
76  Ver_Stream_t * p;
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 );
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;
108  // start the arrays
109  p->nLineCounter = 1; // 1-based line counting
110  return p;
111 }
char * memset()
char * pBufferEnd
Definition: verStream.c:48
static Llb_Mgr_t * p
Definition: llb3Image.c:950
iword nLineCounter
Definition: verStream.c:43
#define SEEK_END
Definition: zconf.h:392
char * pBuffer
Definition: verStream.c:46
iword nBufferSize
Definition: verStream.c:45
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define VER_BUFFER_SIZE
DECLARATIONS ///.
Definition: verStream.c:30
iword nFileSize
Definition: verStream.c:41
char * pBufferCur
Definition: verStream.c:47
FILE * pFile
Definition: verStream.c:40
char * pBufferStop
Definition: verStream.c:49
iword nFileRead
Definition: verStream.c:42
#define VER_MINIMUM(a, b)
Definition: verStream.c:34
char * pFileName
Definition: verStream.c:39
#define VER_OFFSET_SIZE
Definition: verStream.c:31
VOID_HACK rewind()
void Ver_StreamFree ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Stops the file reader.]

Description []

SideEffects []

SeeAlso []

Definition at line 157 of file verStream.c.

158 {
159  if ( p->pFile )
160  fclose( p->pFile );
161  ABC_FREE( p->pBuffer );
162  ABC_FREE( p );
163 }
char * pBuffer
Definition: verStream.c:46
FILE * pFile
Definition: verStream.c:40
#define ABC_FREE(obj)
Definition: abc_global.h:232
int Ver_StreamGetCurPosition ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns the current reading position.]

Description []

SideEffects []

SeeAlso []

Definition at line 208 of file verStream.c.

209 {
210  return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
211 }
char * pBufferEnd
Definition: verStream.c:48
char * pBufferCur
Definition: verStream.c:47
iword nFileRead
Definition: verStream.c:42
char* Ver_StreamGetFileName ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns the file size.]

Description []

SideEffects []

SeeAlso []

Definition at line 176 of file verStream.c.

177 {
178  return p->pFileName;
179 }
char * pFileName
Definition: verStream.c:39
int Ver_StreamGetFileSize ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns the file size.]

Description []

SideEffects []

SeeAlso []

Definition at line 192 of file verStream.c.

193 {
194  return p->nFileSize;
195 }
iword nFileSize
Definition: verStream.c:41
int Ver_StreamGetLineNumber ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns the line number for the given token.]

Description []

SideEffects []

SeeAlso []

Definition at line 224 of file verStream.c.

225 {
226  return p->nLineCounter;
227 }
iword nLineCounter
Definition: verStream.c:43
char* Ver_StreamGetWord ( Ver_Stream_t p,
char *  pCharsToStop 
)

Function*************************************************************

Synopsis [Returns current word delimited by the set of symbols.]

Description [Modifies the stream by inserting 0 at the first encounter of one of the symbols in the list.]

SideEffects []

SeeAlso []

Definition at line 397 of file verStream.c.

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 }
char * pBufferEnd
Definition: verStream.c:48
iword nLineCounter
Definition: verStream.c:43
static void Ver_StreamReload(Ver_Stream_t *p)
Definition: verStream.c:124
char pChars[VER_WORD_SIZE+5]
Definition: verStream.c:51
char * pBufferCur
Definition: verStream.c:47
char * pBufferStop
Definition: verStream.c:49
char * pFileName
Definition: verStream.c:39
#define VER_WORD_SIZE
Definition: verStream.c:32
#define assert(ex)
Definition: util_old.h:213
int Ver_StreamIsOkey ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns current symbol.]

Description []

SideEffects []

SeeAlso []

Definition at line 242 of file verStream.c.

243 {
244  return !p->fStop;
245 }
char Ver_StreamPopChar ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns current symbol and moves to the next.]

Description []

SideEffects []

SeeAlso []

Definition at line 275 of file verStream.c.

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 }
char * pBufferEnd
Definition: verStream.c:48
iword nLineCounter
Definition: verStream.c:43
static void Ver_StreamReload(Ver_Stream_t *p)
Definition: verStream.c:124
char * pBufferCur
Definition: verStream.c:47
char * pBufferStop
Definition: verStream.c:49
#define assert(ex)
Definition: util_old.h:213
void Ver_StreamReload ( Ver_Stream_t p)
static

Function*************************************************************

Synopsis [Loads new data into the file reader.]

Description []

SideEffects []

SeeAlso []

Definition at line 124 of file verStream.c.

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, 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;
144 }
char * pBufferEnd
Definition: verStream.c:48
char * pBuffer
Definition: verStream.c:46
iword nBufferSize
Definition: verStream.c:45
char * memmove()
#define VER_BUFFER_SIZE
DECLARATIONS ///.
Definition: verStream.c:30
iword nFileSize
Definition: verStream.c:41
char * pBufferCur
Definition: verStream.c:47
FILE * pFile
Definition: verStream.c:40
char * pBufferStop
Definition: verStream.c:49
iword nFileRead
Definition: verStream.c:42
#define VER_MINIMUM(a, b)
Definition: verStream.c:34
#define assert(ex)
Definition: util_old.h:213
#define VER_OFFSET_SIZE
Definition: verStream.c:31
char Ver_StreamScanChar ( Ver_Stream_t p)

Function*************************************************************

Synopsis [Returns current symbol.]

Description []

SideEffects []

SeeAlso []

Definition at line 258 of file verStream.c.

259 {
260  assert( !p->fStop );
261  return *p->pBufferCur;
262 }
char * pBufferCur
Definition: verStream.c:47
#define assert(ex)
Definition: util_old.h:213
void Ver_StreamSkipChars ( Ver_Stream_t p,
char *  pCharsToSkip 
)

Function*************************************************************

Synopsis [Skips the current symbol and all symbols from the list.]

Description []

SideEffects []

SeeAlso []

Definition at line 304 of file verStream.c.

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 }
char * pBufferEnd
Definition: verStream.c:48
iword nLineCounter
Definition: verStream.c:43
static void Ver_StreamReload(Ver_Stream_t *p)
Definition: verStream.c:124
char * pBufferCur
Definition: verStream.c:47
char * pBufferStop
Definition: verStream.c:49
char * pFileName
Definition: verStream.c:39
#define assert(ex)
Definition: util_old.h:213
void Ver_StreamSkipToChars ( Ver_Stream_t p,
char *  pCharsToStop 
)

Function*************************************************************

Synopsis [Skips all symbols until encountering one from the list.]

Description []

SideEffects []

SeeAlso []

Definition at line 349 of file verStream.c.

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 }
char * pBufferEnd
Definition: verStream.c:48
iword nLineCounter
Definition: verStream.c:43
static void Ver_StreamReload(Ver_Stream_t *p)
Definition: verStream.c:124
char * pBufferCur
Definition: verStream.c:47
char * pBufferStop
Definition: verStream.c:49
char * pFileName
Definition: verStream.c:39
#define assert(ex)
Definition: util_old.h:213