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