scuffed-code/icu4c/source/extra/ustdio/loccache.c

89 lines
2.3 KiB
C
Raw Normal View History

1999-08-16 21:50:52 +00:00
/*
*******************************************************************************
*
* Copyright (C) 1998-1999, International Business Machines
* Corporation and others. All Rights Reserved.
*
1999-08-16 21:50:52 +00:00
*******************************************************************************
*
* File loccache.c
*
* Modification History:
*
* Date Name Description
* 11/18/98 stephen Creation.
* 03/11/99 stephen Modified for new C API.
* 06/16/99 stephen Added #include for uloc.h
*******************************************************************************
*/
#include "loccache.h"
#include "uhash.h"
#include "unicode/uloc.h"
1999-08-16 21:50:52 +00:00
#include "umutex.h"
/* The global cache */
UHashtable *gLocaleCache = 0;
ULocaleBundle*
u_loccache_get(const char *loc)
{
ULocaleBundle *result;
ULocaleBundle *tempBundle;
/*Mutex *lock;*/
UHashtable *tempCache;
int32_t locCount;
UErrorCode status = U_ZERO_ERROR;
1999-08-16 21:50:52 +00:00
/* Create the cache, if needed */
if(gLocaleCache == 0) {
locCount = uloc_countAvailable();
tempCache = uhash_openSize(uhash_hashChars, uhash_compareChars, locCount, &status);
if(U_FAILURE(status)) return 0;
1999-08-16 21:50:52 +00:00
/* Lock the cache */
umtx_lock(0);
/* Make sure it didn't change while we were acquiring the lock */
if(gLocaleCache == 0) {
gLocaleCache = tempCache;
}
else {
uhash_close(tempCache);
}
/* Unlock the cache */
umtx_unlock(0);
}
/* Try and get the bundle from the cache */
/* This will be slightly wasteful the first time around, */
/* since we know the cache will be empty. But, it simplifies */
/* the code a great deal. */
2000-08-28 21:36:13 +00:00
result = (ULocaleBundle*)uhash_get(gLocaleCache, loc);
1999-08-16 21:50:52 +00:00
/* If the bundle wasn't found, create it and add it to the cache */
if(result == 0) {
/* Create the bundle */
tempBundle = u_locbund_new(loc);
/* Lock the cache */
umtx_lock(0);
/* Make sure the cache didn't change while we were locking it */
2000-08-28 21:36:13 +00:00
result = (ULocaleBundle*)uhash_get(gLocaleCache, loc);
1999-08-16 21:50:52 +00:00
if(result == 0) {
result = tempBundle;
uhash_put(gLocaleCache, tempBundle->fLocale, tempBundle, &status);
1999-08-16 21:50:52 +00:00
}
else
u_locbund_delete(tempBundle);
/* Unlock the cache */
umtx_unlock(0);
}
return result;
}