00001
00021 #include "extra.h"
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00046
00047
00048
00049
00053
00054
00055
00056
00068 char * Extra_FileGetSimilarName( char * pFileNameWrong, char * pS1, char * pS2, char * pS3, char * pS4, char * pS5 )
00069 {
00070 FILE * pFile;
00071 char * pFileNameOther;
00072 char * pFileGen;
00073
00074 if ( pS1 == NULL )
00075 return NULL;
00076
00077
00078 pFileGen = Extra_FileNameGeneric( pFileNameWrong );
00079 pFileNameOther = Extra_FileNameAppend( pFileGen, pS1 );
00080 pFile = fopen( pFileNameOther, "r" );
00081 if ( pFile == NULL && pS2 )
00082 {
00083 pFileNameOther = Extra_FileNameAppend( pFileGen, pS2 );
00084 pFile = fopen( pFileNameOther, "r" );
00085 if ( pFile == NULL && pS3 )
00086 {
00087 pFileNameOther = Extra_FileNameAppend( pFileGen, pS3 );
00088 pFile = fopen( pFileNameOther, "r" );
00089 if ( pFile == NULL && pS4 )
00090 {
00091 pFileNameOther = Extra_FileNameAppend( pFileGen, pS4 );
00092 pFile = fopen( pFileNameOther, "r" );
00093 if ( pFile == NULL && pS5 )
00094 {
00095 pFileNameOther = Extra_FileNameAppend( pFileGen, pS5 );
00096 pFile = fopen( pFileNameOther, "r" );
00097 }
00098 }
00099 }
00100 }
00101 FREE( pFileGen );
00102 if ( pFile )
00103 {
00104 fclose( pFile );
00105 return pFileNameOther;
00106 }
00107
00108 return NULL;
00109 }
00110
00122 char * Extra_FileNameExtension( char * FileName )
00123 {
00124 char * pDot;
00125
00126 for ( pDot = FileName + strlen(FileName)-1; pDot >= FileName; pDot-- )
00127 if ( *pDot == '.' )
00128 return pDot + 1;
00129 return NULL;
00130 }
00131
00143 char * Extra_FileNameAppend( char * pBase, char * pSuffix )
00144 {
00145 static char Buffer[500];
00146 sprintf( Buffer, "%s%s", pBase, pSuffix );
00147 return Buffer;
00148 }
00149
00161 char * Extra_FileNameGeneric( char * FileName )
00162 {
00163 char * pDot;
00164 char * pUnd;
00165 char * pRes;
00166
00167
00168 pRes = Extra_UtilStrsav( FileName );
00169
00170
00171 pUnd = NULL;
00172 pDot = strstr( FileName, "." );
00173 if ( pUnd )
00174 pRes[pUnd - FileName] = 0;
00175 else if ( pDot )
00176 pRes[pDot - FileName] = 0;
00177 return pRes;
00178 }
00179
00191 int Extra_FileSize( char * pFileName )
00192 {
00193 FILE * pFile;
00194 int nFileSize;
00195 pFile = fopen( pFileName, "r" );
00196 if ( pFile == NULL )
00197 {
00198 printf( "Extra_FileSize(): The file is unavailable (absent or open).\n" );
00199 return 0;
00200 }
00201 fseek( pFile, 0, SEEK_END );
00202 nFileSize = ftell( pFile );
00203 fclose( pFile );
00204 return nFileSize;
00205 }
00206
00207
00219 char * Extra_FileRead( FILE * pFile )
00220 {
00221 int nFileSize;
00222 char * pBuffer;
00223
00224 fseek( pFile, 0, SEEK_END );
00225 nFileSize = ftell( pFile );
00226
00227 rewind( pFile );
00228
00229 pBuffer = ALLOC( char, nFileSize + 3 );
00230 fread( pBuffer, nFileSize, 1, pFile );
00231
00232 pBuffer[ nFileSize + 0] = '\n';
00233 pBuffer[ nFileSize + 1] = '\0';
00234 return pBuffer;
00235 }
00236
00248 char * Extra_TimeStamp()
00249 {
00250 static char Buffer[100];
00251 char * TimeStamp;
00252 time_t ltime;
00253
00254 time( <ime );
00255 TimeStamp = asctime( localtime( <ime ) );
00256 TimeStamp[ strlen(TimeStamp) - 1 ] = 0;
00257 strcpy( Buffer, TimeStamp );
00258 return Buffer;
00259 }
00260
00272 unsigned Extra_ReadBinary( char * Buffer )
00273 {
00274 unsigned Result;
00275 int i;
00276
00277 Result = 0;
00278 for ( i = 0; Buffer[i]; i++ )
00279 if ( Buffer[i] == '0' || Buffer[i] == '1' )
00280 Result = Result * 2 + Buffer[i] - '0';
00281 else
00282 {
00283 assert( 0 );
00284 }
00285 return Result;
00286 }
00287
00299 void Extra_PrintBinary( FILE * pFile, unsigned Sign[], int nBits )
00300 {
00301 int Remainder, nWords;
00302 int w, i;
00303
00304 Remainder = (nBits%(sizeof(unsigned)*8));
00305 nWords = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
00306
00307 for ( w = nWords-1; w >= 0; w-- )
00308 for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
00309 fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
00310
00311
00312 }
00313
00325 int Extra_ReadHexadecimal( unsigned Sign[], char * pString, int nVars )
00326 {
00327 int nWords, nDigits, Digit, k, c;
00328 nWords = Extra_TruthWordNum( nVars );
00329 for ( k = 0; k < nWords; k++ )
00330 Sign[k] = 0;
00331
00332 nDigits = (1 << nVars) / 4;
00333 if ( nDigits == 0 )
00334 nDigits = 1;
00335 for ( k = 0; k < nDigits; k++ )
00336 {
00337 c = nDigits-1-k;
00338 if ( pString[c] >= '0' && pString[c] <= '9' )
00339 Digit = pString[c] - '0';
00340 else if ( pString[c] >= 'A' && pString[c] <= 'F' )
00341 Digit = pString[c] - 'A' + 10;
00342 else if ( pString[c] >= 'a' && pString[c] <= 'f' )
00343 Digit = pString[c] - 'a' + 10;
00344 else { assert( 0 ); return 0; }
00345 Sign[k/8] |= ( (Digit & 15) << ((k%8) * 4) );
00346 }
00347 return 1;
00348 }
00349
00361 void Extra_PrintHexadecimal( FILE * pFile, unsigned Sign[], int nVars )
00362 {
00363 int nDigits, Digit, k;
00364
00365 nDigits = (1 << nVars) / 4;
00366 for ( k = nDigits - 1; k >= 0; k-- )
00367 {
00368 Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
00369 if ( Digit < 10 )
00370 fprintf( pFile, "%d", Digit );
00371 else
00372 fprintf( pFile, "%c", 'a' + Digit-10 );
00373 }
00374
00375 }
00376
00388 void Extra_PrintHexadecimalString( char * pString, unsigned Sign[], int nVars )
00389 {
00390 int nDigits, Digit, k;
00391
00392 nDigits = (1 << nVars) / 4;
00393 for ( k = nDigits - 1; k >= 0; k-- )
00394 {
00395 Digit = ((Sign[k/8] >> ((k%8) * 4)) & 15);
00396 if ( Digit < 10 )
00397 *pString++ = '0' + Digit;
00398 else
00399 *pString++ = 'a' + Digit-10;
00400 }
00401
00402 *pString = 0;
00403 }
00404
00416 void Extra_PrintHex( FILE * pFile, unsigned uTruth, int nVars )
00417 {
00418 int nMints, nDigits, Digit, k;
00419
00420
00421 fprintf( pFile, "0x" );
00422 nMints = (1 << nVars);
00423 nDigits = nMints / 4;
00424 for ( k = nDigits - 1; k >= 0; k-- )
00425 {
00426 Digit = ((uTruth >> (k * 4)) & 15);
00427 if ( Digit < 10 )
00428 fprintf( pFile, "%d", Digit );
00429 else
00430 fprintf( pFile, "%c", 'a' + Digit-10 );
00431 }
00432
00433 }
00434
00446 void Extra_PrintSymbols( FILE * pFile, char Char, int nTimes, int fPrintNewLine )
00447 {
00448 int i;
00449 for ( i = 0; i < nTimes; i++ )
00450 printf( "%c", Char );
00451 if ( fPrintNewLine )
00452 printf( "\n" );
00453 }
00454
00468 char * Extra_StringAppend( char * pStrGiven, char * pStrAdd )
00469 {
00470 char * pTemp;
00471 if ( pStrGiven )
00472 {
00473 pTemp = ALLOC( char, strlen(pStrGiven) + strlen(pStrAdd) + 2 );
00474 sprintf( pTemp, "%s%s", pStrGiven, pStrAdd );
00475 free( pStrGiven );
00476 }
00477 else
00478 pTemp = Extra_UtilStrsav( pStrAdd );
00479 return pTemp;
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00494
00495