1 /**CFile****************************************************************
2 
3   FileName    [extraUtilFile.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [extra]
8 
9   Synopsis    [File management utilities.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: extraUtilFile.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "extra.h"
22 
23 ABC_NAMESPACE_IMPL_START
24 
25 
26 /*---------------------------------------------------------------------------*/
27 /* Constant declarations                                                     */
28 /*---------------------------------------------------------------------------*/
29 
30 /*---------------------------------------------------------------------------*/
31 /* Stucture declarations                                                     */
32 /*---------------------------------------------------------------------------*/
33 
34 /*---------------------------------------------------------------------------*/
35 /* Type declarations                                                         */
36 /*---------------------------------------------------------------------------*/
37 
38 /*---------------------------------------------------------------------------*/
39 /* Variable declarations                                                     */
40 /*---------------------------------------------------------------------------*/
41 
42 /*---------------------------------------------------------------------------*/
43 /* Macro declarations                                                        */
44 /*---------------------------------------------------------------------------*/
45 
46 
47 /**AutomaticStart*************************************************************/
48 
49 /*---------------------------------------------------------------------------*/
50 /* Static function prototypes                                                */
51 /*---------------------------------------------------------------------------*/
52 
53 /**AutomaticEnd***************************************************************/
54 
55 
56 /*---------------------------------------------------------------------------*/
57 /* Definition of exported functions                                          */
58 /*---------------------------------------------------------------------------*/
59 
60 /**Function*************************************************************
61 
62   Synopsis    [Tries to find a file name with a different extension.]
63 
64   Description []
65 
66   SideEffects []
67 
68   SeeAlso     []
69 
70 ***********************************************************************/
Extra_FileGetSimilarName(char * pFileNameWrong,char * pS1,char * pS2,char * pS3,char * pS4,char * pS5)71 char * Extra_FileGetSimilarName( char * pFileNameWrong, char * pS1, char * pS2, char * pS3, char * pS4, char * pS5 )
72 {
73     FILE * pFile;
74     char * pFileNameOther;
75     char * pFileGen;
76 
77     if ( pS1 == NULL )
78         return NULL;
79 
80     // get the generic file name
81     pFileGen = Extra_FileNameGeneric( pFileNameWrong );
82     pFileNameOther = Extra_FileNameAppend( pFileGen, pS1 );
83     pFile = fopen( pFileNameOther, "r" );
84     if ( pFile == NULL && pS2 )
85     { // try one more
86         pFileNameOther = Extra_FileNameAppend( pFileGen, pS2 );
87         pFile = fopen( pFileNameOther, "r" );
88         if ( pFile == NULL && pS3 )
89         { // try one more
90             pFileNameOther = Extra_FileNameAppend( pFileGen, pS3 );
91             pFile = fopen( pFileNameOther, "r" );
92             if ( pFile == NULL && pS4 )
93             { // try one more
94                 pFileNameOther = Extra_FileNameAppend( pFileGen, pS4 );
95                 pFile = fopen( pFileNameOther, "r" );
96                 if ( pFile == NULL && pS5 )
97                 { // try one more
98                     pFileNameOther = Extra_FileNameAppend( pFileGen, pS5 );
99                     pFile = fopen( pFileNameOther, "r" );
100                 }
101             }
102         }
103     }
104     ABC_FREE( pFileGen );
105     if ( pFile )
106     {
107         fclose( pFile );
108         return pFileNameOther;
109     }
110     // did not find :(
111     return NULL;
112 }
113 
114 /**Function*************************************************************
115 
116   Synopsis    [Returns the pointer to the file extension.]
117 
118   Description []
119 
120   SideEffects []
121 
122   SeeAlso     []
123 
124 ***********************************************************************/
Extra_FileNameExtension(char * FileName)125 char * Extra_FileNameExtension( char * FileName )
126 {
127     char * pDot;
128     // find the last "dot" in the file name, if it is present
129     for ( pDot = FileName + strlen(FileName)-1; pDot >= FileName; pDot-- )
130         if ( *pDot == '.' )
131             return pDot + 1;
132    return FileName;
133 }
134 
135 /**Function*************************************************************
136 
137   Synopsis    [Returns the composite name of the file.]
138 
139   Description []
140 
141   SideEffects []
142 
143   SeeAlso     []
144 
145 ***********************************************************************/
Extra_FileNameAppend(char * pBase,char * pSuffix)146 char * Extra_FileNameAppend( char * pBase, char * pSuffix )
147 {
148     static char Buffer[500];
149     assert( strlen(pBase) + strlen(pSuffix) < 500 );
150     sprintf( Buffer, "%s%s", pBase, pSuffix );
151     return Buffer;
152 }
153 
154 /**Function*************************************************************
155 
156   Synopsis    []
157 
158   Description []
159 
160   SideEffects []
161 
162   SeeAlso     []
163 
164 ***********************************************************************/
Extra_FileNameGeneric(char * FileName)165 char * Extra_FileNameGeneric( char * FileName )
166 {
167     char * pDot, * pRes;
168     pRes = Extra_UtilStrsav( FileName );
169     if ( (pDot = strrchr( pRes, '.' )) )
170         *pDot = 0;
171     return pRes;
172 }
173 
174 /**Function*************************************************************
175 
176   Synopsis    [Returns the composite name of the file.]
177 
178   Description []
179 
180   SideEffects []
181 
182   SeeAlso     []
183 
184 ***********************************************************************/
Extra_FileNameGenericAppend(char * pBase,char * pSuffix)185 char * Extra_FileNameGenericAppend( char * pBase, char * pSuffix )
186 {
187     static char Buffer[1000];
188     char * pDot;
189     assert( strlen(pBase) + strlen(pSuffix) < 1000 );
190     strcpy( Buffer, pBase );
191     if ( (pDot = strrchr( Buffer, '.' )) )
192         *pDot = 0;
193     strcat( Buffer, pSuffix );
194     return Buffer;
195 }
196 
197 /**Function*************************************************************
198 
199   Synopsis    []
200 
201   Description []
202 
203   SideEffects []
204 
205   SeeAlso     []
206 
207 ***********************************************************************/
Extra_FileNameCorrectPath(char * FileName)208 void Extra_FileNameCorrectPath( char * FileName )
209 {
210     char * pStart;
211     if ( FileName )
212         for ( pStart = FileName; *pStart; pStart++ )
213             if ( *pStart == '>' || *pStart == '\\' )
214                 *pStart = '/';
215 }
216 
217 /**Function*************************************************************
218 
219   Synopsis    []
220 
221   Description []
222 
223   SideEffects []
224 
225   SeeAlso     []
226 
227 ***********************************************************************/
Extra_FileNameWithoutPath(char * FileName)228 char * Extra_FileNameWithoutPath( char * FileName )
229 {
230     char * pRes;
231     for ( pRes = FileName + strlen(FileName) - 1; pRes >= FileName; pRes-- )
232         if ( *pRes == '\\' || *pRes == '/' )
233             return pRes + 1;
234     return FileName;
235 }
Extra_FilePathWithoutName(char * FileName)236 char * Extra_FilePathWithoutName( char * FileName )
237 {
238     char * pRes;
239     FileName = Abc_UtilStrsav( FileName );
240     for ( pRes = FileName + strlen(FileName) - 1; pRes >= FileName; pRes-- )
241         if ( *pRes == '\\' || *pRes == '/' )
242         {
243            *pRes = 0;
244            Extra_FileNameCorrectPath( FileName );
245            return FileName;
246         }
247     ABC_FREE( FileName );
248     return NULL;
249 }
Extra_FileDesignName(char * pFileName)250 char * Extra_FileDesignName( char * pFileName )
251 {
252     char * pBeg, * pEnd, * pStore, * pCur;
253     // find the first dot
254     for ( pEnd = pFileName; *pEnd; pEnd++ )
255         if ( *pEnd == '.' )
256             break;
257     // find the first char
258     for ( pBeg = pEnd - 1; pBeg >= pFileName; pBeg-- )
259         if ( !((*pBeg >= 'a' && *pBeg <= 'z') || (*pBeg >= 'A' && *pBeg <= 'Z') || (*pBeg >= '0' && *pBeg <= '9') || *pBeg == '_') )
260             break;
261     pBeg++;
262     // fill up storage
263     pStore = ABC_ALLOC( char, pEnd - pBeg + 1 );
264     for ( pCur = pStore; pBeg < pEnd; pBeg++, pCur++ )
265         *pCur = *pBeg;
266     *pCur = 0;
267     return pStore;
268 }
269 
270 /**Function*************************************************************
271 
272   Synopsis    [Returns the file size.]
273 
274   Description [The file should be closed.]
275 
276   SideEffects []
277 
278   SeeAlso     []
279 
280 ***********************************************************************/
Extra_FileCheck(char * pFileName)281 int Extra_FileCheck( char * pFileName )
282 {
283     FILE * pFile;
284     pFile = fopen( pFileName, "rb" );
285     if ( pFile == NULL )
286     {
287         printf( "Extra_FileCheck():  File \"%s\" does not exist.\n", pFileName );
288         return 0;
289     }
290     fseek( pFile, 0, SEEK_END );
291     if ( ftell( pFile ) == 0 )
292         printf( "Extra_FileCheck():  File \"%s\" is empty.\n", pFileName );
293     fclose( pFile );
294     return 1;
295 }
296 
297 /**Function*************************************************************
298 
299   Synopsis    [Returns the file size.]
300 
301   Description [The file should be closed.]
302 
303   SideEffects []
304 
305   SeeAlso     []
306 
307 ***********************************************************************/
Extra_FileSize(char * pFileName)308 int Extra_FileSize( char * pFileName )
309 {
310     FILE * pFile;
311     int nFileSize;
312     pFile = fopen( pFileName, "rb" );
313     if ( pFile == NULL )
314     {
315         printf( "Extra_FileSize(): The file is unavailable (absent or open).\n" );
316         return 0;
317     }
318     fseek( pFile, 0, SEEK_END );
319     nFileSize = ftell( pFile );
320     fclose( pFile );
321     return nFileSize;
322 }
323 
324 
325 /**Function*************************************************************
326 
327   Synopsis    [Read the file into the internal buffer.]
328 
329   Description []
330 
331   SideEffects []
332 
333   SeeAlso     []
334 
335 ***********************************************************************/
Extra_FileRead(FILE * pFile)336 char * Extra_FileRead( FILE * pFile )
337 {
338     int nFileSize;
339     char * pBuffer;
340     int RetValue;
341     // get the file size, in bytes
342     fseek( pFile, 0, SEEK_END );
343     nFileSize = ftell( pFile );
344     // move the file current reading position to the beginning
345     rewind( pFile );
346     // load the contents of the file into memory
347     pBuffer = ABC_ALLOC( char, nFileSize + 3 );
348     RetValue = fread( pBuffer, nFileSize, 1, pFile );
349     // terminate the string with '\0'
350     pBuffer[ nFileSize + 0] = '\n';
351     pBuffer[ nFileSize + 1] = '\0';
352     return pBuffer;
353 }
Extra_FileRead2(FILE * pFile,FILE * pFile2)354 char * Extra_FileRead2( FILE * pFile, FILE * pFile2 )
355 {
356     char * pBuffer;
357     int nSize, nSize2;
358     int RetValue;
359     // get the file size, in bytes
360     fseek( pFile, 0, SEEK_END );
361     nSize = ftell( pFile );
362     rewind( pFile );
363     // get the file size, in bytes
364     fseek( pFile2, 0, SEEK_END );
365     nSize2 = ftell( pFile2 );
366     rewind( pFile2 );
367     // load the contents of the file into memory
368     pBuffer = ABC_ALLOC( char, nSize + nSize2 + 3 );
369     RetValue = fread( pBuffer,         nSize,  1, pFile );
370     RetValue = fread( pBuffer + nSize, nSize2, 1, pFile2 );
371     // terminate the string with '\0'
372     pBuffer[ nSize + nSize2 + 0] = '\n';
373     pBuffer[ nSize + nSize2 + 1] = '\0';
374     return pBuffer;
375 }
376 
377 /**Function*************************************************************
378 
379   Synopsis    [Read the file into the internal buffer.]
380 
381   Description []
382 
383   SideEffects []
384 
385   SeeAlso     []
386 
387 ***********************************************************************/
Extra_FileReadContents(char * pFileName)388 char * Extra_FileReadContents( char * pFileName )
389 {
390     FILE * pFile;
391     char * pBuffer;
392     pFile = fopen( pFileName, "rb" );
393     pBuffer = pFile ? Extra_FileRead( pFile ) : NULL;
394     if ( pFile )  fclose( pFile );
395     return pBuffer;
396 }
Extra_FileReadContents2(char * pFileName,char * pFileName2)397 char * Extra_FileReadContents2( char * pFileName, char * pFileName2 )
398 {
399     FILE * pFile, * pFile2;
400     char * pBuffer;
401     pFile  = fopen( pFileName, "rb" );
402     pFile2 = fopen( pFileName2, "rb" );
403     pBuffer = (pFile && pFile2) ? Extra_FileRead2( pFile, pFile2 ) : NULL;
404     if ( pFile )  fclose( pFile );
405     if ( pFile2 ) fclose( pFile2 );
406     return pBuffer;
407 }
408 
409 /**Function*************************************************************
410 
411   Synopsis    [Returns one if the file has a given extension.]
412 
413   Description []
414 
415   SideEffects []
416 
417   SeeAlso     []
418 
419 ***********************************************************************/
Extra_FileIsType(char * pFileName,char * pS1,char * pS2,char * pS3)420 int Extra_FileIsType( char * pFileName, char * pS1, char * pS2, char * pS3 )
421 {
422     int lenS, lenF = strlen(pFileName);
423     lenS = pS1 ? strlen(pS1) : 0;
424     if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS1, lenS ) )
425         return 1;
426     lenS = pS2 ? strlen(pS2) : 0;
427     if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS2, lenS ) )
428         return 1;
429     lenS = pS3 ? strlen(pS3) : 0;
430     if ( lenS && lenF > lenS && !strncmp( pFileName+lenF-lenS, pS3, lenS ) )
431         return 1;
432     return 0;
433 }
434 
435 /**Function*************************************************************
436 
437   Synopsis    [Returns the time stamp.]
438 
439   Description [The file should be closed.]
440 
441   SideEffects []
442 
443   SeeAlso     []
444 
445 ***********************************************************************/
Extra_TimeStamp()446 char * Extra_TimeStamp()
447 {
448     static char Buffer[100];
449     char * TimeStamp;
450     time_t ltime;
451     // get the current time
452     time( &ltime );
453     TimeStamp = asctime( localtime( &ltime ) );
454     TimeStamp[ strlen(TimeStamp) - 1 ] = 0;
455     strcpy( Buffer, TimeStamp );
456     return Buffer;
457 }
458 
459 /**Function*************************************************************
460 
461   Synopsis    []
462 
463   Description []
464 
465   SideEffects []
466 
467   SeeAlso     []
468 
469 ***********************************************************************/
Extra_ReadBinary(char * Buffer)470 unsigned Extra_ReadBinary( char * Buffer )
471 {
472     unsigned Result;
473     int i;
474 
475     Result = 0;
476     for ( i = 0; Buffer[i]; i++ )
477         if ( Buffer[i] == '0' || Buffer[i] == '1' )
478             Result = Result * 2 + Buffer[i] - '0';
479         else
480         {
481             assert( 0 );
482         }
483     return Result;
484 }
485 
486 /**Function*************************************************************
487 
488   Synopsis    [Prints the bit string.]
489 
490   Description []
491 
492   SideEffects []
493 
494   SeeAlso     []
495 
496 ***********************************************************************/
Extra_PrintBinary(FILE * pFile,unsigned Sign[],int nBits)497 void Extra_PrintBinary( FILE * pFile, unsigned Sign[], int nBits )
498 {
499     int Remainder, nWords;
500     int w, i;
501 
502     Remainder = (nBits%(sizeof(unsigned)*8));
503     nWords    = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
504 
505     for ( w = nWords-1; w >= 0; w-- )
506         for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
507             fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
508 
509 //  fprintf( pFile, "\n" );
510 }
Extra_PrintBinary2(FILE * pFile,unsigned Sign[],int nBits)511 void Extra_PrintBinary2( FILE * pFile, unsigned Sign[], int nBits )
512 {
513     int Remainder, nWords;
514     int w, i;
515 
516     Remainder = (nBits%(sizeof(unsigned)*8));
517     nWords    = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
518 
519     for ( w = 0; w < nWords; w++ )
520     {
521         int Limit = w == nWords-1 ? Remainder : 32;
522         for ( i = 0; i < Limit; i++ )
523             fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
524     }
525 
526 //    fprintf( pFile, "\n" );
527 }
528 
529 /**Function*************************************************************
530 
531   Synopsis    [Reads the hex unsigned into the bit-string.]
532 
533   Description []
534 
535   SideEffects []
536 
537   SeeAlso     []
538 
539 ***********************************************************************/
Extra_ReadHex(unsigned Sign[],char * pString,int nDigits)540 int Extra_ReadHex( unsigned Sign[], char * pString, int nDigits )
541 {
542     int Digit, k, c;
543     for ( k = 0; k < nDigits; k++ )
544     {
545         c = nDigits-1-k;
546         if ( pString[c] >= '0' && pString[c] <= '9' )
547             Digit = pString[c] - '0';
548         else if ( pString[c] >= 'A' && pString[c] <= 'F' )
549             Digit = pString[c] - 'A' + 10;
550         else if ( pString[c] >= 'a' && pString[c] <= 'f' )
551             Digit = pString[c] - 'a' + 10;
552         else { assert( 0 ); return 0; }
553         Sign[k/8] |= ( (Digit & 15) << ((k%8) * 4) );
554     }
555     return 1;
556 }
Extra_ReadHexadecimal(unsigned Sign[],char * pString,int nVars)557 int Extra_ReadHexadecimal( unsigned Sign[], char * pString, int nVars )
558 {
559     int nWords, nDigits, k;
560     nWords = Extra_TruthWordNum( nVars );
561     for ( k = 0; k < nWords; k++ )
562         Sign[k] = 0;
563     // read the number from the string
564     nDigits = (1 << nVars) / 4;
565     if ( nDigits == 0 )
566         nDigits = 1;
567     Extra_ReadHex( Sign, pString, nDigits );
568     return 1;
569 }
570 
571 /**Function*************************************************************
572 
573   Synopsis    [Prints the hex unsigned into a file.]
574 
575   Description []
576 
577   SideEffects []
578 
579   SeeAlso     []
580 
581 ***********************************************************************/
Extra_PrintHexadecimal(FILE * pFile,unsigned Sign[],int nVars)582 void Extra_PrintHexadecimal( FILE * pFile, unsigned Sign[], int nVars )
583 {
584     int nDigits, Digit, k;
585     // write the number into the file
586     nDigits = (1 << nVars) / 4;
587     for ( k = nDigits - 1; k >= 0; k-- )
588     {
589         Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
590         if ( Digit < 10 )
591             fprintf( pFile, "%d", Digit );
592         else
593             fprintf( pFile, "%c", 'a' + Digit-10 );
594     }
595 //    fprintf( pFile, "\n" );
596 }
597 
598 /**Function*************************************************************
599 
600   Synopsis    [Prints the hex unsigned into a file.]
601 
602   Description []
603 
604   SideEffects []
605 
606   SeeAlso     []
607 
608 ***********************************************************************/
Extra_PrintHexadecimalString(char * pString,unsigned Sign[],int nVars)609 void Extra_PrintHexadecimalString( char * pString, unsigned Sign[], int nVars )
610 {
611     int nDigits, Digit, k;
612     if ( nVars == 0 && !(Sign[0] & 1) ) { sprintf(pString, "0"); return; } // const0
613     if ( nVars == 0 &&  (Sign[0] & 1) ) { sprintf(pString, "1"); return; } // const1
614     if ( nVars == 1 &&  (Sign[0] & 1) ) { sprintf(pString, "1"); return; } // inverter
615     if ( nVars == 1 && !(Sign[0] & 1) ) { sprintf(pString, "2"); return; } // buffer
616     // write the number into the file
617     nDigits = (1 << nVars) / 4;
618     for ( k = nDigits - 1; k >= 0; k-- )
619     {
620         Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
621         if ( Digit < 10 )
622             *pString++ = '0' + Digit;
623         else
624             *pString++ = 'a' + Digit-10;
625     }
626 //    fprintf( pFile, "\n" );
627     *pString = 0;
628 }
629 
630 /**Function*************************************************************
631 
632   Synopsis    [Prints the hex unsigned into a file.]
633 
634   Description []
635 
636   SideEffects []
637 
638   SeeAlso     []
639 
640 ***********************************************************************/
Extra_PrintHex(FILE * pFile,unsigned * pTruth,int nVars)641 void Extra_PrintHex( FILE * pFile, unsigned * pTruth, int nVars )
642 {
643     int nMints, nDigits, Digit, k;
644 
645     // write the number into the file
646     fprintf( pFile, "0x" );
647     nMints  = (1 << nVars);
648     nDigits = nMints / 4 + ((nMints % 4) > 0);
649     for ( k = nDigits - 1; k >= 0; k-- )
650     {
651         Digit = ((pTruth[k/8] >> (k * 4)) & 15);
652         if ( Digit < 10 )
653             fprintf( pFile, "%d", Digit );
654         else
655             fprintf( pFile, "%c", 'A' + Digit-10 );
656     }
657 //    fprintf( pFile, "\n" );
658 }
Extra_PrintHex2(FILE * pFile,unsigned * pTruth,int nVars)659 void Extra_PrintHex2( FILE * pFile, unsigned * pTruth, int nVars )
660 {
661     int nMints, nDigits, Digit, k;
662 
663     // write the number into the file
664     //fprintf( pFile, "0x" );
665     nMints  = (1 << nVars);
666     nDigits = nMints / 4 + ((nMints % 4) > 0);
667     for ( k = nDigits - 1; k >= 0; k-- )
668     {
669         Digit = ((pTruth[k/8] >> (k * 4)) & 15);
670         if ( Digit < 10 )
671             fprintf( pFile, "%d", Digit );
672         else
673             fprintf( pFile, "%c", 'A' + Digit-10 );
674     }
675 //    fprintf( pFile, "\n" );
676 }
Extra_PrintHexReverse(FILE * pFile,unsigned * pTruth,int nVars)677 void Extra_PrintHexReverse( FILE * pFile, unsigned * pTruth, int nVars )
678 {
679     int nMints, nDigits, Digit, k;
680 
681     // write the number into the file
682     fprintf( pFile, "0x" );
683     nMints  = (1 << nVars);
684     nDigits = nMints / 4 + ((nMints % 4) > 0);
685     for ( k = 0; k < nDigits; k++ )
686     {
687         Digit = ((pTruth[k/8] >> (k * 4)) & 15);
688         if ( Digit < 10 )
689             fprintf( pFile, "%d", Digit );
690         else
691             fprintf( pFile, "%c", 'A' + Digit-10 );
692     }
693 //    fprintf( pFile, "\n" );
694 }
695 
696 /**Function*************************************************************
697 
698   Synopsis    [Returns the composite name of the file.]
699 
700   Description []
701 
702   SideEffects []
703 
704   SeeAlso     []
705 
706 ***********************************************************************/
Extra_PrintSymbols(FILE * pFile,char Char,int nTimes,int fPrintNewLine)707 void Extra_PrintSymbols( FILE * pFile, char Char, int nTimes, int fPrintNewLine )
708 {
709     int i;
710     for ( i = 0; i < nTimes; i++ )
711         printf( "%c", Char );
712     if ( fPrintNewLine )
713         printf( "\n" );
714 }
715 
716 /**Function*************************************************************
717 
718   Synopsis    [Appends the string.]
719 
720   Description [Assumes that the given string (pStrGiven) has been allocated
721   before using malloc(). The additional string has not been allocated.
722   Allocs more root, appends the additional part, frees the old given string.]
723 
724   SideEffects []
725 
726   SeeAlso     []
727 
728 ***********************************************************************/
Extra_StringAppend(char * pStrGiven,char * pStrAdd)729 char * Extra_StringAppend( char * pStrGiven, char * pStrAdd )
730 {
731     char * pTemp;
732     if ( pStrGiven )
733     {
734         pTemp = ABC_ALLOC( char, strlen(pStrGiven) + strlen(pStrAdd) + 2 );
735         sprintf( pTemp, "%s%s", pStrGiven, pStrAdd );
736         ABC_FREE( pStrGiven );
737     }
738     else
739         pTemp = Extra_UtilStrsav( pStrAdd );
740     return pTemp;
741 }
742 
743 /**Function*************************************************************
744 
745   Synopsis    [Only keep characters belonging to the second string.]
746 
747   Description []
748 
749   SideEffects []
750 
751   SeeAlso     []
752 
753 ***********************************************************************/
Extra_StringClean(char * pStrGiven,char * pCharKeep)754 void Extra_StringClean( char * pStrGiven, char * pCharKeep )
755 {
756     char * pTemp, * pChar, * pSave = pStrGiven;
757     for ( pTemp = pStrGiven; *pTemp; pTemp++ )
758     {
759         for ( pChar = pCharKeep; *pChar; pChar++ )
760             if ( *pTemp == *pChar )
761                 break;
762         if ( *pChar == 0 )
763             continue;
764         *pSave++ = *pTemp;
765     }
766     *pSave = 0;
767 }
768 
769 /**Function*************************************************************
770 
771   Synopsis    [String comparison procedure.]
772 
773   Description []
774 
775   SideEffects []
776 
777   SeeAlso     []
778 
779 ***********************************************************************/
Extra_StringCompare(const char * pp1,const char * pp2)780 int Extra_StringCompare( const char * pp1, const char * pp2 )
781 {
782     return strcmp(*(char **)pp1, *(char **)pp2);
783 }
784 
785 /**Function*************************************************************
786 
787   Synopsis    [Sorts lines in the file alphabetically.]
788 
789   Description []
790 
791   SideEffects []
792 
793   SeeAlso     []
794 
795 ***********************************************************************/
Extra_FileSort(char * pFileName,char * pFileNameOut)796 void Extra_FileSort( char * pFileName, char * pFileNameOut )
797 {
798     FILE * pFile;
799     char * pContents;
800     char ** pLines;
801     int i, nLines, Begin;
802     pFile = fopen( pFileName, "rb" );
803     if ( pFile == NULL )
804     {
805         printf( "Extra_FileSort(): Cannot open file \"%s\".\n", pFileName );
806         return;
807     }
808     pContents = Extra_FileRead( pFile );
809     fclose( pFile );
810     if ( pContents == NULL )
811     {
812         printf( "Extra_FileSort(): Cannot read contents of file \"%s\".\n", pFileName );
813         return;
814     }
815     // count end of lines
816     for ( nLines = 0, i = 0; pContents[i]; i++ )
817         nLines += (pContents[i] == '\n');
818     // break the file into lines
819     pLines = (char **)malloc( sizeof(char *) * nLines );
820     Begin = 0;
821     for ( nLines = 0, i = 0; pContents[i]; i++ )
822         if ( pContents[i] == '\n' )
823         {
824             pContents[i] = 0;
825             pLines[nLines++] = pContents + Begin;
826             Begin = i + 1;
827         }
828     // sort the lines
829     qsort( pLines, (size_t)nLines, sizeof(char *), (int(*)(const void *,const void *))Extra_StringCompare );
830     // write a new file
831     pFile = fopen( pFileNameOut, "wb" );
832     for ( i = 0; i < nLines; i++ )
833         if ( pLines[i][0] )
834             fprintf( pFile, "%s\n", pLines[i] );
835     fclose( pFile );
836     // cleanup
837     free( pLines );
838     free( pContents );
839     // report the result
840     printf( "The file after sorting is \"%s\".\n", pFileNameOut );
841 }
842 
843 
844 /**Function*************************************************************
845 
846   Synopsis    [Appends line number in the end.]
847 
848   Description []
849 
850   SideEffects []
851 
852   SeeAlso     []
853 
854 ***********************************************************************/
Extra_FileLineNumAdd(char * pFileName,char * pFileNameOut)855 void Extra_FileLineNumAdd( char * pFileName, char * pFileNameOut )
856 {
857     char Buffer[1000];
858     FILE * pFile;
859     FILE * pFile2;
860     int iLine;
861     pFile = fopen( pFileName, "rb" );
862     if ( pFile == NULL )
863     {
864         printf( "Extra_FileLineNumAdd(): Cannot open file \"%s\".\n", pFileName );
865         return;
866     }
867     pFile2 = fopen( pFileNameOut, "wb" );
868     if ( pFile2 == NULL )
869     {
870         fclose( pFile );
871         printf( "Extra_FileLineNumAdd(): Cannot open file \"%s\".\n", pFileNameOut );
872         return;
873     }
874     for ( iLine = 0; fgets( Buffer, 1000, pFile ); iLine++ )
875     {
876         sprintf( Buffer + strlen(Buffer) - 2, "%03d\n%c", iLine, 0 );
877         fputs( Buffer, pFile2 );
878     }
879     fclose( pFile );
880     fclose( pFile2 );
881     // report the result
882     printf( "The resulting file is \"%s\".\n", pFileNameOut );
883 }
884 
885 /**Function*************************************************************
886 
887   Synopsis    []
888 
889   Description []
890 
891   SideEffects []
892 
893   SeeAlso     []
894 
895 ***********************************************************************/
896 /*
897 int main( int argc, char ** argv )
898 {
899     if ( argc == 2 )
900         Extra_FileSort( argv[1], Extra_FileNameAppend(argv[1], "_sorted") );
901     else
902         printf( "%s: Wrong number of command line arguments.\n", argv[0] );
903     return 1;
904 }
905 */
906 
907 /*---------------------------------------------------------------------------*/
908 /* Definition of internal functions                                          */
909 /*---------------------------------------------------------------------------*/
910 
911 /*---------------------------------------------------------------------------*/
912 /* Definition of static Functions                                            */
913 /*---------------------------------------------------------------------------*/
914 
915 
916 ////////////////////////////////////////////////////////////////////////
917 ///                       END OF FILE                                ///
918 ////////////////////////////////////////////////////////////////////////
919 
920 
921 ABC_NAMESPACE_IMPL_END
922 
923