1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-21 00:42:43 +03:00

Write to the Windows eventlog in UTF16, converting the message encoding

as necessary.

Itagaki Takahiro with some changes from me
This commit is contained in:
Magnus Hagander
2009-10-17 00:24:51 +00:00
parent 76c09dbe8d
commit 748771379b
4 changed files with 214 additions and 146 deletions

View File

@@ -4,7 +4,7 @@
*
* Tatsuo Ishii
*
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.89 2009/07/07 19:28:56 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.90 2009/10/17 00:24:51 mha Exp $
*/
#include "postgres.h"
@@ -58,6 +58,7 @@ static FmgrInfo *ToClientConvProc = NULL;
*/
static pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static pg_enc2name *PlatformEncoding = NULL;
/*
* During backend startup we can't set client encoding because we (a)
@@ -978,3 +979,66 @@ pg_client_encoding(PG_FUNCTION_ARGS)
Assert(ClientEncoding);
return DirectFunctionCall1(namein, CStringGetDatum(ClientEncoding->name));
}
int
GetPlatformEncoding(void)
{
if (PlatformEncoding == NULL)
PlatformEncoding = &pg_enc2name_tbl[pg_get_encoding_from_locale("")];
return PlatformEncoding->encoding;
}
#ifdef WIN32
/*
* Result is palloc'ed null-terminated utf16 string. The character length
* is also passed to utf16len if not null. Returns NULL iff failed.
*/
WCHAR *
pgwin32_toUTF16(const char *str, int len, int *utf16len)
{
WCHAR *utf16;
int dstlen;
UINT codepage;
codepage = pg_enc2name_tbl[GetDatabaseEncoding()].codepage;
/*
* Use MultiByteToWideChar directly if there is a corresponding codepage,
* or double conversion through UTF8 if not.
*/
if (codepage != 0)
{
utf16 = (WCHAR *) palloc(sizeof(WCHAR) * (len + 1));
dstlen = MultiByteToWideChar(codepage, 0, str, len, utf16, len);
utf16[dstlen] = L'\0';
}
else
{
char *utf8;
utf8 = (char *) pg_do_encoding_conversion((unsigned char *) str,
len, GetDatabaseEncoding(), PG_UTF8);
if (utf8 != str)
len = strlen(utf8);
utf16 = (WCHAR *) palloc(sizeof(WCHAR) * (len + 1));
dstlen = MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, len);
utf16[dstlen] = L'\0';
if (utf8 != str)
pfree(utf8);
}
if (dstlen == 0 && len > 0)
{
pfree(utf16);
return NULL; /* error */
}
if (utf16len)
*utf16len = len;
return utf16;
}
#endif