📄 fileutlc.c
字号:
*n = 0 ;
data = NULL ;
oldDataPtr = data ;
while( ( nread = fscanf( fp, "%x", &tint ) ) != EOF ) {
*n += 1 ;
if( nread != 1 ) {
fprintf( stderr, "Failed to read %d data value from file '%s', aborting.\n\n", *n, fileName ) ;
if( oldDataPtr != NULL ) {
free( oldDataPtr ) ;
}
*n = 0 ;
return NULL ;
}
data = realloc( oldDataPtr, *n*bytes ) ;
if( data == NULL ) {
fprintf( stderr, "Cannot allocate memory for data, aborting.\n\n" ) ;
if( oldDataPtr != NULL ) {
free( oldDataPtr ) ;
}
*n = 0 ;
return NULL ;
}
oldDataPtr = data ;
switch( bytes ) {
case BYTEBYTES :
( ( unsigned char * )data )[ *n - 1 ] = ( unsigned char )tint ;
break ;
case HWORDBYTES :
( ( unsigned short * )data )[ *n - 1 ] = ( unsigned short )tint ;
break ;
case WORDBYTES :
( ( unsigned int * )data )[ *n - 1 ] = tint ;
break ;
}
}
if( *n == 0 ) {
fprintf( stderr, "No data in file '%s', aborting.\n\n", fileName ) ;
free( data ) ;
return NULL ;
}
if( bigendian ) {
data = SwitchEndian( data, *n, bytes, 1 ) ;
}
printf( "Hexadecimal data read from file '%s'.\n\n", fileName ) ;
return data ;
}
/**** ResetStdIO ********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* procedure to redirect stdin, stdout or stderr to their standard locations
*
* Inputs
* ------
* stdIO
* - pointer to FILE descriptor
* stdin, stdout or stderr only
* Return Values
* ------ ------
* 0 : stdio successfully redirected to default location
* non-zero : some error occurred (stdio may or may not be redirected)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
int ResetStdIO( FILE *stdIO )
{
char tmpname[ LIMIT ] ;
if( stdIO == stdin ) {
strcpy( tmpname, ":tt" ) ;
}
else {
strcpy( tmpname, tmpnam( NULL ) ) ;
}
freopen( tmpname, "r", stdIO ) ;
return remove( tmpname ) ;
}
/**** SaveData **********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* get the file name from the user and open the file for writing in either binary or
* text mode (given the user), write the data of the required type and close the file
*
* Inputs
* ------
* data
* - an array of data to write to the file cast to void *
* n
* - the number of data points in the array passed
* bytes
* - the number of bytes in the data that is to be written
* 1 = char data
* 2 = short data
* 4 = int data
* isUnsigned
* - 0 : the data to be written is signed
* 1 : the data to be written is unsigned
* dataType
* - an optional string that defines the data that is to written out
* displayed to the user during file selection time
* pass NULL if non-specific data is to be written
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
void SaveData( void *data, unsigned int n, unsigned int bytes, unsigned int isUnsigned, char *dataType )
{
char fileMode[ 3 ] = "w" ;
FILE *fp ;
char fileName[ LIMIT ] ;
unsigned int binary ;
unsigned int bigendian ;
binary = 0 ;
do {
if( binary ) {
fileMode[ 1 ] = '\0' ; /* remove any previous binary addition */
}
printf( "Please give the name of the file to write " ) ;
if( dataType != NULL ) {
printf( "%s ", dataType ) ;
}
printf( "data to.\n\n" ) ;
GetFileName( fileName, LIMIT, "writing" ) ;
binary = YesNo( "Data in Binary mode", "binary", "hexadecimal text" ) ;
if( binary ) {
strcat( fileMode, "b" ) ;
}
} while( ( fp = OpenFileWrite( fileMode, fileName ) ) == NULL ) ;
if( BIGENDIAN && binary ) {
bigendian = !YesNo( "Data will be written in Big-Endian mode. Write data in Little-Endian mode", "little-endian", "big-endian" ) ;
}
else {
bigendian = YesNo( "Data will be written in Little-Endian mode. Write data in Big-Endian mode", "big-endian", "little-endian" ) ;
}
if( binary ) {
WriteBinaryToFile( fp, fileName, data, n, bytes, bigendian ) ;
}
else {
WriteTextToFile( fp, fileName, data, n, bytes, isUnsigned, bigendian ) ;
}
fclose( fp ) ;
}
/**** TestFileForOverwrite **********************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given the file + path name and the mode for writing, testing the file for existence
* and possible overwriting and open for writing if possible
*
* if the file exists, the user is given a choice of overwriting the file or leaving it
* unmodified in which case (NULL returned) the calling routine should try to save
* the file again giving the user another chance to specify a file name
*
* Inputs
* ------
* fileMode
* - "w", "wb", "a", "ab", "r+", "r+b", "w+", "w+b", "a+", "a+b"
* as defined for writing by fopen
* oFName
* - the file + path name in which to open the file
* fileStatus
* - a pointer to a location to store the file's status
* Outputs
* -------
* fileStatus
* - 0. file created for writing
* 1. file could not be created for writing (permissions?)
* 2. file already exists, overwriting contents
* 3. file already exists, not overwriting contents
* 4. file already exists, permission to overwrite denied
* Return Values
* ------ ------
* FILE * - a pointer to the opened file with file + path name oFName
* NULL - some error occurred (e.g. user may have declined overwrite)
*
* Notes
* -----
* if the file pointer is non-NULL, the file should be closed after use
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
FILE *TestFileForOverwrite( char *fileMode, char oFName[ ], unsigned int *fileStatus )
{
char readMode[ 3 ] = "r" ;
unsigned int fileModeLength ;
FILE *ofp ;
char continued[ LIMIT ] ;
if( ( !fileMode ) || ( !fileStatus ) || ( !oFName ) ) {
fprintf( stderr, "[TestFileForOverwrite] Error in arguments, aborting.\n\n" ) ;
/* function name given since intended as internal error for programmer */
return NULL ;
}
if( ( strcmp( fileMode, "r" ) == 0 ) || ( strcmp( fileMode, "rb" ) == 0 ) ) {
/* only two reading modes, all others are writing modes */
fprintf( stderr, "[TestFileForOverwrite] Error in arguments, aborting.\n\n" ) ;
/* function name given since intended as internal error for programmer */
return NULL ;
}
if( ( ( fileModeLength = strlen( fileMode ) ) > 1 ) && ( fileMode[ 1 ] == 'b' ) ) {
strcpy( readMode, "rb" ) ;
}
else if( ( fileModeLength > 2 ) && ( fileMode[ 2 ] == 'b' ) ) {
strcpy( readMode, "rb" ) ;
}
if( ( ofp = OpenFile( oFName, readMode ) ) == NULL ) {
/* this tests if the file already exists but does not alter it */
/* the test fails if it doesn't exist or the file is read protected */
if( ( ofp = OpenFile( oFName, fileMode ) ) == NULL ) {
/* file may exist but be read & write protected */
*fileStatus = 1 ;
return NULL ;
}
else { /* file created and opened */
*fileStatus = 0 ;
return ofp ;
}
}
else { /* file exists, overwrite it? */
fclose( ofp ) ;
printf( "The file '%s' already exists.\n", oFName ) ;
printf( "To save to this file will overwrite its contents.\n" ) ;
printf( "Do you wish to continue to save to this file?\n\n" ) ;
printf( "Please enter your choice (y or n) : " ) ;
ReadInString( stdin, continued, LIMIT ) ;
printf( "\n" ) ;
switch( tolower( continued[ 0 ] ) ) {
case 'y' :
if( ( ofp = OpenFile( oFName, fileMode ) ) == NULL ) {
/* tries to open the output file (which exists) to write to */
*fileStatus = 4 ;
return NULL ;
/* the file is write protected by some means so cannot save */
}
else { /* opened file for saving */
*fileStatus = 2 ;
return ofp ;
}
case 'n' :
default :
*fileStatus = 3 ;
return NULL ;
}
}
}
/**** WriteBinaryToFile *************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given an open file with the given file name (for error reporting), write the given
* data in binary mode to the file
*
* Inputs
* ------
* fp
* - a pointer to the open file, as returned from fopen
* fileName
* - the file name that references the open file (for error reporting)
* data
* - the array of data to write in binary mode to the file
* n
* - the number of data items to write
* bytes
* - the number of bytes in the data that is to be written
* 1 = char data
* 2 = short data
* 4 = int data
* bigendian
* - 0 : the data must be written in little-endian format
* if file operation is big-endian, data is converted
* 1 : the data must be written in big-endian format
* if file operation is little-endian data is converted
*
* Notes
* -----
* if the file is not opened in binary mode, the result is undertermined
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
void WriteBinaryToFile( FILE *fp, char *fileName, void *data, unsigned int n, unsigned int bytes, unsigned int bigendian )
{
void *writeData ;
if( ( !fp ) || ( !fileName ) || ( !data ) || ( n == 0 ) ) {
fprintf( stderr, "[WriteBinaryToFile] Error in arguments, aborting.\n\n" ) ;
/* function name given since intended as internal error for programmer */
return ;
}
printf( "Writing binary data to file '%s'...\n\n", fileName ) ;
if( bigendian != BIGENDIAN ) {
writeData = SwitchEndian( data, n, bytes, 0 ) ;
}
else {
writeData = data ;
}
if( ( fwrite( writeData, bytes, n, fp ) ) != n ) {
fprintf( stderr, "Error writing data to file, file may be corrupted.\n\n" ) ;
}
if( bigendian != BIGENDIAN ) {
free( writeData ) ;
}
printf( "Binary data written to file '%s'.\n\n", fileName ) ;
}
/**** WriteTextToFile ***************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given an open file with the given file name (for error reporting), write the given
* data in hexadecimal text mode to the file
*
* Inputs
* ------
* fp
* - a pointer to the open file, as returned from fopen
* fileName
* - the file name that references the open file (for error reporting)
* data
* - the array of data to write in hexadecimal text mode to the file
* n
* - the number of data items to write
* bytes
* - the number of bytes in the data that is to be written
* 1 = char data
* 2 = short data
* 4 = int data
* isUnsigned
* - 0 : the data to be written is signed
* 1 : the data to be written is unsigned
* bigendian
* - 0 : the data must be written in little-endian format
* 1 : the data must be written in big-endian format
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
void WriteTextToFile( FILE *fp, char *fileName, void *data, unsigned int n, unsigned int bytes, unsigned int isUnsigned, unsigned int bigendian )
{
void *writeData ;
unsigned int i ;
if( ( !fp ) || ( !fileName ) || ( !data ) || ( n == 0 ) ) {
fprintf( stderr, "[WriteTextToFile] Error in arguments, aborting.\n\n" ) ;
return ;
}
printf( "Writing hexadecimal 'text' data to file '%s'...\n\n", fileName ) ;
if( bigendian ) {
writeData = SwitchEndian( data, n, bytes, 0 ) ;
}
else {
writeData = data ;
}
for( i = 0 ; i < n ; i += 1 ) {
switch( bytes ) {
case BYTEBYTES :
if( isUnsigned ) {
fprintf( fp, "0x%.8x\n", ( ( unsigned char * )writeData )[ i ] ) ;
}
else {
fprintf( fp, "0x%.8x\n", ( ( char * )writeData )[ i ] ) ;
}
break ;
case HWORDBYTES :
if( isUnsigned ) {
fprintf( fp, "0x%.8x\n", ( ( unsigned short * )writeData )[ i ] ) ;
}
else {
fprintf( fp, "0x%.8x\n", ( ( short * )writeData )[ i ] ) ;
}
break ;
case WORDBYTES :
if( isUnsigned ) {
fprintf( fp, "0x%.8x\n", ( ( unsigned int * )writeData )[ i ] ) ;
}
else {
fprintf( fp, "0x%.8x\n", ( ( int * )writeData )[ i ] ) ;
}
break ;
default :
fprintf( stderr, "[WriteTextToFile] Error in arguments, aborting.\n\n" ) ;
/* function name given since intended as internal error for programmer */
if( bigendian != BIGENDIAN ) {
free( writeData ) ;
}
return ;
}
}
if( bigendian ) {
free( writeData ) ;
}
printf( "Hexadecimal data written to file '%s'.\n\n", fileName ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -