00001 /******************************************************************************* 00002 00003 @file UTransform.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.UTransform; 00086 00087 private import mango.icu.ICU, 00088 mango.icu.UString; 00089 00090 /******************************************************************************* 00091 00092 See <A HREF="http://oss.software.ibm.com/icu/apiref/utrans_8h.html"> 00093 this page</A> for full details. 00094 00095 *******************************************************************************/ 00096 00097 class UTransform : ICU 00098 { 00099 private Handle handle; 00100 00101 enum Direction 00102 { 00103 Forward, 00104 Reverse 00105 } 00106 00107 00108 /*********************************************************************** 00109 00110 ***********************************************************************/ 00111 00112 this (UText id) 00113 { 00114 Error e; 00115 00116 handle = utrans_openU (id.get(), id.len, 0, null, 0, null, e); 00117 testError (e, "failed to open ID transform"); 00118 } 00119 00120 /*********************************************************************** 00121 00122 ***********************************************************************/ 00123 00124 this (UText rule, Direction dir) 00125 { 00126 Error e; 00127 00128 handle = utrans_openU (null, 0, dir, rule.get(), rule.len, null, e); 00129 testError (e, "failed to open rule-based transform"); 00130 } 00131 00132 /*********************************************************************** 00133 00134 ***********************************************************************/ 00135 00136 ~this () 00137 { 00138 utrans_close (handle); 00139 } 00140 00141 /*********************************************************************** 00142 00143 ***********************************************************************/ 00144 00145 UText getID () 00146 { 00147 uint len; 00148 wchar *s = utrans_getUnicodeID (handle, len); 00149 return new UText (s[0..len]); 00150 } 00151 00152 /*********************************************************************** 00153 00154 ***********************************************************************/ 00155 00156 UTransform setFilter (UText filter) 00157 { 00158 Error e; 00159 00160 if (filter.length) 00161 utrans_setFilter (handle, filter.get, filter.len, e); 00162 else 00163 utrans_setFilter (handle, null, 0, e); 00164 00165 testError (e, "failed to set transform filter"); 00166 return this; 00167 } 00168 00169 /*********************************************************************** 00170 00171 ***********************************************************************/ 00172 00173 UTransform execute (UString text) 00174 { 00175 Error e; 00176 uint textLen = text.len; 00177 00178 utrans_transUChars (handle, text.get, &textLen, text.content.length, 0, &text.len, e); 00179 testError (e, "failed to execute transform"); 00180 return this; 00181 } 00182 00183 00184 00185 /*********************************************************************** 00186 00187 Bind the ICU functions from a shared library. This is 00188 complicated by the issues regarding D and DLLs on the 00189 Windows platform 00190 00191 ***********************************************************************/ 00192 00193 private static void* library; 00194 00195 /*********************************************************************** 00196 00197 ***********************************************************************/ 00198 00199 private static extern (C) 00200 { 00201 Handle function (wchar*, uint, uint, wchar*, uint, void*, inout Error) utrans_openU; 00202 void function (Handle) utrans_close; 00203 wchar* function (Handle, inout uint) utrans_getUnicodeID; 00204 void function (Handle, wchar*, uint, inout Error) utrans_setFilter; 00205 void function (Handle, wchar*, uint*, uint, uint, uint*, inout Error) utrans_transUChars; 00206 } 00207 00208 /*********************************************************************** 00209 00210 ***********************************************************************/ 00211 00212 static FunctionLoader.Bind[] targets = 00213 [ 00214 {cast(void**) &utrans_openU, "utrans_openU"}, 00215 {cast(void**) &utrans_close, "utrans_close"}, 00216 {cast(void**) &utrans_getUnicodeID, "utrans_getUnicodeID"}, 00217 {cast(void**) &utrans_setFilter, "utrans_setFilter"}, 00218 {cast(void**) &utrans_transUChars, "utrans_transUChars"}, 00219 ]; 00220 00221 /*********************************************************************** 00222 00223 ***********************************************************************/ 00224 00225 static this () 00226 { 00227 library = FunctionLoader.bind (icuin, targets); 00228 } 00229 00230 /*********************************************************************** 00231 00232 ***********************************************************************/ 00233 00234 static ~this () 00235 { 00236 FunctionLoader.unbind (library); 00237 } 00238 } 00239