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

📄 fileutlc.c

📁 arm ads1.2 with crack.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * Utility Library: File Utility
 * Copyright (C) ARM Limited 1998-1999. All rights reserved.
 */

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "fileutlc.h"

#include "bitutilc.h"
#include "custredc.h"
#include "definesc.h"
#include "optionsc.h"

/*
define weak function prototypes

if a CustomFOpen function does not exist, &CustomFOpen, is NULL
else if a CustomFOpen function exists the pointer points to it

likewise for CustomFreOpen
*/
#if	__ARMCC_VERSION >= 5.00
#define	___weak	__weak
#endif	/* __ARMCC_VERSION >= 5.00 */
extern ___weak FILE *CustomFOpen( const char *filename, const char *filemode ) ;
extern ___weak FILE *CustomFreOpen( const char *filename, const char *filemode, FILE *stream ) ;

static FILE *FreOpenFile( const char *filename, const char *filemode, FILE *stream ) ;

/**** ChangeStdIO *******************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * procedure to redirect stdin, stdout or stderr to files from command line arguments
 * at run time
 *
 * Inputs
 * ------
 *   argc
 *	 - pointer to number of command line arguments
 *	 argv
 *   - pointer to the command line arguments
 *     next argument after first argument referenced must be redirect option if given
 *	 newStdIO
 *   - new file name to open for redirection
 *   oldStdIO
 *	 - pointer to FILE descriptor
 *     stdin, stdout or stderr only
 * Outputs
 * -------
 *   argc
 *   - the number of arguments remaining after argument for redirection is removed
 *   argv
 *   - the input arguments referecing the last argument read
 * Return Values
 * ------ ------
 *     0     : stdin has been redirected
 *     1     : stdout has been redirected
 *     2     : stderr has been redirected
 *     -1    : stdio was not redirected which may or may not be an error
 *     other : stdio redirection was not required
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
int ChangeStdIO( int *argc, char ***argv, char *newStdIO, FILE *oldStdIO )
{
	unsigned int	change = 0 ;
	char			fileMode[ 3 ] = "a+" ;
	int				stdio = 666 ;
	
	if( ( !argc ) || ( !argv ) || ( !( *argv ) ) || ( !newStdIO ) ||
		( !oldStdIO ) || ( ( oldStdIO != stdin ) && ( oldStdIO != stdout ) && ( oldStdIO != stderr ) )
		) {
		fprintf( stderr, "[ChangeStdIO] Error in input arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return -1 ;
	}
	
	/* first argument is the binary and location being run */
	if( --( *argc ) > 0 ) {
		if( sscanf( *( ++( *argv ) ), "%d", &change ) == EOF ) {
			( *argc )++ ;
			( *argv )++ ;
			return -1 ;
		}
	}
	else {
		( *argc )++ ;
		return stdio ;
	}
	
	if( change == 666 ) {
		if( oldStdIO == stdin ) {
			stdio = STDINID ;
			strcpy( fileMode, "r" ) ;
		}
		if( ( FreOpenFile( newStdIO, fileMode, oldStdIO ) ) == NULL ) {
			fprintf( stderr, "Could not open '%s', aborting.\n\n", newStdIO ) ;
			exit( 1 ) ;	/* quitting because nasty failure */
		}
		if( ( oldStdIO == stdout ) || ( oldStdIO == stderr ) ) {
			if( oldStdIO == stdout ) {
				stdio = STDOUTID ;
			}
			else {
				stdio = STDERRID ;
			}
			setbuf( oldStdIO, NULL ) ;
		}
	}
	else {
		( *argc ) += 1 ;
		( *argv ) += 1 ;
	}
	
	return stdio ;
}

/**** FreOpenFile *******************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * freopen's the file in the given mode using the custom freopen 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 freopen
 *   stream
 *   - the stream to associate with the file
 * Return Values
 * ------ ------
 *     FILE * - the stream associated with the opened file
 *     NULL   - some error occurred
 *
 * Notes
 * -----
 * if the stream is non-NULL, the stream should be closed after use
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
static FILE *FreOpenFile( const char *filename, const char *filemode, FILE *stream )
{
	if( &CustomFreOpen == NULL ) {
		return freopen( filename, filemode, stream ) ;
	}
	else {
		return CustomFreOpen( filename, filemode, stream ) ;
	}
}

/**** FreOpenFileRead ***************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * freopen's the file for reading in the given mode
 *
 * Inputs
 * ------
 *   fileMode
 *   - "r" or "rb" as defined for reading by freopen
 *   iFName
 *   - an array of characters that is the file name of the file to open
 *   stream
 *   - the stream to associate with the open file
 * Return Values
 * ------ ------
 *     FILE * - the stream associated with the opened file with file name iFName
 *     NULL   - some error occurred
 *
 * Notes
 * -----
 * if the stream is non-NULL, the file stream be closed after use
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
FILE *FreOpenFileRead( char *fileMode, char iFName[ ], FILE *stream )
{
	FILE	*openstream ;

	if( ( !fileMode ) || ( !iFName ) || ( !stream ) ) {
		fprintf( stderr, "[FreOpenFileRead] 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, "[FreOpenFileRead] 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, "[FreOpenFileRead] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}

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

/**** GetData ***********************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * get the file name from the user and open the file for reading in either binary or
 * text mode by user choice, read in the data of the required type, close the file 
 * and return the data read in
 *
 * Inputs
 * ------
 *   bytes
 *   - the number of bytes in the data that is to be read
 *     BYTEBYTES = char data
 *     HWORDBYTES = short data
 *     WORDBYTES = int data
 *     anything else is undefined and returns NULL
 *   isUnsigned
 *   - 1 (true) the data to be read is unsigned and returned unsigned
 *     0 (false) the data to be read is signed and returned signed
 *   dataType
 *   - an optional string that defines the data that is to read in
 *     displayed to the user during file selection time
 *     pass NULL if non-specific data is to be read
 *	 n
 *   - a pointer to an integer location to store the number of data points read
 * Outputs
 * -------
 *   n
 *   - the number of data points in the returned array
 *     undefined if NULL returned
 * Return Values
 * ------ ------
 *     (unsigned) char *  - an array of character data read from the file
 *     (unsigned) short * - an array of short data read from the file
 *     (unsigned) int *   - an array of integer data read from the file
 *     NULL    			  - some error occurred, size is not valid
 *
 * Notes
 * -----
 * the array returned is returned as void * and will need casting to appropriate type
 *
 * Memory allocated (not deallocated)
 * ------ ---------  --- -----
 * the array returned
 * deallocate after use
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
#define	ASKONLYONCEFORFILENAME		1

void *GetData( unsigned int bytes, char *dataType, unsigned int *n )
{
	char			fileMode[ 3 ] = "r" ;
	FILE			*fp ;
	char			fileName[ LIMIT ] ;
	void			*data = NULL ;
	unsigned int	binary ;
	unsigned int	bigendian ;
	
#if	!ASKONLYONCEFORFILENAME
	binary = 0 ;
	
	do {

	if( binary ) {
		fileMode[ 1 ] = '\0' ;					/* remove any previous binary addition */
	}
#endif
	
	printf( "Please give the name of the file to read " ) ;
	if( dataType != NULL ) {
		printf( "%s ", dataType ) ;
	}
	printf( "data from.\n\n" ) ;
	
	GetFileName( fileName, LIMIT, "reading" ) ;
	
	binary = YesNo( "Data in Binary mode", "binary", "hexadecimal text" ) ;
	if( binary ) {
		strcat( fileMode, "b" ) ;
	}
			
#if	!ASKONLYONCEFORFILENAME
	/* loop until valid file name given or... */
	} while( ( fp = OpenFileRead( fileMode, fileName ) ) == NULL ) ;
#else
	/* ...bail if if no file opened */
	if( ( fp = OpenFileRead( fileMode, fileName ) ) == NULL ) {
		return NULL ;
	}
#endif
	
	if( BIGENDIAN && binary ) {
		bigendian = !YesNo( "Data will be read in Big-Endian mode.  Convert to Little-Endian", "little-endian", "big-endian" ) ;
	}
	else {
		bigendian = YesNo( "Data will be read in Little-Endian mode.  Convert to Big-Endian", "big-endian", "little-endian" ) ;
	}
	
	if( binary ) {
		data = ReadBinaryFromFile( fp, fileName, bytes, bigendian, n ) ;
	}
	else {
		data = ReadTextFromFile( fp, fileName, bytes, bigendian, n ) ;
	}
	
	fclose( fp ) ;
	
	return data ;
}

/**** GetFileName *******************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * get a file name from the user
 *
 * Inputs
 * ------
 *   fName
 *   - an array of characters to hold the file name given by the user
 *   limit
 *   - the maximum number of characters that can be read into the filename which includes
 *     the string termination
 * Outputs
 * -------
 *   iFName
 *   - the file name given by the user
 *     undefined if some error occurrs (NULL returned)
 * Return Values
 * ------ ------
 *     char * - a pointer to the character array for the file name (pointer to fName)
 *     NULL   - some error occurred, fName is not valid
 *
 * History (with dates)
 * -------  ---- -----
 * 1.0.0, 30/06/1998    first release
 *
 ************************************************************************************/
char *GetFileName( char fName[ ], unsigned int limit, char *readwrite )
{
	if( ( !fName ) || ( limit == 0 ) ) {
		fprintf( stderr, "[GetFileName] Error in arguments, aborting.\n\n" ) ;
		/* function name given since intended as internal error for programmer */
		return NULL ;
	}
		
	printf( "Name of the file to open " ) ;
	if( readwrite != NULL ) {
		printf( "for %s ", readwrite ) ;
	}
	printf( ": " ) ;
	ReadInString( stdin, fName, limit ) ;
	/* ignore size of file name returned */
	printf( "\n" ) ;
	
	return fName ;
}

/**** GetLineFromFile ***************************************************************
 *
 * Version & Date
 * -------   ----
 * 1.0.0, 30/06/1998
 *
 * Description
 * -----------
 * get the next line from the given file, upto the the maximum number of given characters
 * and skip any blank lines or any special lines that begin with sequence given
 *
 * Inputs
 * ------
 *   fp
 *   - pointer to the file to read the line from
 *     stdin = keyboard
 *   line
 *   - an array of characters to hold the line from the file
 *	 isUnsigned
 *   - 0 : the data should be read in signed
 *     1 : the data should be read in unsigned
 *   limit
 *   - the maximum number of characters that can be read into line which includes
 *     the string termination
 *   skipThis
 *   - a string for testing that if appears at the beginning of a line, the line is
 *     skipped over
 *     pass NULL if all lines of the file should be retrieved
 *   skipLength
 *   - the length of skipThis to compare with the retrieved line [not necessarily 
 *     strlen( skipThis )] or 0 if not lines to skip
 * Outputs
 * -------
 *   line
 *   - the line read from the given file
 *     undefined if some error occurrs (negative value returned)
 * Return Values
 * ------ ------
 *     unsigned int - the length of the line retrieved
 *     0  			- some error occurred, line may not be valid
 *
 * 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
 *
 ************************************************************************************/
unsigned int GetLineFromFile( FILE *fp, void *line, unsigned int isUnsigned, unsigned int limit, void *skipThis, unsigned int skipLength )
{
	unsigned int	counter ;
	int				cha ;
	unsigned int	ucha ;
	

⌨️ 快捷键说明

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