michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2008-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * michael@0: * File GENDER.CPP michael@0: * michael@0: * Modification History:* michael@0: * Date Name Description michael@0: * michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/gender.h" michael@0: #include "unicode/ugender.h" michael@0: #include "unicode/ures.h" michael@0: michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "mutex.h" michael@0: #include "uassert.h" michael@0: #include "ucln_in.h" michael@0: #include "umutex.h" michael@0: #include "uhash.h" michael@0: michael@0: static UHashtable* gGenderInfoCache = NULL; michael@0: static UMutex gGenderMetaLock = U_MUTEX_INITIALIZER; michael@0: static const char* gNeutralStr = "neutral"; michael@0: static const char* gMailTaintsStr = "maleTaints"; michael@0: static const char* gMixedNeutralStr = "mixedNeutral"; michael@0: static icu::GenderInfo* gObjs = NULL; michael@0: static icu::UInitOnce gGenderInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: enum GenderStyle { michael@0: NEUTRAL, michael@0: MIXED_NEUTRAL, michael@0: MALE_TAINTS, michael@0: GENDER_STYLE_LENGTH michael@0: }; michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: static UBool U_CALLCONV gender_cleanup(void) { michael@0: if (gGenderInfoCache != NULL) { michael@0: uhash_close(gGenderInfoCache); michael@0: gGenderInfoCache = NULL; michael@0: delete [] gObjs; michael@0: } michael@0: gGenderInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: void U_CALLCONV GenderInfo_initCache(UErrorCode &status) { michael@0: ucln_i18n_registerCleanup(UCLN_I18N_GENDERINFO, gender_cleanup); michael@0: U_ASSERT(gGenderInfoCache == NULL); michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: gObjs = new GenderInfo[GENDER_STYLE_LENGTH]; michael@0: if (gObjs == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: for (int i = 0; i < GENDER_STYLE_LENGTH; i++) { michael@0: gObjs[i]._style = i; michael@0: } michael@0: gGenderInfoCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status); michael@0: if (U_FAILURE(status)) { michael@0: delete [] gObjs; michael@0: return; michael@0: } michael@0: uhash_setKeyDeleter(gGenderInfoCache, uprv_free); michael@0: } michael@0: michael@0: michael@0: GenderInfo::GenderInfo() { michael@0: } michael@0: michael@0: GenderInfo::~GenderInfo() { michael@0: } michael@0: michael@0: const GenderInfo* GenderInfo::getInstance(const Locale& locale, UErrorCode& status) { michael@0: // Make sure our cache exists. michael@0: umtx_initOnce(gGenderInitOnce, &GenderInfo_initCache, status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: const GenderInfo* result = NULL; michael@0: const char* key = locale.getName(); michael@0: { michael@0: Mutex lock(&gGenderMetaLock); michael@0: result = (const GenderInfo*) uhash_get(gGenderInfoCache, key); michael@0: } michael@0: if (result) { michael@0: return result; michael@0: } michael@0: michael@0: // On cache miss, try to create GenderInfo from CLDR data michael@0: result = loadInstance(locale, status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: // Try to put our GenderInfo object in cache. If there is a race condition, michael@0: // favor the GenderInfo object that is already in the cache. michael@0: { michael@0: Mutex lock(&gGenderMetaLock); michael@0: GenderInfo* temp = (GenderInfo*) uhash_get(gGenderInfoCache, key); michael@0: if (temp) { michael@0: result = temp; michael@0: } else { michael@0: uhash_put(gGenderInfoCache, uprv_strdup(key), (void*) result, &status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: const GenderInfo* GenderInfo::loadInstance(const Locale& locale, UErrorCode& status) { michael@0: LocalUResourceBundlePointer rb( michael@0: ures_openDirect(NULL, "genderList", &status)); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), "genderList", NULL, &status)); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: int32_t resLen = 0; michael@0: const char* curLocaleName = locale.getName(); michael@0: UErrorCode key_status = U_ZERO_ERROR; michael@0: const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &key_status); michael@0: if (s == NULL) { michael@0: key_status = U_ZERO_ERROR; michael@0: char parentLocaleName[ULOC_FULLNAME_CAPACITY]; michael@0: uprv_strcpy(parentLocaleName, curLocaleName); michael@0: while (s == NULL && uloc_getParent(parentLocaleName, parentLocaleName, ULOC_FULLNAME_CAPACITY, &key_status) > 0) { michael@0: key_status = U_ZERO_ERROR; michael@0: resLen = 0; michael@0: s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &key_status); michael@0: key_status = U_ZERO_ERROR; michael@0: } michael@0: } michael@0: if (s == NULL) { michael@0: return &gObjs[NEUTRAL]; michael@0: } michael@0: char type_str[256]; michael@0: u_UCharsToChars(s, type_str, resLen + 1); michael@0: if (uprv_strcmp(type_str, gNeutralStr) == 0) { michael@0: return &gObjs[NEUTRAL]; michael@0: } michael@0: if (uprv_strcmp(type_str, gMixedNeutralStr) == 0) { michael@0: return &gObjs[MIXED_NEUTRAL]; michael@0: } michael@0: if (uprv_strcmp(type_str, gMailTaintsStr) == 0) { michael@0: return &gObjs[MALE_TAINTS]; michael@0: } michael@0: return &gObjs[NEUTRAL]; michael@0: } michael@0: michael@0: UGender GenderInfo::getListGender(const UGender* genders, int32_t length, UErrorCode& status) const { michael@0: if (U_FAILURE(status)) { michael@0: return UGENDER_OTHER; michael@0: } michael@0: if (length == 0) { michael@0: return UGENDER_OTHER; michael@0: } michael@0: if (length == 1) { michael@0: return genders[0]; michael@0: } michael@0: UBool has_female = FALSE; michael@0: UBool has_male = FALSE; michael@0: switch (_style) { michael@0: case NEUTRAL: michael@0: return UGENDER_OTHER; michael@0: case MIXED_NEUTRAL: michael@0: for (int32_t i = 0; i < length; ++i) { michael@0: switch (genders[i]) { michael@0: case UGENDER_OTHER: michael@0: return UGENDER_OTHER; michael@0: break; michael@0: case UGENDER_FEMALE: michael@0: if (has_male) { michael@0: return UGENDER_OTHER; michael@0: } michael@0: has_female = TRUE; michael@0: break; michael@0: case UGENDER_MALE: michael@0: if (has_female) { michael@0: return UGENDER_OTHER; michael@0: } michael@0: has_male = TRUE; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: return has_male ? UGENDER_MALE : UGENDER_FEMALE; michael@0: break; michael@0: case MALE_TAINTS: michael@0: for (int32_t i = 0; i < length; ++i) { michael@0: if (genders[i] != UGENDER_FEMALE) { michael@0: return UGENDER_MALE; michael@0: } michael@0: } michael@0: return UGENDER_FEMALE; michael@0: break; michael@0: default: michael@0: return UGENDER_OTHER; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: const GenderInfo* GenderInfo::getNeutralInstance() { michael@0: return &gObjs[NEUTRAL]; michael@0: } michael@0: michael@0: const GenderInfo* GenderInfo::getMixedNeutralInstance() { michael@0: return &gObjs[MIXED_NEUTRAL]; michael@0: } michael@0: michael@0: const GenderInfo* GenderInfo::getMaleTaintsInstance() { michael@0: return &gObjs[MALE_TAINTS]; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: U_CAPI const UGenderInfo* U_EXPORT2 michael@0: ugender_getInstance(const char* locale, UErrorCode* status) { michael@0: return (const UGenderInfo*) icu::GenderInfo::getInstance(locale, *status); michael@0: } michael@0: michael@0: U_CAPI UGender U_EXPORT2 michael@0: ugender_getListGender(const UGenderInfo* genderInfo, const UGender* genders, int32_t size, UErrorCode* status) { michael@0: return ((const icu::GenderInfo *)genderInfo)->getListGender(genders, size, *status); michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */