//-------------------------------------------------------------- // Convert a wide Unicode string to an UTF8 string string ofxDetectDisplays::utf8_encode(const std::wstring &wstr) { int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo( size_needed, 0 ); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
//! Converts a wide Unicode string to an UTF8 string std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr) { int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), NULL, 0, NULL, NULL); std::string strTo(size_needed, 0); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), &strTo[0], size_needed, NULL, NULL); return strTo; }
// Convert an wide Unicode string to ANSI string std::string unicode2ansi(const std::wstring &wstr) { int size_needed = WideCharToMultiByte(CP_ACP, 0, &wstr[0], -1, NULL, 0, NULL, NULL); std::string strTo(size_needed, 0); WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
inline static std::string wstring_to_string(const std::wstring &wstr) { int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo(size_needed, 0); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
// Convert a wide Unicode string to an UTF8 string static std::string utf8_encode(const std::wstring &wstr, size_t size_needed) { if (size_needed == 0) size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), NULL, 0, NULL, NULL); std::string strTo(size_needed, 0); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
//TODO: remove this, or create c version of this std::string utf8_encode(const std::wstring &wstr) { if( wstr.empty() ) return std::string(); int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo( size_needed, 0 ); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
std::string fromUnicode(const std::wstring &wstr, UINT codePage) { if( wstr.empty() ) return std::string(); int size_needed = WideCharToMultiByte(codePage, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo( size_needed, 0 ); WideCharToMultiByte (codePage, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; }
std::string CSQLiteConversion::ConvertToUTF8(const wchar_t *wstr) { const int nLen = wcslen(wstr); if( nLen <= 0 ) return std::string(); const int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], nLen, NULL, 0, NULL, NULL); std::string strTo(size_needed, 0); WideCharToMultiByte(CP_UTF8, 0, &wstr[0], nLen, &strTo[0], size_needed, NULL, NULL); return strTo; }
BOOL CSxSParser::ExportFile(LPCTSTR szTag, LPCTSTR szName, LPCTSTR szSrc, LPCTSTR szDst, CString& strMsg) const { CString strTag(szTag); strTag += _T("*.manifest"); CString strSrc(szSrc); if(strSrc[strSrc.GetLength() - 1] != _T('\\')) strSrc += _T('\\'); StringList folders = FindMatchedFSObjects(strSrc, strTag, TRUE); UINT uCount = folders.GetSize(); if(uCount == 0) { strMsg.Format(IDS_ERR_SXS_FOLDER_NOT_EXISTS, szTag); return FALSE; } CString strDst(szDst); if(strDst[strDst.GetLength() - 1] != _T('\\')) strDst += _T('\\'); strDst += szName; for(UINT i=0; i<uCount; ++ i) { CString strFrom(strSrc + folders[i]); CString strTo(strDst); strTo += _T("\\"); strTo += szName; strTo += _T(".manifest"); if(!::CopyFile(strFrom, strTo, FALSE)) { strMsg.LoadString(IDS_ERR_COPY_FILES); return FALSE; } } return TRUE; }
void CMainDialog::OnSend(void) { CCrashReporterApp* pApp = DYNAMIC_DOWNCAST(CCrashReporterApp, AfxGetApp()); ASSERT_VALID(pApp); CString strIniPath = pApp->m_strAppDataPath; strIniPath += INI_FILE_NAME; CIniAppSettings iniFile(strIniPath); TCHAR szSubject[256] = { 0 }; _sntprintf(szSubject, _countof(szSubject) - 2, _T("Error report for %s"), pApp->m_strUpdateItExe); TCHAR szMessage[MAX_USER_COMMENTS_SIZE] = { 0 }; m_What.GetWindowText(szMessage, _countof(szMessage) - 2); TCHAR szAttachmentPath[MAX_PATH * 2] = { 0 }; // create new zip file, in case user unchecked some files m_nFilesInZip = ZipFiles(); if (m_nFilesInZip > 0) { _tcsncpy(szAttachmentPath, m_strZipFile, _countof(szAttachmentPath) - 2); } else if (szMessage[0] == _T('\0')) { // no files in zip - check if there is a message AfxMessageBox(IDS_NO_ZIP_AND_MESSAGE, MB_ICONSTOP | MB_OK); return; } try { CSmtpConnection smtpConn; CSmtpMessage smtpMsg; CSmtpBodyPart smtpTextPart; CSmtpBodyPart smtpZipPart; CString strFrom = pApp->GetProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_FROM); if (strFrom.IsEmpty()) { static const TCHAR szDefFrom[] = _T("*****@*****.**"); ::GetPrivateProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_FROM, szDefFrom, strFrom.GetBuffer(256), 256, strIniPath); strFrom.ReleaseBuffer(); } CString strTo(XCRASHREPORT_SEND_TO_ADDRESS); CString strHost = pApp->GetProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_HOST); if (strHost.IsEmpty()) { static const TCHAR szDefHost[] = _T("mail.unknown.org"); ::GetPrivateProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_HOST, szDefHost, strHost.GetBuffer(256), 256, strIniPath); strHost.ReleaseBuffer(); } INTERNET_PORT nPort = LOWORD(pApp->GetProfileInt(SZ_REGK_SMTP, SZ_REGV_SMTP_PORT, IPPORT_SMTP)); if (nPort == INTERNET_INVALID_PORT_NUMBER || nPort > INTERNET_MAX_PORT_NUMBER_VALUE) { nPort = IPPORT_SMTP; } smtpMsg.m_sXMailer = _T("CrashReporter/1.0"); smtpMsg.m_From = CSmtpAddress(strFrom); smtpMsg.ParseMultipleRecipients(strTo, smtpMsg.m_To); smtpMsg.m_sSubject = szSubject; smtpZipPart.SetFilename(szAttachmentPath); smtpTextPart.SetText(szMessage); #if defined(UNICODE) || defined(_UNICODE) smtpTextPart.SetCharset(_T("UTF-8")); #else CString strCharSet = pApp->GetProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_CHARSET); if (strCharSet.IsEmpty()) { strCharSet.Format(IDS_CHARSET_FORMAT, ::GetACP()); TCHAR szIniCharSet[256] = { 0 }; ::GetPrivateProfileString(SZ_REGK_SMTP, SZ_REGV_SMTP_CHARSET, strCharSet, szIniCharSet, _countof(szIniCharSet), strIniPath); if (strCharSet.CompareNoCase(szIniCharSet) != 0) { strCharSet = szIniCharSet; } } smtpTextPart.SetCharset(strCharSet); #endif // UNICODE smtpMsg.AddBodyPart(smtpTextPart); smtpMsg.AddBodyPart(smtpZipPart); smtpConn.Connect(strHost, CSmtpConnection::AUTH_NONE, NULL, NULL, nPort); smtpConn.SendMessage(smtpMsg); smtpConn.Disconnect(); } catch (CSmtpException* pErr) { AfxMessageBox(pErr->GetErrorMessage(), MB_ICONSTOP | MB_OK); delete pErr; } catch (CAppSettingsException* pErr) { AfxMessageBox(pErr->GetErrorMessage(), MB_ICONSTOP | MB_OK); delete pErr; } __super::OnOK(); }
BOOL CSxSParser::ExportFolder(LPCTSTR szTag, LPCTSTR szName, LPCTSTR szSrc, LPCTSTR szDst, CString& strMsg) const { CString strTag(szTag); strTag += _T("*"); CString strSrc(szSrc); if(strSrc[strSrc.GetLength() - 1] != _T('\\')) strSrc += _T('\\'); CString strDst(szDst); if(strDst[strDst.GetLength() - 1] != _T('\\')) strDst += _T('\\'); StringList folders = FindMatchedFSObjects(strSrc, strTag, FALSE); UINT uCount = folders.GetSize(); if(uCount == 0) { strMsg.Format(IDS_ERR_SXS_FOLDER_NOT_EXISTS, szTag); return FALSE; } CString strTo(strDst); strTo.AppendChar(0); CString orgFolder, targetFolder; for(UINT i=0; i<uCount; ++ i) { CString strFrom(strSrc + folders[i]); strFrom.AppendChar(0); if(!MakeEmptyFolder(strDst + szName)) { strMsg.LoadString(IDS_ERR_MAKE_EMPTY_FOLDER); return FALSE; } SHFILEOPSTRUCT fo = {0}; fo.hwnd = NULL; fo.wFunc = FO_COPY; fo.pFrom = strFrom; fo.pTo = strTo; fo.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NORECURSION; if(0 != SHFileOperation(&fo)) { strMsg.LoadString(IDS_ERR_COPY_FOLDERS); return FALSE; } orgFolder = strDst; orgFolder += folders[i]; targetFolder = strDst; targetFolder += szName; if(!::MoveFile(orgFolder, targetFolder)) { strMsg.LoadString(IDS_ERR_COPY_FOLDERS); return FALSE; } } return TRUE; }