Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

UMessageFormat.d

Go to the documentation of this file.
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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, November 2004      
00031         @author         Kris
00032 
00033         Note that this package and documentation is built around the ICU 
00034         project (http://oss.software.ibm.com/icu/). Below is the license 
00035         statement as specified by that software:
00036 
00037 
00038                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00039 
00040 
00041         ICU License - ICU 1.8.1 and later
00042 
00043         COPYRIGHT AND PERMISSION NOTICE
00044 
00045         Copyright (c) 1995-2003 International Business Machines Corporation and 
00046         others.
00047 
00048         All rights reserved.
00049 
00050         Permission is hereby granted, free of charge, to any person obtaining a
00051         copy of this software and associated documentation files (the
00052         "Software"), to deal in the Software without restriction, including
00053         without limitation the rights to use, copy, modify, merge, publish,
00054         distribute, and/or sell copies of the Software, and to permit persons
00055         to whom the Software is furnished to do so, provided that the above
00056         copyright notice(s) and this permission notice appear in all copies of
00057         the Software and that both the above copyright notice(s) and this
00058         permission notice appear in supporting documentation.
00059 
00060         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00061         OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00062         MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
00063         OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00064         HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
00065         INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
00066         FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00067         NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
00068         WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00069 
00070         Except as contained in this notice, the name of a copyright holder
00071         shall not be used in advertising or otherwise to promote the sale, use
00072         or other dealings in this Software without prior written authorization
00073         of the copyright holder.
00074 
00075         ----------------------------------------------------------------------
00076 
00077         All trademarks and registered trademarks mentioned herein are the 
00078         property of their respective owners.
00079 
00080 *******************************************************************************/
00081 
00082 module mango.icu.UMessageFormat;
00083 
00084 private import  mango.icu.ICU,
00085                 mango.icu.UString;
00086 
00087 public  import  mango.icu.ULocale;
00088 
00089 /*******************************************************************************
00090 
00091         Provides means to produce concatenated messages in language-neutral 
00092         way. Use this for all concatenations that show up to end users. Takes 
00093         a set of objects, formats them, then inserts the formatted strings into 
00094         the pattern at the appropriate places. 
00095 
00096         See <A HREF="http://oss.software.ibm.com/icu/apiref/umsg_8h.html">
00097         this page</A> for full details.
00098 
00099 *******************************************************************************/
00100 
00101 class UMessageFormat : ICU
00102 {       
00103         private Handle handle;
00104 
00105         /***********************************************************************
00106 
00107                 Open a message formatter with given pattern and for the 
00108                 given locale.
00109 
00110         ***********************************************************************/
00111 
00112         this (UText pattern, inout ULocale locale)
00113         {
00114                 Error e;
00115 
00116                 handle = umsg_open (pattern.get(), pattern.len, toString(locale.name), null, e);
00117                 testError (e, "failed to open message formatter");
00118         }
00119 
00120         /***********************************************************************
00121         
00122                 Release message formatter
00123 
00124         ***********************************************************************/
00125 
00126         ~this ()
00127         {
00128                 umsg_close (handle);
00129         }
00130 
00131         /***********************************************************************
00132 
00133                 This locale is used for fetching default number or date 
00134                 format information
00135 
00136         ***********************************************************************/
00137 
00138         UMessageFormat setLocale (inout ULocale locale)
00139         {
00140                 umsg_setLocale (handle, toString(locale.name));
00141                 return this;
00142         }
00143 
00144         /***********************************************************************
00145 
00146                 This locale is used for fetching default number or date 
00147                 format information
00148 
00149         ***********************************************************************/
00150 
00151         UMessageFormat getLocale (inout ULocale locale)
00152         {
00153                 locale.name = toArray (umsg_getLocale (handle));
00154                 return this;
00155         }
00156 
00157         /***********************************************************************
00158 
00159                 Sets the pattern
00160 
00161         ***********************************************************************/
00162 
00163         UMessageFormat setPattern (UText pattern)
00164         {
00165                 Error e;
00166 
00167                 umsg_applyPattern (handle, pattern.get(), pattern.len, null, e);
00168                 testError (e, "failed to set formatter pattern");
00169                 return this;
00170         }
00171 
00172         /***********************************************************************
00173 
00174                 Gets the pattern
00175                       
00176         ***********************************************************************/
00177 
00178         UMessageFormat getPattern (UString s)
00179         {
00180                 uint fmt (wchar* dst, uint length, inout Error e)
00181                 {
00182                         return umsg_toPattern (handle, dst, length, e);
00183                 }
00184 
00185                 s.format (&fmt, "failed to get formatter pattern");
00186                 return this;
00187         }
00188 
00189         /***********************************************************************
00190 
00191                 This function may perform re-ordering of the arguments 
00192                 depending on the locale. For all numeric arguments, double 
00193                 is assumed unless the type is explicitly integer. All choice 
00194                 format arguments must be of type double.
00195 
00196         ***********************************************************************/
00197 
00198         UMessageFormat format (UString s, UList list)
00199         {
00200                 uint fmt (wchar* dst, uint length, inout Error e)
00201                 {
00202                         return umsg_vformat (handle, dst, length, list.args, e);
00203                 }
00204 
00205                 s.format (&fmt, "failed to format pattern");
00206                 return this;
00207         }
00208         
00209         
00210 
00211         /***********************************************************************
00212         
00213                 Bind the ICU functions from a shared library. This is
00214                 complicated by the issues regarding D and DLLs on the
00215                 Windows platform
00216 
00217         ***********************************************************************/
00218 
00219         version (Win32)
00220         {
00221                 private static void*    library;
00222                 private static char[]   libraryName = "icuin30.dll";     
00223 
00224                 /***************************************************************
00225 
00226                 ***************************************************************/
00227 
00228                 private static extern (C) 
00229                 {
00230                         Handle  function (wchar*, uint, char*, void*, inout Error) umsg_open;
00231                         void    function (Handle) umsg_close;
00232                         void    function (Handle, char*) umsg_setLocale;
00233                         char*   function (Handle) umsg_getLocale;
00234                         uint    function (Handle, wchar*, uint, inout Error) umsg_toPattern;
00235                         void    function (Handle, wchar*, uint, void*, inout Error) umsg_applyPattern;
00236                         uint    function (Handle, wchar*, uint, void*, inout Error) umsg_vformat;
00237                 }
00238 
00239                 /***************************************************************
00240 
00241                 ***************************************************************/
00242 
00243                 static  FunctionLoader.Bind[] targets = 
00244                         [
00245                         {cast(void**) &umsg_open,               "umsg_open"}, 
00246                         {cast(void**) &umsg_close,              "umsg_close"},
00247                         {cast(void**) &umsg_setLocale,          "umsg_setLocale"},
00248                         {cast(void**) &umsg_getLocale,          "umsg_getLocale"},
00249                         {cast(void**) &umsg_toPattern,          "umsg_toPattern"},
00250                         {cast(void**) &umsg_applyPattern,       "umsg_applyPattern"},
00251                         {cast(void**) &umsg_vformat,            "umsg_vformat"},
00252                         ];
00253 
00254                 /***************************************************************
00255 
00256                 ***************************************************************/
00257 
00258                 static this ()
00259                 {
00260                         library = FunctionLoader.bind (libraryName, targets);
00261                         //test ();
00262                 }
00263 
00264                 /***************************************************************
00265 
00266                 ***************************************************************/
00267 
00268                 static ~this ()
00269                 {
00270                         FunctionLoader.unbind (library);
00271                 }
00272         }
00273 
00274         /***********************************************************************
00275         
00276         ***********************************************************************/
00277 
00278         static void test()
00279         {
00280                 UString s = new UString(100);
00281                 UMessageFormat m = new UMessageFormat (new UText("{0} {1, number, currency} {2, number, integer}"), ULocale.Default);
00282 
00283                 m.format (s, (new UList).add("abc").add(152.0).add(666));
00284         }
00285 }
00286 
00287 
00288 
00289 /*******************************************************************************
00290 
00291         A list of arguments for the format() method
00292 
00293 *******************************************************************************/
00294 
00295 class UList
00296 {
00297         private uint[32] args;
00298         private uint     index;
00299 
00300         /***********************************************************************
00301         
00302         ***********************************************************************/
00303 
00304         invariant 
00305         {
00306                assert (index < args.length);
00307         }
00308 
00309         /***********************************************************************
00310         
00311         ***********************************************************************/
00312 
00313         UList clear ()
00314         {
00315                 index = 0;
00316                 return this;
00317         }
00318 
00319         /***********************************************************************
00320         
00321         ***********************************************************************/
00322 
00323         UList add (UText x)
00324         {
00325                 args[index] = cast(uint) cast(wchar*) x.get();
00326                 ++index;
00327                 return this;
00328         }
00329 
00330         /***********************************************************************
00331         
00332         ***********************************************************************/
00333 
00334         UList add (wchar[] x)
00335         {
00336                 args[index] = cast(uint) cast(wchar*) x;
00337                 ++index;
00338                 return this;
00339         }
00340 
00341         /***********************************************************************
00342         
00343         ***********************************************************************/
00344 
00345         UList add (int x)
00346         {
00347                 args[index] = x;
00348                 ++index;
00349                 return this;
00350         }
00351 
00352         /***********************************************************************
00353         
00354         ***********************************************************************/
00355 
00356         UList add (double x)
00357         {
00358                 *(cast(double*) &args[index]) = x;
00359                 index += 2;
00360                 return this;
00361         }
00362 
00363 }

Generated on Sun Nov 7 19:06:54 2004 for Mango by doxygen 1.3.6