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