From ba06deac7058a716d63803d2840534a6da974231 Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Sun, 10 Apr 2016 09:25:38 +0000
Subject: nasty crutch to allow db_get_tstatic to work correctly

git-svn-id: http://svn.miranda-ng.org/main/trunk@16620 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 plugins/Db3x_mmap/src/dbsettings.cpp | 25 +++++++++++++++++++++++--
 src/mir_core/src/utf.cpp             |  2 +-
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/plugins/Db3x_mmap/src/dbsettings.cpp b/plugins/Db3x_mmap/src/dbsettings.cpp
index bea58a8884..a1e89cb4d4 100644
--- a/plugins/Db3x_mmap/src/dbsettings.cpp
+++ b/plugins/Db3x_mmap/src/dbsettings.cpp
@@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "stdafx.h"
 
+#pragma warning(disable: 4701)
+
 DWORD GetModuleNameOfs(const char *szName);
 DBCachedContact* AddToCachedContactList(MCONTACT contactID, int index);
 
@@ -331,10 +333,28 @@ STDMETHODIMP_(BOOL) CDb3Mmap::GetContactSettingStr(MCONTACT contactID, LPCSTR sz
 
 STDMETHODIMP_(BOOL) CDb3Mmap::GetContactSettingStatic(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv)
 {
+	bool bNeedsWchars;
+	size_t cbSaved;
+
+	if (dbv->type == DBVT_WCHAR) { // there's no wchar_t strings in a database, we need conversion
+		cbSaved = dbv->cchVal-1;
+		dbv->cchVal *= sizeof(wchar_t); // extend a room for the utf8 string
+		dbv->type = DBVT_UTF8;
+		bNeedsWchars = true;
+	}
+	else bNeedsWchars = false;
+
 	if (GetContactSettingWorker(contactID, szModule, szSetting, dbv, 1))
 		return 1;
 
-	if (dbv->type == DBVT_UTF8) {
+	if (bNeedsWchars) {
+		char *pBuf = NEWSTR_ALLOCA(dbv->pszVal);
+		if (Utf8toUcs2(pBuf, dbv->cchVal, dbv->pwszVal, cbSaved) < 0)
+			return 1;
+		
+		dbv->pwszVal[cbSaved] = 0;
+	}
+	else if (dbv->type == DBVT_UTF8) {
 		mir_utf8decode(dbv->pszVal, NULL);
 		dbv->type = DBVT_ASCIIZ;
 	}
@@ -835,7 +855,8 @@ STDMETHODIMP_(BOOL) CDb3Mmap::EnumResidentSettings(DBMODULEENUMPROC pFunc, void
 {
 	for (int i = 0; i < m_lResidentSettings.getCount(); i++) {
 		int ret = pFunc(m_lResidentSettings[i], 0, (LPARAM)pParam);
-		if (ret) return ret;
+		if (ret)
+			return ret;
 	}
 	return 0;
 }
diff --git a/src/mir_core/src/utf.cpp b/src/mir_core/src/utf.cpp
index b0240b28ec..d6050f81f4 100644
--- a/src/mir_core/src/utf.cpp
+++ b/src/mir_core/src/utf.cpp
@@ -207,7 +207,7 @@ static int Utf8toUcs2Len(const char *src, size_t srclen)
 MIR_CORE_DLL(int) Utf8toUcs2(const char *src, size_t srclen, wchar_t *dst, size_t dstlen)
 {
 	unsigned int res;
-	const char *srcend = src + srclen;
+	const char *srcend = src + srclen; // including trailing zero
 	wchar_t *dstend = dst + dstlen;
 
 	while ((dst < dstend) && (src < srcend)) {
-- 
cgit v1.2.3