00001 /******************************************************************************* 00002 00003 @file ICU.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, October 2004 00031 @author Kris 00032 00033 00034 Note that this package and documentation is built around the ICU 00035 project (http://oss.software.ibm.com/icu/). Below is the license 00036 statement as specified by that software: 00037 00038 00039 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00040 00041 00042 ICU License - ICU 1.8.1 and later 00043 00044 COPYRIGHT AND PERMISSION NOTICE 00045 00046 Copyright (c) 1995-2003 International Business Machines Corporation and 00047 others. 00048 00049 All rights reserved. 00050 00051 Permission is hereby granted, free of charge, to any person obtaining a 00052 copy of this software and associated documentation files (the 00053 "Software"), to deal in the Software without restriction, including 00054 without limitation the rights to use, copy, modify, merge, publish, 00055 distribute, and/or sell copies of the Software, and to permit persons 00056 to whom the Software is furnished to do so, provided that the above 00057 copyright notice(s) and this permission notice appear in all copies of 00058 the Software and that both the above copyright notice(s) and this 00059 permission notice appear in supporting documentation. 00060 00061 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00062 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00063 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00064 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00065 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00066 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00067 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00068 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00069 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00070 00071 Except as contained in this notice, the name of a copyright holder 00072 shall not be used in advertising or otherwise to promote the sale, use 00073 or other dealings in this Software without prior written authorization 00074 of the copyright holder. 00075 00076 ---------------------------------------------------------------------- 00077 00078 All trademarks and registered trademarks mentioned herein are the 00079 property of their respective owners. 00080 00081 *******************************************************************************/ 00082 00083 module mango.icu.ICU; 00084 00085 /******************************************************************************* 00086 00087 *******************************************************************************/ 00088 00089 private static extern (C) uint strlen (char *s); 00090 private static extern (C) uint wcslen (wchar *s); 00091 00092 /******************************************************************************* 00093 00094 Some low-level routines to help bind the ICU C-API to D. 00095 00096 See http://oss.software.ibm.com/icu/ for full documentation on the 00097 ICU package 00098 00099 *******************************************************************************/ 00100 00101 protected class ICU 00102 { 00103 /*********************************************************************** 00104 00105 Use this for the primary argument-type to most ICU functions 00106 00107 ***********************************************************************/ 00108 00109 protected typedef void* Handle; 00110 00111 /*********************************************************************** 00112 00113 ICU error codes (the ones which are referenced) 00114 00115 ***********************************************************************/ 00116 00117 protected enum Error:int 00118 { 00119 OK, 00120 BufferOverflow=15 00121 } 00122 00123 /*********************************************************************** 00124 00125 ***********************************************************************/ 00126 00127 protected static final bool isError (Error e) 00128 { 00129 return e > 0; 00130 } 00131 00132 /*********************************************************************** 00133 00134 ***********************************************************************/ 00135 00136 protected static final void exception (char[] msg) 00137 { 00138 throw new ICUException (msg); 00139 } 00140 00141 /*********************************************************************** 00142 00143 ***********************************************************************/ 00144 00145 protected static final void testError (Error e, char[] msg) 00146 { 00147 if (e > 0) 00148 exception (msg); 00149 } 00150 00151 /*********************************************************************** 00152 00153 ***********************************************************************/ 00154 00155 protected static final char* toString (char[] string) 00156 { 00157 if (! string.length) 00158 return ((cast(char*) string) ? "" : cast(char*) null); 00159 00160 if (* (&string[0] + string.length)) 00161 { 00162 // Need to make a copy 00163 char[] copy = new char [string.length + 1]; 00164 copy [0..string.length] = string; 00165 copy [string.length] = 0; 00166 string = copy; 00167 } 00168 return string; 00169 } 00170 00171 /*********************************************************************** 00172 00173 ***********************************************************************/ 00174 00175 protected static final wchar* toString (wchar[] string) 00176 { 00177 return cast(wchar*) toString (cast(char[]) string); 00178 } 00179 00180 /*********************************************************************** 00181 00182 ***********************************************************************/ 00183 00184 protected static final uint length (char* s) 00185 { 00186 return strlen (s); 00187 } 00188 00189 /*********************************************************************** 00190 00191 ***********************************************************************/ 00192 00193 protected static final uint length (wchar* s) 00194 { 00195 return wcslen (s); 00196 } 00197 00198 /*********************************************************************** 00199 00200 ***********************************************************************/ 00201 00202 protected static final char[] toArray (char* s) 00203 { 00204 if (s) 00205 return s[0..strlen (s)]; 00206 return null; 00207 } 00208 00209 /*********************************************************************** 00210 00211 ***********************************************************************/ 00212 00213 protected static final wchar[] toArray (wchar* s) 00214 { 00215 if (s) 00216 return s[0..wcslen (s)]; 00217 return null; 00218 } 00219 } 00220 00221 00222 /******************************************************************************* 00223 00224 *******************************************************************************/ 00225 00226 class ICUException : Exception 00227 { 00228 /*********************************************************************** 00229 00230 Construct exception with the provided text string 00231 00232 ***********************************************************************/ 00233 00234 this (char[] msg) 00235 { 00236 super (msg); 00237 } 00238 } 00239 00240 00241 /******************************************************************************* 00242 00243 *******************************************************************************/ 00244 00245 class FunctionLoader 00246 { 00247 version (Win32) 00248 { 00249 private import std.c.windows.windows; 00250 00251 /*************************************************************** 00252 00253 ***************************************************************/ 00254 00255 protected struct Bind 00256 { 00257 void** fnc; 00258 char[] name; 00259 } 00260 00261 /*************************************************************** 00262 00263 ***************************************************************/ 00264 00265 static final void* bind (char[] library, inout Bind[] targets) 00266 { 00267 static char[] vrsn = "_3_0\0"; 00268 00269 HANDLE lib = LoadLibraryA (ICU.toString(library)); 00270 00271 foreach (Bind b; targets) 00272 { 00273 char[] name = b.name ~ vrsn; 00274 *b.fnc = GetProcAddress (lib, name); 00275 if (*b.fnc) 00276 {}//printf ("bound '%.*s'\n", name); 00277 else 00278 throw new Error ("required " ~ name ~ " in library " ~ library); 00279 } 00280 return lib; 00281 } 00282 00283 /*************************************************************** 00284 00285 ***************************************************************/ 00286 00287 static final void unbind (void* library) 00288 { 00289 //FreeLibrary (cast(HANDLE) library); 00290 } 00291 } 00292 00293 00294 /*********************************************************************** 00295 00296 ***********************************************************************/ 00297 00298 version (linux) 00299 { 00300 static this () 00301 { 00302 throw new Exception ("ICU is currently unsupported on linux"); 00303 } 00304 } 00305 } 00306 00307