home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / extra / ustdio / loccache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  2.8 KB  |  101 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File loccache.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   11/18/98    stephen     Creation.
  18. *   03/11/99    stephen     Modified for new C API.
  19. *   06/16/99    stephen     Added #include for uloc.h
  20. *******************************************************************************
  21. */
  22.  
  23. #include "loccache.h"
  24.  
  25. #include "uloc.h"
  26. #include "umutex.h"
  27.  
  28. /* The global cache */
  29. UHashtable     *gLocaleCache         = 0;
  30.  
  31. /* my custom hashing function */
  32. int32_t
  33. u_locbund_hash(const void *parm)
  34. {
  35.   return uhash_hashString(((ULocaleBundle*)parm)->fLocale);
  36. }
  37.  
  38. ULocaleBundle*
  39. u_loccache_get(const char *loc)
  40. {
  41.   ULocaleBundle *result;
  42.   ULocaleBundle *tempBundle;
  43.   /*Mutex     *lock;*/
  44.   UHashtable     *tempCache;
  45.   int32_t     locCount;
  46.   int32_t     hashKey;
  47.   UErrorCode     status = U_ZERO_ERROR;
  48.  
  49.   /* Create the cache, if needed */
  50.   if(gLocaleCache == 0) {
  51.     locCount = uloc_countAvailable();
  52.     
  53.     tempCache = uhash_openSize(u_locbund_hash, locCount, &status);
  54.     if(U_FAILURE(status)) return 0;
  55.     
  56.     /* Lock the cache */
  57.     umtx_lock(0);
  58.     /* Make sure it didn't change while we were acquiring the lock */
  59.     if(gLocaleCache == 0) {
  60.       gLocaleCache = tempCache;
  61.     }
  62.     else {
  63.       uhash_close(tempCache);
  64.     }
  65.     
  66.     /* Unlock the cache */
  67.     umtx_unlock(0);
  68.   }
  69.   
  70.   /* Try and get the bundle from the cache */
  71.   /* This will be slightly wasteful the first time around, */
  72.   /* since we know the cache will be empty.  But, it simplifies */
  73.   /* the code a great deal. */
  74.   
  75.   hashKey = uhash_hashString(loc);
  76.   result = uhash_get(gLocaleCache, hashKey);
  77.  
  78.   /* If the bundle wasn't found, create it and add it to the cache */
  79.   if(result == 0) {
  80.     /* Create the bundle */
  81.     tempBundle = u_locbund_new(loc);
  82.  
  83.     /* Lock the cache */
  84.     umtx_lock(0);
  85.     
  86.     /* Make sure the cache didn't change while we were locking it */
  87.     result = uhash_get(gLocaleCache, hashKey);
  88.     if(result == 0) {
  89.       result = tempBundle;
  90.       uhash_put(gLocaleCache, result, &status);
  91.     }
  92.     else
  93.       u_locbund_delete(tempBundle);
  94.     
  95.     /* Unlock the cache */
  96.     umtx_unlock(0);
  97.   }
  98.   
  99.   return result;
  100. }
  101.