⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutlc.c

📁 arm ads1.2 with crack.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
	if( ( !fp ) || ( !line ) ) {
		fprintf( stderr, "[GetLineFromFile] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return 0 ;
	}
	
	counter = 0 ;
	if( isUnsigned ) {
		while(     ( --limit > 0 ) 
				&& ( ( ucha = fgetc( fp ) ) != EOF ) 
				&&   ( ucha != '\n' ) ) {
			( ( unsigned char * )line )[ counter++ ] = ( unsigned char )ucha ;
		}
		( ( unsigned char * )line )[ counter ] = '\0' ;

		if( ( ( unsigned char * )skipThis ) != NULL ) {
			if( strncmp( ( ( char * )skipThis ), ( ( char * )line ), skipLength ) == 0 ) {
				return GetLineFromFile( fp, line, isUnsigned, limit, skipThis, skipLength ) ;
			}
		}
	}
	else {
		while(     ( --limit > 0 ) 
				&& ( ( cha = fgetc( fp ) ) != EOF ) 
				&&   ( cha != '\n' ) ) {
			( ( char * )line )[ counter++ ] = ( char )cha ;
		}
		( ( char * )line )[ counter ] = '\0' ;

		if( ( ( char * )skipThis ) != NULL ) {
			if( strncmp( ( ( char * )skipThis ), ( ( char * )line ), skipLength ) == 0 ) {
				return GetLineFromFile( fp, line, isUnsigned, limit, skipThis, skipLength ) ;
			}
		}
	}
		
	return counter ;
}

/**** GetPathNameForFile ************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * get a directory location for the given file
 *
 * Inputs
 * ------
 *   fName
 *   - the file name that the directory given is to relate
 *   path
 *   - an array of characters that will hold the path given by the user
 *   limit
 *   - the maximum number of characters that can be read into the path which includes
 *     the string termination
 * Outputs
 * -------
 *   path
 *   - the path given by the user for saving the file
 *     if path is empty then path required as current directory
 *     undefined if some error occurrs (NULL returned)
 * Return Values
 * ------ ------
 *     char * - a pointer to the character array for the path name (pointer to path)
 *     NULL   - some error occurred, path is not valid
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
char *GetPathNameForFile( char fName[ ], char path[ ], unsigned int limit, char *readwrite )
{
	if( ( !fName ) || ( !path ) || ( limit == 0 ) ) {
		fprintf( stderr, "[GetPathNameForFile] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
		
	printf( "Path for file '%s' ", fName ) ;
	if( readwrite != NULL ) {
		printf( "for %s ", readwrite ) ;
	}
	printf( "(<return> for no path) : " ) ;
	ReadInString( stdin, fName, limit ) ;
	if( strlen( fName ) + ReadInString( stdin, path, limit ) > LIMIT ) {
		fprintf( stderr, "Path and file name given are too long, aborting.\n\n" ) ;
		return NULL ;
	}
	printf( "\n" ) ;
	
	return path ;
}

/**** OpenFile **********************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * open the file in the given mode using the custom fopen function if defined
 *
 * Inputs
 * ------
 *   filename
 *   - an array of characters that is the file name of the file to open
 *   filemode
 *   - file mode as necessary for fopen
 * Return Values
 * ------ ------
 *     FILE * - a pointer to the opened file with file name filename
 *     NULL   - some error occurred
 *
 * 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
 *
 ************************************************************************************/
static FILE *OpenFile( const char *filename, const char *filemode )
{
	if( &CustomFOpen == NULL ) {
		return fopen( filename, filemode ) ;
	}
	else {
		return CustomFOpen( filename, filemode ) ;
	}
}

/**** OpenFileRead ******************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * open the file for reading in the given mode
 *
 * Inputs
 * ------
 *   fileMode
 *   - "r" or "rb" as defined for reading by fopen
 *   iFName
 *   - an array of characters that is the file name of the file to open
 * Return Values
 * ------ ------
 *     FILE * - a pointer to the opened file with file name iFName
 *     NULL   - some error occurred
 *
 * 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 *OpenFileRead( char *fileMode, char iFName[ ] )
{
	FILE	*ifp ;

	if( ( !fileMode ) || ( !iFName ) ) {
		fprintf( stderr, "[OpenFileRead] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
	
	if( strncmp( fileMode, "r", 1 ) != 0 ) {	/* must begin "r..." if reading */
		fprintf( stderr, "[OpenFileRead] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
	
	if( strncmp( fileMode, "r+", 2 ) == 0 ) {	/* writing mode, not reading */
		fprintf( stderr, "[OpenFileRead] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}

    printf( "Trying to open file '%s'...\n\n", iFName ) ;
    
    if( ( ifp = OpenFile( iFName, fileMode ) ) == NULL ) {
		fprintf( stderr, "Error opening file '%s', aborting\n\n", iFName ) ;
		/* ifp = NULL, so just return this */
    }
    else {
		printf( "File '%s' opened.\n\n", iFName ) ;
	}
    
    return ifp ;
}

/**** OpenFileWrite *****************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * given the file + path name and the mode for writing, try to open the file in the
 * given mode testing it first for existence and possible overwriting
 *
 * 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
 * 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 *OpenFileWrite( char *fileMode, char oFName[ ] )
{
 	FILE			*ofp ;
	unsigned int	fileStatus ;
	
	if( ( !fileMode ) || ( !oFName ) ) {
		fprintf( stderr, "[OpenFileWrite] 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, "[OpenFileWrite] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
	
	ofp = TestFileForOverwrite( fileMode, oFName, &fileStatus ) ;
	/* incorporates test for fileMode validity */
    switch( fileStatus ) {
    	case 0 :
    	case 2 :
    		if( fileStatus == 0 ) {
    			printf( "The file '%s' has been created.\n\n", oFName ) ;
    		}
    		else if( fileStatus == 2 ) {
      			printf( "The file '%s' has been overwritten.\n\n", oFName ) ;
  			}
    		return ofp ;
    	case 1 :
            fprintf( stderr, "The file '%s' cannot be created.\n", oFName ) ;
            fprintf( stderr, "Possible reasons for this include: \n\n" ) ;
            
            fprintf( stderr, "a: the file may exist and be protected or in use.\n" ) ;
            fprintf( stderr, "b. the file name may not be valid for the system.\n" ) ;
            fprintf( stderr, "c. their may not be sufficient space to create the file.\n" ) ;
            fprintf( stderr, "d. you may not have sufficient access privileges.\n\n" ) ;
            
            fprintf( stderr, "Please check and try again.\n\n" ) ;
   			return NULL ;
  		case 3 :
            fprintf( stderr, "No data has been saved.\n" ) ;
            fprintf( stderr, "The file '%s' has not been altered.\n\n", oFName ) ;
            return NULL ;
    	case 4 :
            fprintf( stderr, "Cannot save to the file '%s'.\n", oFName ) ;
            fprintf( stderr, "The file may be write-protected.\n" ) ;
            fprintf( stderr, "Please check and try again.\n\n" ) ;
            return NULL ;
    	default :
    		fprintf( stderr, "Please check and try again.\n\n" ) ;
    		return NULL ;
    }
}

/**** ReadBinaryFromFile ************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * given an open file with the given file name (for error reporting), read the binary
 * data in from the file in the size of data given returning the results in an array that is
 * allocated internally
 *
 * Inputs
 * ------
 *   fp
 *   - a pointer to the open file, as returned from fopen
 *   fileName
 *   - the file name that references the open file (for error reporting)
 *   bytes
 *   - the size of the data in bytes to read
 *	   1 : char data
 *     2 : short data
 *     4 : int data
 *   bigendian
 *   - 0 : the data is returned in little-endian format
 *         if file operation is big-endian, data is converted
 *     1 : the data is returned in big-endian format
 *         if file operation is little-endian data is converted
 *   n
 *   - a pointer to the location to store the number of data items read
 * Outputs
 * -------
 *   n
 *   - the number of data items read from the file
 *     undefined if some error occurred (NULL returned)
 * Return Values
 * ------ ------
 *     void * - an array that holds the data read from the file
 *              array is either signed (isUnsigned == 0) or unsigned (isUnsigned == 1)
 *              and of type char (bytes == 1), short (bytes == 2) or int (bytes == 4)
 *     NULL   - some error occurred
 *
 * Notes
 * -----
 * returned data requires casting to the appropriate type before it can be used
 *
 * if the file is not opened in binary mode, the result is undertermined
 *
 * Memory allocated (not deallocated)
 * ------ ---------  --- -----
 * the returned array of (unsigned) chars/shorts/ints
 * deallocate after using the data
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
void *ReadBinaryFromFile( FILE *fp, char *fileName, unsigned int bytes, unsigned int bigendian, unsigned int *n )
{
	void	*data ;

	if( ( !fp ) || ( !fileName ) || ( !n ) ) {
		fprintf( stderr, "[ReadBinaryFromFile] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
	
	printf( "Reading binary data from file '%s'...\n\n", fileName ) ;
	
	if( fseek( fp, 0, SEEK_END ) != 0 ) {
		fprintf( stderr, "Cannot find the end of the file '%s', aborting.\n\n", fileName ) ;
		return NULL ;
	}
	
	*n = ( unsigned int )ftell( fp ) ;		/* get number of bytes in file */
	rewind( fp ) ;
	*n /= bytes ;
	
	if( ( data = calloc( *n, bytes ) ) == NULL ) {
		fprintf( stderr, "Cannot allocate memory to hold data, aborting.\n\n" ) ;
		return NULL ;
	}
	
	if( ( fread( data, bytes, *n, fp ) ) != *n ) {
		fprintf( stderr, "Error reading data from file '%s', aborting.\n\n", fileName ) ;
		free( data ) ;
		return NULL ;
	}
	
	if( bigendian != BIGENDIAN ) {
		data = SwitchEndian( data, *n, bytes, 1 ) ;
	}
	
	printf( "Binary data read from file '%s'.\n\n", fileName ) ;
	
	return data ;
}

/**** ReadTextFromFile **************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * given an open file with the given file name (for error reporting), read the
 * data in from the file assuming each entry is in hexadecimal format and is
 * separated by white space (such as space, newline or tab),
 * returning the results in an array that is allocated internally
 *
 * Inputs
 * ------
 *   fp
 *   - a pointer to the open file, as returned from fopen
 *   fileName
 *   - the file name that references the open file (for error reporting)
 *   bytes
 *   - the size of the data in bytes to read
 *	   1 : char data
 *     2 : short data
 *     4 : int data
 *   bigendian
 *   - 0 : the data is returned in little-endian format
 *     1 : the data is returned in big-endian format
 *   n
 *   - a pointer to the location to store the number of data items read
 * Outputs
 * -------
 *   n
 *   - the number of data items read from the file
 *     undefined if some error occurred (NULL returned)
 * Return Values
 * ------ ------
 *     void * - an array that holds the data read from the file
 *              array is either signed (isUnsigned == 0) or unsigned (isUnsigned == 1)
 *              and of type char (bytes == 1), short (bytes == 2) or int (bytes == 4)
 *     NULL   - some error occurred
 *
 * Notes
 * -----
 * returned data requires casting to the appropriate type before it can be used
 *
 * Memory allocated (not deallocated)
 * ------ ---------  --- -----
 * the returned array of (unsigned) chars/shorts/ints
 * deallocate after using the data
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
void *ReadTextFromFile( FILE *fp, char *fileName, unsigned int bytes, unsigned int bigendian, unsigned int *n )
{
	void 			*data ;
	void			*oldDataPtr ;
	unsigned int	tint ;
	unsigned int	nread ;

	if( ( !fp ) || ( !fileName ) || ( !n ) ) {
		fprintf( stderr, "[ReadTextFromFile] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
	
	printf( "Reading hexadecimal 'text' data from file '%s'...\n\n", fileName ) ;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -