extraUtilFile.c 25.3 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 21 22
/**CFile****************************************************************

  FileName    [extraUtilFile.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [extra]

  Synopsis    [File management utilities.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: extraUtilFile.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "extra.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
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
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

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

  Synopsis    [Tries to find a file name with a different extension.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileGetSimilarName( char * pFileNameWrong, char * pS1, char * pS2, char * pS3, char * pS4, char * pS5 )
{
    FILE * pFile;
    char * pFileNameOther;
    char * pFileGen;

    if ( pS1 == NULL )
        return NULL;

    // get the generic file name
    pFileGen = Extra_FileNameGeneric( pFileNameWrong );
    pFileNameOther = Extra_FileNameAppend( pFileGen, pS1 );
    pFile = fopen( pFileNameOther, "r" );
    if ( pFile == NULL && pS2 )
    { // try one more
        pFileNameOther = Extra_FileNameAppend( pFileGen, pS2 );
        pFile = fopen( pFileNameOther, "r" );
        if ( pFile == NULL && pS3 )
        { // try one more
            pFileNameOther = Extra_FileNameAppend( pFileGen, pS3 );
            pFile = fopen( pFileNameOther, "r" );
            if ( pFile == NULL && pS4 )
            { // try one more
                pFileNameOther = Extra_FileNameAppend( pFileGen, pS4 );
                pFile = fopen( pFileNameOther, "r" );
                if ( pFile == NULL && pS5 )
                { // try one more
                    pFileNameOther = Extra_FileNameAppend( pFileGen, pS5 );
                    pFile = fopen( pFileNameOther, "r" );
                }
            }
        }
    }
Alan Mishchenko committed
104
    ABC_FREE( pFileGen );
Alan Mishchenko committed
105 106 107 108 109 110 111 112 113 114 115
    if ( pFile )
    {
        fclose( pFile );
        return pFileNameOther;
    }
    // did not find :(
    return NULL;            
}

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

Alan Mishchenko committed
116
  Synopsis    [Returns the pointer to the file extension.]
Alan Mishchenko committed
117 118 119 120 121 122 123 124

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
125
char * Extra_FileNameExtension( char * FileName )
Alan Mishchenko committed
126 127
{
    char * pDot;
Alan Mishchenko committed
128
    // find the last "dot" in the file name, if it is present
Alan Mishchenko committed
129 130
    for ( pDot = FileName + strlen(FileName)-1; pDot >= FileName; pDot-- )
        if ( *pDot == '.' )
Alan Mishchenko committed
131
            return pDot + 1;
132
   return FileName;
Alan Mishchenko committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

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

  Synopsis    [Returns the composite name of the file.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileNameAppend( char * pBase, char * pSuffix )
{
    static char Buffer[500];
149
    assert( strlen(pBase) + strlen(pSuffix) < 500 );
Alan Mishchenko committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    sprintf( Buffer, "%s%s", pBase, pSuffix );
    return Buffer;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileNameGeneric( char * FileName )
{
Alan Mishchenko committed
167
    char * pDot, * pRes;
Alan Mishchenko committed
168
    pRes = Extra_UtilStrsav( FileName );
Alan Mishchenko committed
169 170
    if ( (pDot = strrchr( pRes, '.' )) )
        *pDot = 0;
Alan Mishchenko committed
171 172 173 174 175
    return pRes;
}

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

Alan Mishchenko committed
176 177 178 179 180 181 182 183 184 185 186 187 188
  Synopsis    [Returns the composite name of the file.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileNameGenericAppend( char * pBase, char * pSuffix )
{
    static char Buffer[1000];
    char * pDot;
189
    assert( strlen(pBase) + strlen(pSuffix) < 1000 );
Alan Mishchenko committed
190
    strcpy( Buffer, pBase );
Alan Mishchenko committed
191
    if ( (pDot = strrchr( Buffer, '.' )) )
Alan Mishchenko committed
192 193 194 195 196 197 198
        *pDot = 0;
    strcat( Buffer, pSuffix );
    return Buffer;
}

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

199 200 201 202 203 204 205 206 207
  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
void Extra_FileNameCorrectPath( char * FileName )
{
    char * pStart;
    if ( FileName )
        for ( pStart = FileName; *pStart; pStart++ )
            if ( *pStart == '>' || *pStart == '\\' )
                *pStart = '/';
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
228 229 230 231 232 233 234 235
char * Extra_FileNameWithoutPath( char * FileName )
{
    char * pRes;
    for ( pRes = FileName + strlen(FileName) - 1; pRes >= FileName; pRes-- )
        if ( *pRes == '\\' || *pRes == '/' )
            return pRes + 1;
    return FileName;
}
236 237 238 239 240 241 242
char * Extra_FilePathWithoutName( char * FileName )
{
    char * pRes;
    FileName = Abc_UtilStrsav( FileName );
    for ( pRes = FileName + strlen(FileName) - 1; pRes >= FileName; pRes-- )
        if ( *pRes == '\\' || *pRes == '/' )
        {
Alan Mishchenko committed
243
           pRes[1] = '\0';
244 245
           Extra_FileNameCorrectPath( FileName );
           return FileName;
246 247 248 249
        }
    ABC_FREE( FileName );
    return NULL;
}
Alan Mishchenko committed
250 251
char * Extra_FileInTheSameDir( char * pPathFile, char * pFileName )
{
Alan Mishchenko committed
252 253 254 255 256 257 258 259
    static char pBuffer[1000]; char * pThis;
    assert( strlen(pPathFile) + strlen(pFileName) < 990 );
    memmove( pBuffer, pPathFile, strlen(pPathFile) );
    for ( pThis = pBuffer + strlen(pPathFile) - 1; pThis >= pBuffer; pThis-- )
        if ( *pThis == '\\' || *pThis == '/' )
            break;
    memmove( ++pThis, pFileName, strlen(pFileName) );
    pThis[strlen(pFileName)] = '\0';
Alan Mishchenko committed
260 261
    return pBuffer;
}
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
char * Extra_FileDesignName( char * pFileName )
{
    char * pBeg, * pEnd, * pStore, * pCur;
    // find the first dot
    for ( pEnd = pFileName; *pEnd; pEnd++ )
        if ( *pEnd == '.' )
            break;
    // find the first char
    for ( pBeg = pEnd - 1; pBeg >= pFileName; pBeg-- )
        if ( !((*pBeg >= 'a' && *pBeg <= 'z') || (*pBeg >= 'A' && *pBeg <= 'Z') || (*pBeg >= '0' && *pBeg <= '9') || *pBeg == '_') )
            break;
    pBeg++;
    // fill up storage
    pStore = ABC_ALLOC( char, pEnd - pBeg + 1 );
    for ( pCur = pStore; pBeg < pEnd; pBeg++, pCur++ )
        *pCur = *pBeg;
    *pCur = 0;
    return pStore;
}
281 282 283

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

Alan Mishchenko committed
284 285 286 287 288 289 290 291 292
  Synopsis    [Returns the file size.]

  Description [The file should be closed.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
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
int Extra_FileCheck( char * pFileName )
{
    FILE * pFile;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Extra_FileCheck():  File \"%s\" does not exist.\n", pFileName );
        return 0;
    }
    fseek( pFile, 0, SEEK_END );
    if ( ftell( pFile ) == 0 )
        printf( "Extra_FileCheck():  File \"%s\" is empty.\n", pFileName );
    fclose( pFile );
    return 1;
}

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

  Synopsis    [Returns the file size.]

  Description [The file should be closed.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
320 321 322 323
int Extra_FileSize( char * pFileName )
{
    FILE * pFile;
    int nFileSize;
324
    pFile = fopen( pFileName, "rb" );
Alan Mishchenko committed
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
    if ( pFile == NULL )
    {
        printf( "Extra_FileSize(): The file is unavailable (absent or open).\n" );
        return 0;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    fclose( pFile );
    return nFileSize;
}


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

  Synopsis    [Read the file into the internal buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileRead( FILE * pFile )
{
    int nFileSize;
    char * pBuffer;
352
    int RetValue;
Alan Mishchenko committed
353 354 355 356 357 358
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile );  
    // move the file current reading position to the beginning
    rewind( pFile ); 
    // load the contents of the file into memory
Alan Mishchenko committed
359
    pBuffer = ABC_ALLOC( char, nFileSize + 3 );
360
    RetValue = fread( pBuffer, nFileSize, 1, pFile );
Alan Mishchenko committed
361 362 363 364 365
    // terminate the string with '\0'
    pBuffer[ nFileSize + 0] = '\n';
    pBuffer[ nFileSize + 1] = '\0';
    return pBuffer;
}
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
char * Extra_FileRead2( FILE * pFile, FILE * pFile2 )
{
    char * pBuffer;
    int nSize, nSize2;
    int RetValue;
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    nSize = ftell( pFile );  
    rewind( pFile ); 
    // get the file size, in bytes
    fseek( pFile2, 0, SEEK_END );  
    nSize2 = ftell( pFile2 );  
    rewind( pFile2 ); 
    // load the contents of the file into memory
    pBuffer = ABC_ALLOC( char, nSize + nSize2 + 3 );
    RetValue = fread( pBuffer,         nSize,  1, pFile );
    RetValue = fread( pBuffer + nSize, nSize2, 1, pFile2 );
    // terminate the string with '\0'
    pBuffer[ nSize + nSize2 + 0] = '\n';
    pBuffer[ nSize + nSize2 + 1] = '\0';
    return pBuffer;
}
Alan Mishchenko committed
388 389 390

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

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  Synopsis    [Read the file into the internal buffer.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_FileReadContents( char * pFileName )
{
    FILE * pFile;
    char * pBuffer;
    pFile = fopen( pFileName, "rb" );
    pBuffer = pFile ? Extra_FileRead( pFile ) : NULL;
406 407 408 409 410 411 412 413 414 415 416 417
    if ( pFile )  fclose( pFile );
    return pBuffer;
}
char * Extra_FileReadContents2( char * pFileName, char * pFileName2 )
{
    FILE * pFile, * pFile2;
    char * pBuffer;
    pFile  = fopen( pFileName, "rb" );
    pFile2 = fopen( pFileName2, "rb" );
    pBuffer = (pFile && pFile2) ? Extra_FileRead2( pFile, pFile2 ) : NULL;
    if ( pFile )  fclose( pFile );
    if ( pFile2 ) fclose( pFile2 );
418 419 420 421 422
    return pBuffer;
}

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

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
  Synopsis    [Returns one if the file has a given extension.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Extra_FileIsType( char * pFileName, char * pS1, char * pS2, char * pS3 )
{
    int lenS, lenF = strlen(pFileName);
    lenS = pS1 ? strlen(pS1) : 0;
    if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS1, lenS ) )
        return 1;
    lenS = pS2 ? strlen(pS2) : 0;
    if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS2, lenS ) )
        return 1;
    lenS = pS3 ? strlen(pS3) : 0;
    if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS3, lenS ) )
        return 1;
    return 0;
}

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

Alan Mishchenko committed
449 450 451 452 453 454 455 456 457 458 459 460
  Synopsis    [Returns the time stamp.]

  Description [The file should be closed.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_TimeStamp()
{
    static char Buffer[100];
Alan Mishchenko committed
461
    char * TimeStamp;
Alan Mishchenko committed
462
    time_t ltime;
Alan Mishchenko committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    // get the current time
    time( &ltime );
    TimeStamp = asctime( localtime( &ltime ) );
    TimeStamp[ strlen(TimeStamp) - 1 ] = 0;
    strcpy( Buffer, TimeStamp );
    return Buffer;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Extra_ReadBinary( char * Buffer )
{
    unsigned Result;
    int i;

    Result = 0;
    for ( i = 0; Buffer[i]; i++ )
        if ( Buffer[i] == '0' || Buffer[i] == '1' )
            Result = Result * 2 + Buffer[i] - '0';
        else
        {
            assert( 0 );
        }
    return Result;
}

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

  Synopsis    [Prints the bit string.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_PrintBinary( FILE * pFile, unsigned Sign[], int nBits )
{
511 512 513 514
    int i;
    for ( i = nBits-1; i >= 0; i-- )
        fprintf( pFile, "%c", '0' + Abc_InfoHasBit(Sign, i) );
//    fprintf( pFile, "\n" );
Alan Mishchenko committed
515
}
516 517
void Extra_PrintBinary2( FILE * pFile, unsigned Sign[], int nBits )
{
518 519 520
    int i;
    for ( i = 0; i < nBits; i++ )
        fprintf( pFile, "%c", '0' + Abc_InfoHasBit(Sign, i) );
521 522
//    fprintf( pFile, "\n" );
}
Alan Mishchenko committed
523

Alan Mishchenko committed
524 525
/**Function*************************************************************

Alan Mishchenko committed
526 527 528 529 530 531 532 533 534
  Synopsis    [Reads the hex unsigned into the bit-string.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
535
int Extra_ReadHex( unsigned Sign[], char * pString, int nDigits )
Alan Mishchenko committed
536
{
537
    int Digit, k, c;
Alan Mishchenko committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551
    for ( k = 0; k < nDigits; k++ )
    {
        c = nDigits-1-k;
        if ( pString[c] >= '0' && pString[c] <= '9' )
            Digit = pString[c] - '0';
        else if ( pString[c] >= 'A' && pString[c] <= 'F' )
            Digit = pString[c] - 'A' + 10;
        else if ( pString[c] >= 'a' && pString[c] <= 'f' )
            Digit = pString[c] - 'a' + 10;
        else { assert( 0 ); return 0; }
        Sign[k/8] |= ( (Digit & 15) << ((k%8) * 4) );
    }
    return 1;
}
552 553 554 555 556 557 558 559 560 561 562 563 564
int Extra_ReadHexadecimal( unsigned Sign[], char * pString, int nVars )
{
    int nWords, nDigits, k;
    nWords = Extra_TruthWordNum( nVars );
    for ( k = 0; k < nWords; k++ )
        Sign[k] = 0;
    // read the number from the string
    nDigits = (1 << nVars) / 4;
    if ( nDigits == 0 )
        nDigits = 1;
    Extra_ReadHex( Sign, pString, nDigits );
    return 1;
}
Alan Mishchenko committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

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

  Synopsis    [Prints the hex unsigned into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_PrintHexadecimal( FILE * pFile, unsigned Sign[], int nVars )
{
    int nDigits, Digit, k;
    // write the number into the file
    nDigits = (1 << nVars) / 4;
    for ( k = nDigits - 1; k >= 0; k-- )
    {
        Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
        if ( Digit < 10 )
            fprintf( pFile, "%d", Digit );
        else
            fprintf( pFile, "%c", 'a' + Digit-10 );
    }
//    fprintf( pFile, "\n" );
}

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

  Synopsis    [Prints the hex unsigned into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_PrintHexadecimalString( char * pString, unsigned Sign[], int nVars )
{
    int nDigits, Digit, k;
607 608 609 610
    if ( nVars == 0 && !(Sign[0] & 1) ) { sprintf(pString, "0"); return; } // const0
    if ( nVars == 0 &&  (Sign[0] & 1) ) { sprintf(pString, "1"); return; } // const1
    if ( nVars == 1 &&  (Sign[0] & 1) ) { sprintf(pString, "1"); return; } // inverter
    if ( nVars == 1 && !(Sign[0] & 1) ) { sprintf(pString, "2"); return; } // buffer
Alan Mishchenko committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    // write the number into the file
    nDigits = (1 << nVars) / 4;
    for ( k = nDigits - 1; k >= 0; k-- )
    {
        Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
        if ( Digit < 10 )
            *pString++ = '0' + Digit;
        else
            *pString++ = 'a' + Digit-10;
    }
//    fprintf( pFile, "\n" );
    *pString = 0;
}

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

Alan Mishchenko committed
627 628 629 630 631 632 633 634 635
  Synopsis    [Prints the hex unsigned into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
636
void Extra_PrintHex( FILE * pFile, unsigned * pTruth, int nVars )
Alan Mishchenko committed
637 638 639 640 641 642
{
    int nMints, nDigits, Digit, k;

    // write the number into the file
    fprintf( pFile, "0x" );
    nMints  = (1 << nVars);
643
    nDigits = nMints / 4 + ((nMints % 4) > 0);
Alan Mishchenko committed
644 645
    for ( k = nDigits - 1; k >= 0; k-- )
    {
646
        Digit = ((pTruth[k/8] >> (k * 4)) & 15);
Alan Mishchenko committed
647 648 649
        if ( Digit < 10 )
            fprintf( pFile, "%d", Digit );
        else
650
            fprintf( pFile, "%c", 'A' + Digit-10 );
Alan Mishchenko committed
651 652 653
    }
//    fprintf( pFile, "\n" );
}
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
void Extra_PrintHex2( FILE * pFile, unsigned * pTruth, int nVars )
{
    int nMints, nDigits, Digit, k;

    // write the number into the file
    //fprintf( pFile, "0x" );
    nMints  = (1 << nVars);
    nDigits = nMints / 4 + ((nMints % 4) > 0);
    for ( k = nDigits - 1; k >= 0; k-- )
    {
        Digit = ((pTruth[k/8] >> (k * 4)) & 15);
        if ( Digit < 10 )
            fprintf( pFile, "%d", Digit );
        else
            fprintf( pFile, "%c", 'A' + Digit-10 );
    }
//    fprintf( pFile, "\n" );
}
672 673 674 675 676 677 678
void Extra_PrintHexReverse( FILE * pFile, unsigned * pTruth, int nVars )
{
    int nMints, nDigits, Digit, k;

    // write the number into the file
    fprintf( pFile, "0x" );
    nMints  = (1 << nVars);
679
    nDigits = nMints / 4 + ((nMints % 4) > 0);
680 681 682 683 684 685 686 687 688 689
    for ( k = 0; k < nDigits; k++ )
    {
        Digit = ((pTruth[k/8] >> (k * 4)) & 15);
        if ( Digit < 10 )
            fprintf( pFile, "%d", Digit );
        else
            fprintf( pFile, "%c", 'A' + Digit-10 );
    }
//    fprintf( pFile, "\n" );
}
Alan Mishchenko committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

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

  Synopsis    [Returns the composite name of the file.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_PrintSymbols( FILE * pFile, char Char, int nTimes, int fPrintNewLine )
{
    int i;
    for ( i = 0; i < nTimes; i++ )
        printf( "%c", Char );
    if ( fPrintNewLine )
        printf( "\n" );
}

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

  Synopsis    [Appends the string.]

  Description [Assumes that the given string (pStrGiven) has been allocated
  before using malloc(). The additional string has not been allocated.
  Allocs more root, appends the additional part, frees the old given string.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Extra_StringAppend( char * pStrGiven, char * pStrAdd )
{
    char * pTemp;
    if ( pStrGiven )
    {
Alan Mishchenko committed
729
        pTemp = ABC_ALLOC( char, strlen(pStrGiven) + strlen(pStrAdd) + 2 );
Alan Mishchenko committed
730
        sprintf( pTemp, "%s%s", pStrGiven, pStrAdd );
Alan Mishchenko committed
731
        ABC_FREE( pStrGiven );
Alan Mishchenko committed
732 733
    }
    else
Alan Mishchenko committed
734
        pTemp = Extra_UtilStrsav( pStrAdd );
Alan Mishchenko committed
735 736 737
    return pTemp;
}

738 739
/**Function*************************************************************

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  Synopsis    [Only keep characters belonging to the second string.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_StringClean( char * pStrGiven, char * pCharKeep )
{
    char * pTemp, * pChar, * pSave = pStrGiven;
    for ( pTemp = pStrGiven; *pTemp; pTemp++ )
    {
        for ( pChar = pCharKeep; *pChar; pChar++ )
            if ( *pTemp == *pChar )
                break;
        if ( *pChar == 0 )
            continue;
        *pSave++ = *pTemp;
    }
    *pSave = 0;
}

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

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  Synopsis    [String comparison procedure.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Extra_StringCompare( const char * pp1, const char * pp2 )
{
    return strcmp(*(char **)pp1, *(char **)pp2);
}

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

  Synopsis    [Sorts lines in the file alphabetically.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_FileSort( char * pFileName, char * pFileNameOut )
{
    FILE * pFile;
    char * pContents;
    char ** pLines;
    int i, nLines, Begin;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Extra_FileSort(): Cannot open file \"%s\".\n", pFileName );
        return;
    }
    pContents = Extra_FileRead( pFile );
    fclose( pFile );
    if ( pContents == NULL )
    {
        printf( "Extra_FileSort(): Cannot read contents of file \"%s\".\n", pFileName );
        return;
    }
    // count end of lines
    for ( nLines = 0, i = 0; pContents[i]; i++ )
        nLines += (pContents[i] == '\n');
    // break the file into lines
    pLines = (char **)malloc( sizeof(char *) * nLines );
    Begin = 0;
    for ( nLines = 0, i = 0; pContents[i]; i++ )
        if ( pContents[i] == '\n' )
        {
            pContents[i] = 0;
            pLines[nLines++] = pContents + Begin;
            Begin = i + 1;
        }
    // sort the lines
824
    qsort( pLines, (size_t)nLines, sizeof(char *), (int(*)(const void *,const void *))Extra_StringCompare );
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    // write a new file
    pFile = fopen( pFileNameOut, "wb" );
    for ( i = 0; i < nLines; i++ )
        if ( pLines[i][0] )
            fprintf( pFile, "%s\n", pLines[i] );
    fclose( pFile );
    // cleanup
    free( pLines );
    free( pContents );
    // report the result
    printf( "The file after sorting is \"%s\".\n", pFileNameOut );
}


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

Alan Mishchenko committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
  Synopsis    [Appends line number in the end.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Extra_FileLineNumAdd( char * pFileName, char * pFileNameOut )
{
    char Buffer[1000];
    FILE * pFile;
    FILE * pFile2;
    int iLine;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Extra_FileLineNumAdd(): Cannot open file \"%s\".\n", pFileName );
        return;
    }
    pFile2 = fopen( pFileNameOut, "wb" );
    if ( pFile2 == NULL )
    {
        fclose( pFile );
        printf( "Extra_FileLineNumAdd(): Cannot open file \"%s\".\n", pFileNameOut );
        return;
    }
    for ( iLine = 0; fgets( Buffer, 1000, pFile ); iLine++ )
    {
        sprintf( Buffer + strlen(Buffer) - 2, "%03d\n%c", iLine, 0 );
        fputs( Buffer, pFile2 );
    }
    fclose( pFile );
    fclose( pFile2 );
    // report the result
    printf( "The resulting file is \"%s\".\n", pFileNameOut );
}

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

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
/*
int main( int argc, char ** argv )
{
    if ( argc == 2 )
        Extra_FileSort( argv[1], Extra_FileNameAppend(argv[1], "_sorted") );
    else
        printf( "%s: Wrong number of command line arguments.\n", argv[0] );
    return 1;
}
*/

Alan Mishchenko committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915
/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Definition of static Functions                                            */
/*---------------------------------------------------------------------------*/


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


916 917
ABC_NAMESPACE_IMPL_END