abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
extraUtilReader.c File Reference
#include <stdio.h>
#include "extra.h"
#include "misc/vec/vec.h"

Go to the source code of this file.

Data Structures

struct  Extra_FileReader_t_
 

Macros

#define EXTRA_BUFFER_SIZE   4*1048576
 DECLARATIONS ///. More...
 
#define EXTRA_OFFSET_SIZE   4096
 
#define EXTRA_MINIMUM(a, b)   (((a) < (b))? (a) : (b))
 

Enumerations

enum  Extra_CharType_t { EXTRA_CHAR_COMMENT, EXTRA_CHAR_NORMAL, EXTRA_CHAR_STOP, EXTRA_CHAR_CLEAN }
 

Functions

static void * Extra_FileReaderGetTokens_int (Extra_FileReader_t *p)
 
static void Extra_FileReaderReload (Extra_FileReader_t *p)
 
Extra_FileReader_tExtra_FileReaderAlloc (char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean)
 FUNCTION DEFINITIONS ///. More...
 
void Extra_FileReaderFree (Extra_FileReader_t *p)
 
char * Extra_FileReaderGetFileName (Extra_FileReader_t *p)
 
int Extra_FileReaderGetFileSize (Extra_FileReader_t *p)
 
int Extra_FileReaderGetCurPosition (Extra_FileReader_t *p)
 
int Extra_FileReaderGetLineNumber (Extra_FileReader_t *p, int iToken)
 
void * Extra_FileReaderGetTokens (Extra_FileReader_t *p)
 

Macro Definition Documentation

#define EXTRA_BUFFER_SIZE   4*1048576

DECLARATIONS ///.

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

FileName [extraUtilReader.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [extra]

Synopsis [File reading utilities.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - June 20, 2005.]

Revision [

Id:
extraUtilReader.c,v 1.00 2005/06/20 00:00:00 alanmi Exp

]

Definition at line 32 of file extraUtilReader.c.

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

Definition at line 35 of file extraUtilReader.c.

#define EXTRA_OFFSET_SIZE   4096

Definition at line 33 of file extraUtilReader.c.

Enumeration Type Documentation

Enumerator
EXTRA_CHAR_COMMENT 
EXTRA_CHAR_NORMAL 
EXTRA_CHAR_STOP 
EXTRA_CHAR_CLEAN 

Definition at line 61 of file extraUtilReader.c.

61  {
62  EXTRA_CHAR_COMMENT, // a character that begins the comment
63  EXTRA_CHAR_NORMAL, // a regular character
64  EXTRA_CHAR_STOP, // a character that delimits a series of tokens
65  EXTRA_CHAR_CLEAN // a character that should be cleaned
Extra_CharType_t

Function Documentation

Extra_FileReader_t* Extra_FileReaderAlloc ( char *  pFileName,
char *  pCharsComment,
char *  pCharsStop,
char *  pCharsClean 
)

FUNCTION DEFINITIONS ///.

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

Synopsis [Starts the file reader.]

Description []

SideEffects []

SeeAlso []

Definition at line 87 of file extraUtilReader.c.

89 {
91  FILE * pFile;
92  char * pChar;
93  int nCharsToRead;
94  int RetValue;
95  // check if the file can be opened
96  pFile = fopen( pFileName, "rb" );
97  if ( pFile == NULL )
98  {
99  printf( "Extra_FileReaderAlloc(): Cannot open input file \"%s\".\n", pFileName );
100  return NULL;
101  }
102  // start the file reader
103  p = ABC_ALLOC( Extra_FileReader_t, 1 );
104  memset( p, 0, sizeof(Extra_FileReader_t) );
105  p->pFileName = pFileName;
106  p->pFile = pFile;
107  // set the character map
108  memset( p->pCharMap, EXTRA_CHAR_NORMAL, 256 );
109  for ( pChar = pCharsComment; *pChar; pChar++ )
110  p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_COMMENT;
111  for ( pChar = pCharsStop; *pChar; pChar++ )
112  p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_STOP;
113  for ( pChar = pCharsClean; *pChar; pChar++ )
114  p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_CLEAN;
115  // get the file size, in bytes
116  fseek( pFile, 0, SEEK_END );
117  p->nFileSize = ftell( pFile );
118  rewind( pFile );
119  // allocate the buffer
120  p->pBuffer = ABC_ALLOC( char, EXTRA_BUFFER_SIZE+1 );
122  p->pBufferCur = p->pBuffer;
123  // determine how many chars to read
124  nCharsToRead = EXTRA_MINIMUM(p->nFileSize, EXTRA_BUFFER_SIZE);
125  // load the first part into the buffer
126  RetValue = fread( p->pBuffer, nCharsToRead, 1, p->pFile );
127  p->nFileRead = nCharsToRead;
128  // set the ponters to the end and the stopping point
129  p->pBufferEnd = p->pBuffer + nCharsToRead;
131  // start the arrays
132  p->vTokens = Vec_PtrAlloc( 100 );
133  p->vLines = Vec_IntAlloc( 100 );
134  p->nLineCounter = 1; // 1-based line counting
135  return p;
136 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define SEEK_END
Definition: zconf.h:392
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define EXTRA_OFFSET_SIZE
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
#define EXTRA_MINIMUM(a, b)
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
#define EXTRA_BUFFER_SIZE
DECLARATIONS ///.
VOID_HACK rewind()
void Extra_FileReaderFree ( Extra_FileReader_t p)

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

Synopsis [Stops the file reader.]

Description []

SideEffects []

SeeAlso []

Definition at line 149 of file extraUtilReader.c.

150 {
151  if ( p->pFile )
152  fclose( p->pFile );
153  ABC_FREE( p->pBuffer );
154  Vec_PtrFree( p->vTokens );
155  Vec_IntFree( p->vLines );
156  ABC_FREE( p );
157 }
#define ABC_FREE(obj)
Definition: abc_global.h:232
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
int Extra_FileReaderGetCurPosition ( Extra_FileReader_t p)

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

Synopsis [Returns the current reading position.]

Description []

SideEffects []

SeeAlso []

Definition at line 202 of file extraUtilReader.c.

203 {
204  return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
205 }
char* Extra_FileReaderGetFileName ( Extra_FileReader_t p)

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

Synopsis [Returns the file size.]

Description []

SideEffects []

SeeAlso []

Definition at line 170 of file extraUtilReader.c.

171 {
172  return p->pFileName;
173 }
int Extra_FileReaderGetFileSize ( Extra_FileReader_t p)

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

Synopsis [Returns the file size.]

Description []

SideEffects []

SeeAlso []

Definition at line 186 of file extraUtilReader.c.

187 {
188  return p->nFileSize;
189 }
int Extra_FileReaderGetLineNumber ( Extra_FileReader_t p,
int  iToken 
)

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

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

Description []

SideEffects []

SeeAlso []

Definition at line 218 of file extraUtilReader.c.

219 {
220  assert( iToken >= 0 && iToken < p->vTokens->nSize );
221  return p->vLines->pArray[iToken];
222 }
#define assert(ex)
Definition: util_old.h:213
void* Extra_FileReaderGetTokens ( Extra_FileReader_t p)

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

Synopsis [Returns the next set of tokens.]

Description []

SideEffects []

SeeAlso []

Definition at line 236 of file extraUtilReader.c.

237 {
238  Vec_Ptr_t * vTokens;
239  while ( (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens_int( p )) )
240  if ( vTokens->nSize > 0 )
241  break;
242  return vTokens;
243 }
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static void * Extra_FileReaderGetTokens_int(Extra_FileReader_t *p)
void * Extra_FileReaderGetTokens_int ( Extra_FileReader_t p)
static

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

Synopsis [Returns the next set of tokens.]

Description []

SideEffects []

SeeAlso []

Definition at line 256 of file extraUtilReader.c.

257 {
258  char * pChar;
259  int fTokenStarted, MapValue;
260  if ( p->fStop )
261  return NULL;
262  // reset the token info
263  p->vTokens->nSize = 0;
264  p->vLines->nSize = 0;
265  fTokenStarted = 0;
266  // check if the new data should to be loaded
267  if ( p->pBufferCur > p->pBufferStop )
269 
270 // printf( "%d\n", p->pBufferEnd - p->pBufferCur );
271 
272  // process the string starting from the current position
273  for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
274  {
275  // count the lines
276  if ( *pChar == '\n' )
277  p->nLineCounter++;
278  // switch depending on the character
279  MapValue = p->pCharMap[(int)*pChar];
280 
281 // printf( "Char value = %d. Map value = %d.\n", *pChar, MapValue );
282 
283 
284  switch ( MapValue )
285  {
286  case EXTRA_CHAR_COMMENT:
287  if ( *pChar != '/' || *(pChar+1) == '/' )
288  { // dealing with the need to have // as a comment
289  // if the token was being written, stop it
290  if ( fTokenStarted )
291  fTokenStarted = 0;
292  // eraze the comment till the end of line
293  while ( *pChar != '\n' )
294  {
295  *pChar++ = 0;
296  if ( pChar == p->pBufferEnd )
297  { // this failure is due to the fact the comment continued
298  // through EXTRA_OFFSET_SIZE chars till the end of the buffer
299  printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
300  return NULL;
301  }
302  }
303  pChar--;
304  break;
305  }
306  // otherwise it is a normal character
307  case EXTRA_CHAR_NORMAL:
308  if ( !fTokenStarted )
309  {
310  Vec_PtrPush( p->vTokens, pChar );
311  Vec_IntPush( p->vLines, p->nLineCounter );
312  fTokenStarted = 1;
313  }
314  break;
315  case EXTRA_CHAR_STOP:
316  if ( fTokenStarted )
317  fTokenStarted = 0;
318  *pChar = 0;
319  // prepare before leaving
320  p->pBufferCur = pChar + 1;
321  return p->vTokens;
322  case EXTRA_CHAR_CLEAN:
323  if ( fTokenStarted )
324  fTokenStarted = 0;
325  *pChar = 0;
326  break;
327  default:
328  assert( 0 );
329  }
330  }
331  // the file is finished or the last part continued
332  // through EXTRA_OFFSET_SIZE chars till the end of the buffer
333  if ( p->pBufferStop == p->pBufferEnd ) // end of file
334  {
335  *pChar = 0;
336  p->fStop = 1;
337  return p->vTokens;
338  }
339  printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
340 /*
341  {
342  int i;
343  for ( i = 0; i < p->vTokens->nSize; i++ )
344  printf( "%s ", p->vTokens->pArray[i] );
345  printf( "\n" );
346  }
347 */
348  return NULL;
349 }
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
static void Extra_FileReaderReload(Extra_FileReader_t *p)
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
#define assert(ex)
Definition: util_old.h:213
void Extra_FileReaderReload ( Extra_FileReader_t p)
static

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

Synopsis [Loads new data into the file reader.]

Description []

SideEffects []

SeeAlso []

Definition at line 362 of file extraUtilReader.c.

363 {
364  int nCharsUsed, nCharsToRead;
365  int RetValue;
366  assert( !p->fStop );
367  assert( p->pBufferCur > p->pBufferStop );
368  assert( p->pBufferCur < p->pBufferEnd );
369  // figure out how many chars are still not processed
370  nCharsUsed = p->pBufferEnd - p->pBufferCur;
371  // move the remaining data to the beginning of the buffer
372  memmove( p->pBuffer, p->pBufferCur, nCharsUsed );
373  p->pBufferCur = p->pBuffer;
374  // determine how many chars we will read
375  nCharsToRead = EXTRA_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
376  // read the chars
377  RetValue = fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
378  p->nFileRead += nCharsToRead;
379  // set the ponters to the end and the stopping point
380  p->pBufferEnd = p->pBuffer + nCharsUsed + nCharsToRead;
382 }
char * memmove()
#define EXTRA_OFFSET_SIZE
#define EXTRA_MINIMUM(a, b)
#define assert(ex)
Definition: util_old.h:213
#define EXTRA_BUFFER_SIZE
DECLARATIONS ///.