#include <stdio.h>
#include "extra.h"
#include "vec.h"
Go to the source code of this file.
Data Structures | |
struct | Extra_FileReader_t_ |
Defines | |
#define | EXTRA_BUFFER_SIZE 4*1048576 |
#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_t * | Extra_FileReaderAlloc (char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean) |
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) |
#define EXTRA_BUFFER_SIZE 4*1048576 |
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 [
] DECLARATIONS ///
Definition at line 29 of file extraUtilReader.c.
#define EXTRA_MINIMUM | ( | a, | |||
b | ) | (((a) < (b))? (a) : (b)) |
Definition at line 32 of file extraUtilReader.c.
#define EXTRA_OFFSET_SIZE 4096 |
Definition at line 30 of file extraUtilReader.c.
enum Extra_CharType_t |
Definition at line 58 of file extraUtilReader.c.
00058 { 00059 EXTRA_CHAR_COMMENT, // a character that begins the comment 00060 EXTRA_CHAR_NORMAL, // a regular character 00061 EXTRA_CHAR_STOP, // a character that delimits a series of tokens 00062 EXTRA_CHAR_CLEAN // a character that should be cleaned 00063 } Extra_CharType_t;
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 84 of file extraUtilReader.c.
00086 { 00087 Extra_FileReader_t * p; 00088 FILE * pFile; 00089 char * pChar; 00090 int nCharsToRead; 00091 // check if the file can be opened 00092 pFile = fopen( pFileName, "rb" ); 00093 if ( pFile == NULL ) 00094 { 00095 printf( "Extra_FileReaderAlloc(): Cannot open input file \"%s\".\n", pFileName ); 00096 return NULL; 00097 } 00098 // start the file reader 00099 p = ALLOC( Extra_FileReader_t, 1 ); 00100 memset( p, 0, sizeof(Extra_FileReader_t) ); 00101 p->pFileName = pFileName; 00102 p->pFile = pFile; 00103 // set the character map 00104 memset( p->pCharMap, EXTRA_CHAR_NORMAL, 256 ); 00105 for ( pChar = pCharsComment; *pChar; pChar++ ) 00106 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_COMMENT; 00107 for ( pChar = pCharsStop; *pChar; pChar++ ) 00108 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_STOP; 00109 for ( pChar = pCharsClean; *pChar; pChar++ ) 00110 p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_CLEAN; 00111 // get the file size, in bytes 00112 fseek( pFile, 0, SEEK_END ); 00113 p->nFileSize = ftell( pFile ); 00114 rewind( pFile ); 00115 // allocate the buffer 00116 p->pBuffer = ALLOC( char, EXTRA_BUFFER_SIZE+1 ); 00117 p->nBufferSize = EXTRA_BUFFER_SIZE; 00118 p->pBufferCur = p->pBuffer; 00119 // determine how many chars to read 00120 nCharsToRead = EXTRA_MINIMUM(p->nFileSize, EXTRA_BUFFER_SIZE); 00121 // load the first part into the buffer 00122 fread( p->pBuffer, nCharsToRead, 1, p->pFile ); 00123 p->nFileRead = nCharsToRead; 00124 // set the ponters to the end and the stopping point 00125 p->pBufferEnd = p->pBuffer + nCharsToRead; 00126 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE; 00127 // start the arrays 00128 p->vTokens = Vec_PtrAlloc( 100 ); 00129 p->vLines = Vec_IntAlloc( 100 ); 00130 p->nLineCounter = 1; // 1-based line counting 00131 return p; 00132 }
void Extra_FileReaderFree | ( | Extra_FileReader_t * | p | ) |
Function*************************************************************
Synopsis [Stops the file reader.]
Description []
SideEffects []
SeeAlso []
Definition at line 145 of file extraUtilReader.c.
00146 { 00147 if ( p->pFile ) 00148 fclose( p->pFile ); 00149 FREE( p->pBuffer ); 00150 Vec_PtrFree( p->vTokens ); 00151 Vec_IntFree( p->vLines ); 00152 free( p ); 00153 }
int Extra_FileReaderGetCurPosition | ( | Extra_FileReader_t * | p | ) |
Function*************************************************************
Synopsis [Returns the current reading position.]
Description []
SideEffects []
SeeAlso []
Definition at line 198 of file extraUtilReader.c.
00199 { 00200 return p->nFileRead - (p->pBufferEnd - p->pBufferCur); 00201 }
char* Extra_FileReaderGetFileName | ( | Extra_FileReader_t * | p | ) |
Function*************************************************************
Synopsis [Returns the file size.]
Description []
SideEffects []
SeeAlso []
Definition at line 166 of file extraUtilReader.c.
00167 { 00168 return p->pFileName; 00169 }
int Extra_FileReaderGetFileSize | ( | Extra_FileReader_t * | p | ) |
Function*************************************************************
Synopsis [Returns the file size.]
Description []
SideEffects []
SeeAlso []
Definition at line 182 of file extraUtilReader.c.
00183 { 00184 return p->nFileSize; 00185 }
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 214 of file extraUtilReader.c.
void* Extra_FileReaderGetTokens | ( | Extra_FileReader_t * | p | ) |
Function*************************************************************
Synopsis [Returns the next set of tokens.]
Description []
SideEffects []
SeeAlso []
Definition at line 232 of file extraUtilReader.c.
00233 { 00234 Vec_Ptr_t * vTokens; 00235 while ( vTokens = Extra_FileReaderGetTokens_int( p ) ) 00236 if ( vTokens->nSize > 0 ) 00237 break; 00238 return vTokens; 00239 }
void * Extra_FileReaderGetTokens_int | ( | Extra_FileReader_t * | p | ) | [static] |
Function*************************************************************
Synopsis [Returns the next set of tokens.]
Description []
SideEffects []
SeeAlso []
Definition at line 252 of file extraUtilReader.c.
00253 { 00254 char * pChar; 00255 int fTokenStarted, MapValue; 00256 if ( p->fStop ) 00257 return NULL; 00258 // reset the token info 00259 p->vTokens->nSize = 0; 00260 p->vLines->nSize = 0; 00261 fTokenStarted = 0; 00262 // check if the new data should to be loaded 00263 if ( p->pBufferCur > p->pBufferStop ) 00264 Extra_FileReaderReload( p ); 00265 00266 // printf( "%d\n", p->pBufferEnd - p->pBufferCur ); 00267 00268 // process the string starting from the current position 00269 for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ ) 00270 { 00271 // count the lines 00272 if ( *pChar == '\n' ) 00273 p->nLineCounter++; 00274 // switch depending on the character 00275 MapValue = p->pCharMap[*pChar]; 00276 00277 // printf( "Char value = %d. Map value = %d.\n", *pChar, MapValue ); 00278 00279 00280 switch ( MapValue ) 00281 { 00282 case EXTRA_CHAR_COMMENT: 00283 if ( *pChar != '/' || *(pChar+1) == '/' ) 00284 { // dealing with the need to have // as a comment 00285 // if the token was being written, stop it 00286 if ( fTokenStarted ) 00287 fTokenStarted = 0; 00288 // eraze the comment till the end of line 00289 while ( *pChar != '\n' ) 00290 { 00291 *pChar++ = 0; 00292 if ( pChar == p->pBufferEnd ) 00293 { // this failure is due to the fact the comment continued 00294 // through EXTRA_OFFSET_SIZE chars till the end of the buffer 00295 printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName ); 00296 return NULL; 00297 } 00298 } 00299 pChar--; 00300 break; 00301 } 00302 // otherwise it is a normal character 00303 case EXTRA_CHAR_NORMAL: 00304 if ( !fTokenStarted ) 00305 { 00306 Vec_PtrPush( p->vTokens, pChar ); 00307 Vec_IntPush( p->vLines, p->nLineCounter ); 00308 fTokenStarted = 1; 00309 } 00310 break; 00311 case EXTRA_CHAR_STOP: 00312 if ( fTokenStarted ) 00313 fTokenStarted = 0; 00314 *pChar = 0; 00315 // prepare before leaving 00316 p->pBufferCur = pChar + 1; 00317 return p->vTokens; 00318 case EXTRA_CHAR_CLEAN: 00319 if ( fTokenStarted ) 00320 fTokenStarted = 0; 00321 *pChar = 0; 00322 break; 00323 default: 00324 assert( 0 ); 00325 } 00326 } 00327 // the file is finished or the last part continued 00328 // through EXTRA_OFFSET_SIZE chars till the end of the buffer 00329 if ( p->pBufferStop == p->pBufferEnd ) // end of file 00330 { 00331 *pChar = 0; 00332 p->fStop = 1; 00333 return p->vTokens; 00334 } 00335 printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName ); 00336 /* 00337 { 00338 int i; 00339 for ( i = 0; i < p->vTokens->nSize; i++ ) 00340 printf( "%s ", p->vTokens->pArray[i] ); 00341 printf( "\n" ); 00342 } 00343 */ 00344 return NULL; 00345 }
void Extra_FileReaderReload | ( | Extra_FileReader_t * | p | ) | [static] |
Function*************************************************************
Synopsis [Loads new data into the file reader.]
Description []
SideEffects []
SeeAlso []
Definition at line 358 of file extraUtilReader.c.
00359 { 00360 int nCharsUsed, nCharsToRead; 00361 assert( !p->fStop ); 00362 assert( p->pBufferCur > p->pBufferStop ); 00363 assert( p->pBufferCur < p->pBufferEnd ); 00364 // figure out how many chars are still not processed 00365 nCharsUsed = p->pBufferEnd - p->pBufferCur; 00366 // move the remaining data to the beginning of the buffer 00367 memmove( p->pBuffer, p->pBufferCur, nCharsUsed ); 00368 p->pBufferCur = p->pBuffer; 00369 // determine how many chars we will read 00370 nCharsToRead = EXTRA_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead ); 00371 // read the chars 00372 fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile ); 00373 p->nFileRead += nCharsToRead; 00374 // set the ponters to the end and the stopping point 00375 p->pBufferEnd = p->pBuffer + nCharsUsed + nCharsToRead; 00376 p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE; 00377 }