comparison dwtx/dwtxhelper/mangoicu/UEnumeration.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 UEnumeration.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.UEnumeration;
86
87 private import dwtx.dwthelper.mangoicu.ICU;
88
89 /*******************************************************************************
90
91 UEnumeration is returned by a number of ICU classes, for providing
92 access to such things as ULocale lists and so on,
93
94 *******************************************************************************/
95
96 class UEnumeration : ICU
97 {
98 package Handle handle;
99
100 /***********************************************************************
101
102 ***********************************************************************/
103
104 this (Handle handle)
105 {
106 this.handle = handle;
107 }
108
109 /***********************************************************************
110
111 Disposes of the storage used by a UEnumeration object
112
113 ***********************************************************************/
114
115 ~this ()
116 {
117 uenum_close (handle);
118 }
119
120 /***********************************************************************
121
122 Returns the next element in the iterator's list.
123
124 If there are no more elements, returns NULL. If the
125 iterator is out-of-sync with its service, status is
126 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
127 If the native service string is a UChar* string, it
128 is converted to char* with the invariant converter.
129 The result is terminated by (char)0. If the conversion
130 fails (because a character cannot be converted) then
131 status is set to U_INVARIANT_CONVERSION_ERROR and the
132 return value is undefined (but non-NULL).
133
134 ***********************************************************************/
135
136 uint count ()
137 {
138 Error e;
139
140 uint x = uenum_count (handle, e);
141 testError (e, "enumeration out of sync");
142 return x;
143 }
144
145 /***********************************************************************
146
147 Resets the iterator to the current list of service IDs.
148
149 This re-establishes sync with the service and rewinds
150 the iterator to start at the first element
151
152 ***********************************************************************/
153
154 void reset ()
155 {
156 ICU.Error e;
157
158 uenum_reset (handle, e);
159 testError (e, "failed to reset enumeration");
160 }
161
162 /***********************************************************************
163
164 Returns the next element in the iterator's list.
165
166 If there are no more elements, returns NULL. If the
167 iterator is out-of-sync with its service, status is
168 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
169 If the native service string is a char* string, it is
170 converted to UChar* with the invariant converter.
171
172 ***********************************************************************/
173
174 bool next (out char[] dst)
175 {
176 ICU.Error e;
177 uint len;
178
179 char* p = uenum_next (handle, &len, e);
180 testError (e, "failed to traverse enumeration");
181 if (p)
182 return dst = p[0..len], true;
183 return false;
184 }
185
186 /***********************************************************************
187
188 Returns the next element in the iterator's list.
189
190 If there are no more elements, returns NULL. If the
191 iterator is out-of-sync with its service, status is
192 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
193 If the native service string is a char* string, it is
194 converted to UChar* with the invariant converter.
195
196 ***********************************************************************/
197
198 bool next (inout wchar[] dst)
199 {
200 ICU.Error e;
201 uint len;
202
203 wchar* p = uenum_unext (handle, &len, e);
204 testError (e, "failed to traverse enumeration");
205 if (p)
206 return dst = p[0..len], true;
207 return false;
208 }
209
210
211 /***********************************************************************
212
213 Bind the ICU functions from a shared library. This is
214 complicated by the issues regarding D and DLLs on the
215 Windows platform
216
217 ***********************************************************************/
218
219 private static void* library;
220
221 /***********************************************************************
222
223 ***********************************************************************/
224
225 private static extern (C)
226 {
227 void function (Handle) uenum_close;
228 uint function (Handle, inout Error) uenum_count;
229 void function (Handle, inout Error) uenum_reset;
230 char* function (Handle, uint*, inout Error) uenum_next;
231 wchar* function (Handle, uint*, inout Error) uenum_unext;
232 }
233
234 /***********************************************************************
235
236 ***********************************************************************/
237
238 static FunctionLoader.Bind[] targets =
239 [
240 {cast(void**) &uenum_close, "uenum_close"},
241 {cast(void**) &uenum_count, "uenum_count"},
242 {cast(void**) &uenum_reset, "uenum_reset"},
243 {cast(void**) &uenum_next, "uenum_next"},
244 {cast(void**) &uenum_unext, "uenum_unext"},
245 ];
246
247 /***********************************************************************
248
249 ***********************************************************************/
250
251 static this ()
252 {
253 library = FunctionLoader.bind (icuuc, targets);
254 }
255
256 /***********************************************************************
257
258 ***********************************************************************/
259
260 static ~this ()
261 {
262 FunctionLoader.unbind (library);
263 }
264 }