1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32ole.c: make ole_mb2wc more generic

* ext/win32ole/win32ole.c (ole_vstr2wc): use ole_mb2wc.

* ext/win32ole/win32ole.c (ole_mb2wc): parametrize code page.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51911 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-09-21 14:40:57 +00:00
parent 5deaacb0f1
commit 068394a7d6

View file

@ -99,7 +99,7 @@ static void ole_freeexceptinfo(EXCEPINFO *pExInfo);
static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo); static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo);
static void ole_free(void *ptr); static void ole_free(void *ptr);
static size_t ole_size(const void *ptr); static size_t ole_size(const void *ptr);
static LPWSTR ole_mb2wc(char *pm, int len); static LPWSTR ole_mb2wc(char *pm, int len, UINT cp);
static VALUE ole_ary_m_entry(VALUE val, LONG *pid); static VALUE ole_ary_m_entry(VALUE val, LONG *pid);
static VALUE is_all_index_under(LONG *pid, long *pub, long dim); static VALUE is_all_index_under(LONG *pid, long *pub, long dim);
static void * get_ptr_of_variant(VARIANT *pvar); static void * get_ptr_of_variant(VARIANT *pvar);
@ -853,7 +853,6 @@ ole_vstr2wc(VALUE vstr)
{ {
rb_encoding *enc; rb_encoding *enc;
int cp; int cp;
UINT size = 0;
LPWSTR pw; LPWSTR pw;
st_data_t data; st_data_t data;
struct st_table *tbl = DATA_PTR(enc2cp_hash); struct st_table *tbl = DATA_PTR(enc2cp_hash);
@ -877,58 +876,40 @@ ole_vstr2wc(VALUE vstr)
rb_raise(eWIN32OLERuntimeError, "not installed Windows codepage(%d) according to `%s'", cp, rb_enc_name(enc)); rb_raise(eWIN32OLERuntimeError, "not installed Windows codepage(%d) according to `%s'", cp, rb_enc_name(enc));
} }
} }
if (conv_51932(cp)) { pw = ole_mb2wc(RSTRING_PTR(vstr), RSTRING_LENINT(vstr), cp);
#ifndef pIMultiLanguage RB_GC_GUARD(vstr);
DWORD dw = 0;
UINT len = RSTRING_LENINT(vstr);
HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
&dw, cp, RSTRING_PTR(vstr), &len, NULL, &size);
if (FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
}
pw = SysAllocStringLen(NULL, size);
len = RSTRING_LEN(vstr);
hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
&dw, cp, RSTRING_PTR(vstr), &len, pw, &size);
if (FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
}
return pw;
#endif
}
size = MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), NULL, 0);
pw = SysAllocStringLen(NULL, size);
MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), pw, size);
return pw; return pw;
} }
static LPWSTR static LPWSTR
ole_mb2wc(char *pm, int len) ole_mb2wc(char *pm, int len, UINT cp)
{ {
UINT size = 0; UINT size = 0;
LPWSTR pw; LPWSTR pw;
if (conv_51932(cWIN32OLE_cp)) { if (conv_51932(cp)) {
#ifndef pIMultiLanguage #ifndef pIMultiLanguage
DWORD dw = 0; DWORD dw = 0;
UINT n = len; UINT n = len;
HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
&dw, cWIN32OLE_cp, pm, &n, NULL, &size); &dw, cp, pm, &n, NULL, &size);
if (FAILED(hr)) { if (FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp); ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
} }
pw = SysAllocStringLen(NULL, size); pw = SysAllocStringLen(NULL, size);
n = len;
hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
&dw, cWIN32OLE_cp, pm, &n, pw, &size); &dw, cp, pm, &n, pw, &size);
if (FAILED(hr)) { if (FAILED(hr)) {
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp); ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
} }
return pw; return pw;
#endif #endif
} }
size = MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, NULL, 0); size = MultiByteToWideChar(cp, 0, pm, len, NULL, 0);
pw = SysAllocStringLen(NULL, size - 1); pw = SysAllocStringLen(NULL, size);
MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, pw, size); pw[size-1] = 0;
MultiByteToWideChar(cp, 0, pm, len, pw, size);
return pw; return pw;
} }
@ -1839,7 +1820,7 @@ clsid_from_remote(VALUE host, VALUE com, CLSID *pclsid)
len = sizeof(clsid); len = sizeof(clsid);
err = RegQueryValueEx(hpid, "", NULL, &dwtype, (BYTE *)clsid, &len); err = RegQueryValueEx(hpid, "", NULL, &dwtype, (BYTE *)clsid, &len);
if (err == ERROR_SUCCESS && dwtype == REG_SZ) { if (err == ERROR_SUCCESS && dwtype == REG_SZ) {
pbuf = ole_mb2wc(clsid, -1); pbuf = ole_mb2wc(clsid, -1, cWIN32OLE_cp);
hr = CLSIDFromString(pbuf, pclsid); hr = CLSIDFromString(pbuf, pclsid);
SysFreeString(pbuf); SysFreeString(pbuf);
} }