comparison dwtx/dwtxhelper/mangoicu/UTransform.d @ 89:040da1cb0d76

Add a local copy of the mango ICU binding to work out the utf8 usability. Will hopefully go back into mango.
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Jun 2008 22:57:31 +0200
parents
children 11e8159caf7a
comparison
equal deleted inserted replaced
88:cd18fa3b71f1 89:040da1cb0d76
1 /*******************************************************************************
2
3 @file UTransform.d
4
5 Copyright (c) 2004 Kris Bell
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for damages
9 of any kind arising from the use of this software.
10
11 Permission is hereby granted to anyone to use this software for any
12 purpose, including commercial applications, and to alter it and/or
13 redistribute it freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must
16 not claim that you wrote the original software. If you use this
17 software in a product, an acknowledgment within documentation of
18 said product would be appreciated but is not required.
19
20 2. Altered source versions must be plainly marked as such, and must
21 not be misrepresented as being the original software.
22
23 3. This notice may not be removed or altered from any distribution
24 of the source.
25
26 4. Derivative works are permitted, but they must carry this notice
27 in full and credit the original source.
28
29
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
32
33 @version Initial version, November 2004
34 @author Kris
35
36 Note that this package and documentation is built around the ICU
37 project (http://oss.software.ibm.com/icu/). Below is the license
38 statement as specified by that software:
39
40
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43
44 ICU License - ICU 1.8.1 and later
45
46 COPYRIGHT AND PERMISSION NOTICE
47
48 Copyright (c) 1995-2003 International Business Machines Corporation and
49 others.
50
51 All rights reserved.
52
53 Permission is hereby granted, free of charge, to any person obtaining a
54 copy of this software and associated documentation files (the
55 "Software"), to deal in the Software without restriction, including
56 without limitation the rights to use, copy, modify, merge, publish,
57 distribute, and/or sell copies of the Software, and to permit persons
58 to whom the Software is furnished to do so, provided that the above
59 copyright notice(s) and this permission notice appear in all copies of
60 the Software and that both the above copyright notice(s) and this
61 permission notice appear in supporting documentation.
62
63 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
64 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
66 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
68 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
69 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
70 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
71 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72
73 Except as contained in this notice, the name of a copyright holder
74 shall not be used in advertising or otherwise to promote the sale, use
75 or other dealings in this Software without prior written authorization
76 of the copyright holder.
77
78 ----------------------------------------------------------------------
79
80 All trademarks and registered trademarks mentioned herein are the
81 property of their respective owners.
82
83 *******************************************************************************/
84
85 module dwtx.dwthelper.mangoicu.UTransform;
86
87 private import dwtx.dwthelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.UString;
89
90 /*******************************************************************************
91
92 See <A HREF="http://oss.software.ibm.com/icu/apiref/utrans_8h.html">
93 this page</A> for full details.
94
95 *******************************************************************************/
96
97 class UTransform : ICU
98 {
99 private Handle handle;
100
101 enum Direction
102 {
103 Forward,
104 Reverse
105 }
106
107
108 /***********************************************************************
109
110 ***********************************************************************/
111
112 this (UText id)
113 {
114 Error e;
115
116 handle = utrans_openU (id.get.ptr, id.len, 0, null, 0, null, e);
117 testError (e, "failed to open ID transform");
118 }
119
120 /***********************************************************************
121
122 ***********************************************************************/
123
124 this (UText rule, Direction dir)
125 {
126 Error e;
127
128 handle = utrans_openU (null, 0, dir, rule.get.ptr, rule.len, null, e);
129 testError (e, "failed to open rule-based transform");
130 }
131
132 /***********************************************************************
133
134 ***********************************************************************/
135
136 ~this ()
137 {
138 utrans_close (handle);
139 }
140
141 /***********************************************************************
142
143 ***********************************************************************/
144
145 UText getID ()
146 {
147 uint len;
148 wchar *s = utrans_getUnicodeID (handle, len);
149 return new UText (s[0..len]);
150 }
151
152 /***********************************************************************
153
154 ***********************************************************************/
155
156 UTransform setFilter (UText filter)
157 {
158 Error e;
159
160 if (filter.length)
161 utrans_setFilter (handle, filter.get.ptr, filter.len, e);
162 else
163 utrans_setFilter (handle, null, 0, e);
164
165 testError (e, "failed to set transform filter");
166 return this;
167 }
168
169 /***********************************************************************
170
171 ***********************************************************************/
172
173 UTransform execute (UString text)
174 {
175 Error e;
176 uint textLen = text.len;
177
178 utrans_transUChars (handle, text.get.ptr, &textLen, text.content.length, 0, &text.len, e);
179 testError (e, "failed to execute transform");
180 return this;
181 }
182
183
184
185 /***********************************************************************
186
187 Bind the ICU functions from a shared library. This is
188 complicated by the issues regarding D and DLLs on the
189 Windows platform
190
191 ***********************************************************************/
192
193 private static void* library;
194
195 /***********************************************************************
196
197 ***********************************************************************/
198
199 private static extern (C)
200 {
201 Handle function (wchar*, uint, uint, wchar*, uint, void*, inout Error) utrans_openU;
202 void function (Handle) utrans_close;
203 wchar* function (Handle, inout uint) utrans_getUnicodeID;
204 void function (Handle, wchar*, uint, inout Error) utrans_setFilter;
205 void function (Handle, wchar*, uint*, uint, uint, uint*, inout Error) utrans_transUChars;
206 }
207
208 /***********************************************************************
209
210 ***********************************************************************/
211
212 static FunctionLoader.Bind[] targets =
213 [
214 {cast(void**) &utrans_openU, "utrans_openU"},
215 {cast(void**) &utrans_close, "utrans_close"},
216 {cast(void**) &utrans_getUnicodeID, "utrans_getUnicodeID"},
217 {cast(void**) &utrans_setFilter, "utrans_setFilter"},
218 {cast(void**) &utrans_transUChars, "utrans_transUChars"},
219 ];
220
221 /***********************************************************************
222
223 ***********************************************************************/
224
225 static this ()
226 {
227 library = FunctionLoader.bind (icuin, targets);
228 }
229
230 /***********************************************************************
231
232 ***********************************************************************/
233
234 static ~this ()
235 {
236 FunctionLoader.unbind (library);
237 }
238 }
239