1*627f7eb2Smrg // Written in the D programming language.
2*627f7eb2Smrg
3*627f7eb2Smrg /**
4*627f7eb2Smrg * Support UTF-8 on Windows 95, 98 and ME systems.
5*627f7eb2Smrg *
6*627f7eb2Smrg * Copyright: Copyright Digital Mars 2005 - 2009.
7*627f7eb2Smrg * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
8*627f7eb2Smrg * Authors: $(HTTP digitalmars.com, Walter Bright)
9*627f7eb2Smrg */
10*627f7eb2Smrg /* Copyright Digital Mars 2005 - 2009.
11*627f7eb2Smrg * Distributed under the Boost Software License, Version 1.0.
12*627f7eb2Smrg * (See accompanying file LICENSE_1_0.txt or copy at
13*627f7eb2Smrg * http://www.boost.org/LICENSE_1_0.txt)
14*627f7eb2Smrg */
15*627f7eb2Smrg module std.windows.charset;
16*627f7eb2Smrg
version(StdDdoc)17*627f7eb2Smrg version (StdDdoc)
18*627f7eb2Smrg {
19*627f7eb2Smrg /******************************************
20*627f7eb2Smrg * Converts the UTF-8 string s into a null-terminated string in a Windows
21*627f7eb2Smrg * 8-bit character set.
22*627f7eb2Smrg *
23*627f7eb2Smrg * Params:
24*627f7eb2Smrg * s = UTF-8 string to convert.
25*627f7eb2Smrg * codePage = is the number of the target codepage, or
26*627f7eb2Smrg * 0 - ANSI,
27*627f7eb2Smrg * 1 - OEM,
28*627f7eb2Smrg * 2 - Mac
29*627f7eb2Smrg *
30*627f7eb2Smrg * Authors:
31*627f7eb2Smrg * yaneurao, Walter Bright, Stewart Gordon
32*627f7eb2Smrg */
33*627f7eb2Smrg const(char)* toMBSz(in char[] s, uint codePage = 0);
34*627f7eb2Smrg
35*627f7eb2Smrg /**********************************************
36*627f7eb2Smrg * Converts the null-terminated string s from a Windows 8-bit character set
37*627f7eb2Smrg * into a UTF-8 char array.
38*627f7eb2Smrg *
39*627f7eb2Smrg * Params:
40*627f7eb2Smrg * s = UTF-8 string to convert.
41*627f7eb2Smrg * codePage = is the number of the source codepage, or
42*627f7eb2Smrg * 0 - ANSI,
43*627f7eb2Smrg * 1 - OEM,
44*627f7eb2Smrg * 2 - Mac
45*627f7eb2Smrg * Authors: Stewart Gordon, Walter Bright
46*627f7eb2Smrg */
47*627f7eb2Smrg string fromMBSz(immutable(char)* s, int codePage = 0);
48*627f7eb2Smrg }
49*627f7eb2Smrg else:
50*627f7eb2Smrg
51*627f7eb2Smrg version (Windows):
52*627f7eb2Smrg
53*627f7eb2Smrg import core.sys.windows.windows;
54*627f7eb2Smrg import std.conv;
55*627f7eb2Smrg import std.string;
56*627f7eb2Smrg import std.windows.syserror;
57*627f7eb2Smrg
58*627f7eb2Smrg import std.internal.cstring;
59*627f7eb2Smrg
60*627f7eb2Smrg const(char)* toMBSz(in char[] s, uint codePage = 0)
61*627f7eb2Smrg {
62*627f7eb2Smrg // Only need to do this if any chars have the high bit set
foreach(char c;s)63*627f7eb2Smrg foreach (char c; s)
64*627f7eb2Smrg {
65*627f7eb2Smrg if (c >= 0x80)
66*627f7eb2Smrg {
67*627f7eb2Smrg char[] result;
68*627f7eb2Smrg int readLen;
69*627f7eb2Smrg auto wsTmp = s.tempCStringW();
70*627f7eb2Smrg result.length = WideCharToMultiByte(codePage, 0, wsTmp, -1, null, 0,
71*627f7eb2Smrg null, null);
72*627f7eb2Smrg
73*627f7eb2Smrg if (result.length)
74*627f7eb2Smrg {
75*627f7eb2Smrg readLen = WideCharToMultiByte(codePage, 0, wsTmp, -1, result.ptr,
76*627f7eb2Smrg to!int(result.length), null, null);
77*627f7eb2Smrg }
78*627f7eb2Smrg
79*627f7eb2Smrg if (!readLen || readLen != result.length)
80*627f7eb2Smrg {
81*627f7eb2Smrg throw new Exception("Couldn't convert string: " ~
82*627f7eb2Smrg sysErrorString(GetLastError()));
83*627f7eb2Smrg }
84*627f7eb2Smrg
85*627f7eb2Smrg return result.ptr;
86*627f7eb2Smrg }
87*627f7eb2Smrg }
88*627f7eb2Smrg return std.string.toStringz(s);
89*627f7eb2Smrg }
90*627f7eb2Smrg
immutable(char)91*627f7eb2Smrg string fromMBSz(immutable(char)* s, int codePage = 0)
92*627f7eb2Smrg {
93*627f7eb2Smrg const(char)* c;
94*627f7eb2Smrg
95*627f7eb2Smrg for (c = s; *c != 0; c++)
96*627f7eb2Smrg {
97*627f7eb2Smrg if (*c >= 0x80)
98*627f7eb2Smrg {
99*627f7eb2Smrg wchar[] result;
100*627f7eb2Smrg int readLen;
101*627f7eb2Smrg
102*627f7eb2Smrg result.length = MultiByteToWideChar(codePage, 0, s, -1, null, 0);
103*627f7eb2Smrg
104*627f7eb2Smrg if (result.length)
105*627f7eb2Smrg {
106*627f7eb2Smrg readLen = MultiByteToWideChar(codePage, 0, s, -1, result.ptr,
107*627f7eb2Smrg to!int(result.length));
108*627f7eb2Smrg }
109*627f7eb2Smrg
110*627f7eb2Smrg if (!readLen || readLen != result.length)
111*627f7eb2Smrg {
112*627f7eb2Smrg throw new Exception("Couldn't convert string: " ~
113*627f7eb2Smrg sysErrorString(GetLastError()));
114*627f7eb2Smrg }
115*627f7eb2Smrg
116*627f7eb2Smrg return result[0 .. result.length-1].to!string; // omit trailing null
117*627f7eb2Smrg }
118*627f7eb2Smrg }
119*627f7eb2Smrg return s[0 .. c-s]; // string is ASCII, no conversion necessary
120*627f7eb2Smrg }
121*627f7eb2Smrg
122*627f7eb2Smrg
123