summaryrefslogtreecommitdiff
path: root/src/backend/utils/mb/mbutils.c
diff options
context:
space:
mode:
authorMagnus Hagander <magnus@hagander.net>2009-10-17 00:24:51 +0000
committerMagnus Hagander <magnus@hagander.net>2009-10-17 00:24:51 +0000
commit748771379b9463ee7454630b3cb3f3087b3157cb (patch)
treee5b2b785157c95b41748f4f31bc82bc4772c32f4 /src/backend/utils/mb/mbutils.c
parent76c09dbe8d01a6932de321b97307b488b0f47d25 (diff)
Write to the Windows eventlog in UTF16, converting the message encoding
as necessary. Itagaki Takahiro with some changes from me
Diffstat (limited to 'src/backend/utils/mb/mbutils.c')
-rw-r--r--src/backend/utils/mb/mbutils.c66
1 files changed, 65 insertions, 1 deletions
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c
index cd3ebb219e2..c6e32ef6643 100644
--- a/src/backend/utils/mb/mbutils.c
+++ b/src/backend/utils/mb/mbutils.c
@@ -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