00001 /******************************************************************************* 00002 00003 @file UResourceBundle.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, November 2004 00034 @author Kris 00035 00036 Note that this package and documentation is built around the ICU 00037 project (http://oss.software.ibm.com/icu/). Below is the license 00038 statement as specified by that software: 00039 00040 00041 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00042 00043 00044 ICU License - ICU 1.8.1 and later 00045 00046 COPYRIGHT AND PERMISSION NOTICE 00047 00048 Copyright (c) 1995-2003 International Business Machines Corporation and 00049 others. 00050 00051 All rights reserved. 00052 00053 Permission is hereby granted, free of charge, to any person obtaining a 00054 copy of this software and associated documentation files (the 00055 "Software"), to deal in the Software without restriction, including 00056 without limitation the rights to use, copy, modify, merge, publish, 00057 distribute, and/or sell copies of the Software, and to permit persons 00058 to whom the Software is furnished to do so, provided that the above 00059 copyright notice(s) and this permission notice appear in all copies of 00060 the Software and that both the above copyright notice(s) and this 00061 permission notice appear in supporting documentation. 00062 00063 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00064 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00065 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00066 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00067 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00068 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00069 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00070 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00071 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00072 00073 Except as contained in this notice, the name of a copyright holder 00074 shall not be used in advertising or otherwise to promote the sale, use 00075 or other dealings in this Software without prior written authorization 00076 of the copyright holder. 00077 00078 ---------------------------------------------------------------------- 00079 00080 All trademarks and registered trademarks mentioned herein are the 00081 property of their respective owners. 00082 00083 *******************************************************************************/ 00084 00085 module mango.icu.UResourceBundle; 00086 00087 private import mango.icu.ICU, 00088 mango.icu.UString; 00089 00090 public import mango.icu.ULocale; 00091 00092 /******************************************************************************* 00093 00094 API representing a collection of resource information pertaining to 00095 a given locale. A resource bundle provides a way of accessing locale- 00096 specific information in a data file. You create a resource bundle that 00097 manages the resources for a given locale and then ask it for individual 00098 resources. 00099 00100 Resource bundles in ICU4C are currently defined using text files which 00101 conform to the following BNF definition. More on resource bundle concepts 00102 and syntax can be found in the Users Guide. 00103 00104 See <A HREF="http://oss.software.ibm.com/icu/apiref/ures_8h.html"> 00105 this page</A> for full details. 00106 00107 *******************************************************************************/ 00108 00109 class UResourceBundle : ICU 00110 { 00111 private Handle handle; 00112 00113 /*********************************************************************** 00114 00115 Internals opened up to the public 00116 00117 ***********************************************************************/ 00118 00119 // Numeric constants for types of resource items 00120 public enum ResType 00121 { 00122 None = -1, 00123 String = 0, 00124 Binary = 1, 00125 Table = 2, 00126 Alias = 3, 00127 Int = 7, 00128 Array = 8, 00129 IntVector = 14 00130 } 00131 00132 /*********************************************************************** 00133 00134 private constructor for internal use only 00135 00136 ***********************************************************************/ 00137 00138 private this (Handle handle) 00139 { 00140 this.handle = handle; 00141 } 00142 00143 /*********************************************************************** 00144 00145 Constructs a resource bundle for the locale-specific bundle 00146 in the specified path. 00147 00148 locale This is the locale this resource bundle is for. To 00149 get resources for the French locale, for example, you 00150 would create a ResourceBundle passing ULocale::FRENCH 00151 for the "locale" parameter, and all subsequent calls 00152 to that resource bundle will return resources that 00153 pertain to the French locale. If the caller passes a 00154 Locale.Default parameter, the default locale for the 00155 system (as returned by ULocale.getDefault()) will be 00156 used. Passing Locale.Root will cause the root-locale 00157 to be used. 00158 00159 path This is a full pathname in the platform-specific 00160 format for the directory containing the resource 00161 data files we want to load resources from. We use 00162 locale IDs to generate filenames, and the filenames 00163 have this string prepended to them before being passed 00164 to the C++ I/O functions. Therefore, this string must 00165 always end with a directory delimiter (whatever that 00166 is for the target OS) for this class to work correctly. 00167 A null value will open the default ICU data-files 00168 00169 ***********************************************************************/ 00170 00171 this (inout ULocale locale, char[] path = null) 00172 { 00173 Error e; 00174 00175 handle = ures_open (toString(path), toString(locale.name), e); 00176 testError (e, "failed to open resource bundle"); 00177 } 00178 00179 /*********************************************************************** 00180 00181 ***********************************************************************/ 00182 00183 ~this () 00184 { 00185 ures_close (handle); 00186 } 00187 00188 /*********************************************************************** 00189 00190 Returns the size of a resource. Size for scalar types is 00191 always 1, and for vector/table types is the number of child 00192 resources. 00193 00194 ***********************************************************************/ 00195 00196 uint getSize () 00197 { 00198 return ures_getSize (handle); 00199 } 00200 00201 /*********************************************************************** 00202 00203 Returns a signed integer from a resource. This integer is 00204 originally 28 bit and the sign gets propagated. 00205 00206 ***********************************************************************/ 00207 00208 int getInt () 00209 { 00210 Error e; 00211 00212 int x = ures_getInt (handle, e); 00213 testError (e, "failed to get resource integer"); 00214 return x; 00215 } 00216 00217 /*********************************************************************** 00218 00219 Returns a string from a string resource type 00220 00221 ***********************************************************************/ 00222 00223 UText getString () 00224 { 00225 Error e; 00226 uint len; 00227 00228 wchar* x = ures_getString (handle, len, e); 00229 testError (e, "failed to get resource string"); 00230 return new UText (x[0..len]); 00231 } 00232 00233 /*********************************************************************** 00234 00235 Returns the string in a given resource at the specified 00236 index 00237 00238 ***********************************************************************/ 00239 00240 UText getString (uint index) 00241 { 00242 Error e; 00243 uint len; 00244 00245 wchar* x = ures_getStringByIndex (handle, index, len, e); 00246 testError (e, "failed to get resource string"); 00247 return new UText (x[0..len]); 00248 } 00249 00250 /*********************************************************************** 00251 00252 Returns a string in a resource that has a given key. This 00253 procedure works only with table resources. 00254 00255 ***********************************************************************/ 00256 00257 UText getString (char[] key) 00258 { 00259 Error e; 00260 uint len; 00261 00262 wchar* x = ures_getStringByKey (handle, toString(key), len, e); 00263 testError (e, "failed to get resource string"); 00264 return new UText (x[0..len]); 00265 } 00266 00267 /*********************************************************************** 00268 00269 Returns the next string in a resource or NULL if there are 00270 no more resources to iterate over 00271 00272 ***********************************************************************/ 00273 00274 UText getNextString () 00275 { 00276 Error e; 00277 uint len; 00278 char* key; 00279 00280 wchar* x = ures_getNextString (handle, len, key, e); 00281 testError (e, "failed to get next resource string"); 00282 return new UText (x[0..len]); 00283 } 00284 00285 /*********************************************************************** 00286 00287 Returns a binary data from a resource. Can be used at most 00288 primitive resource types (binaries, strings, ints) 00289 00290 ***********************************************************************/ 00291 00292 void[] getBinary () 00293 { 00294 Error e; 00295 uint len; 00296 00297 void* x = ures_getBinary (handle, len, e); 00298 testError (e, "failed to get binary resource"); 00299 return x[0..len]; 00300 } 00301 00302 /*********************************************************************** 00303 00304 Returns an integer vector from a resource 00305 00306 ***********************************************************************/ 00307 00308 int[] getIntVector () 00309 { 00310 Error e; 00311 uint len; 00312 00313 int* x = ures_getIntVector (handle, len, e); 00314 testError (e, "failed to get vector resource"); 00315 return x[0..len]; 00316 } 00317 00318 /*********************************************************************** 00319 00320 Checks whether the resource has another element to 00321 iterate over 00322 00323 ***********************************************************************/ 00324 00325 bool hasNext () 00326 { 00327 return ures_hasNext (handle) != 0; 00328 } 00329 00330 /*********************************************************************** 00331 00332 Resets the internal context of a resource so that 00333 iteration starts from the first element 00334 00335 ***********************************************************************/ 00336 00337 void resetIterator () 00338 { 00339 ures_resetIterator (handle); 00340 } 00341 00342 /*********************************************************************** 00343 00344 Returns the next resource in a given resource or NULL if 00345 there are no more resources 00346 00347 ***********************************************************************/ 00348 00349 UResourceBundle getNextResource () 00350 { 00351 Error e; 00352 00353 return get (ures_getNextResource (handle, null, e), e); 00354 } 00355 00356 /*********************************************************************** 00357 00358 Returns a resource that has a given key. This procedure 00359 works only with table resources. 00360 00361 ***********************************************************************/ 00362 00363 UResourceBundle getResource (char[] key) 00364 { 00365 Error e; 00366 00367 return get (ures_getByKey (handle, toString(key), null, e), e); 00368 } 00369 00370 /*********************************************************************** 00371 00372 Returns the resource at the specified index 00373 00374 ***********************************************************************/ 00375 00376 UResourceBundle getResource (uint index) 00377 { 00378 Error e; 00379 00380 return get (ures_getByIndex (handle, index, null, e), e); 00381 } 00382 00383 /*********************************************************************** 00384 00385 Return the version number associated with this ResourceBundle 00386 as a UVersionInfo array 00387 00388 ***********************************************************************/ 00389 00390 void getVersion (inout Version info) 00391 { 00392 ures_getVersion (handle, info); 00393 } 00394 00395 /*********************************************************************** 00396 00397 Return the ULocale associated with this ResourceBundle 00398 00399 ***********************************************************************/ 00400 00401 void getLocale (inout ULocale locale) 00402 { 00403 Error e; 00404 00405 locale.name = toArray (ures_getLocale (handle, e)); 00406 testError (e, "failed to get resource locale"); 00407 } 00408 00409 /*********************************************************************** 00410 00411 Returns the key associated with this resource. Not all 00412 the resources have a key - only those that are members 00413 of a table. 00414 00415 ***********************************************************************/ 00416 00417 char[] getKey () 00418 { 00419 return toArray (ures_getKey (handle)); 00420 } 00421 00422 /*********************************************************************** 00423 00424 Returns the type of a resource. Available types are 00425 defined in enum UResType 00426 00427 ***********************************************************************/ 00428 00429 ResType getType () 00430 { 00431 return cast(ResType) ures_getType (handle); 00432 } 00433 00434 /*********************************************************************** 00435 00436 Worker function for constructing internal ResourceBundle 00437 instances. Returns null when the provided handle is null. 00438 00439 ***********************************************************************/ 00440 00441 private static final UResourceBundle get (Handle handle, inout Error e) 00442 { 00443 testError (e, "failed to create resource bundle"); 00444 if (handle) 00445 return new UResourceBundle (handle); 00446 return null; 00447 } 00448 00449 00450 /*********************************************************************** 00451 00452 Bind the ICU functions from a shared library. This is 00453 complicated by the issues regarding D and DLLs on the 00454 Windows platform 00455 00456 ***********************************************************************/ 00457 00458 private static void* library; 00459 00460 /*********************************************************************** 00461 00462 ***********************************************************************/ 00463 00464 private static extern (C) 00465 { 00466 Handle function (char*, char*, inout Error) ures_open; 00467 void function (Handle) ures_close; 00468 char* function (Handle, inout Error) ures_getLocale; 00469 void function (Handle, inout Version) ures_getVersion; 00470 uint function (Handle) ures_getSize; 00471 int function (Handle, inout Error) ures_getInt; 00472 wchar* function (Handle, inout uint, inout Error) ures_getString; 00473 wchar* function (Handle, uint, inout uint, inout Error) ures_getStringByIndex; 00474 wchar* function (Handle, char*, inout uint, inout Error) ures_getStringByKey; 00475 void* function (Handle, inout uint, inout Error) ures_getBinary; 00476 int* function (Handle, inout uint, inout Error) ures_getIntVector; 00477 byte function (Handle) ures_hasNext; 00478 void function (Handle) ures_resetIterator; 00479 wchar* function (Handle, inout uint, inout char*, inout Error) ures_getNextString; 00480 char* function (Handle) ures_getKey; 00481 int function (Handle) ures_getType; 00482 Handle function (Handle, Handle, inout Error) ures_getNextResource; 00483 Handle function (Handle, uint, Handle, inout Error) ures_getByIndex; 00484 Handle function (Handle, char*, Handle, inout Error) ures_getByKey; 00485 } 00486 00487 /*********************************************************************** 00488 00489 ***********************************************************************/ 00490 00491 static FunctionLoader.Bind[] targets = 00492 [ 00493 {cast(void**) &ures_open, "ures_open"}, 00494 {cast(void**) &ures_close, "ures_close"}, 00495 {cast(void**) &ures_getLocale, "ures_getLocale"}, 00496 {cast(void**) &ures_getVersion, "ures_getVersion"}, 00497 {cast(void**) &ures_getSize, "ures_getSize"}, 00498 {cast(void**) &ures_getInt, "ures_getInt"}, 00499 {cast(void**) &ures_getString, "ures_getString"}, 00500 {cast(void**) &ures_getStringByIndex, "ures_getStringByIndex"}, 00501 {cast(void**) &ures_getStringByKey, "ures_getStringByKey"}, 00502 {cast(void**) &ures_getBinary, "ures_getBinary"}, 00503 {cast(void**) &ures_hasNext, "ures_hasNext"}, 00504 {cast(void**) &ures_resetIterator, "ures_resetIterator"}, 00505 {cast(void**) &ures_getNextString, "ures_getNextString"}, 00506 {cast(void**) &ures_getKey, "ures_getKey"}, 00507 {cast(void**) &ures_getType, "ures_getType"}, 00508 {cast(void**) &ures_getNextResource, "ures_getNextResource"}, 00509 {cast(void**) &ures_getByIndex, "ures_getByIndex"}, 00510 {cast(void**) &ures_getByKey, "ures_getByKey"}, 00511 ]; 00512 00513 /*********************************************************************** 00514 00515 ***********************************************************************/ 00516 00517 static this () 00518 { 00519 library = FunctionLoader.bind (icuuc, targets); 00520 //test (); 00521 } 00522 00523 /*********************************************************************** 00524 00525 ***********************************************************************/ 00526 00527 static ~this () 00528 { 00529 FunctionLoader.unbind (library); 00530 } 00531 00532 /*********************************************************************** 00533 00534 ***********************************************************************/ 00535 00536 static void test() 00537 { 00538 UResourceBundle b = new UResourceBundle (ULocale.Default); 00539 UText t = b.getNextString(); 00540 UResourceBundle b1 = b.getNextResource (); 00541 } 00542 } 00543 00544