comparison dwtx/dwtxhelper/mangoicu/UResourceBundle.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 UResourceBundle.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.UResourceBundle;
86
87 private import dwtx.dwthelper.mangoicu.ICU,
88 dwtx.dwthelper.mangoicu.UString;
89
90 public import dwtx.dwthelper.mangoicu.ULocale;
91
92 /*******************************************************************************
93
94 API representing a collection of resource information pertaining to
95 a given locale. A resource bundle provides a way of accessing locale-
96 specific information in a data file. You create a resource bundle that
97 manages the resources for a given locale and then ask it for individual
98 resources.
99
100 Resource bundles in ICU4C are currently defined using text files which
101 conform to the following BNF definition. More on resource bundle concepts
102 and syntax can be found in the Users Guide.
103
104 See <A HREF="http://oss.software.ibm.com/icu/apiref/ures_8h.html">
105 this page</A> for full details.
106
107 *******************************************************************************/
108
109 class UResourceBundle : ICU
110 {
111 private Handle handle;
112
113 /***********************************************************************
114
115 Internals opened up to the public
116
117 ***********************************************************************/
118
119 // Numeric constants for types of resource items
120 public enum ResType
121 {
122 None = -1,
123 String = 0,
124 Binary = 1,
125 Table = 2,
126 Alias = 3,
127 Int = 7,
128 Array = 8,
129 IntVector = 14
130 }
131
132 /***********************************************************************
133
134 private constructor for internal use only
135
136 ***********************************************************************/
137
138 private this (Handle handle)
139 {
140 this.handle = handle;
141 }
142
143 /***********************************************************************
144
145 Constructs a resource bundle for the locale-specific bundle
146 in the specified path.
147
148 locale This is the locale this resource bundle is for. To
149 get resources for the French locale, for example, you
150 would create a ResourceBundle passing ULocale::FRENCH
151 for the "locale" parameter, and all subsequent calls
152 to that resource bundle will return resources that
153 pertain to the French locale. If the caller passes a
154 Locale.Default parameter, the default locale for the
155 system (as returned by ULocale.getDefault()) will be
156 used. Passing Locale.Root will cause the root-locale
157 to be used.
158
159 path This is a full pathname in the platform-specific
160 format for the directory containing the resource
161 data files we want to load resources from. We use
162 locale IDs to generate filenames, and the filenames
163 have this string prepended to them before being passed
164 to the C++ I/O functions. Therefore, this string must
165 always end with a directory delimiter (whatever that
166 is for the target OS) for this class to work correctly.
167 A null value will open the default ICU data-files
168
169 ***********************************************************************/
170
171 this (inout ULocale locale, char[] path = null)
172 {
173 Error e;
174
175 handle = ures_open (toString(path), toString(locale.name), e);
176 testError (e, "failed to open resource bundle");
177 }
178
179 /***********************************************************************
180
181 ***********************************************************************/
182
183 ~this ()
184 {
185 ures_close (handle);
186 }
187
188 /***********************************************************************
189
190 Returns the size of a resource. Size for scalar types is
191 always 1, and for vector/table types is the number of child
192 resources.
193
194 ***********************************************************************/
195
196 uint getSize ()
197 {
198 return ures_getSize (handle);
199 }
200
201 /***********************************************************************
202
203 Returns a signed integer from a resource. This integer is
204 originally 28 bit and the sign gets propagated.
205
206 ***********************************************************************/
207
208 int getInt ()
209 {
210 Error e;
211
212 int x = ures_getInt (handle, e);
213 testError (e, "failed to get resource integer");
214 return x;
215 }
216
217 /***********************************************************************
218
219 Returns a string from a string resource type
220
221 ***********************************************************************/
222
223 UText getString ()
224 {
225 Error e;
226 uint len;
227
228 wchar* x = ures_getString (handle, len, e);
229 testError (e, "failed to get resource string");
230 return new UText (x[0..len]);
231 }
232
233 /***********************************************************************
234
235 Returns the string in a given resource at the specified
236 index
237
238 ***********************************************************************/
239
240 UText getString (uint index)
241 {
242 Error e;
243 uint len;
244
245 wchar* x = ures_getStringByIndex (handle, index, len, e);
246 testError (e, "failed to get resource string");
247 return new UText (x[0..len]);
248 }
249
250 /***********************************************************************
251
252 Returns a string in a resource that has a given key. This
253 procedure works only with table resources.
254
255 ***********************************************************************/
256
257 UText getString (char[] key)
258 {
259 Error e;
260 uint len;
261
262 wchar* x = ures_getStringByKey (handle, toString(key), len, e);
263 testError (e, "failed to get resource string");
264 return new UText (x[0..len]);
265 }
266
267 /***********************************************************************
268
269 Returns the next string in a resource or NULL if there are
270 no more resources to iterate over
271
272 ***********************************************************************/
273
274 UText getNextString ()
275 {
276 Error e;
277 uint len;
278 char* key;
279
280 wchar* x = ures_getNextString (handle, len, key, e);
281 testError (e, "failed to get next resource string");
282 return new UText (x[0..len]);
283 }
284
285 /***********************************************************************
286
287 Returns a binary data from a resource. Can be used at most
288 primitive resource types (binaries, strings, ints)
289
290 ***********************************************************************/
291
292 void[] getBinary ()
293 {
294 Error e;
295 uint len;
296
297 void* x = ures_getBinary (handle, len, e);
298 testError (e, "failed to get binary resource");
299 return x[0..len];
300 }
301
302 /***********************************************************************
303
304 Returns an integer vector from a resource
305
306 ***********************************************************************/
307
308 int[] getIntVector ()
309 {
310 Error e;
311 uint len;
312
313 int* x = ures_getIntVector (handle, len, e);
314 testError (e, "failed to get vector resource");
315 return x[0..len];
316 }
317
318 /***********************************************************************
319
320 Checks whether the resource has another element to
321 iterate over
322
323 ***********************************************************************/
324
325 bool hasNext ()
326 {
327 return ures_hasNext (handle) != 0;
328 }
329
330 /***********************************************************************
331
332 Resets the internal context of a resource so that
333 iteration starts from the first element
334
335 ***********************************************************************/
336
337 void resetIterator ()
338 {
339 ures_resetIterator (handle);
340 }
341
342 /***********************************************************************
343
344 Returns the next resource in a given resource or NULL if
345 there are no more resources
346
347 ***********************************************************************/
348
349 UResourceBundle getNextResource ()
350 {
351 Error e;
352
353 return get (ures_getNextResource (handle, null, e), e);
354 }
355
356 /***********************************************************************
357
358 Returns a resource that has a given key. This procedure
359 works only with table resources.
360
361 ***********************************************************************/
362
363 UResourceBundle getResource (char[] key)
364 {
365 Error e;
366
367 return get (ures_getByKey (handle, toString(key), null, e), e);
368 }
369
370 /***********************************************************************
371
372 Returns the resource at the specified index
373
374 ***********************************************************************/
375
376 UResourceBundle getResource (uint index)
377 {
378 Error e;
379
380 return get (ures_getByIndex (handle, index, null, e), e);
381 }
382
383 /***********************************************************************
384
385 Return the version number associated with this ResourceBundle
386 as a UVersionInfo array
387
388 ***********************************************************************/
389
390 void getVersion (inout Version info)
391 {
392 ures_getVersion (handle, info);
393 }
394
395 /***********************************************************************
396
397 Return the ULocale associated with this ResourceBundle
398
399 ***********************************************************************/
400
401 void getLocale (inout ULocale locale)
402 {
403 Error e;
404
405 locale.name = toArray (ures_getLocale (handle, e));
406 testError (e, "failed to get resource locale");
407 }
408
409 /***********************************************************************
410
411 Returns the key associated with this resource. Not all
412 the resources have a key - only those that are members
413 of a table.
414
415 ***********************************************************************/
416
417 char[] getKey ()
418 {
419 return toArray (ures_getKey (handle));
420 }
421
422 /***********************************************************************
423
424 Returns the type of a resource. Available types are
425 defined in enum UResType
426
427 ***********************************************************************/
428
429 ResType getType ()
430 {
431 return cast(ResType) ures_getType (handle);
432 }
433
434 /***********************************************************************
435
436 Worker function for constructing internal ResourceBundle
437 instances. Returns null when the provided handle is null.
438
439 ***********************************************************************/
440
441 private static final UResourceBundle get (Handle handle, inout Error e)
442 {
443 testError (e, "failed to create resource bundle");
444 if (handle)
445 return new UResourceBundle (handle);
446 return null;
447 }
448
449
450 /***********************************************************************
451
452 Bind the ICU functions from a shared library. This is
453 complicated by the issues regarding D and DLLs on the
454 Windows platform
455
456 ***********************************************************************/
457
458 private static void* library;
459
460 /***********************************************************************
461
462 ***********************************************************************/
463
464 private static extern (C)
465 {
466 Handle function (char*, char*, inout Error) ures_open;
467 void function (Handle) ures_close;
468 char* function (Handle, inout Error) ures_getLocale;
469 void function (Handle, inout Version) ures_getVersion;
470 uint function (Handle) ures_getSize;
471 int function (Handle, inout Error) ures_getInt;
472 wchar* function (Handle, inout uint, inout Error) ures_getString;
473 wchar* function (Handle, uint, inout uint, inout Error) ures_getStringByIndex;
474 wchar* function (Handle, char*, inout uint, inout Error) ures_getStringByKey;
475 void* function (Handle, inout uint, inout Error) ures_getBinary;
476 int* function (Handle, inout uint, inout Error) ures_getIntVector;
477 byte function (Handle) ures_hasNext;
478 void function (Handle) ures_resetIterator;
479 wchar* function (Handle, inout uint, inout char*, inout Error) ures_getNextString;
480 char* function (Handle) ures_getKey;
481 int function (Handle) ures_getType;
482 Handle function (Handle, Handle, inout Error) ures_getNextResource;
483 Handle function (Handle, uint, Handle, inout Error) ures_getByIndex;
484 Handle function (Handle, char*, Handle, inout Error) ures_getByKey;
485 }
486
487 /***********************************************************************
488
489 ***********************************************************************/
490
491 static FunctionLoader.Bind[] targets =
492 [
493 {cast(void**) &ures_open, "ures_open"},
494 {cast(void**) &ures_close, "ures_close"},
495 {cast(void**) &ures_getLocale, "ures_getLocale"},
496 {cast(void**) &ures_getVersion, "ures_getVersion"},
497 {cast(void**) &ures_getSize, "ures_getSize"},
498 {cast(void**) &ures_getInt, "ures_getInt"},
499 {cast(void**) &ures_getString, "ures_getString"},
500 {cast(void**) &ures_getStringByIndex, "ures_getStringByIndex"},
501 {cast(void**) &ures_getStringByKey, "ures_getStringByKey"},
502 {cast(void**) &ures_getBinary, "ures_getBinary"},
503 {cast(void**) &ures_hasNext, "ures_hasNext"},
504 {cast(void**) &ures_resetIterator, "ures_resetIterator"},
505 {cast(void**) &ures_getNextString, "ures_getNextString"},
506 {cast(void**) &ures_getKey, "ures_getKey"},
507 {cast(void**) &ures_getType, "ures_getType"},
508 {cast(void**) &ures_getNextResource, "ures_getNextResource"},
509 {cast(void**) &ures_getByIndex, "ures_getByIndex"},
510 {cast(void**) &ures_getByKey, "ures_getByKey"},
511 ];
512
513 /***********************************************************************
514
515 ***********************************************************************/
516
517 static this ()
518 {
519 library = FunctionLoader.bind (icuuc, targets);
520 //test ();
521 }
522
523 /***********************************************************************
524
525 ***********************************************************************/
526
527 static ~this ()
528 {
529 FunctionLoader.unbind (library);
530 }
531
532 /***********************************************************************
533
534 ***********************************************************************/
535
536 static void test()
537 {
538 UResourceBundle b = new UResourceBundle (ULocale.Default);
539 UText t = b.getNextString();
540 UResourceBundle b1 = b.getNextResource ();
541 }
542 }
543
544