]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/i18n/gender.cpp
ICU-511.32.tar.gz
[apple/icu.git] / icuSources / i18n / gender.cpp
... / ...
CommitLineData
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
31static UHashtable* gGenderInfoCache = NULL;
32static UMutex gGenderMetaLock = U_MUTEX_INITIALIZER;
33static const char* gNeutralStr = "neutral";
34static const char* gMailTaintsStr = "maleTaints";
35static const char* gMixedNeutralStr = "mixedNeutral";
36static icu::GenderInfo* gObjs = NULL;
37
38enum GenderStyle {
39 NEUTRAL,
40 MIXED_NEUTRAL,
41 MALE_TAINTS,
42 GENDER_STYLE_LENGTH
43};
44
45U_CDECL_BEGIN
46
47static 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
56U_CDECL_END
57
58U_NAMESPACE_BEGIN
59
60GenderInfo::GenderInfo() {
61}
62
63GenderInfo::~GenderInfo() {
64}
65
66const GenderInfo* GenderInfo::getInstance(const Locale& locale, UErrorCode& status) {
67 if (U_FAILURE(status)) {
68 return NULL;
69 }
70
71 // Make sure our cache exists.
72 UBool needed;
73 UMTX_CHECK(&gGenderMetaLock, (gGenderInfoCache == NULL), needed);
74 if (needed) {
75 Mutex lock(&gGenderMetaLock);
76 if (gGenderInfoCache == NULL) {
77 gObjs = new GenderInfo[GENDER_STYLE_LENGTH];
78 if (gObjs == NULL) {
79 status = U_MEMORY_ALLOCATION_ERROR;
80 return NULL;
81 }
82 for (int i = 0; i < GENDER_STYLE_LENGTH; i++) {
83 gObjs[i]._style = i;
84 }
85 gGenderInfoCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
86 if (U_FAILURE(status)) {
87 delete [] gObjs;
88 return NULL;
89 }
90 uhash_setKeyDeleter(gGenderInfoCache, uprv_free);
91 ucln_i18n_registerCleanup(UCLN_I18N_GENDERINFO, gender_cleanup);
92 }
93 }
94
95 const GenderInfo* result = NULL;
96 const char* key = locale.getName();
97 {
98 Mutex lock(&gGenderMetaLock);
99 result = (const GenderInfo*) uhash_get(gGenderInfoCache, key);
100 }
101 if (result) {
102 return result;
103 }
104
105 // On cache miss, try to create GenderInfo from CLDR data
106 result = loadInstance(locale, status);
107 if (U_FAILURE(status)) {
108 return NULL;
109 }
110
111 // Try to put our GenderInfo object in cache. If there is a race condition,
112 // favor the GenderInfo object that is already in the cache.
113 {
114 Mutex lock(&gGenderMetaLock);
115 GenderInfo* temp = (GenderInfo*) uhash_get(gGenderInfoCache, key);
116 if (temp) {
117 result = temp;
118 } else {
119 uhash_put(gGenderInfoCache, uprv_strdup(key), (void*) result, &status);
120 if (U_FAILURE(status)) {
121 return NULL;
122 }
123 }
124 }
125 return result;
126}
127
128const GenderInfo* GenderInfo::loadInstance(const Locale& locale, UErrorCode& status) {
129 LocalUResourceBundlePointer rb(
130 ures_openDirect(NULL, "genderList", &status));
131 if (U_FAILURE(status)) {
132 return NULL;
133 }
134 LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), "genderList", NULL, &status));
135 if (U_FAILURE(status)) {
136 return NULL;
137 }
138 int32_t resLen = 0;
139 const char* curLocaleName = locale.getName();
140 UErrorCode key_status = U_ZERO_ERROR;
141 const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &key_status);
142 if (s == NULL) {
143 key_status = U_ZERO_ERROR;
144 char parentLocaleName[ULOC_FULLNAME_CAPACITY];
145 uprv_strcpy(parentLocaleName, curLocaleName);
146 while (s == NULL && uloc_getParent(parentLocaleName, parentLocaleName, ULOC_FULLNAME_CAPACITY, &key_status) > 0) {
147 key_status = U_ZERO_ERROR;
148 resLen = 0;
149 s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &key_status);
150 key_status = U_ZERO_ERROR;
151 }
152 }
153 if (s == NULL) {
154 return &gObjs[NEUTRAL];
155 }
156 char type_str[256];
157 u_UCharsToChars(s, type_str, resLen + 1);
158 if (uprv_strcmp(type_str, gNeutralStr) == 0) {
159 return &gObjs[NEUTRAL];
160 }
161 if (uprv_strcmp(type_str, gMixedNeutralStr) == 0) {
162 return &gObjs[MIXED_NEUTRAL];
163 }
164 if (uprv_strcmp(type_str, gMailTaintsStr) == 0) {
165 return &gObjs[MALE_TAINTS];
166 }
167 return &gObjs[NEUTRAL];
168}
169
170UGender GenderInfo::getListGender(const UGender* genders, int32_t length, UErrorCode& status) const {
171 if (U_FAILURE(status)) {
172 return UGENDER_OTHER;
173 }
174 if (length == 0) {
175 return UGENDER_OTHER;
176 }
177 if (length == 1) {
178 return genders[0];
179 }
180 UBool has_female = FALSE;
181 UBool has_male = FALSE;
182 switch (_style) {
183 case NEUTRAL:
184 return UGENDER_OTHER;
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
223const GenderInfo* GenderInfo::getNeutralInstance() {
224 return &gObjs[NEUTRAL];
225}
226
227const GenderInfo* GenderInfo::getMixedNeutralInstance() {
228 return &gObjs[MIXED_NEUTRAL];
229}
230
231const GenderInfo* GenderInfo::getMaleTaintsInstance() {
232 return &gObjs[MALE_TAINTS];
233}
234
235U_NAMESPACE_END
236
237U_CAPI const UGenderInfo* U_EXPORT2
238ugender_getInstance(const char* locale, UErrorCode* status) {
239 return (const UGenderInfo*) icu::GenderInfo::getInstance(locale, *status);
240}
241
242U_CAPI UGender U_EXPORT2
243ugender_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 */