00001 /******************************************************************************* 00002 00003 @file UMessageFormat.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.UMessageFormat; 00086 00087 private import mango.icu.ICU, 00088 mango.icu.UString; 00089 00090 public import mango.icu.ULocale; 00091 00092 /******************************************************************************* 00093 00094 Provides means to produce concatenated messages in language-neutral 00095 way. Use this for all concatenations that show up to end users. Takes 00096 a set of objects, formats them, then inserts the formatted strings into 00097 the pattern at the appropriate places. 00098 00099 See <A HREF="http://oss.software.ibm.com/icu/apiref/umsg_8h.html"> 00100 this page</A> for full details. 00101 00102 *******************************************************************************/ 00103 00104 class UMessageFormat : ICU 00105 { 00106 private Handle handle; 00107 00108 /*********************************************************************** 00109 00110 Open a message formatter with given wchar[] and for the 00111 given locale. 00112 00113 ***********************************************************************/ 00114 00115 this (wchar[] pattern, inout ULocale locale = ULocale.Default) 00116 { 00117 Error e; 00118 00119 handle = umsg_open (pattern, pattern.length, toString(locale.name), null, e); 00120 testError (e, "failed to open message formatter"); 00121 } 00122 00123 /*********************************************************************** 00124 00125 Open a message formatter with given pattern and for the 00126 given locale. 00127 00128 ***********************************************************************/ 00129 00130 this (UText pattern, inout ULocale locale = ULocale.Default) 00131 { 00132 this (pattern.get, locale); 00133 } 00134 00135 /*********************************************************************** 00136 00137 Release message formatter 00138 00139 ***********************************************************************/ 00140 00141 ~this () 00142 { 00143 umsg_close (handle); 00144 } 00145 00146 /*********************************************************************** 00147 00148 This locale is used for fetching default number or date 00149 format information 00150 00151 ***********************************************************************/ 00152 00153 UMessageFormat setLocale (inout ULocale locale) 00154 { 00155 umsg_setLocale (handle, toString(locale.name)); 00156 return this; 00157 } 00158 00159 /*********************************************************************** 00160 00161 This locale is used for fetching default number or date 00162 format information 00163 00164 ***********************************************************************/ 00165 00166 UMessageFormat getLocale (inout ULocale locale) 00167 { 00168 locale.name = toArray (umsg_getLocale (handle)); 00169 return this; 00170 } 00171 00172 /*********************************************************************** 00173 00174 Sets the pattern 00175 00176 ***********************************************************************/ 00177 00178 UMessageFormat setPattern (UText pattern) 00179 { 00180 Error e; 00181 00182 umsg_applyPattern (handle, pattern.get(), pattern.len, null, e); 00183 testError (e, "failed to set formatter pattern"); 00184 return this; 00185 } 00186 00187 /*********************************************************************** 00188 00189 Gets the pattern 00190 00191 ***********************************************************************/ 00192 00193 UMessageFormat getPattern (UString s) 00194 { 00195 uint fmt (wchar* dst, uint length, inout Error e) 00196 { 00197 return umsg_toPattern (handle, dst, length, e); 00198 } 00199 00200 s.format (&fmt, "failed to get formatter pattern"); 00201 return this; 00202 } 00203 00204 /*********************************************************************** 00205 00206 This function may perform re-ordering of the arguments 00207 depending on the locale. For all numeric arguments, double 00208 is assumed unless the type is explicitly integer. All choice 00209 format arguments must be of type double. 00210 00211 ***********************************************************************/ 00212 00213 UMessageFormat format (UString s, Args* list) 00214 { 00215 uint fmt (wchar* dst, uint length, inout Error e) 00216 { 00217 return umsg_vformat (handle, dst, length, list.args, e); 00218 } 00219 00220 s.format (&fmt, "failed to format pattern"); 00221 return this; 00222 } 00223 00224 00225 /*********************************************************************** 00226 00227 A typesafe list of arguments for the UMessageFormat.format() 00228 method. This should be used in the following manner: 00229 00230 @code 00231 wchar[] format = "{0} {1, number, currency} {2, number, integer}"; 00232 UMessageFormat msg = new UMessageFormat (format); 00233 00234 msg.Args args; 00235 msg.format (output, args.add("abc").add(152.0).add(456)); 00236 @endcode 00237 00238 Note that the argument order must follow that of the format 00239 string, although the format string may dictate the ultimate 00240 position of each argument. 00241 00242 See http://oss.software.ibm.com/icu/apiref/umsg_8h.html for 00243 details on the format string. 00244 00245 @todo this will likely fail on certain CPU architectures. 00246 00247 ***********************************************************************/ 00248 00249 struct Args 00250 { 00251 private uint[32] args; 00252 private uint index; 00253 00254 /*************************************************************** 00255 00256 ***************************************************************/ 00257 00258 invariant 00259 { 00260 assert (index < args.length); 00261 } 00262 00263 /*************************************************************** 00264 00265 ***************************************************************/ 00266 00267 Args* reset () 00268 { 00269 index = 0; 00270 return this; 00271 } 00272 00273 /*************************************************************** 00274 00275 ***************************************************************/ 00276 00277 Args* add (UText x) 00278 { 00279 args[index] = cast(uint) cast(wchar*) x.get(); 00280 ++index; 00281 return this; 00282 } 00283 00284 /*************************************************************** 00285 00286 ***************************************************************/ 00287 00288 Args* add (wchar[] x) 00289 { 00290 args[index] = cast(uint) cast(wchar*) x; 00291 ++index; 00292 return this; 00293 } 00294 00295 /*************************************************************** 00296 00297 ***************************************************************/ 00298 00299 Args* add (int x) 00300 { 00301 args[index] = x; 00302 ++index; 00303 return this; 00304 } 00305 00306 /*************************************************************** 00307 00308 ***************************************************************/ 00309 00310 Args* add (double x) 00311 { 00312 *(cast(double*) &args[index]) = x; 00313 index += 2; 00314 return this; 00315 } 00316 } 00317 00318 00319 /*********************************************************************** 00320 00321 Bind the ICU functions from a shared library. This is 00322 complicated by the issues regarding D and DLLs on the 00323 Windows platform 00324 00325 ***********************************************************************/ 00326 00327 private static void* library; 00328 00329 /*********************************************************************** 00330 00331 ***********************************************************************/ 00332 00333 private static extern (C) 00334 { 00335 Handle function (wchar*, uint, char*, void*, inout Error) umsg_open; 00336 void function (Handle) umsg_close; 00337 void function (Handle, char*) umsg_setLocale; 00338 char* function (Handle) umsg_getLocale; 00339 uint function (Handle, wchar*, uint, inout Error) umsg_toPattern; 00340 void function (Handle, wchar*, uint, void*, inout Error) umsg_applyPattern; 00341 uint function (Handle, wchar*, uint, void*, inout Error) umsg_vformat; 00342 } 00343 00344 /*********************************************************************** 00345 00346 ***********************************************************************/ 00347 00348 static FunctionLoader.Bind[] targets = 00349 [ 00350 {cast(void**) &umsg_open, "umsg_open"}, 00351 {cast(void**) &umsg_close, "umsg_close"}, 00352 {cast(void**) &umsg_setLocale, "umsg_setLocale"}, 00353 {cast(void**) &umsg_getLocale, "umsg_getLocale"}, 00354 {cast(void**) &umsg_toPattern, "umsg_toPattern"}, 00355 {cast(void**) &umsg_applyPattern, "umsg_applyPattern"}, 00356 {cast(void**) &umsg_vformat, "umsg_vformat"}, 00357 ]; 00358 00359 /*********************************************************************** 00360 00361 ***********************************************************************/ 00362 00363 static this () 00364 { 00365 library = FunctionLoader.bind (icuin, targets); 00366 //test (); 00367 } 00368 00369 /*********************************************************************** 00370 00371 ***********************************************************************/ 00372 00373 static ~this () 00374 { 00375 FunctionLoader.unbind (library); 00376 } 00377 00378 /*********************************************************************** 00379 00380 ***********************************************************************/ 00381 00382 static void test() 00383 { 00384 UString output = new UString(100); 00385 wchar[] format = "{0} {1, number, currency} {2, number, integer}"; 00386 00387 UMessageFormat msg = new UMessageFormat (format); 00388 00389 msg.Args args; 00390 msg.format (output, args.add("abc").add(152.0).add(456)); 00391 } 00392 } 00393 00394 00395