extraUtilReader.c 12.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**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 $]

***********************************************************************/

Alan Mishchenko committed
21
#include <stdio.h>
Alan Mishchenko committed
22
#include "extra.h"
23
#include "misc/vec/vec.h"
Alan Mishchenko committed
24

25 26 27
#if (__GNUC__ >= 8)
  #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif
28

29
ABC_NAMESPACE_IMPL_START
30

Alan Mishchenko committed
31 32 33 34
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
35
#define EXTRA_BUFFER_SIZE        4*1048576    // 1M   - size of the data chunk stored in memory
Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#define EXTRA_OFFSET_SIZE           4096    // 4K   - load new data when less than this is left

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

struct Extra_FileReader_t_
{
    // the input file
    char *           pFileName;     // the input file name
    FILE *           pFile;         // the input file pointer
    int              nFileSize;     // the total number of bytes in the file
    int              nFileRead;     // the number of bytes currently read from file
    // info about processing different types of input chars
    char             pCharMap[256]; // the character map
    // temporary storage for data 
    char *           pBuffer;       // the buffer
    int              nBufferSize;   // the size of the buffer
    char *           pBufferCur;    // the current reading position
    char *           pBufferEnd;    // the first position not used by currently loaded data
    char *           pBufferStop;   // the position where loading new data will be done
    // tokens given to the user
    Vec_Ptr_t *      vTokens;       // the vector of tokens returned to the user
    Vec_Int_t *      vLines;        // the vector of line numbers for each token
    int              nLineCounter;  // the counter of lines processed
    // status of the parser
    int              fStop;         // this flag goes high when the end of file is reached
};

// character types
typedef enum { 
    EXTRA_CHAR_COMMENT,  // a character that begins the comment
    EXTRA_CHAR_NORMAL,   // a regular character
    EXTRA_CHAR_STOP,     // a character that delimits a series of tokens
    EXTRA_CHAR_CLEAN     // a character that should be cleaned
} Extra_CharType_t;

// the static functions
static void * Extra_FileReaderGetTokens_int( Extra_FileReader_t * p );
static void Extra_FileReaderReload( Extra_FileReader_t * p );

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
76
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Starts the file reader.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Extra_FileReader_t * Extra_FileReaderAlloc( char * pFileName, 
    char * pCharsComment, char * pCharsStop, char * pCharsClean )
{
    Extra_FileReader_t * p;
    FILE * pFile;
    char * pChar;
    int nCharsToRead;
97
    int RetValue;
Alan Mishchenko committed
98 99 100 101 102 103 104 105
    // check if the file can be opened
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Extra_FileReaderAlloc(): Cannot open input file \"%s\".\n", pFileName );
        return NULL;
    }
    // start the file reader    
Alan Mishchenko committed
106
    p = ABC_ALLOC( Extra_FileReader_t, 1 );
Alan Mishchenko committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    memset( p, 0, sizeof(Extra_FileReader_t) );
    p->pFileName   = pFileName;
    p->pFile       = pFile;
    // set the character map
    memset( p->pCharMap, EXTRA_CHAR_NORMAL, 256 );
    for ( pChar = pCharsComment; *pChar; pChar++ )
        p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_COMMENT;
    for ( pChar = pCharsStop; *pChar; pChar++ )
        p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_STOP;
    for ( pChar = pCharsClean; *pChar; pChar++ )
        p->pCharMap[(unsigned char)*pChar] = EXTRA_CHAR_CLEAN;
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    p->nFileSize = ftell( pFile );  
    rewind( pFile ); 
    // allocate the buffer
Alan Mishchenko committed
123
    p->pBuffer = ABC_ALLOC( char, EXTRA_BUFFER_SIZE+1 );
Alan Mishchenko committed
124 125 126 127 128
    p->nBufferSize = EXTRA_BUFFER_SIZE;
    p->pBufferCur  = p->pBuffer;
    // determine how many chars to read
    nCharsToRead = EXTRA_MINIMUM(p->nFileSize, EXTRA_BUFFER_SIZE);
    // load the first part into the buffer
129
    RetValue = fread( p->pBuffer, nCharsToRead, 1, p->pFile );
Alan Mishchenko committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    p->nFileRead = nCharsToRead;
    // set the ponters to the end and the stopping point
    p->pBufferEnd  = p->pBuffer + nCharsToRead;
    p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE;
    // start the arrays
    p->vTokens = Vec_PtrAlloc( 100 );
    p->vLines = Vec_IntAlloc( 100 );
    p->nLineCounter = 1; // 1-based line counting
    return p;
}

/**Function*************************************************************

  Synopsis    [Stops the file reader.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_FileReaderFree( Extra_FileReader_t * p )
{
    if ( p->pFile )
        fclose( p->pFile );
Alan Mishchenko committed
156
    ABC_FREE( p->pBuffer );
Alan Mishchenko committed
157 158
    Vec_PtrFree( p->vTokens );
    Vec_IntFree( p->vLines );
Alan Mishchenko committed
159
    ABC_FREE( p );
Alan Mishchenko committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

/**Function*************************************************************

  Synopsis    [Returns the file size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileReaderGetFileName( Extra_FileReader_t * p )
{
    return p->pFileName;
}

/**Function*************************************************************

  Synopsis    [Returns the file size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Extra_FileReaderGetFileSize( Extra_FileReader_t * p )
{
    return p->nFileSize;
}

/**Function*************************************************************

  Synopsis    [Returns the current reading position.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Extra_FileReaderGetCurPosition( Extra_FileReader_t * p )
{
    return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
}

/**Function*************************************************************

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Extra_FileReaderGetLineNumber( Extra_FileReader_t * p, int iToken )
{
    assert( iToken >= 0 && iToken < p->vTokens->nSize );
    return p->vLines->pArray[iToken];
}


/**Function*************************************************************

  Synopsis    [Returns the next set of tokens.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void * Extra_FileReaderGetTokens( Extra_FileReader_t * p )
{
    Vec_Ptr_t * vTokens;
242
    while ( (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens_int( p )) )
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if ( vTokens->nSize > 0 )
            break;
    return vTokens;
}

/**Function*************************************************************

  Synopsis    [Returns the next set of tokens.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void * Extra_FileReaderGetTokens_int( Extra_FileReader_t * p )
{
    char * pChar;
    int fTokenStarted, MapValue;
    if ( p->fStop )
        return NULL;
    // reset the token info
    p->vTokens->nSize = 0;
    p->vLines->nSize = 0;
    fTokenStarted = 0;
    // check if the new data should to be loaded
    if ( p->pBufferCur > p->pBufferStop )
        Extra_FileReaderReload( p );
Alan Mishchenko committed
272 273 274

//    printf( "%d\n", p->pBufferEnd - p->pBufferCur );

Alan Mishchenko committed
275 276 277 278 279 280 281
    // process the string starting from the current position
    for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
    {
        // count the lines
        if ( *pChar == '\n' )
            p->nLineCounter++;
        // switch depending on the character
Alan Mishchenko committed
282
        MapValue = p->pCharMap[(int)*pChar];
Alan Mishchenko committed
283 284 285 286

//        printf( "Char value = %d. Map value = %d.\n", *pChar, MapValue );


Alan Mishchenko committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        switch ( MapValue )
        {
            case EXTRA_CHAR_COMMENT:
                if ( *pChar != '/' || *(pChar+1) == '/' ) 
                { // dealing with the need to have // as a comment
                    // if the token was being written, stop it
                    if ( fTokenStarted )
                        fTokenStarted = 0;
                    // eraze the comment till the end of line
                    while ( *pChar != '\n' )
                    {
                        *pChar++ = 0;
                        if ( pChar == p->pBufferEnd )
                        {   // this failure is due to the fact the comment continued 
                            // through EXTRA_OFFSET_SIZE chars till the end of the buffer
                            printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
                            return NULL;
                        }
                    }
                    pChar--;
                    break;
                }
                // otherwise it is a normal character
            case EXTRA_CHAR_NORMAL:
                if ( !fTokenStarted )
                {
                    Vec_PtrPush( p->vTokens, pChar );
                    Vec_IntPush( p->vLines, p->nLineCounter );
                    fTokenStarted = 1;
                }
                break;
            case EXTRA_CHAR_STOP:
                if ( fTokenStarted )
                    fTokenStarted = 0;
                *pChar = 0;
                // prepare before leaving
                p->pBufferCur = pChar + 1;
                return p->vTokens;
            case EXTRA_CHAR_CLEAN:
                if ( fTokenStarted )
                    fTokenStarted = 0;
                *pChar = 0;
                break;
            default:
                assert( 0 );
        }
    }
    // the file is finished or the last part continued 
    // through EXTRA_OFFSET_SIZE chars till the end of the buffer
    if ( p->pBufferStop == p->pBufferEnd ) // end of file
    {
Alan Mishchenko committed
338
        *pChar = 0;
Alan Mishchenko committed
339 340 341 342
        p->fStop = 1;
        return p->vTokens;
    }
    printf( "Extra_FileReader failed to parse the file \"%s\".\n", p->pFileName );
Alan Mishchenko committed
343 344 345 346 347 348 349 350
/*
    {
        int i;
        for ( i = 0; i < p->vTokens->nSize; i++ )
            printf( "%s ", p->vTokens->pArray[i] );
        printf( "\n" );
    }
*/
Alan Mishchenko committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    return NULL;
}

/**Function*************************************************************

  Synopsis    [Loads new data into the file reader.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_FileReaderReload( Extra_FileReader_t * p )
{
    int nCharsUsed, nCharsToRead;
368
    int RetValue;
Alan Mishchenko committed
369 370 371 372 373 374
    assert( !p->fStop );
    assert( p->pBufferCur > p->pBufferStop );
    assert( p->pBufferCur < p->pBufferEnd );
    // figure out how many chars are still not processed
    nCharsUsed = p->pBufferEnd - p->pBufferCur;
    // move the remaining data to the beginning of the buffer
375
    memmove( p->pBuffer, p->pBufferCur, (size_t)nCharsUsed );
Alan Mishchenko committed
376 377 378 379
    p->pBufferCur = p->pBuffer;
    // determine how many chars we will read
    nCharsToRead = EXTRA_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
    // read the chars
380
    RetValue = fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
Alan Mishchenko committed
381 382 383 384 385 386 387 388 389 390 391
    p->nFileRead += nCharsToRead;
    // set the ponters to the end and the stopping point
    p->pBufferEnd  = p->pBuffer + nCharsUsed + nCharsToRead;
    p->pBufferStop = (p->nFileRead == p->nFileSize)? p->pBufferEnd : p->pBuffer + EXTRA_BUFFER_SIZE - EXTRA_OFFSET_SIZE;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


392 393
ABC_NAMESPACE_IMPL_END