forked from let-def/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wstr.c
60 lines (50 loc) · 2.08 KB
/
wstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**************************************************************************/
/* */
/* OCaml */
/* */
/* David Allsopp, OCaml Labs, Cambridge. */
/* */
/* Copyright 2017 MetaStack Solutions Ltd. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* Need at least Windows Vista for WC_ERR_INVALID_CHARS */
#define _WIN32_WINNT 0x600
#define WINVER 0x600
#include <windows.h>
/* See corresponding values in runtime/win32.c */
static int windows_unicode_enabled = WINDOWS_UNICODE;
static int windows_unicode_strict = 1;
/* Adapted from runtime/win32.c */
int win_wide_char_to_multi_byte(const wchar_t *s, int slen,
char *out, int outlen)
{
int retcode;
if (slen == 0)
return 0;
if (windows_unicode_enabled != 0)
retcode =
WideCharToMultiByte(CP_UTF8,
windows_unicode_strict ? WC_ERR_INVALID_CHARS : 0,
s, slen, out, outlen, NULL, NULL);
else
retcode =
WideCharToMultiByte(CP_ACP, 0, s, slen, out, outlen, NULL, NULL);
if (retcode == 0)
return -1;
return retcode;
}
char* caml_stat_strdup_of_utf16(const wchar_t *s)
{
char *out = NULL;
int retcode;
retcode = win_wide_char_to_multi_byte(s, -1, NULL, 0);
if (retcode >= 0) {
out = (char *)malloc(retcode);
win_wide_char_to_multi_byte(s, -1, out, retcode);
}
return out;
}