telsrc_main.c
资源名称:dongruanC.rar [点击查看]
上传用户:xqing521
上传日期:2014-11-02
资源大小:1942k
文件大小:19k
源码类别:
嵌入式/单片机编程
开发平台:
C/C++
- /********************************************************************
- 挊嶌尃強桳幰 丗 WangHongjun@neusoft.com
- 僜乕僗僼傽僀儖柤 丗 telsrc_main.c
- 撪梕 丗 揹榖斣崋専嶕儊僀儞IF娭悢掕媊
- 嶌惉擔丒幰 丗 2005.07.08 Wang Hongjun
- 廋惓擔丒幰 丗
- *********************************************************************/
- /*------------------------------------------------------------------*/
- /* */
- /* 僀儞僋儖乕僪僼傽僀儖晹暘 */
- /* */
- /*------------------------------------------------------------------*/
- #include "telsrc.h"
- /*------------------------------------------------------------------*/
- /* */
- /* 娭悢掕媊晹暘 */
- /* */
- /*------------------------------------------------------------------*/
- /********************************************************************/
- /* TelSrc_Main娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Tel No. search main funciton. */
- /* */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_Main()
- {
- int iRet = RET_SUCCESS;
- /* Return value */
- int iDataSource = TELSRC_DATASRC_FILE;
- /* Data Source */
- int iReadRslt = 0;
- /* Result of read from key buffer */
- int iMaxDataNum = 0;/* Number of data for search */
- H_TELSRC hTelSrc = NULL;/* Tel search handle */
- S_TELSRC_COND stTelSrcCond; /* Tel search conditon struct */
- /* Welcome information */
- printf( "n************************************************n" );
- printf( "*This is a Tel No. register and search program!*n" );
- printf( "************************************************n" );
- /* Initialization */
- memset( &stTelSrcCond, 0, (WORD)sizeof(stTelSrcCond) );
- /* Prompt user to input the number of data for searching */
- printf( "nHow many Tel No. can be saved!n" );
- iReadRslt = scanf( "%8d", iMaxDataNum );
- while( ( 0 >= iMaxDataNum ) || ( 0 >= iReadRslt ) )
- {
- if( 0 >= iReadRslt )
- {
- while( 10 != getchar() )
- {
- continue;
- }
- }
- printf( "Invalid number, please input the correct one." );
- printf( "nHow many Tel No. can be saved!n" );
- iReadRslt = scanf( "%8d", &iMaxDataNum );
- }
- /* Key buffer clear */
- while( 10 != getchar() )
- {
- continue;
- }
- /* Max Data num limited (1000) */
- if( TELSRC_MAXDATANUM < iMaxDataNum )
- {
- iMaxDataNum = TELSRC_MAXDATANUM;
- }
- /* Prompt user to select the type of set data */
- printf( "Please select the Type of data saved!n" );
- printf( "(0 : File; 1 : Memory)!n" );
- iReadRslt = scanf( "%8d", &iDataSource );
- while( ( ( TELSRC_DATASRC_FILE != iDataSource ) &&
- ( TELSRC_DATASRC_MEM != iDataSource ) ) ||
- ( 0 >= iReadRslt ) )
- {
- /* In the case that string is input, clear the key buffer */
- if( 0 >= iReadRslt )
- {
- while( 10 != getchar() )
- {
- continue;
- }
- }
- /* Prompt the user to select the correct type */
- printf( "Invalid type, please input the correct one.n" );
- iReadRslt = scanf( "%8d", &iDataSource );
- }
- /* Key buffer clear */
- while( 10 != getchar() )
- {
- continue;
- }
- /* Tel No. search condition set */
- stTelSrcCond.iMaxDataNum = iMaxDataNum;
- stTelSrcCond.iDataSource = iDataSource;
- /* Handle create */
- hTelSrc = TelSrc_CreHandle( &stTelSrcCond );
- if( NULL == hTelSrc )
- {
- printf( "nHandle create failed!!n" );
- return RET_FAILED;
- }
- /* Execute different operation by the type user specified */
- iRet = TelSrc_UserSel( hTelSrc );
- /* Handle free */
- iRet = TelSrc_FreeHandle( hTelSrc );
- return iRet;
- }
- /********************************************************************/
- /* TelSrc_UserSel */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* int : Tel No. search class handle[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Excute the different program by user select. */
- /* */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_UserSel(
- H_TELSRC hTelSrc /* Tel No. search class handle */
- )
- {
- char cQuitConf = 'Y';
- /* Confirm to exit or not */
- char cDelConf = 'N';
- /* Confirm to delete or not */
- int iRet = RET_SUCCESS;
- /* Return value */
- int iGetChrNum = 0;
- /* Number of char from Key Buf */
- int iOperFlg = TELSRC_INIT;
- /* User operation type */
- S_TELSRC* pstTelSrc = NULL;
- /* Telsearch struct */
- S_TELSRC_COND* pstTelSrcCond = NULL;
- /* Tel search conditon struct */
- /* Exception prevention */
- if( NULL == hTelSrc )
- {
- return RET_FAILED;
- }
- /* Data area get */
- pstTelSrc = (S_TELSRC*)hTelSrc;
- pstTelSrcCond = &pstTelSrc->stTelSrcCond;
- /* Execute different operation by the type user specified */
- while( (TELSRC_INIT <= iOperFlg) && (TELSRC_DATADEL >= iOperFlg) )
- {
- iOperFlg = TELSRC_INIT;
- printf( "nPlease select your operation!n" );
- printf( "( 1 : Tel No. Regist.)n" );
- printf( "( 2 : Tel No. Search.)n" );
- printf( "( 3 : Tel No. Delete.)n" );
- printf( "( Other : Exit. )n" );
- /* Read user's selection */
- iGetChrNum = scanf( "%8d", &iOperFlg );
- if( 0 >= iGetChrNum )
- {
- scanf( "%8s", &iOperFlg );
- }
- /* Key buffer clear */
- while( 10 != getchar() )
- {
- continue;
- }
- switch( iOperFlg )
- {
- case TELSRC_DATAREG: /* New data register */
- if( pstTelSrc->iDataNum >= pstTelSrcCond->iMaxDataNum )
- {
- printf( "nIt cannt be registerd any more.n" );
- printf( "Delete part of the data?n" );
- printf( "Y(y): Delete; N(n): Select anothern" );
- scanf( "%1s", &cDelConf );
- if( ( 'Y' == cDelConf ) || ( 'y' == cDelConf ) )
- {
- iRet = TelSrc_DataDel( hTelSrc );
- }
- /* Key buffer clear */
- while( 10 != getchar() )
- {
- continue;
- }
- }
- else
- {
- iRet = TelSrc_DataReg( hTelSrc );
- }
- break;
- case TELSRC_DATASRC: /* Tel No. search */
- iRet = TelSrc_DataSrc( hTelSrc );
- break;
- case TELSRC_DATADEL: /* Delete an item */
- iRet = TelSrc_DataDel( hTelSrc );
- break;
- default: /* Quit */
- printf( "nAre you sure that you want to quit the program?n" );
- printf( "n(Y(y)/N(n))n" );
- scanf( "%1s", &cQuitConf );
- if( ( 'Y' == cQuitConf ) || ( 'y' == cQuitConf ) )
- {
- iOperFlg = TELSRC_QUIT;
- }
- else
- {
- iOperFlg = TELSRC_INIT;
- }
- /* Key buffer clear */
- while( 10 != getchar() )
- {
- continue;
- }
- }
- }
- return iRet;
- }
- /********************************************************************/
- /* TelSrc_CreHandle娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* int : Search condition[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯Telphone search handle */
- /* 乮堎忢乯NULL */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Create telphone search handle. */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- H_TELSRC TelSrc_CreHandle(
- S_TELSRC_COND* pstTelSrcCond
- /* I/ Search condition */
- )
- {
- H_TELSRC hTelSrc = NULL;
- /* Telsearch class handle */
- /* Exception prevention */
- if( NULL == pstTelSrcCond )
- {
- return (H_TELSRC)NULL;
- }
- /* Create TelSrc handle by conditons user specified */
- if( TELSRC_DATASRC_FILE == (pstTelSrcCond->iDataSource) )
- {
- hTelSrc = telsrc_crehandle_file( pstTelSrcCond );
- }
- else
- {
- hTelSrc = telsrc_crehandle_mem( pstTelSrcCond );
- }
- return hTelSrc;
- }
- /********************************************************************/
- /* TelSrc_DataReg娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* H_TELSRC : Tel No. search handle[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Regist new tel no. */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_DataReg(
- H_TELSRC hTelSrc /* I/ Telphone search handle */
- )
- {
- int iRet = RET_SUCCESS;
- /* Return value */
- int iLoopCnt = 0;
- /* Loop counter */
- int iOkFlg = TELSRC_ILLEGAL;
- /* The No. input illegal flag */
- int iTelStrLen = 0;
- /* The lenth of Tel No. input */
- int iReadBuf = 0;
- /* Read form key buffer */
- S_TELSRC* pstTelSrc = NULL;
- /* Tel No. search class data struct */
- S_TELSRC_COND* pstTelSrcCond = NULL;
- /* Tel No. search class condition */
- S_TELSRC_DATA stTelSrcData;
- char acTelNo[TELSRC_NO_MAXLEN];
- char acName[TELSRC_NAME_MAXLEN];
- /* Exception prevention */
- if( NULL == hTelSrc )
- {
- return RET_FAILED;
- }
- /* Initialization */
- memset( &stTelSrcData, 0, (WORD)sizeof(stTelSrcData) );
- /* Tel No. search class data area adress get */
- pstTelSrc = (S_TELSRC*)hTelSrc;
- pstTelSrcCond = &pstTelSrc->stTelSrcCond;
- /* Max number reaches on, exception prevention */
- if( pstTelSrcCond->iMaxDataNum <= pstTelSrc->iDataNum )
- {
- printf( "nIt cannt be registerd any more.n" );
- printf( "nPlease delete part of the data.n" );
- }
- /* Tel No. input prompt */
- printf( "nPlease input the Tel No. you want to register.n" );
- printf( "nNotice: Only the No. 0~9 is legal.n" );
- /*==============================================================*/
- /* Read the Tel No. input by user. */
- /* Notice: */
- /* 1.If the Tel No. input is illeagal, user must input the */
- /* correct one. */
- /* 2.If the Tel No. input is over 11 digit, the No. after */
- /* the 11st digit will be cut autometically. */
- /*==============================================================*/
- while( TELSRC_ILLEGAL == iOkFlg )
- {
- iReadBuf = getchar();
- while( 10 != iReadBuf )
- {
- if( strlen(acTelNo) <= (TELSRC_NO_MAXLEN -1) )
- {
- acTelNo[strlen(acTelNo)] = (char)iReadBuf;
- }
- iReadBuf = getchar();
- }
- /* Get the No. lenth input by user */
- iTelStrLen = strlen( acTelNo );
- for( iLoopCnt = 0; iLoopCnt < iTelStrLen; iLoopCnt ++ )
- {
- /* Illegal, prompt the user to correct it */
- if( (0x30 > acTelNo[iLoopCnt]) || (0x39 < acTelNo[iLoopCnt]) )
- {
- printf( "nIllegal No. was input. Please input the correct one.n" );
- iOkFlg = TELSRC_ILLEGAL;
- memset( acTelNo, 0, TELSRC_NO_MAXLEN );
- break;
- }
- /* Input complete */
- else
- {
- iOkFlg = TELSRC_LEGAL;
- }
- }
- }
- /* Save the new Tel No. input by user */
- strncpy( stTelSrcData.acTelNo, acTelNo,
- ((WORD)sizeof(stTelSrcData.acTelNo) - 1) );
- /* Name input prompt */
- printf( "nPlease input the point name for the Tel No.n" );
- printf( "nNotice: Should be less than 64 characters.n" );
- /*==============================================================*/
- /* Read the Tel No. input by user. */
- /* Notice: */
- /* If the Point Name input is over 64 characters, the */
- /* characters after the 64th will be cut autometically. */
- /*==============================================================*/
- /* Read the Name input by user */
- iReadBuf = getchar();
- while( 10 != iReadBuf )
- {
- if( strlen(acName) <= (TELSRC_NAME_MAXLEN -1) )
- {
- acName[strlen(acName)] = (char)iReadBuf;
- }
- iReadBuf = getchar();
- }
- /* Save the name input by user */
- strncpy( stTelSrcData.acName, acName,
- ((WORD)sizeof(stTelSrcData.acName) - 1) );
- /* New data regist by different data source */
- if( TELSRC_DATASRC_FILE == pstTelSrcCond->iDataSource )
- {
- iRet = telsrc_datareg_file( pstTelSrc, &stTelSrcData );
- }
- else
- {
- iRet = telsrc_datareg_mem( pstTelSrc, &stTelSrcData );
- }
- /* Result print */
- if( RET_SUCCESS == iRet )
- {
- printf( "nNew Tel No. has been registered successfully!n" );
- }
- return iRet;
- }
- /********************************************************************/
- /* TelSrc_DataSrc娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* H_TELSRC : Telphone search handle[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Tel No. search. */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_DataSrc(
- H_TELSRC hTelSrc /* I/ Telphone search handle */
- )
- {
- int iRet = RET_SUCCESS;
- /* Return value */
- S_TELSRC* pstTelSrc = NULL;
- /* Tel No. search class data struct */
- S_TELSRC_COND* pstTelSrcCond = NULL;
- /* Tel No. search class condition */
- char acTelNo[TELSRC_NO_MAXLEN];
- /* Exception prevention */
- if( NULL == hTelSrc )
- {
- return RET_FAILED;
- }
- /* Tel No. search class data area adress get */
- pstTelSrc = (S_TELSRC*)hTelSrc;
- pstTelSrcCond = &pstTelSrc->stTelSrcCond;
- /* Tel No. input prompt */
- printf( "nPlease input the Tel No. you want to search.n" );
- printf( "nNotice: Only the No. 0~9 is legal.n" );
- printf( "nPart match is possiable.n" );
- /*==============================================================*/
- /* Read the Tel No. input by user. */
- /* Notice: */
- /* If the Tel No. input is over 11 digit, the No. after */
- /* the 11st digit will be cut autometically. */
- /*==============================================================*/
- /* Read the Tel No. input by user */
- memset( acTelNo, 0, (WORD)sizeof(acTelNo) );
- scanf( "%11s", acTelNo );
- /* More than 11 digit was input, cut autometically */
- if( 0 != acTelNo[TELSRC_NO_MAXLEN -1] )
- {
- printf( "nYou have input more than 11 digit Tel No.n" );
- printf( "nThe No. after 11 digit will be cut.n" );
- acTelNo[TELSRC_NO_MAXLEN -1] = ' ';
- }
- /* New data regist by different data source */
- if( TELSRC_DATASRC_FILE == pstTelSrcCond->iDataSource )
- {
- iRet = telsrc_datasrc_file( pstTelSrc, acTelNo );
- }
- else
- {
- iRet = telsrc_datasrc_mem( pstTelSrc, acTelNo );
- }
- return iRet;
- }
- /********************************************************************/
- /* TelSrc_DataDel娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* H_TELSRC : Telphone search handle[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Tel No. search. */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_DataDel(
- H_TELSRC hTelSrc /* I/ Telphone search handle */
- )
- {
- int iRet = RET_SUCCESS;
- /* Return value */
- if( NULL == hTelSrc )
- {
- return RET_FAILED;
- }
- /* DUMMY */
- printf( "nDelete funciton is not completed yet!n" );
- return iRet;
- }
- /********************************************************************/
- /* TelSrc_FreeHandle娭悢 */
- /* ----------------------------------------------------------------*/
- /* Input: */
- /* H_TELSRC : Telphone search handle[IN] */
- /* */
- /* ----------------------------------------------------------------*/
- /* Output: */
- /* RETURN : 乮惓忢乯RET_SUCCESS */
- /* 乮堎忢乯RET_FAILED */
- /* */
- /* ----------------------------------------------------------------*/
- /* Outline: */
- /* Tel No. search class handle free. */
- /* ----------------------------------------------------------------*/
- /* Others: */
- /* */
- /********************************************************************/
- int TelSrc_FreeHandle(
- H_TELSRC hTelSrc /* I/ Telphone search handle */
- )
- {
- int iRet = RET_SUCCESS;
- /* Return value */
- S_TELSRC* pstTelSrc = NULL;
- /* Tel No. search class data struct */
- S_TELSRC_COND* pstTelSrcCond = NULL;
- /* Tel No. search class condition */
- /* Exception prevention */
- if( NULL == hTelSrc )
- {
- return RET_FAILED;
- }
- /* Tel No. search class data area adress get */
- pstTelSrc = (S_TELSRC*)hTelSrc;
- pstTelSrcCond = &pstTelSrc->stTelSrcCond;
- /* New data regist by different data source */
- if( TELSRC_DATASRC_FILE == pstTelSrcCond->iDataSource )
- {
- iRet = telsrc_freehandle_file( pstTelSrc );
- }
- else
- {
- iRet = telsrc_freehandle_mem( pstTelSrc );
- }
- /* Pointer NULL */
- if( RET_SUCCESS == iRet )
- {
- pstTelSrc = (S_TELSRC*)NULL;
- hTelSrc = (H_TELSRC)NULL;
- }
- return iRet;
- }
- /* End of file-----------------------------------------------------*/