From 950e3ca01c66ff4e7ecabd702f5142a9ac55579b Mon Sep 17 00:00:00 2001 From: "shrutiv%netscape.com" Date: Fri, 5 Apr 2002 21:31:22 +0000 Subject: [PATCH] Adding files to handle charset conversion (r=smeredith) git-svn-id: svn://10.0.0.236/trunk@118285 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/cck/globals/encoding.cpp | 120 +++++++++++++++++++++++++++++++ mozilla/cck/globals/encoding.h | 5 ++ 2 files changed, 125 insertions(+) create mode 100644 mozilla/cck/globals/encoding.cpp create mode 100644 mozilla/cck/globals/encoding.h diff --git a/mozilla/cck/globals/encoding.cpp b/mozilla/cck/globals/encoding.cpp new file mode 100644 index 00000000000..d64469befa8 --- /dev/null +++ b/mozilla/cck/globals/encoding.cpp @@ -0,0 +1,120 @@ +#include "encoding.h" + +// For supporting customization with non-ascii characters through Cecko, +// the encoding of cck.che is changed to UTF8. Widget values are converted +// to and fro between native encoding and UTF8 encoding. That is, the +// native text entered in the wizard are saved to in-memory copy as UTF8, +// and when displayed in the wizard they are converted back to native +// encoding. +// For customizing the Windows system text strings like program folder +// name, additional components and CD autorun strings, encoding is +// changed from UTF8 back to native encoding. + + +// Convert native encoding to UTF8 encoding +__declspec(dllexport) +CString ConvertANSItoUTF8(CString strAnsi) +{ + int wLen, mLen, retval; + CString strUtf8; + + wLen = MultiByteToWideChar(CP_ACP, 0, strAnsi, -1, NULL, 0); + if (wLen == 0) + { + ShowLastError(); + return strAnsi; + } + LPWSTR strWidechar = new WCHAR[wLen]; + retval = MultiByteToWideChar(CP_ACP, 0, strAnsi, -1, strWidechar, wLen); + if (retval == 0) + { + ShowLastError(); + delete [] strWidechar; + return strAnsi; + } + + mLen = WideCharToMultiByte(CP_UTF8, 0, strWidechar, -1, NULL, 0, NULL, NULL); + if (mLen == 0) + { + ShowLastError(); + delete [] strWidechar; + return strAnsi; + } + LPSTR strMultibyte = new TCHAR[mLen]; + retval = WideCharToMultiByte(CP_UTF8, 0, strWidechar, -1, strMultibyte, + mLen, NULL, NULL); + if (retval == 0) + { + ShowLastError(); + delete [] strWidechar; + delete [] strMultibyte; + return strAnsi; + } + + strUtf8 = strMultibyte; + delete [] strWidechar; + delete [] strMultibyte; + return strUtf8; +} + +// Convert UTF8 encoding to native encoding +__declspec(dllexport) +CString ConvertUTF8toANSI(CString strUtf8) +{ + int wLen, mLen, retval; + CString strAnsi; + wLen = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0); + if (wLen == 0) + { + ShowLastError(); + return strUtf8; + } + LPWSTR strWidechar = new WCHAR[wLen]; + retval = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, strWidechar, wLen); + if (retval == 0) + { + ShowLastError(); + delete [] strWidechar; + return strUtf8; + } + + mLen = WideCharToMultiByte(CP_ACP, 0, strWidechar, -1, NULL, 0, NULL, NULL); + if (mLen == 0) + { + ShowLastError(); + delete [] strWidechar; + return strUtf8; + } + LPSTR strMultibyte = new TCHAR[mLen]; + retval = WideCharToMultiByte(CP_ACP, 0, strWidechar, -1, strMultibyte, + mLen, NULL, NULL); + if (retval == 0) + { + ShowLastError(); + delete [] strWidechar; + delete [] strMultibyte; + return strUtf8; + } + + strAnsi = strMultibyte; + delete [] strWidechar; + delete [] strMultibyte; + return strAnsi; +} + +// Display error message +extern "C" __declspec(dllexport) +void ShowLastError() +{ + char* lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, + 0, + NULL); + MessageBox( NULL, lpMsgBuf, "Error", MB_OK|MB_ICONINFORMATION ); + LocalFree(lpMsgBuf); +} diff --git a/mozilla/cck/globals/encoding.h b/mozilla/cck/globals/encoding.h new file mode 100644 index 00000000000..c5578248e8f --- /dev/null +++ b/mozilla/cck/globals/encoding.h @@ -0,0 +1,5 @@ +#include "stdafx.h" + +extern "C" __declspec(dllexport) void ShowLastError(); +__declspec(dllexport) CString ConvertANSItoUTF8(CString strAnsi); +__declspec(dllexport) CString ConvertUTF8toANSI(CString strUtf8);