1 |
/******************************************************************************* |
---|
2 |
tango.stdc.stringz lacks some useful functions |
---|
3 |
copyright: Copyright (c) 2007 yidabu.com All rights reserved |
---|
4 |
|
---|
5 |
license: BSD style: $(LICENSE) |
---|
6 |
|
---|
7 |
version: Initial release: 2007 |
---|
8 |
|
---|
9 |
author: yidabu.com |
---|
10 |
|
---|
11 |
*******************************************************************************/ |
---|
12 |
|
---|
13 |
module dwin.stdc.stringz; |
---|
14 |
|
---|
15 |
public import tango.stdc.stringz; |
---|
16 |
private import tango.text.convert.Utf; |
---|
17 |
private import tango.core.Exception; |
---|
18 |
private import tango.sys.Common; |
---|
19 |
|
---|
20 |
|
---|
21 |
//////////////////////////////////////////////////////////////////////////////// |
---|
22 |
// UTF to MBSz string |
---|
23 |
//////////////////////////////////////////////////////////////////////////////// |
---|
24 |
|
---|
25 |
/****************************************** |
---|
26 |
* Converts the UTF-8, UTF-16, UTF-32 string s into a null-terminated string in a Windows |
---|
27 |
* 8-bit character set. |
---|
28 |
* |
---|
29 |
* Params: |
---|
30 |
* s = UTF-8 or UTF-16 or UTF-32 string to convert. |
---|
31 |
* codePage = is the number of the target codepage, or |
---|
32 |
* 0 - ANSI, |
---|
33 |
* 1 - OEM, |
---|
34 |
* 2 - Mac |
---|
35 |
* |
---|
36 |
* Authors: |
---|
37 |
* yidabu |
---|
38 |
*/ |
---|
39 |
|
---|
40 |
|
---|
41 |
char* toMbsz(char[] s, uint page = 0) |
---|
42 |
{ |
---|
43 |
return ( toMbsz(toUtf16z(s), page) ); |
---|
44 |
} |
---|
45 |
// |
---|
46 |
|
---|
47 |
/// doitto |
---|
48 |
char* toMbsz(wchar[] ws, uint page = 0) |
---|
49 |
{ |
---|
50 |
return toMbsz( tango.stdc.stringz.toUtf16z(ws), page ); |
---|
51 |
} |
---|
52 |
// |
---|
53 |
|
---|
54 |
/// doitto |
---|
55 |
char* toMbsz(dchar[] s, uint page = 0) |
---|
56 |
{ |
---|
57 |
return ( toMbsz(toUtf16z(s), page) ); |
---|
58 |
} |
---|
59 |
// |
---|
60 |
|
---|
61 |
|
---|
62 |
/****************************************** |
---|
63 |
* Converts the UTF-16 null-terminated string in a Windows |
---|
64 |
* 8-bit character set. |
---|
65 |
* |
---|
66 |
* Params: |
---|
67 |
* wsz = UTF-16 null-terminated string. |
---|
68 |
* codePage = is the number of the target codepage, or |
---|
69 |
* 0 - ANSI, |
---|
70 |
* 1 - OEM, |
---|
71 |
* 2 - Mac |
---|
72 |
* |
---|
73 |
* Authors: |
---|
74 |
* yidabu |
---|
75 |
*/ |
---|
76 |
// modified from std.c.windows.charset : toMBSz |
---|
77 |
char* toMbsz(wchar* wsz, uint page = 0) |
---|
78 |
{ |
---|
79 |
char[] result; |
---|
80 |
int readLen; |
---|
81 |
if(wsz) |
---|
82 |
{ |
---|
83 |
result.length = WideCharToMultiByte(page, 0, wsz, -1, null, 0, null, null); |
---|
84 |
|
---|
85 |
if (result.length) |
---|
86 |
readLen = WideCharToMultiByte(page, 0, wsz, -1, result.ptr, result.length, null, null); |
---|
87 |
|
---|
88 |
if (!readLen || readLen != result.length) |
---|
89 |
throw new IllegalArgumentException ("Stringz.toMbsz :: " ~ SysError.lastMsg); |
---|
90 |
} |
---|
91 |
return result.ptr; |
---|
92 |
} |
---|
93 |
// |
---|
94 |
|
---|
95 |
|
---|
96 |
//////////////////////////////////////////////////////////////////////////////// |
---|
97 |
// UTF to UTF16z |
---|
98 |
//////////////////////////////////////////////////////////////////////////////// |
---|
99 |
|
---|
100 |
/********************************* |
---|
101 |
* Convert array of UTF-8 or UTF-32 chars s[] to a C-style 0 terminated string. |
---|
102 |
* tango.stdc.stringz lack this function |
---|
103 |
*/ |
---|
104 |
wchar* toUtf16z(char[] s) |
---|
105 |
{ |
---|
106 |
return tango.stdc.stringz.toUtf16z( tango.text.convert.Utf.toUtf16(s) ); |
---|
107 |
} |
---|
108 |
// |
---|
109 |
|
---|
110 |
/// doitto |
---|
111 |
wchar* toUtf16z(dchar[] s) |
---|
112 |
{ |
---|
113 |
return tango.stdc.stringz.toUtf16z( tango.text.convert.Utf.toUtf16(s) ); |
---|
114 |
} |
---|
115 |
// |
---|
116 |
|
---|
117 |
|
---|
118 |
//////////////////////////////////////////////////////////////////////////////// |
---|
119 |
// MBSz to UTF |
---|
120 |
//////////////////////////////////////////////////////////////////////////////// |
---|
121 |
|
---|
122 |
/********************************************** |
---|
123 |
* Converts the null-terminated string s from a Windows 8-bit character set |
---|
124 |
* into a UTF-8 or UTF-16 or UTF-32 char array. |
---|
125 |
* |
---|
126 |
* Params: |
---|
127 |
* s = UTF-8 or UTF-16 or UTF-32 string to convert. |
---|
128 |
* codePage = is the number of the source codepage, or |
---|
129 |
* 0 - ANSI, |
---|
130 |
* 1 - OEM, |
---|
131 |
* 2 - Mac |
---|
132 |
* Authors: yidabu |
---|
133 |
*/ |
---|
134 |
char[] toUtf8(char* s, int page = 0) |
---|
135 |
{ |
---|
136 |
return tango.text.convert.Utf.toUtf8( toUtf16(s, page) ); |
---|
137 |
} |
---|
138 |
// |
---|
139 |
|
---|
140 |
/// doitto |
---|
141 |
wchar[] toUtf16(char* s, int page = 0) |
---|
142 |
{ |
---|
143 |
char* c; |
---|
144 |
|
---|
145 |
for (c = s; *c != 0; c++) |
---|
146 |
{ |
---|
147 |
if (*c >= 0x80) |
---|
148 |
{ |
---|
149 |
wchar[] result; |
---|
150 |
int readLen; |
---|
151 |
|
---|
152 |
result.length = MultiByteToWideChar(page, 0, s, -1, null, 0); |
---|
153 |
|
---|
154 |
if (result.length) |
---|
155 |
readLen = MultiByteToWideChar(page, 0, s, -1, result.ptr, result.length); |
---|
156 |
|
---|
157 |
if (!readLen || readLen != result.length) |
---|
158 |
throw new IllegalArgumentException ("Stringz.toUtf16 :: "~SysError.lastMsg); |
---|
159 |
|
---|
160 |
return result[0 .. result.length-1]; // omit trailing null |
---|
161 |
} |
---|
162 |
} |
---|
163 |
return tango.text.convert.Utf.toUtf16( s[0 .. c-s] ); // string is ASCII, no conversion necessary |
---|
164 |
} |
---|
165 |
// |
---|
166 |
|
---|
167 |
/// doitto |
---|
168 |
dchar[] toUtf32(char* s, int page = 0) |
---|
169 |
{ |
---|
170 |
return tango.text.convert.Utf.toUtf32( toUtf16(s, page) ); |
---|
171 |
} |
---|
172 |
// |
---|