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

📄 telsrc_filesrc.c

📁 这是东软嵌入式课件
💻 C
字号:
/********************************************************************


	挊嶌尃強桳幰		丗	WangHongjun@neusoft.com

	僜乕僗僼傽僀儖柤	丗	telsrc_filesrc.c

	撪梕				丗	Tel No. search(File search IF)

	嶌惉擔丒幰			丗	2005.07.08		Wang Hongjun


	廋惓擔丒幰			丗


*********************************************************************/

/*------------------------------------------------------------------*/
/*																	*/
/*	僀儞僋儖乕僪僼傽僀儖晹暘										*/
/*																	*/
/*------------------------------------------------------------------*/
#include "telsrc_in.h"

/*------------------------------------------------------------------*/
/*																	*/
/*	娭悢掕媊晹暘													*/
/*																	*/
/*------------------------------------------------------------------*/

/********************************************************************/
/*	telsrc_crehandle_file娭悢(Tel Search handle create)				*/
/*	----------------------------------------------------------------*/
/*	Input:															*/
/*		int					:	Search condition[IN]				*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Output:															*/
/*		RETURN				:	乮惓忢乯Telphone search handle		*/
/*								乮堎忢乯NULL						*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Outline:														*/
/*		Create telphone search handle (Data saved in file).			*/
/*	----------------------------------------------------------------*/
/*	Others:															*/
/*																	*/
/********************************************************************/
H_TELSRC telsrc_crehandle_file(
	S_TELSRC_COND*	pstTelSrcCond	/* I/ Search condition			*/
	)
{
	int				iReadItem		= 0;
					/* Result of read file							*/
	S_TELSRC*		pstTelSrc		= NULL;
					/* Telsearch struct								*/
	H_TELSRC		hTelSrc			= NULL;
					/* Telsearch class handle						*/
	FILE*			pFile			= NULL;
					/* File handle									*/
	S_TELSRC_DATA	stTelSrcData;


	/* Exception prevention											*/
	if( NULL == pstTelSrcCond )
	{
		return (H_TELSRC)NULL;
	}

	/* Initialization												*/
	memset( &stTelSrcData, 0, (WORD)sizeof(stTelSrcData) );

	/* Search class handle area allocation							*/
	pstTelSrc	= (S_TELSRC*)malloc( (WORD)sizeof(S_TELSRC) );
	if( NULL == pstTelSrc )
	{
		return (H_TELSRC)NULL;
	}
	/* Initialization			*/
	memset( pstTelSrc, 0, (WORD)sizeof(S_TELSRC) );

	/* Search condition copy										*/
	memcpy( &pstTelSrc->stTelSrcCond, pstTelSrcCond, 
										(WORD)sizeof(S_TELSRC_COND) );

	/* Tel search data area allocation								*/
	pstTelSrc->pstTelSrcData	
			= (S_TELSRC_DATA*)malloc( (WORD)sizeof(S_TELSRC_DATA) );
	if( NULL == pstTelSrc->pstTelSrcData )
	{
		return (H_TELSRC)NULL;
	}

	/* File teldat.dat open											*/
	pFile = fopen( "teldat.dat", "a+" );

	/* Exception prevention											*/
	if( NULL == pFile )
	{
		return (H_TELSRC)NULL;
	}

	/* Current data number get										*/
	while( 0 == feof( pFile ) )
	{
		/* Read 1 item from file									*/
		iReadItem = fread( 
			&stTelSrcData, (WORD)sizeof(S_TELSRC_DATA), 1, pFile );
		if( iReadItem <= 0 )
		{	
			pstTelSrc->iDataNum++;
		}
	}

	/* Tel No. search handle set									*/
	hTelSrc		= (H_TELSRC)pstTelSrc;

	return hTelSrc;
}

/********************************************************************/
/*	telsrc_datareg_file娭悢(New data regist)						*/
/*	----------------------------------------------------------------*/
/*	Input:															*/
/*		S_TELSRC*			:	Telphone search data area[IN]		*/
/*		S_TELSRC_DATA*		:	The Tel No. Data for searching[IN]	*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Output:															*/
/*		RETURN				:	乮惓忢乯RET_SUCCESS					*/
/*								乮堎忢乯RET_FAILED					*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Outline:														*/
/*		Regist new Tel No.											*/
/*	----------------------------------------------------------------*/
/*	Others:															*/
/*																	*/
/********************************************************************/
int telsrc_datareg_file(
	S_TELSRC*		pstTelSrc,	/* I/ Telphone search data area		*/
	S_TELSRC_DATA*	pstSrcData	/* I/ The Tel No. Data for searching*/
	)
{
	int				iFWrite			= 0;
					/* Data save result								*/
	FILE*			pFile			= NULL;
					/* File handle									*/


	/* Exception prevention											*/
	if( (NULL == pstSrcData) || (NULL == pstTelSrc) )
	{
		return RET_FAILED;
	}

	/* File teldat.dat open											*/
	pFile = fopen( "teldat.dat", "a+" );

	/* Exception prevention											*/
	if( NULL == pFile )
	{
		return RET_FAILED;
	}

	/* Current data ID set											*/
	pstSrcData->iNo	= pstTelSrc->iDataNum;


	/* Write into file teldat.dat									*/
	iFWrite = fwrite( pstSrcData, sizeof(S_TELSRC_DATA), 1, pFile );
	if( 0 < iFWrite )
	{
		(pstTelSrc->iDataNum)++;
	}

	/* File close													*/
	fclose( pFile );

	return RET_SUCCESS;
}

/********************************************************************/
/*	telsrc_datasrc_file												*/
/*	----------------------------------------------------------------*/
/*	Input:															*/
/*		S_TELSRC*			:	Telphone search data area[IN]		*/
/*		char*				:	The Tel No. for searching[IN]		*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Output:															*/
/*		RETURN				:	乮惓忢乯RET_SUCCESS					*/
/*								乮堎忢乯RET_FAILED					*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Outline:														*/
/*		Tel No. search.												*/
/*	----------------------------------------------------------------*/
/*	Others:															*/
/*																	*/
/********************************************************************/
int telsrc_datasrc_file(
	S_TELSRC*		pstTelSrc,	/* I/ Telphone search data area		*/
	char*			pcTelNo		/* I/ The Tel No. for searching		*/
	)
{
	int				iReadItem		= 0;	/* Result of read file	*/
	int				iSrcRslt= TELSRC_RESULT_NG;	/* Search result	*/
	int				iRet	= TELSRC_RESULT_NG;	/* Return value		*/
	int				iCmpCnt	= 0;				/* Times for compare*/
	int				iCurPos	= 0;				/* Current char pos	*/
	FILE*			pFile	= NULL;				/* File handle		*/
	S_TELSRC_DATA	stTelSrcData;				/* Tel No. data info*/


	/* Exception prevention											*/
	if( NULL == pstTelSrc )
	{
		return TELSRC_RESULT_NG;
	}

	/* Initialization												*/
	memset( &stTelSrcData, 0, (WORD)sizeof(stTelSrcData) );

	/* File open													*/
	pFile = fopen( "teldat.dat", "r" );
	if( NULL == pFile )
	{
		printf( "\nFailed to open file teldat.dat.\n" );
		return TELSRC_RESULT_NG;
	}

	/* Searching													*/
	while( 0 == feof( pFile ) )
	{
		/* Read 1 item from file									*/
		iReadItem = 
			fread( &stTelSrcData, sizeof(S_TELSRC_DATA), 1, pFile );
		if( iReadItem <= 0 )
		{	
			/* File close											*/
			fclose( pFile );

			return TELSRC_RESULT_NG;
		}

		/* Compare, output the result								*/
		iCmpCnt = strlen( stTelSrcData.acTelNo ) - strlen(pcTelNo);
		for( iCurPos = 0; iCurPos <= iCmpCnt; iCurPos++ )
		{
			iSrcRslt = strncmp( 
					stTelSrcData.acTelNo, pcTelNo, strlen(pcTelNo) );
			if( TELSRC_RESULT_OK == iSrcRslt )
			{
				if( (0 == iCurPos) && (0 == iCmpCnt) )
				{
					printf( "\nSearch OK(Complete match)!\n" ); 
					printf( "Tel  No: %s\n", stTelSrcData.acTelNo );
					printf( "Address: %s\n", stTelSrcData.acName );
					iRet = iSrcRslt;
				}
				else
				{
					printf( "\nSearch OK(Part match)!\n" );
					printf( "Tel  No: %s\n", stTelSrcData.acTelNo );
					printf( "Address: %s\n", stTelSrcData.acName );
					iRet = iSrcRslt;
				}

				break;
			}
		}
	}

	/* File close													*/
	fclose( pFile );

	/* Search NG, ouput	the result									*/
	if( TELSRC_RESULT_NG == iRet )
	{
		printf( "\nSearch NG! No corresponding Tel No.\n" );
	}

	return iRet;	
}

/********************************************************************/
/*	telsrc_datasrc_file												*/
/*	----------------------------------------------------------------*/
/*	Input:															*/
/*		S_TELSRC*			:	Telphone search data area[IN]		*/
/*		char*				:	The Tel No. for searching[IN]		*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Output:															*/
/*		RETURN				:	RET_SUCCESS							*/
/*																	*/
/*	----------------------------------------------------------------*/
/*	Outline:														*/
/*		Tel No. search class handle free.							*/
/*	----------------------------------------------------------------*/
/*	Others:															*/
/*																	*/
/********************************************************************/
int telsrc_freehandle_file(
	S_TELSRC*		pstTelSrc	/* I/ Telphone search data area		*/
	)
{
	/* Exception prevention											*/
	if( NULL == pstTelSrc )
	{
		return RET_SUCCESS;
	}

	/* Tel No. search class handle area free						*/
	free( pstTelSrc );
	pstTelSrc = NULL;

	return RET_SUCCESS;
}

/*	End of file-----------------------------------------------------*/

⌨️ 快捷键说明

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