Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

loccache.c

00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1998-1999, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *
00009 * File loccache.c
00010 *
00011 * Modification History:
00012 *
00013 *   Date        Name        Description
00014 *   11/18/98    stephen     Creation.
00015 *   03/11/99    stephen     Modified for new C API.
00016 *   06/16/99    stephen     Added #include for uloc.h
00017 *******************************************************************************
00018 */
00019 
00020 #include "loccache.h"
00021 #include "uhash.h"
00022 #include "unicode/uloc.h"
00023 #include "umutex.h"
00024 
00025 /* The global cache */
00026 UHashtable     *gLocaleCache         = 0;
00027 
00028 ULocaleBundle*
00029 u_loccache_get(const char *loc)
00030 {
00031   ULocaleBundle *result;
00032   ULocaleBundle *tempBundle;
00033   /*Mutex     *lock;*/
00034   UHashtable     *tempCache;
00035   int32_t     locCount;
00036   UErrorCode     status = U_ZERO_ERROR;
00037 
00038   /* Create the cache, if needed */
00039   if(gLocaleCache == 0) {
00040     locCount = uloc_countAvailable();
00041     
00042     tempCache = uhash_openSize(uhash_hashChars, uhash_compareChars, locCount, &status);
00043     if(U_FAILURE(status)) return 0;
00044     
00045     /* Lock the cache */
00046     umtx_lock(0);
00047     /* Make sure it didn't change while we were acquiring the lock */
00048     if(gLocaleCache == 0) {
00049       gLocaleCache = tempCache;
00050     }
00051     else {
00052       uhash_close(tempCache);
00053     }
00054     
00055     /* Unlock the cache */
00056     umtx_unlock(0);
00057   }
00058   
00059   /* Try and get the bundle from the cache */
00060   /* This will be slightly wasteful the first time around, */
00061   /* since we know the cache will be empty.  But, it simplifies */
00062   /* the code a great deal. */
00063   
00064   result = (ULocaleBundle*)uhash_get(gLocaleCache, loc);
00065 
00066   /* If the bundle wasn't found, create it and add it to the cache */
00067   if(result == 0) {
00068     /* Create the bundle */
00069     tempBundle = u_locbund_new(loc);
00070 
00071     /* Lock the cache */
00072     umtx_lock(0);
00073     
00074     /* Make sure the cache didn't change while we were locking it */
00075     result = (ULocaleBundle*)uhash_get(gLocaleCache, loc);
00076     if(result == 0) {
00077       result = tempBundle;
00078       uhash_put(gLocaleCache, tempBundle->fLocale, tempBundle, &status);
00079     }
00080     else
00081       u_locbund_delete(tempBundle);
00082     
00083     /* Unlock the cache */
00084     umtx_unlock(0);
00085   }
00086   
00087   return result;
00088 }

Generated at Tue Dec 5 10:47:47 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000