| 1 |
/* |
| 2 |
* Small utilities for Windows support. |
| 3 |
* |
| 4 |
* Note: Most compatibility stuff are in system.c. This is splitted |
| 5 |
* away so that it can be used from both libgauche and gauche-config, |
| 6 |
* the latter of which doesn't link to the rest of gauche stuff. |
| 7 |
* |
| 8 |
* This file is to included from both system.c and gauche-config.c. |
| 9 |
* We distinguish which file we're included by checking GAUCHE_H. |
| 10 |
* |
| 11 |
* If used from gauche-config.c, we always allocate memory by malloc |
| 12 |
* (gauche-config doesn't link to GC), regardless of use_gc flag. |
| 13 |
* If used from system.c, we allocate GC-able memory when use_gc is true, |
| 14 |
* and malloc when it is false. |
| 15 |
* The caller is responsible to free it if malloc is used. |
| 16 |
*/ |
| 17 |
|
| 18 |
static WCHAR *mbs2wcs(const char *s, int use_gc, |
| 19 |
void (*errfn)(const char *, ...)) |
| 20 |
{ |
| 21 |
WCHAR *wb; |
| 22 |
int nc = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0); |
| 23 |
if (nc == 0) { |
| 24 |
errfn("Windows error %d on MultiByteToWideChar", GetLastError()); |
| 25 |
} |
| 26 |
#if defined(GAUCHE_H) |
| 27 |
if (use_gc) wb = SCM_NEW_ATOMIC_ARRAY(WCHAR, nc); |
| 28 |
else wb = (WCHAR*)malloc(nc * sizeof(WCHAR)); |
| 29 |
#else |
| 30 |
(void)use_gc; /* suppress unused var warning */ |
| 31 |
wb = (WCHAR*)malloc(nc * sizeof(WCHAR)); |
| 32 |
#endif |
| 33 |
if (MultiByteToWideChar(CP_UTF8, 0, s, -1, wb, nc) == 0) { |
| 34 |
errfn("Windows error %d on MultiByteToWideChar", GetLastError()); |
| 35 |
} |
| 36 |
return wb; |
| 37 |
} |
| 38 |
|
| 39 |
static const char *wcs2mbs(const WCHAR *s, int use_gc, |
| 40 |
void (*errfn)(const char*, ...)) |
| 41 |
{ |
| 42 |
char *mb; |
| 43 |
int nb = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, 0, 0); |
| 44 |
if (nb == 0) { |
| 45 |
errfn("Windows error %d on WideCharToMultiByte", GetLastError()); |
| 46 |
} |
| 47 |
#if defined(GAUCHE_H) |
| 48 |
if (use_gc) mb = SCM_NEW_ATOMIC_ARRAY(char, nb); |
| 49 |
else mb = (char*)malloc(nb); |
| 50 |
#else |
| 51 |
(void)use_gc; /* suppress unused var warning */ |
| 52 |
mb = (char*)malloc(nb); |
| 53 |
#endif |
| 54 |
if (WideCharToMultiByte(CP_UTF8, 0, s, -1, mb, nb, 0, 0) == 0) { |
| 55 |
errfn("Windows error %d on WideCharToMultiByte", GetLastError()); |
| 56 |
} |
| 57 |
return mb; |
| 58 |
} |