CStringA wc2utf8(const CStringW& rwstr) { CStringA strUTF8; int iChars = AtlUnicodeToUTF8(rwstr, rwstr.GetLength(), NULL, 0); if (iChars > 0) { LPSTR pszUTF8 = strUTF8.GetBuffer(iChars); AtlUnicodeToUTF8(rwstr, rwstr.GetLength(), pszUTF8, iChars); strUTF8.ReleaseBuffer(iChars); } return strUTF8; }
void CppSQLite3DB::open(const char* szFile) { //---------------------------------------------- //modified by robet.li int nRet = sqlite3_open(szFile, &mpDB); if(HasChineseChar(szFile)) { char szFileNameUtf8[MAX_PATH*4] = {0}; _bstr_t bstrFileName = szFile; AtlUnicodeToUTF8(bstrFileName,bstrFileName.length(),szFileNameUtf8,sizeof(szFileNameUtf8)); nRet = sqlite3_open(szFileNameUtf8, &mpDB); } else { nRet = sqlite3_open(szFile, &mpDB); } if (nRet != SQLITE_OK) { const char* szError = sqlite3_errmsg(mpDB); throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); } setBusyTimeout(mnBusyTimeoutMs); }
/** * @param uNotifyCode - notification code if the message is from a control. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero. * @param nID - specifies the identifier of the menu item, control, or accelerator. * @param hWndCtl - handle to the control sending the message if the message is from a control. Otherwise, this parameter is NULL. */ void CExpressModeDlg::OnSave(UINT /*uNotifyCode*/, int /*nID*/, HWND /*hWndCtl*/) { try { if (! ValidateXMLDocument()) return; CString strLogFile; GetReportFileName(strLogFile); CCustomFileDialog dlgSaveFile(FALSE, _T("xml"), strLogFile, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, _T("Log Files\0*.log\0All Files\0*.*\0"), m_hWnd); if (dlgSaveFile.DoModal(m_hWnd) == IDOK) { CAtlFile fileLog; if (fileLog.Create(dlgSaveFile.m_szFileName, GENERIC_WRITE, 0, CREATE_ALWAYS) == S_OK) { CString strLogText; GetErrorLog(strLogText); CT2CW pchUnicodeText(strLogText); int nDestSize = AtlUnicodeToUTF8(pchUnicodeText, strLogText.GetLength(), NULL, 0); if (nDestSize >= 0) { static const BYTE arrUTF8Preamble[] = { 0xEF, 0xBB, 0xBF }; int nTotalDestSize = sizeof(arrUTF8Preamble) + nDestSize; boost::scoped_array<CHAR> pchDest(new CHAR[nTotalDestSize]); CopyMemory(pchDest.get(), arrUTF8Preamble, sizeof(arrUTF8Preamble)); AtlUnicodeToUTF8(pchUnicodeText, strLogText.GetLength(), pchDest.get() + sizeof(arrUTF8Preamble), nDestSize); fileLog.Write(pchDest.get(), nTotalDestSize); } } } } catch (std::exception& error) { HandleException(error); } }
/******************************************************************************* 函数名称: WorkClass::WriteString2File 写字符串内容到文件 ================================================================================ 参数说明: const CString strText 文件内容 参数说明: const CString strSavePath 文件路径 参数说明: BOOL bUtf_8 是否为UTF-8格式 -------------------------------------------------------------------------------- 返回值: BOOL -------------------------------------------------------------------------------- 文件作者: King.Sollyu QQ:191067617 *******************************************************************************/ BOOL WorkClass::WriteString2File( const CString strText,const CString strSavePath,BOOL bUtf_8 ) { CFile fileText; CFileException fileException; if (fileText.Open(strSavePath,CFile::modeReadWrite|CFile::modeCreate,&fileException) == FALSE) { ASSERT (FALSE); // 打开文件错误,识别什么错误 switch (fileException.m_cause) { case CFileException::fileNotFound: return SetLastError(ERR_FILENOTFOUND),FALSE;break; case CFileException::accessDenied: return SetLastError(ERR_ACCESSDENIED),FALSE;break; case CFileException::sharingViolation: return SetLastError(ERR_SHARINGVIOLATION),FALSE;break; default:return SetLastError(ERR_UNKNOW),FALSE;break; } } if (bUtf_8 == TRUE) { unsigned char data[3] = {0xEF, 0xBB, 0xBF}; fileText.Write(data,3); int nSrcLen = (int)wcslen(strText); CStringA utf8String(strText); int nBufLen = (nSrcLen+1) * 6; LPSTR buffer = utf8String.GetBufferSetLength(nBufLen); // 将UNICODE 转换成UTF8 // 需要函数AtlUnicodeToUTF8 头文件: <atlenc.h> int nLen = AtlUnicodeToUTF8(strText, nSrcLen, buffer, nBufLen); buffer[nLen] = 0; utf8String.ReleaseBuffer(); //写文件 fileText.SeekToEnd(); fileText.Write((LPCSTR)utf8String, nLen); }else { CStringA strANSI(strText); fileText.Write((LPCSTR)strANSI,strANSI.GetLength()); } fileText.Close(); return TRUE; }