Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2008-2013, International Business Machines Corporation and |
michael@0 | 4 | * others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * |
michael@0 | 7 | * |
michael@0 | 8 | * File GENDER.CPP |
michael@0 | 9 | * |
michael@0 | 10 | * Modification History:* |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * |
michael@0 | 13 | ******************************************************************************** |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #include "unicode/utypes.h" |
michael@0 | 17 | |
michael@0 | 18 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/gender.h" |
michael@0 | 21 | #include "unicode/ugender.h" |
michael@0 | 22 | #include "unicode/ures.h" |
michael@0 | 23 | |
michael@0 | 24 | #include "cmemory.h" |
michael@0 | 25 | #include "cstring.h" |
michael@0 | 26 | #include "mutex.h" |
michael@0 | 27 | #include "uassert.h" |
michael@0 | 28 | #include "ucln_in.h" |
michael@0 | 29 | #include "umutex.h" |
michael@0 | 30 | #include "uhash.h" |
michael@0 | 31 | |
michael@0 | 32 | static UHashtable* gGenderInfoCache = NULL; |
michael@0 | 33 | static UMutex gGenderMetaLock = U_MUTEX_INITIALIZER; |
michael@0 | 34 | static const char* gNeutralStr = "neutral"; |
michael@0 | 35 | static const char* gMailTaintsStr = "maleTaints"; |
michael@0 | 36 | static const char* gMixedNeutralStr = "mixedNeutral"; |
michael@0 | 37 | static icu::GenderInfo* gObjs = NULL; |
michael@0 | 38 | static icu::UInitOnce gGenderInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 39 | |
michael@0 | 40 | enum GenderStyle { |
michael@0 | 41 | NEUTRAL, |
michael@0 | 42 | MIXED_NEUTRAL, |
michael@0 | 43 | MALE_TAINTS, |
michael@0 | 44 | GENDER_STYLE_LENGTH |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | U_CDECL_BEGIN |
michael@0 | 48 | |
michael@0 | 49 | static UBool U_CALLCONV gender_cleanup(void) { |
michael@0 | 50 | if (gGenderInfoCache != NULL) { |
michael@0 | 51 | uhash_close(gGenderInfoCache); |
michael@0 | 52 | gGenderInfoCache = NULL; |
michael@0 | 53 | delete [] gObjs; |
michael@0 | 54 | } |
michael@0 | 55 | gGenderInitOnce.reset(); |
michael@0 | 56 | return TRUE; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | U_CDECL_END |
michael@0 | 60 | |
michael@0 | 61 | U_NAMESPACE_BEGIN |
michael@0 | 62 | |
michael@0 | 63 | void U_CALLCONV GenderInfo_initCache(UErrorCode &status) { |
michael@0 | 64 | ucln_i18n_registerCleanup(UCLN_I18N_GENDERINFO, gender_cleanup); |
michael@0 | 65 | U_ASSERT(gGenderInfoCache == NULL); |
michael@0 | 66 | if (U_FAILURE(status)) { |
michael@0 | 67 | return; |
michael@0 | 68 | } |
michael@0 | 69 | gObjs = new GenderInfo[GENDER_STYLE_LENGTH]; |
michael@0 | 70 | if (gObjs == NULL) { |
michael@0 | 71 | status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | for (int i = 0; i < GENDER_STYLE_LENGTH; i++) { |
michael@0 | 75 | gObjs[i]._style = i; |
michael@0 | 76 | } |
michael@0 | 77 | gGenderInfoCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status); |
michael@0 | 78 | if (U_FAILURE(status)) { |
michael@0 | 79 | delete [] gObjs; |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | uhash_setKeyDeleter(gGenderInfoCache, uprv_free); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | |
michael@0 | 86 | GenderInfo::GenderInfo() { |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | GenderInfo::~GenderInfo() { |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | const GenderInfo* GenderInfo::getInstance(const Locale& locale, UErrorCode& status) { |
michael@0 | 93 | // Make sure our cache exists. |
michael@0 | 94 | umtx_initOnce(gGenderInitOnce, &GenderInfo_initCache, status); |
michael@0 | 95 | if (U_FAILURE(status)) { |
michael@0 | 96 | return NULL; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | const GenderInfo* result = NULL; |
michael@0 | 100 | const char* key = locale.getName(); |
michael@0 | 101 | { |
michael@0 | 102 | Mutex lock(&gGenderMetaLock); |
michael@0 | 103 | result = (const GenderInfo*) uhash_get(gGenderInfoCache, key); |
michael@0 | 104 | } |
michael@0 | 105 | if (result) { |
michael@0 | 106 | return result; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // On cache miss, try to create GenderInfo from CLDR data |
michael@0 | 110 | result = loadInstance(locale, status); |
michael@0 | 111 | if (U_FAILURE(status)) { |
michael@0 | 112 | return NULL; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // Try to put our GenderInfo object in cache. If there is a race condition, |
michael@0 | 116 | // favor the GenderInfo object that is already in the cache. |
michael@0 | 117 | { |
michael@0 | 118 | Mutex lock(&gGenderMetaLock); |
michael@0 | 119 | GenderInfo* temp = (GenderInfo*) uhash_get(gGenderInfoCache, key); |
michael@0 | 120 | if (temp) { |
michael@0 | 121 | result = temp; |
michael@0 | 122 | } else { |
michael@0 | 123 | uhash_put(gGenderInfoCache, uprv_strdup(key), (void*) result, &status); |
michael@0 | 124 | if (U_FAILURE(status)) { |
michael@0 | 125 | return NULL; |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | return result; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | const GenderInfo* GenderInfo::loadInstance(const Locale& locale, UErrorCode& status) { |
michael@0 | 133 | LocalUResourceBundlePointer rb( |
michael@0 | 134 | ures_openDirect(NULL, "genderList", &status)); |
michael@0 | 135 | if (U_FAILURE(status)) { |
michael@0 | 136 | return NULL; |
michael@0 | 137 | } |
michael@0 | 138 | LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), "genderList", NULL, &status)); |
michael@0 | 139 | if (U_FAILURE(status)) { |
michael@0 | 140 | return NULL; |
michael@0 | 141 | } |
michael@0 | 142 | int32_t resLen = 0; |
michael@0 | 143 | const char* curLocaleName = locale.getName(); |
michael@0 | 144 | UErrorCode key_status = U_ZERO_ERROR; |
michael@0 | 145 | const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &key_status); |
michael@0 | 146 | if (s == NULL) { |
michael@0 | 147 | key_status = U_ZERO_ERROR; |
michael@0 | 148 | char parentLocaleName[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 149 | uprv_strcpy(parentLocaleName, curLocaleName); |
michael@0 | 150 | while (s == NULL && uloc_getParent(parentLocaleName, parentLocaleName, ULOC_FULLNAME_CAPACITY, &key_status) > 0) { |
michael@0 | 151 | key_status = U_ZERO_ERROR; |
michael@0 | 152 | resLen = 0; |
michael@0 | 153 | s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &key_status); |
michael@0 | 154 | key_status = U_ZERO_ERROR; |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | if (s == NULL) { |
michael@0 | 158 | return &gObjs[NEUTRAL]; |
michael@0 | 159 | } |
michael@0 | 160 | char type_str[256]; |
michael@0 | 161 | u_UCharsToChars(s, type_str, resLen + 1); |
michael@0 | 162 | if (uprv_strcmp(type_str, gNeutralStr) == 0) { |
michael@0 | 163 | return &gObjs[NEUTRAL]; |
michael@0 | 164 | } |
michael@0 | 165 | if (uprv_strcmp(type_str, gMixedNeutralStr) == 0) { |
michael@0 | 166 | return &gObjs[MIXED_NEUTRAL]; |
michael@0 | 167 | } |
michael@0 | 168 | if (uprv_strcmp(type_str, gMailTaintsStr) == 0) { |
michael@0 | 169 | return &gObjs[MALE_TAINTS]; |
michael@0 | 170 | } |
michael@0 | 171 | return &gObjs[NEUTRAL]; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | UGender GenderInfo::getListGender(const UGender* genders, int32_t length, UErrorCode& status) const { |
michael@0 | 175 | if (U_FAILURE(status)) { |
michael@0 | 176 | return UGENDER_OTHER; |
michael@0 | 177 | } |
michael@0 | 178 | if (length == 0) { |
michael@0 | 179 | return UGENDER_OTHER; |
michael@0 | 180 | } |
michael@0 | 181 | if (length == 1) { |
michael@0 | 182 | return genders[0]; |
michael@0 | 183 | } |
michael@0 | 184 | UBool has_female = FALSE; |
michael@0 | 185 | UBool has_male = FALSE; |
michael@0 | 186 | switch (_style) { |
michael@0 | 187 | case NEUTRAL: |
michael@0 | 188 | return UGENDER_OTHER; |
michael@0 | 189 | case MIXED_NEUTRAL: |
michael@0 | 190 | for (int32_t i = 0; i < length; ++i) { |
michael@0 | 191 | switch (genders[i]) { |
michael@0 | 192 | case UGENDER_OTHER: |
michael@0 | 193 | return UGENDER_OTHER; |
michael@0 | 194 | break; |
michael@0 | 195 | case UGENDER_FEMALE: |
michael@0 | 196 | if (has_male) { |
michael@0 | 197 | return UGENDER_OTHER; |
michael@0 | 198 | } |
michael@0 | 199 | has_female = TRUE; |
michael@0 | 200 | break; |
michael@0 | 201 | case UGENDER_MALE: |
michael@0 | 202 | if (has_female) { |
michael@0 | 203 | return UGENDER_OTHER; |
michael@0 | 204 | } |
michael@0 | 205 | has_male = TRUE; |
michael@0 | 206 | break; |
michael@0 | 207 | default: |
michael@0 | 208 | break; |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | return has_male ? UGENDER_MALE : UGENDER_FEMALE; |
michael@0 | 212 | break; |
michael@0 | 213 | case MALE_TAINTS: |
michael@0 | 214 | for (int32_t i = 0; i < length; ++i) { |
michael@0 | 215 | if (genders[i] != UGENDER_FEMALE) { |
michael@0 | 216 | return UGENDER_MALE; |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | return UGENDER_FEMALE; |
michael@0 | 220 | break; |
michael@0 | 221 | default: |
michael@0 | 222 | return UGENDER_OTHER; |
michael@0 | 223 | break; |
michael@0 | 224 | } |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | const GenderInfo* GenderInfo::getNeutralInstance() { |
michael@0 | 228 | return &gObjs[NEUTRAL]; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | const GenderInfo* GenderInfo::getMixedNeutralInstance() { |
michael@0 | 232 | return &gObjs[MIXED_NEUTRAL]; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | const GenderInfo* GenderInfo::getMaleTaintsInstance() { |
michael@0 | 236 | return &gObjs[MALE_TAINTS]; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | U_NAMESPACE_END |
michael@0 | 240 | |
michael@0 | 241 | U_CAPI const UGenderInfo* U_EXPORT2 |
michael@0 | 242 | ugender_getInstance(const char* locale, UErrorCode* status) { |
michael@0 | 243 | return (const UGenderInfo*) icu::GenderInfo::getInstance(locale, *status); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | U_CAPI UGender U_EXPORT2 |
michael@0 | 247 | ugender_getListGender(const UGenderInfo* genderInfo, const UGender* genders, int32_t size, UErrorCode* status) { |
michael@0 | 248 | return ((const icu::GenderInfo *)genderInfo)->getListGender(genders, size, *status); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | #endif /* #if !UCONFIG_NO_FORMATTING */ |