Exemplo n.º 1
0
// ---------------------------------------------------------------------
void UTF8Conv::ToMultiByteWithLineEnding(
	MBCString& mbcString,		//!< OUTPUT. coverted string
	ConstUTF8Str utf8Str		//!< source UTF-8 string
)
{
	ToMultiByte(mbcString, utf8Str);
}
std::string ToMultiByteString(const std::wstring& wstring, int code_page)
{
	int length = wstring.length();
	char* temp_string = ToMultiByte(wstring.c_str(), code_page, length);

	std::string new_string(temp_string, length);
	free(temp_string);
	return new_string;
}
char *ToMultiByte(const char *src, UINT codePageSrc, UINT codePageDest)
{
    CrashIf(src);
    if (!src) return NULL;

    if (codePageSrc == codePageDest)
        return Dup(src);

    return ToMultiByte(ToWideChar(src, codePageSrc), codePageDest);
}
Exemplo n.º 4
0
/* Caller needs to free() the result */
char *ToMultiByte(const char *src, UINT codePageSrc, UINT codePageDest)
{
    AssertCrash(src);
    if (!src) return nullptr;

    if (codePageSrc == codePageDest)
        return str::Dup(src);

    ScopedMem<WCHAR> tmp(ToWideChar(src, codePageSrc));
    if (!tmp)
        return nullptr;

    return ToMultiByte(tmp.Get(), codePageDest);
}
Exemplo n.º 5
0
std::string CIDCalculate(std::wstring path)
{
	return ToMultiByte(path.c_str());
}
Exemplo n.º 6
0
///////////////////////////////////////////////////////////////////////////////
//
// 函数功能描述:日志打印
// 输入:日志级别、颜色
// 输出:向窗口或文件写日志
// 返回值:
// 其它说明:
//
//2013-10-09 修改内容 : 添加加密日志信息部分 宋亚非
///////////////////////////////////////////////////////////////////////////////
void CLogTrace::TraceColor(BYTE cLevel, DWORD dwColor)
{
	if(IsLogFileEnable()==FALSE)return;
    switch (cLevel)
    {
    case LOGL_TOP:
    case LOGL_MID:
        {
            if (m_hLogWnd) //写日志窗口
            {
                ::SendMessage(m_hLogWnd, TRACE_WND_MSG, 0, (LPARAM)dwColor);
            }
        }

    case LOGL_HIG:
    case LOGL_LOW:
        {
            static DWORD s_dwWriteCount = 0; //总共打印文件的次数
            DWORD dwWritedBytes = 0; //实际写入字节数

			if (NULL == m_hLogFile)
			{			
				CreatLogFile() ;  //创建或打开log文件
			}

			//读取文件开始标志 begin--
			int nFlag = 0 ;
			int nCharCount = 0 ;
			GetEncryptFlag(nFlag) ;
			if ( m_hLogFile )
			{
				//log文件为加密文件 但是此次写日志为明文 || log文件是明文 但是此次写日志需要加密
				//通过重命名备份源文件 并 创建新的文件进行日志打印
				if ( nFlag == 1 && !IsEncryptLog() || nFlag != 1 && IsEncryptLog() )
				{
					CloseHandle(m_hLogFile) ;
					m_hLogFile = NULL ;

					TCHAR szExtension[] = _T(".old") ;
					int nNewPathLen = lstrlen(m_szFullLogFileName) + lstrlen(szExtension) + 1;
					TCHAR *pszNewPath = new TCHAR[nNewPathLen] ;
					memset(pszNewPath, 0, nNewPathLen*sizeof(TCHAR)) ;
					lstrcat(pszNewPath, m_szFullLogFileName) ;
					lstrcat(pszNewPath, szExtension) ;
					MoveFileEx(m_szFullLogFileName, pszNewPath, MOVEFILE_REPLACE_EXISTING) ;

					if ( pszNewPath != NULL )
					{
						delete []pszNewPath ;
						pszNewPath = NULL ;
					}

					CreatLogFile() ;  //创建新文件
				}
			}
			//--end

			if (m_hLogFile) //Log文件可用
            {
                //每写若干次检查一次大小                
                if (0 == s_dwWriteCount % MAX_CHK_SIZE_PER_TIMES)
                {
                    //获取文件大小
                    DWORD dwLogFileSize = GetFileSize(m_hLogFile, NULL);
                    if (INVALID_FILE_SIZE == dwLogFileSize)
                    {
                        OutputDebugString(_T("Error: run GetFileSize fail\r\n"));
                        CloseHandle(m_hLogFile);
                        m_hLogFile = NULL;
                    }
                    else if (dwLogFileSize > MAX_SIZE_OF_LOGFILE) //文件太大
                    {
                        CloseHandle(m_hLogFile);
                        m_hLogFile = NULL;
                        
						//暂时用来保存所有日志文件;
						if (m_bCompressZip)
						{
							CompressLogzip(m_szFullLogFileName);
						}
                        //新开一个文件
                        m_hLogFile = CreateFile(m_szFullLogFileName, GENERIC_WRITE,
							FILE_SHARE_READ | FILE_SHARE_WRITE, 0, TRUNCATE_EXISTING,//CREATE_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, 0);
                        if (INVALID_HANDLE_VALUE == m_hLogFile)
                        {
                            m_hLogFile = NULL;
                            OutputDebugString(_T("Error: Create log file fail\r\n"));
                        }

                    }
                }

				//不管m_szBuffer是不是宽字符,都将它们转换成multiple string
				char *pWriteStr = NULL ;
				char *pEncryptInfo = NULL ;
				int nWriteStrLen = 0 ;

				char * pMultipleStr = NULL ;

#ifndef _MBCS
				pMultipleStr = ToMultiByte(m_szBuffer[1], &nWriteStrLen);
				pWriteStr = pMultipleStr ;
#else
				pWriteStr = m_szBuffer[1] ;
				nWriteStrLen = lstrlen( m_szBuffer[1] ) ;
#endif

				//加密日志信息 begin--
				if ( IsEncryptLog() )
				{
					pEncryptInfo = new char[nWriteStrLen * 2] ;
					memset(pEncryptInfo, 0, nWriteStrLen * 2) ;
					nWriteStrLen = m_base64Encrypt.Base64Encrypt(pEncryptInfo, pWriteStr, LOG_ENCRYPT_KEY) ;
					pEncryptInfo[++nWriteStrLen] = 0 ;  //作为分隔符
					pWriteStr = pEncryptInfo ;
				}
				//--end

				//写入日志文件  begin--
				if (0 == WriteFile(m_hLogFile,pWriteStr, nWriteStrLen, &dwWritedBytes, 0))
				{
					OutputDebugString(_T("Error: Write log file fail\r\n"));
					CloseHandle(m_hLogFile);
					m_hLogFile = NULL;
				}
				//--end

				//内存清理 begin--
				if ( pMultipleStr )
				{
					delete[] pMultipleStr;
					pMultipleStr = NULL;
				}
				if ( pEncryptInfo )
				{
					delete []pEncryptInfo ;
					pEncryptInfo = NULL ;
				}
				//--end

				//每写一次日志就关闭掉日志文件,避免打印日志出现时序上的问题
				CloseHandle(m_hLogFile);
				m_hLogFile = NULL;

                s_dwWriteCount++;
            }
        }

    case LOGL_DBG:
    default:
        {
            //写Debug窗口
            OutputDebugString(m_szBuffer[1]);
        }
    }
}