00001 /******************************************************************************* 00002 00003 @file UEnumeration.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.UEnumeration; 00086 00087 private import mango.icu.ICU; 00088 00089 /******************************************************************************* 00090 00091 UEnumeration is returned by a number of ICU classes, for providing 00092 access to such things as ULocale lists and so on, 00093 00094 *******************************************************************************/ 00095 00096 class UEnumeration : ICU 00097 { 00098 package Handle handle; 00099 00100 /*********************************************************************** 00101 00102 ***********************************************************************/ 00103 00104 this (Handle handle) 00105 { 00106 this.handle = handle; 00107 } 00108 00109 /*********************************************************************** 00110 00111 Disposes of the storage used by a UEnumeration object 00112 00113 ***********************************************************************/ 00114 00115 ~this () 00116 { 00117 uenum_close (handle); 00118 } 00119 00120 /*********************************************************************** 00121 00122 Returns the next element in the iterator's list. 00123 00124 If there are no more elements, returns NULL. If the 00125 iterator is out-of-sync with its service, status is 00126 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned. 00127 If the native service string is a UChar* string, it 00128 is converted to char* with the invariant converter. 00129 The result is terminated by (char)0. If the conversion 00130 fails (because a character cannot be converted) then 00131 status is set to U_INVARIANT_CONVERSION_ERROR and the 00132 return value is undefined (but non-NULL). 00133 00134 ***********************************************************************/ 00135 00136 uint count () 00137 { 00138 Error e; 00139 00140 uint x = uenum_count (handle, e); 00141 testError (e, "enumeration out of sync"); 00142 return x; 00143 } 00144 00145 /*********************************************************************** 00146 00147 Resets the iterator to the current list of service IDs. 00148 00149 This re-establishes sync with the service and rewinds 00150 the iterator to start at the first element 00151 00152 ***********************************************************************/ 00153 00154 void reset () 00155 { 00156 ICU.Error e; 00157 00158 uenum_reset (handle, e); 00159 testError (e, "failed to reset enumeration"); 00160 } 00161 00162 /*********************************************************************** 00163 00164 Returns the next element in the iterator's list. 00165 00166 If there are no more elements, returns NULL. If the 00167 iterator is out-of-sync with its service, status is 00168 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned. 00169 If the native service string is a char* string, it is 00170 converted to UChar* with the invariant converter. 00171 00172 ***********************************************************************/ 00173 00174 bool next (out char[] dst) 00175 { 00176 ICU.Error e; 00177 uint len; 00178 00179 char* p = uenum_next (handle, &len, e); 00180 testError (e, "failed to traverse enumeration"); 00181 if (p) 00182 return dst = p[0..len], true; 00183 return false; 00184 } 00185 00186 /*********************************************************************** 00187 00188 Returns the next element in the iterator's list. 00189 00190 If there are no more elements, returns NULL. If the 00191 iterator is out-of-sync with its service, status is 00192 set to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned. 00193 If the native service string is a char* string, it is 00194 converted to UChar* with the invariant converter. 00195 00196 ***********************************************************************/ 00197 00198 bool next (inout wchar[] dst) 00199 { 00200 ICU.Error e; 00201 uint len; 00202 00203 wchar* p = uenum_unext (handle, &len, e); 00204 testError (e, "failed to traverse enumeration"); 00205 if (p) 00206 return dst = p[0..len], true; 00207 return false; 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 private static void* library; 00220 00221 /*********************************************************************** 00222 00223 ***********************************************************************/ 00224 00225 private static extern (C) 00226 { 00227 void function (Handle) uenum_close; 00228 uint function (Handle, inout Error) uenum_count; 00229 void function (Handle, inout Error) uenum_reset; 00230 char* function (Handle, uint*, inout Error) uenum_next; 00231 wchar* function (Handle, uint*, inout Error) uenum_unext; 00232 } 00233 00234 /*********************************************************************** 00235 00236 ***********************************************************************/ 00237 00238 static FunctionLoader.Bind[] targets = 00239 [ 00240 {cast(void**) &uenum_close, "uenum_close"}, 00241 {cast(void**) &uenum_count, "uenum_count"}, 00242 {cast(void**) &uenum_reset, "uenum_reset"}, 00243 {cast(void**) &uenum_next, "uenum_next"}, 00244 {cast(void**) &uenum_unext, "uenum_unext"}, 00245 ]; 00246 00247 /*********************************************************************** 00248 00249 ***********************************************************************/ 00250 00251 static this () 00252 { 00253 library = FunctionLoader.bind (icuuc, targets); 00254 } 00255 00256 /*********************************************************************** 00257 00258 ***********************************************************************/ 00259 00260 static ~this () 00261 { 00262 FunctionLoader.unbind (library); 00263 } 00264 }