summaryrefslogtreecommitdiffstats
path: root/src/base/ver/verStream.c
blob: 75e183b37f97b8a63f77b5a4515b2a9b1c4e7e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
156
157
158
159
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
242
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**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 $]

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

#include "ver.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define VER_BUFFER_SIZE        1048576    // 1M  - size of the data chunk stored in memory
#define VER_OFFSET_SIZE          65536    // 64K - load new data when less than this is left
#define VER_WORD_SIZE            65536    // 64K - the largest token that can be returned

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

struct Ver_Stream_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
    int              nLineCounter;  // the counter of lines processed
    // 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
    char             pChars[VER_WORD_SIZE+5]; // temporary storage for a word (plus end-of-string and two parantheses)
    int              nChars;        // the total number of characters in the word
    // status of the parser
    int              fStop;         // this flag goes high when the end of file is reached
};

static void Ver_StreamReload( Ver_Stream_t * p );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ver_Stream_t * Ver_StreamAlloc( char * pFileName )
{
    Ver_Stream_t * p;
    FILE * pFile;
    int nCharsToRead;
    // check if the file can be opened
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Ver_StreamAlloc(): Cannot open input file \"%s\".\n", pFileName );
        return NULL;
    }
    // start the file reader    
    p = ABC_ALLOC( Ver_Stream_t, 1 );
    memset( p, 0, sizeof(Ver_Stream_t) );
    p->pFileName   = pFileName;
    p->pFile       = pFile;
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    p->nFileSize = ftell( pFile );  
    rewind( pFile ); 
    // allocate the buffer
    p->pBuffer = ABC_ALLOC( char, VER_BUFFER_SIZE+1 );
    p->nBufferSize = VER_BUFFER_SIZE;
    p->pBufferCur  = p->pBuffer;
    // determine how many chars to read
    nCharsToRead = VER_MINIMUM(p->nFileSize, VER_BUFFER_SIZE);
    // load the first part into the buffer
    fread( p->pBuffer, nCharsToRead, 1, p->pFile );
    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 + VER_BUFFER_SIZE - VER_OFFSET_SIZE;
    // start the arrays
    p->nLineCounter = 1; // 1-based line counting
    return p;
}

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

  Synopsis    [Loads new data into the file reader.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ver_StreamReload( Ver_Stream_t * p )
{
    int nCharsUsed, nCharsToRead;
    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
    memmove( p->pBuffer, p->pBufferCur, nCharsUsed );
    p->pBufferCur = p->pBuffer;
    // determine how many chars we will read
    nCharsToRead = VER_MINIMUM( p->nBufferSize - nCharsUsed, p->nFileSize - p->nFileRead );
    // read the chars
    fread( p->pBuffer + nCharsUsed, nCharsToRead, 1, p->pFile );
    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 + VER_BUFFER_SIZE - VER_OFFSET_SIZE;
}

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

  Synopsis    [Stops the file reader.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ver_StreamFree( Ver_Stream_t * p )
{
    if ( p->pFile )
        fclose( p->pFile );
    ABC_FREE( p->pBuffer );
    ABC_FREE( p );
}

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

  Synopsis    [Returns the file size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Ver_StreamGetFileName( Ver_Stream_t * p )
{
    return p->pFileName;
}

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

  Synopsis    [Returns the file size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ver_StreamGetFileSize( Ver_Stream_t * p )
{
    return p->nFileSize;
}

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

  Synopsis    [Returns the current reading position.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ver_StreamGetCurPosition( Ver_Stream_t * p )
{
    return p->nFileRead - (p->pBufferEnd - p->pBufferCur);
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ver_StreamGetLineNumber( Ver_Stream_t * p )
{
    return p->nLineCounter;
}



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

  Synopsis    [Returns current symbol.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ver_StreamIsOkey( Ver_Stream_t * p )
{
    return !p->fStop;
}

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

  Synopsis    [Returns current symbol.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char Ver_StreamScanChar( Ver_Stream_t * p )
{
    assert( !p->fStop );
    return *p->pBufferCur;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char Ver_StreamPopChar( Ver_Stream_t * p )
{
    assert( !p->fStop );
    // check if the new data should to be loaded
    if ( p->pBufferCur > p->pBufferStop )
        Ver_StreamReload( p );
    // check if there are symbols left
    if ( p->pBufferCur == p->pBufferEnd ) // end of file
    {
        p->fStop = 1;
        return -1;
    }
    // count the lines
    if ( *p->pBufferCur == '\n' )
        p->nLineCounter++;
    return *p->pBufferCur++;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ver_StreamSkipChars( Ver_Stream_t * p, char * pCharsToSkip )
{
    char * pChar, * pTemp;
    assert( !p->fStop );
    assert( pCharsToSkip != NULL );
    // check if the new data should to be loaded
    if ( p->pBufferCur > p->pBufferStop )
        Ver_StreamReload( p );
    // skip the symbols
    for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
    {
        // skip symbols as long as they are in the list
        for ( pTemp = pCharsToSkip; *pTemp; pTemp++ )
            if ( *pChar == *pTemp )
                break;
        if ( *pTemp == 0 ) // pChar is not found in the list
        {
            p->pBufferCur = pChar;
            return;
        }
        // count the lines
        if ( *pChar == '\n' )
            p->nLineCounter++;
    }
    // the file is finished or the last part continued 
    // through VER_OFFSET_SIZE chars till the end of the buffer
    if ( p->pBufferStop == p->pBufferEnd ) // end of file
    {
        p->fStop = 1;
        return;
    }
    printf( "Ver_StreamSkipSymbol() failed to parse the file \"%s\".\n", p->pFileName );
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ver_StreamSkipToChars( Ver_Stream_t * p, char * pCharsToStop )
{
    char * pChar, * pTemp;
    assert( !p->fStop );
    assert( pCharsToStop != NULL );
    // check if the new data should to be loaded
    if ( p->pBufferCur > p->pBufferStop )
        Ver_StreamReload( p );
    // skip the symbols
    for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
    {
        // skip symbols as long as they are NOT in the list
        for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
            if ( *pChar == *pTemp )
                break;
        if ( *pTemp == 0 ) // pChar is not found in the list
        {
            // count the lines
            if ( *pChar == '\n' )
                p->nLineCounter++;
            continue;
        }
        // the symbol is found - move position and return
        p->pBufferCur = pChar;
        return;
    }
    // the file is finished or the last part continued 
    // through VER_OFFSET_SIZE chars till the end of the buffer
    if ( p->pBufferStop == p->pBufferEnd ) // end of file
    {
        p->fStop = 1;
        return;
    }
    printf( "Ver_StreamSkipToSymbol() failed to parse the file \"%s\".\n", p->pFileName );
}

/**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     []

***********************************************************************/
char * Ver_StreamGetWord( Ver_Stream_t * p, char * pCharsToStop )
{
    char * pChar, * pTemp;
    if ( p->fStop )
        return NULL;
    assert( pCharsToStop != NULL );
    // check if the new data should to be loaded
    if ( p->pBufferCur > p->pBufferStop )
        Ver_StreamReload( p );
    // skip the symbols
    p->nChars = 0;
    for ( pChar = p->pBufferCur; pChar < p->pBufferEnd; pChar++ )
    {
        // skip symbols as long as they are NOT in the list
        for ( pTemp = pCharsToStop; *pTemp; pTemp++ )
            if ( *pChar == *pTemp )
                break;
        if ( *pTemp == 0 ) // pChar is not found in the list
        {
            p->pChars[p->nChars++] = *pChar;
            if ( p->nChars == VER_WORD_SIZE )
            {
                printf( "Ver_StreamGetWord(): The buffer size is exceeded.\n" );
                return NULL;
            }
            // count the lines
            if ( *pChar == '\n' )
                p->nLineCounter++;
            continue;
        }
        // the symbol is found - move the position, set the word end, return the word
        p->pBufferCur = pChar;
        p->pChars[p->nChars] = 0;
        return p->pChars;
    }
    // the file is finished or the last part continued 
    // through VER_OFFSET_SIZE chars till the end of the buffer
    if ( p->pBufferStop == p->pBufferEnd ) // end of file
    {
        p->fStop = 1;
        p->pChars[p->nChars] = 0;
        return p->pChars;
    }
    printf( "Ver_StreamGetWord() failed to parse the file \"%s\".\n", p->pFileName );
    return NULL;
}

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


ABC_NAMESPACE_IMPL_END