[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Eliot-dev] eliot/dic encoding.cpp
From: |
Olivier Teulière |
Subject: |
[Eliot-dev] eliot/dic encoding.cpp |
Date: |
Fri, 23 Oct 2009 20:01:49 +0000 |
CVSROOT: /cvsroot/eliot
Module name: eliot
Changes by: Olivier Teulière <ipkiss> 09/10/23 20:01:49
Modified files:
dic : encoding.cpp
Log message:
Fixed an encoding problem on Windows: do not assume input is UTF-8.
This fixes a crash when typing a character like 'é' in an input field.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/dic/encoding.cpp?cvsroot=eliot&r1=1.10&r2=1.11
Patches:
Index: encoding.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/dic/encoding.cpp,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- encoding.cpp 14 Oct 2009 19:36:37 -0000 1.10
+++ encoding.cpp 23 Oct 2009 20:01:49 -0000 1.11
@@ -46,7 +46,6 @@
{
char *lpMsgBuf;
DWORD dw = GetLastError();
- cerr << dw << endl;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
@@ -113,8 +112,22 @@
#ifdef WIN32
if (iStr.empty())
return L"";
- // XXX: Assume the input is in UTF-8
- return readFromUTF8(iStr.c_str(), iStr.size(), "convertToWc");
+
+ const unsigned int bufSize = iStr.size();
+ // Temporary buffer for output
+ // We will have at most as many characters as in the UTF-8 string
+ wchar_t *wideBuf = new wchar_t[bufSize];
+ int number = MultiByteToWideChar(CP_OEMCP, MB_ERR_INVALID_CHARS,
+ iStr.c_str(), bufSize, wideBuf, bufSize);
+ wstring res(wideBuf, number);
+ delete[] wideBuf;
+ if (number == 0)
+ {
+ // Retrieve the system error message for the last-error code
+ throw DicException("convertToWc: MultiByteToWideChar failed:" +
+ GetWin32Error());
+ }
+ return res;
#else
// Get the needed length (we _can't_ use string::size())
size_t len = mbstowcs(NULL, iStr.c_str(), 0);
@@ -148,9 +161,15 @@
if (size == 0)
return "";
char buf[size];
- // XXX: Assume the output is in UTF-8
- int nb = writeInUTF8(iWStr, buf, size, "convertToMb");
- return string(buf, nb);
+ int res = WideCharToMultiByte(CP_OEMCP, 0, iWStr.c_str(), iWStr.size(),
+ buf, size, NULL, NULL);
+ if (res == 0)
+ {
+ // Retrieve the system error message for the last-error code
+ throw DicException("convertToMb: WideCharToMultiByte failed: " +
+ GetWin32Error());
+ }
+ return string(buf, res);
#else
// Get the needed length (we _can't_ use wstring::size())
size_t len = wcstombs(NULL, iWStr.c_str(), 0);
@@ -391,8 +410,6 @@
oBuffer, iBufSize, NULL, NULL);
if (res == 0)
{
- DWORD dw = GetLastError();
- cerr << dw << endl;
// Retrieve the system error message for the last-error code
throw DicException("writeInUTF8: WideCharToMultiByte failed (" +
iContext + "): " + GetWin32Error());