| 1 | //  AprSharp, a wrapper library around the Apache Portable Runtime Library | 
|---|
| 2 | #region Copyright (C) 2004 SOFTEC sa. | 
|---|
| 3 | // | 
|---|
| 4 | //  This library is free software; you can redistribute it and/or | 
|---|
| 5 | //  modify it under the terms of the GNU Lesser General Public | 
|---|
| 6 | //  License as published by the Free Software Foundation; either | 
|---|
| 7 | //  version 2.1 of the License, or (at your option) any later version. | 
|---|
| 8 | // | 
|---|
| 9 | //  This library is distributed in the hope that it will be useful, | 
|---|
| 10 | //  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 
|---|
| 12 | //  Lesser General Public License for more details. | 
|---|
| 13 | // | 
|---|
| 14 | //  You should have received a copy of the GNU Lesser General Public | 
|---|
| 15 | //  License along with this library; if not, write to the Free Software | 
|---|
| 16 | //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
|---|
| 17 | // | 
|---|
| 18 | //  Sources, support options and lastest version of the complete library | 
|---|
| 19 | //  is available from: | 
|---|
| 20 | //              http://www.softec.st/AprSharp | 
|---|
| 21 | // | 
|---|
| 22 | // | 
|---|
| 23 | //  Initial authors : | 
|---|
| 24 | //              Denis Gervalle | 
|---|
| 25 | //              Olivier Desaive | 
|---|
| 26 | #endregion | 
|---|
| 27 | // | 
|---|
| 28 | using System; | 
|---|
| 29 | using System.Diagnostics; | 
|---|
| 30 | using System.Runtime.InteropServices; | 
|---|
| 31 | using System.Collections; | 
|---|
| 32 |  | 
|---|
| 33 | namespace Softec.AprSharp | 
|---|
| 34 | { | 
|---|
| 35 | public struct AprHash : IEnumerable, ICollection, IAprUnmanaged | 
|---|
| 36 | { | 
|---|
| 37 | IntPtr mHash; | 
|---|
| 38 |  | 
|---|
| 39 | #region Generic embedding functions of an IntPtr | 
|---|
| 40 | public AprHash(IntPtr ptr) | 
|---|
| 41 | { | 
|---|
| 42 | mHash = ptr; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | public bool IsNull | 
|---|
| 46 | { | 
|---|
| 47 | get | 
|---|
| 48 | { | 
|---|
| 49 | return( mHash == IntPtr.Zero ); | 
|---|
| 50 | } | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | private void CheckPtr() | 
|---|
| 54 | { | 
|---|
| 55 | if( IsNull ) | 
|---|
| 56 | throw new AprNullReferenceException(); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | public void ClearPtr() | 
|---|
| 60 | { | 
|---|
| 61 | mHash = IntPtr.Zero; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | public IntPtr ToIntPtr() | 
|---|
| 65 | { | 
|---|
| 66 | return mHash; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | public bool ReferenceEquals(IAprUnmanaged obj) | 
|---|
| 70 | { | 
|---|
| 71 | return(obj.ToIntPtr() == ToIntPtr()); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | public static implicit operator IntPtr(AprHash hashIndex) | 
|---|
| 75 | { | 
|---|
| 76 | return hashIndex.mHash; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | public static implicit operator AprHash(IntPtr ptr) | 
|---|
| 80 | { | 
|---|
| 81 | return new AprHash(ptr); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | public override string ToString() | 
|---|
| 85 | { | 
|---|
| 86 | return("[apr_hash_t:"+mHash.ToInt32().ToString("X")+"]"); | 
|---|
| 87 | } | 
|---|
| 88 | #endregion | 
|---|
| 89 |  | 
|---|
| 90 | #region Methods wrappers | 
|---|
| 91 | public static AprHash Make(AprPool pool) | 
|---|
| 92 | { | 
|---|
| 93 | IntPtr ptr; | 
|---|
| 94 |  | 
|---|
| 95 | Debug.Write(String.Format("apr_hash_make({0})...",pool)); | 
|---|
| 96 | ptr = Apr.apr_hash_make(pool); | 
|---|
| 97 | if(ptr == IntPtr.Zero ) | 
|---|
| 98 | throw new AprException("apr_hash_make: Can't create an apr_hash_t"); | 
|---|
| 99 | Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); | 
|---|
| 100 |  | 
|---|
| 101 | return(ptr); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | public AprHash Copy(AprPool pool) | 
|---|
| 105 | { | 
|---|
| 106 | IntPtr ptr; | 
|---|
| 107 |  | 
|---|
| 108 | CheckPtr(); | 
|---|
| 109 | Debug.Write(String.Format("apr_hash_copy({0},{1})...",pool,this)); | 
|---|
| 110 | ptr = Apr.apr_hash_copy(pool,mHash); | 
|---|
| 111 | if(ptr == IntPtr.Zero ) | 
|---|
| 112 | throw new AprException("apr_hash_copy: Can't copy an apr_hash_t"); | 
|---|
| 113 | Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); | 
|---|
| 114 |  | 
|---|
| 115 | return(ptr); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | public AprHash Overlay(AprPool pool, AprHash h) | 
|---|
| 119 | { | 
|---|
| 120 | IntPtr ptr; | 
|---|
| 121 |  | 
|---|
| 122 | CheckPtr(); | 
|---|
| 123 | Debug.Write(String.Format("apr_hash_overlay({0},{1},{2})...",pool,h,this)); | 
|---|
| 124 | ptr = Apr.apr_hash_overlay(pool,h,mHash); | 
|---|
| 125 | if(ptr == IntPtr.Zero ) | 
|---|
| 126 | throw new AprException("apr_hash_overlay: Can't overlay an apr_hash_t"); | 
|---|
| 127 | Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); | 
|---|
| 128 |  | 
|---|
| 129 | return(ptr); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | public void Set(string key, string value) | 
|---|
| 133 | { | 
|---|
| 134 | AprString aprKey = new AprString(key, Pool); | 
|---|
| 135 | AprString aprValue = new AprString(value, Pool); | 
|---|
| 136 | Set((IntPtr)aprKey, -1, (IntPtr)aprValue); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | public void Set(string key, IntPtr value) | 
|---|
| 140 | { | 
|---|
| 141 | AprString aprKey = new AprString(key, Pool); | 
|---|
| 142 | Set((IntPtr)aprKey, -1, value); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | public void Set(AprString key, IntPtr value) | 
|---|
| 146 | { | 
|---|
| 147 | Set((IntPtr)key, -1, value); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | public void Set(AprHashEntry item) | 
|---|
| 151 | { | 
|---|
| 152 | Set(item.Key, item.KeySize, item.Value); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | public void Set(IntPtr key, int size, IntPtr value) | 
|---|
| 156 | { | 
|---|
| 157 | CheckPtr(); | 
|---|
| 158 | Debug.WriteLine(String.Format("apr_hash_set({0},{1:X},{2},{3:X})",this,key.ToInt32(),size,value.ToInt32())); | 
|---|
| 159 | Apr.apr_hash_set(mHash, key, size, value); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | public string GetAsString(string key) | 
|---|
| 163 | { | 
|---|
| 164 | AprString aprKey = new AprString(key, Pool); | 
|---|
| 165 | return(GetAsString((IntPtr)aprKey,-1)); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | public string GetAsString(AprString key) | 
|---|
| 169 | { | 
|---|
| 170 | return(GetAsString((IntPtr)key,-1)); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | public string GetAsString(IntPtr key, int size) | 
|---|
| 174 | { | 
|---|
| 175 | return(((AprString)Get(key,size)).ToString()); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | public IntPtr Get(string key) | 
|---|
| 179 | { | 
|---|
| 180 | AprString aprKey = new AprString(key, Pool); | 
|---|
| 181 | return(Get((IntPtr)aprKey,-1)); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | public IntPtr Get(AprString key) | 
|---|
| 185 | { | 
|---|
| 186 | return(Get((IntPtr)key,-1)); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | public IntPtr Get(AprHashEntry item) | 
|---|
| 190 | { | 
|---|
| 191 | return(Get(item.Key,item.KeySize)); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | public IntPtr Get(IntPtr key, int size) | 
|---|
| 195 | { | 
|---|
| 196 | IntPtr ptr; | 
|---|
| 197 |  | 
|---|
| 198 | CheckPtr(); | 
|---|
| 199 | Debug.Write(String.Format("apr_hash_get({0},{1:X},{2})...",this,key.ToInt32(),size)); | 
|---|
| 200 | ptr = Apr.apr_hash_get(mHash,key,size); | 
|---|
| 201 | Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); | 
|---|
| 202 |  | 
|---|
| 203 | return(ptr); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | public AprHashIndex First(AprPool pool) | 
|---|
| 207 | { | 
|---|
| 208 | return(AprHashIndex.First(pool,this)); | 
|---|
| 209 | } | 
|---|
| 210 | #endregion | 
|---|
| 211 |  | 
|---|
| 212 | #region Wrapper Properties | 
|---|
| 213 | public AprPool Pool | 
|---|
| 214 | { | 
|---|
| 215 | get { | 
|---|
| 216 | Debug.WriteLine(String.Format("apr_hash_pool_get({0:X})",this)); | 
|---|
| 217 | return(Apr.apr_hash_pool_get(mHash)); | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | [CLSCompliant(false)] | 
|---|
| 222 | public uint NativeCount | 
|---|
| 223 | { | 
|---|
| 224 | get { | 
|---|
| 225 | Debug.WriteLine(String.Format("apr_hash_count({0:X})",this)); | 
|---|
| 226 | return(Apr.apr_hash_count(mHash)); | 
|---|
| 227 | } | 
|---|
| 228 | } | 
|---|
| 229 | #endregion | 
|---|
| 230 |  | 
|---|
| 231 | #region ICollection | 
|---|
| 232 | public void CopyTo(Array array, int arrayIndex) | 
|---|
| 233 | { | 
|---|
| 234 | if(null == array) | 
|---|
| 235 | throw new AprArgumentNullException("array"); | 
|---|
| 236 | if(arrayIndex < 0 || arrayIndex > array.Length) | 
|---|
| 237 | throw new AprArgumentOutOfRangeException("arrayIndex"); | 
|---|
| 238 | if(array.Rank > 1) | 
|---|
| 239 | throw new AprArgumentException("array is multidimensional"); | 
|---|
| 240 | if((array.Length - arrayIndex) < Count) | 
|---|
| 241 | throw new AprArgumentException("Not enough room from arrayIndex to end of array for this AprHash"); | 
|---|
| 242 |  | 
|---|
| 243 | int i = arrayIndex; | 
|---|
| 244 | IEnumerator it = GetEnumerator(); | 
|---|
| 245 | while(it.MoveNext()) { | 
|---|
| 246 | array.SetValue(it.Current, i++); | 
|---|
| 247 | } | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | public bool IsSynchronized | 
|---|
| 251 | { | 
|---|
| 252 | get | 
|---|
| 253 | { | 
|---|
| 254 | return false; | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | public object SyncRoot | 
|---|
| 259 | { | 
|---|
| 260 | get | 
|---|
| 261 | { | 
|---|
| 262 | return this; | 
|---|
| 263 | } | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | public int Count | 
|---|
| 267 | { | 
|---|
| 268 | get | 
|---|
| 269 | { | 
|---|
| 270 | Debug.WriteLine(String.Format("apr_hash_count({0:X})",this)); | 
|---|
| 271 | return(unchecked((int)Apr.apr_hash_count(mHash))); | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 | #endregion | 
|---|
| 275 |  | 
|---|
| 276 | #region IEnumerable | 
|---|
| 277 | public IEnumerator GetEnumerator() | 
|---|
| 278 | { | 
|---|
| 279 | return (IEnumerator) new AprHashEnumerator(this, Pool); | 
|---|
| 280 | } | 
|---|
| 281 | #endregion | 
|---|
| 282 |  | 
|---|
| 283 | public IEnumerator GetEnumerator(AprPool pool) | 
|---|
| 284 | { | 
|---|
| 285 | return (IEnumerator) new AprHashEnumerator(this, pool); | 
|---|
| 286 | } | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | // AprHashItem Class | 
|---|
| 290 | public struct AprHashEntry | 
|---|
| 291 | { | 
|---|
| 292 | private AprPool mPool; | 
|---|
| 293 | internal IntPtr mKey; | 
|---|
| 294 | internal int mKeySize; | 
|---|
| 295 | internal IntPtr mValue; | 
|---|
| 296 |  | 
|---|
| 297 | public AprHashEntry(AprPool pool) | 
|---|
| 298 | { | 
|---|
| 299 | mPool = pool; | 
|---|
| 300 | mKey = IntPtr.Zero; | 
|---|
| 301 | mKeySize = 0; | 
|---|
| 302 | mValue = IntPtr.Zero; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | public AprPool Pool | 
|---|
| 306 | { | 
|---|
| 307 | get | 
|---|
| 308 | { | 
|---|
| 309 | return mPool; | 
|---|
| 310 | } | 
|---|
| 311 | set | 
|---|
| 312 | { | 
|---|
| 313 | mPool = value; | 
|---|
| 314 | } | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | public IntPtr Key | 
|---|
| 318 | { | 
|---|
| 319 | get | 
|---|
| 320 | { | 
|---|
| 321 | return mKey; | 
|---|
| 322 | } | 
|---|
| 323 | set | 
|---|
| 324 | { | 
|---|
| 325 | mKey = value; | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | public int KeySize | 
|---|
| 330 | { | 
|---|
| 331 | get | 
|---|
| 332 | { | 
|---|
| 333 | return mKeySize; | 
|---|
| 334 | } | 
|---|
| 335 | set | 
|---|
| 336 | { | 
|---|
| 337 | mKeySize = value; | 
|---|
| 338 | } | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | public IntPtr Value | 
|---|
| 342 | { | 
|---|
| 343 | get | 
|---|
| 344 | { | 
|---|
| 345 | return mValue; | 
|---|
| 346 | } | 
|---|
| 347 | set | 
|---|
| 348 | { | 
|---|
| 349 | mValue = value; | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | public string KeyAsString | 
|---|
| 354 | { | 
|---|
| 355 | get | 
|---|
| 356 | { | 
|---|
| 357 | return Marshal.PtrToStringAnsi(mKey); | 
|---|
| 358 | } | 
|---|
| 359 | set | 
|---|
| 360 | { | 
|---|
| 361 | if( mPool.IsNull ) | 
|---|
| 362 | throw new AprNullReferenceException(); | 
|---|
| 363 | mKey = new AprString(value, mPool); | 
|---|
| 364 | mKeySize = -1; | 
|---|
| 365 | } | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | public string ValueAsString | 
|---|
| 369 | { | 
|---|
| 370 | get | 
|---|
| 371 | { | 
|---|
| 372 | return Marshal.PtrToStringAnsi(mValue); | 
|---|
| 373 | } | 
|---|
| 374 | set | 
|---|
| 375 | { | 
|---|
| 376 | if( mPool.IsNull ) | 
|---|
| 377 | throw new AprNullReferenceException(); | 
|---|
| 378 | mValue = new AprString(value, mPool); | 
|---|
| 379 | } | 
|---|
| 380 | } | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 | // AprHashEnumerator Class | 
|---|
| 384 | public class AprHashEnumerator : IEnumerator | 
|---|
| 385 | { | 
|---|
| 386 | private AprPool mPool; | 
|---|
| 387 | private AprHash mHash; | 
|---|
| 388 | private AprHashIndex mHashIndex; | 
|---|
| 389 | private bool reset; | 
|---|
| 390 |  | 
|---|
| 391 | public AprHashEnumerator( AprHash h ) | 
|---|
| 392 | { | 
|---|
| 393 | mHash = h; | 
|---|
| 394 | mPool = h.Pool; | 
|---|
| 395 | reset = true; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | public AprHashEnumerator( AprHash h, AprPool pool ) | 
|---|
| 399 | { | 
|---|
| 400 | mHash = h; | 
|---|
| 401 | mPool = pool; | 
|---|
| 402 | reset = true; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | #region IEnumerator | 
|---|
| 406 | public bool MoveNext() | 
|---|
| 407 | { | 
|---|
| 408 | if(reset) | 
|---|
| 409 | { | 
|---|
| 410 | mHashIndex = AprHashIndex.First(mPool, mHash); | 
|---|
| 411 | reset = false; | 
|---|
| 412 | } | 
|---|
| 413 | else if(!mHashIndex.IsNull) | 
|---|
| 414 | { | 
|---|
| 415 | mHashIndex.Next(); | 
|---|
| 416 | } | 
|---|
| 417 | return(!mHashIndex.IsNull); | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|
| 420 | public void Reset() | 
|---|
| 421 | { | 
|---|
| 422 | mHashIndex.ClearPtr(); | 
|---|
| 423 | reset = true; | 
|---|
| 424 | } | 
|---|
| 425 |  | 
|---|
| 426 | public object Current | 
|---|
| 427 | { | 
|---|
| 428 | get | 
|---|
| 429 | { | 
|---|
| 430 | AprHashEntry entry = new AprHashEntry(mPool); | 
|---|
| 431 |  | 
|---|
| 432 | mHashIndex.This(out entry.mKey, out entry.mKeySize, out entry.mValue); | 
|---|
| 433 | return(entry); | 
|---|
| 434 | } | 
|---|
| 435 | } | 
|---|
| 436 | #endregion | 
|---|
| 437 | } | 
|---|
| 438 | } | 
|---|