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

📄 aidfunc.cpp

📁 socket编程
💻 CPP
字号:
#include "stdafx.h"
#include <tchar.h>

#include "AidFunc.h"

/*-----------------------------------------------------------------*/
// Function: 
//		Convert HZ code between GB and BIG5 
//
// Parameter:
//		LPCSTR  pSrc  - In  -- The source string(ANSI string) ;
//		LPTSTR* pDes  - Out -- The destination string after transformation ;
//		int     nMark - In  -- The method of converting
//							  nMark=GB_2_BIG5 : GB   --> BIG5 ;
//							  nMark=BIG5_2_GB : BIG5 --> GB   ;
// Procedure :
//		if the version is not UNICODE , 
//			(1) at first , changing the source string into unicode string ;
//			(2) call LCMapStringW to change the HZ-code of the unicode string ;
//			(3) changing the converted string into ansi string ;
//		otherwise ,
//			only do the 2nd processing ;
//
// Notice :
//		GB's   Code Page  : 936 ;
//		BIG5's Code Page  : 950 ;
/*-----------------------------------------------------------------*/

void Covert_HZCode(LPCTSTR pSrc,LPTSTR *pDes,int nMark)
{
	int nRet			;
	int nCodePage_src   ;
	int nCodePage_dest  ;
	int nLanguage_src   ;
	int nLanguage_dest  ;

	// the code changing : GB --> BIG5 
	if(nMark==GB_2_BIG5)
	{
		nCodePage_src   = 936 ;	// Source code : GB
		nCodePage_dest	= 950 ;	// Destination code : BIG5

		nLanguage_src	= LCMAP_SIMPLIFIED_CHINESE  ; // Source string : Simplified Chinese(GB)
		nLanguage_dest	= LCMAP_TRADITIONAL_CHINESE ; // Destination string : Traditional Chinese(BIG5)
	}
	// the code changing : BIG5 --> GB
	else
	{
		nCodePage_src   = 950 ;	// Source code : BIG5
		nCodePage_dest	= 936 ;	// Destination code : GB 

		nLanguage_src	= LCMAP_TRADITIONAL_CHINESE ; // Source string : Traditional Chinese(BIG5)
		nLanguage_dest	= LCMAP_SIMPLIFIED_CHINESE  ; // Destination string : Simplified Chinese(GB)
	}

#ifdef _UNICODE	

	int nLength=_tcslen(pSrc);
	if( nLength == 0) return;

	(*pDes)=new WCHAR[nLength+1];
	ZeroMemory( *pDes , (nLength+1) * sizeof(WCHAR) ) ;

	//Convert the unicode string by changing the page-code
	nRet = LCMapStringW(
					MAKELCID(MAKELANGID(LANG_CHINESE,nLanguage_src) , SORT_DEFAULT ) , 
			        nLanguage_dest,
					pSrc	,
					nLength	,
					(*pDes)	,
					nLength ) ;

	if ( nRet == 0 )
	{
		AfxMessageBox(L"error");
		return ;
	}

#else
		
	WCHAR *pTmpIn,*pTmpOut;//temp variable

	//Convert ANSI string into UNICODE without page-code changing and saved into pTmpIn
	Ansi2Unicode(pSrc,&pTmpIn,nCodePage_src); 

	int nLength = wcslen(pTmpIn);

	if( nLength == 0) return;

	pTmpOut = new WCHAR[nLength+1];
	ZeroMemory( pTmpOut , (nLength+1) * sizeof(WCHAR) ) ;

	//Convert the unicode string by changing the page-code
	nRet = LCMapStringW ( 
					MAKELCID( MAKELANGID(LANG_CHINESE,nLanguage_src) , SORT_DEFAULT ) ,
					nLanguage_dest,
					pTmpIn,
					nLength,
					pTmpOut,
					nLength) ;

	if ( nRet == 0 )
		AfxMessageBox(_T("error"));
	
	// convert UNICODE string to ANSI string by changing page code 
	Unicode2Ansi( pTmpOut , pDes , nCodePage_dest )  ;

	delete[] pTmpOut;
	delete[] pTmpIn ;

	pTmpOut = NULL  ;
	pTmpIn  = NULL  ;
		
#endif

}

/*-----------------------------------------------------------------*/
// Function: 
//		Convert the string from ANSI to UNICODE
//
// Parameter:
//		LPCSTR  pSrc  - In  -- The source string(ANSI string) ;
//		WCHAR** pUni  - Out -- The destination string(UNICODE) after transformation ;
//		int     nCodePage - In  -- The code page ;
//
// Notice :
//		GB's   Code Page  : 936 ;
//		BIG5's Code Page  : 950 ;
/*-----------------------------------------------------------------*/
int Ansi2Unicode(const char *pSrc,WCHAR **pUni, int nCodePage)
{
	DWORD nWideLen = 0;
	DWORD nByteLen=_tcslen(pSrc);

	if( nByteLen == 0) return 0 ;

	//get the number of UNICODE string
	nWideLen=MultiByteToWideChar(nCodePage,MB_PRECOMPOSED,pSrc,nByteLen,0,0);
	(*pUni)=new WCHAR[nWideLen+1];
	ZeroMemory( *pUni , (nWideLen+1) * sizeof(WCHAR) ) ;

	//Converted into UNICODE
	if(MultiByteToWideChar(nCodePage,MB_PRECOMPOSED,pSrc,nByteLen,(*pUni),nWideLen)==0)
		AfxMessageBox(_T("Error1"));
	
	return nWideLen;
}


/*-----------------------------------------------------------------*/
// Function: 
//		Convert the string from UNICODE to ANSI
//
// Parameter:
//		WCHAR *pUni   - In  -- The source string(UNICODE string) ;
//		char**  pByte - Out -- The destination string(ANSI) after transformation ;
//		int     nCodePage - In  -- The code page ;
//
// Notice :
//		GB's   Code Page  : 936 ;
//		BIG5's Code Page  : 950 ;
/*-----------------------------------------------------------------*/
int Unicode2Ansi(const WCHAR *pUni,char **pByte,int nCodePage)
{
	DWORD nByteLen = 0;
	DWORD nWideLen = wcslen(pUni);
	if( nWideLen == 0 ) return 0;

	nByteLen = WideCharToMultiByte(nCodePage,0,pUni,nWideLen,0,0,NULL,NULL);
	(*pByte) = new char[nByteLen+1];
	ZeroMemory( *pByte , (nByteLen+1) * sizeof(char) ) ;

	if(WideCharToMultiByte(nCodePage,0,pUni,nWideLen,(*pByte),nByteLen,NULL,NULL)==0)
		AfxMessageBox(_T("Error1"));

	return nByteLen;
}

/*-----------------------------------------------------------------*/
/*                                                                 */
/*                                                                 */
/*                                                                 */
/*  pick up the error message according to the Error No.           */
/*                                                                 */
/*                                                                 */
/*                                                                 */
/*-----------------------------------------------------------------*/

void TraceLog_send( struct PG_HEAD	* pg_head , LPTSTR p_data)
{
	SYSTEMTIME time ;
	TCHAR buff[1024];
	FILE * fp ;

	GetLocalTime( &time ) ;

    switch( pg_head->reqType )
    {
    case DATA_PACKAGE   :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] send the data to server  : %s\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    case OK_PACKAGE :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] send the response to server  : OK_PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    case NG_PACKAGE :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] send the response to server  : NG_PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    default :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] send the request code to server  : UNKNOWN PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond ) ;

        break;
    }

	fp = _tfopen(_T("history.log"),_T("at")) ;
	if ( fp == NULL )
	{
		::MessageBox(NULL , _T("Can not open this log file : history.log!"), _T("Warning") , MB_OK);
		return ;
	}

	fseek( fp, 0 , SEEK_END );

	fwrite( buff , _tcslen(buff) , 1 , fp ) ;

	fclose(fp);
}

void TraceLog_recv( struct PG_HEAD * pg_head , LPTSTR p_data )
{
	LPTSTR buff;
	FILE * fp ;
	SYSTEMTIME time ;
	int  length ;

	if ( p_data!=NULL )
	{
		length =  _tcslen(p_data) + 256 ; 
	}
	else
	{
		length =  256 ; 
	}

	buff = new TCHAR[length] ;

	GetLocalTime( &time ) ;

    switch( pg_head->reqType )
    {
    case DATA_PACKAGE   :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] get the data from server  : %s\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    case OK_PACKAGE :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] get the response from server  : OK_PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    case NG_PACKAGE :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] get the response from server  : NG_PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond , p_data ) ;
        break ;

    default :
        _stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%02ds] get the request code from server  : UNKNOWN PACKAGE\n") ,
                 time.wYear , time.wMonth , time.wDay , 
				 time.wHour , time.wMinute, time.wSecond ) ;

        break;
    }

	fp = _tfopen(_T("history.log"),_T("at")) ;
	if ( fp == NULL )
	{
		::MessageBox(NULL , _T("Can not open this log file : history.log!"), _T("Warning") , MB_OK);
		delete[] buff ;
		return ;
	}

	fseek( fp, 0 , SEEK_END );

	fwrite( buff , _tcslen(buff) , 1 , fp ) ;

	fclose(fp);

	delete[] buff ;

}


void TraceLog( LPTSTR pMsg )
{
	TCHAR buff[1024];
	SYSTEMTIME time ;
	FILE * fp ;

	ZeroMemory( buff, 1024 ) ;

	GetLocalTime( &time ) ;

	if ( _tcsstr(pMsg,_T("=======")) == NULL )
	{
		_stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%2ds]\nprocess request      : %s\n") , 
		     time.wYear , time.wMonth , time.wDay , 
			 time.wHour , time.wMinute, time.wSecond , pMsg ) ;
	}
	else
	{
		_stprintf( buff , _T("[%04d/%02d/%02d %02dh:%02dm:%2ds]\n%s\n") , 
		     time.wYear , time.wMonth , time.wDay , 
			 time.wHour , time.wMinute, time.wSecond , pMsg ) ;
	}

	fp = _tfopen(_T("history.log"),_T("at")) ;
	if ( fp == NULL )
	{
		::MessageBox(NULL , _T("Can not open this log file : history.log!"), _T("Warning") , MB_OK);
		return ;
	}

	fseek( fp, 0 , SEEK_END );

	fwrite( buff , _tcslen(buff) , 1 , fp ) ;

	fclose(fp);

}


void TraceLog_clear(  )
{
	FILE * fp ;

	fp = _tfopen(_T("history.log"),_T("wt")) ;
	if ( fp == NULL )
	{
		::MessageBox(NULL , _T("Can not open this log file : history.log!"), _T("Warning") , MB_OK);
		return ;
	}

	fclose(fp);

}

⌨️ 快捷键说明

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