xref: /aosp_15_r20/external/cronet/third_party/icu/source/i18n/ulocdata.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *                                                                            *
6 * Copyright (C) 2003-2016, International Business Machines                   *
7 *                Corporation and others. All Rights Reserved.                *
8 *                                                                            *
9 ******************************************************************************
10 *   file name:  ulocdata.c
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2003Oct21
16 *   created by: Ram Viswanadha,John Emmons
17 */
18 
19 #include "cmemory.h"
20 #include "unicode/ustring.h"
21 #include "unicode/ures.h"
22 #include "unicode/uloc.h"
23 #include "unicode/ulocdata.h"
24 #include "uresimp.h"
25 #include "ureslocs.h"
26 #include "ulocimp.h"
27 
28 #define MEASUREMENT_SYSTEM  "MeasurementSystem"
29 #define PAPER_SIZE          "PaperSize"
30 
31 /** A locale data object.
32  *  For usage in C programs.
33  *  @draft ICU 3.4
34  */
35 struct ULocaleData {
36     /**
37      * Controls the "No Substitute" behavior of this locale data object
38      */
39     UBool noSubstitute;
40 
41     /**
42      * Pointer to the resource bundle associated with this locale data object
43      */
44     UResourceBundle *bundle;
45 
46     /**
47      * Pointer to the lang resource bundle associated with this locale data object
48      */
49     UResourceBundle *langBundle;
50 };
51 
52 U_CAPI ULocaleData* U_EXPORT2
ulocdata_open(const char * localeID,UErrorCode * status)53 ulocdata_open(const char *localeID, UErrorCode *status)
54 {
55    ULocaleData *uld;
56 
57    if (U_FAILURE(*status)) {
58        return nullptr;
59    }
60 
61    uld = (ULocaleData *)uprv_malloc(sizeof(ULocaleData));
62    if (uld == nullptr) {
63       *status = U_MEMORY_ALLOCATION_ERROR;
64       return(nullptr);
65    }
66 
67    uld->langBundle = nullptr;
68 
69    uld->noSubstitute = false;
70    uld->bundle = ures_open(nullptr, localeID, status);
71 
72    if (U_FAILURE(*status)) {
73       uprv_free(uld);
74       return nullptr;
75    }
76 
77    // ICU-22149: not all functions require lang data, so fail gracefully if it is not present
78    UErrorCode oldStatus = *status;
79    uld->langBundle = ures_open(U_ICUDATA_LANG, localeID, status);
80    if (*status == U_MISSING_RESOURCE_ERROR) {
81       *status = oldStatus;
82    }
83 
84    return uld;
85 }
86 
87 U_CAPI void U_EXPORT2
ulocdata_close(ULocaleData * uld)88 ulocdata_close(ULocaleData *uld)
89 {
90     if ( uld != nullptr ) {
91        ures_close(uld->langBundle);
92        ures_close(uld->bundle);
93        uprv_free(uld);
94     }
95 }
96 
97 U_CAPI void U_EXPORT2
ulocdata_setNoSubstitute(ULocaleData * uld,UBool setting)98 ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
99 {
100    uld->noSubstitute = setting;
101 }
102 
103 U_CAPI UBool U_EXPORT2
ulocdata_getNoSubstitute(ULocaleData * uld)104 ulocdata_getNoSubstitute(ULocaleData *uld)
105 {
106    return uld->noSubstitute;
107 }
108 
109 U_CAPI USet* U_EXPORT2
ulocdata_getExemplarSet(ULocaleData * uld,USet * fillIn,uint32_t options,ULocaleDataExemplarSetType extype,UErrorCode * status)110 ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
111                         uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
112 
113     static const char* const exemplarSetTypes[] = { "ExemplarCharacters",
114                                                     "AuxExemplarCharacters",
115                                                     "ExemplarCharactersIndex",
116                                                     "ExemplarCharactersPunctuation"};
117     const char16_t *exemplarChars = nullptr;
118     int32_t len = 0;
119     UErrorCode localStatus = U_ZERO_ERROR;
120 
121     if (U_FAILURE(*status))
122         return nullptr;
123 
124     exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
125     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
126         localStatus = U_MISSING_RESOURCE_ERROR;
127     }
128 
129     if (localStatus != U_ZERO_ERROR) {
130         *status = localStatus;
131     }
132 
133     if (U_FAILURE(*status))
134         return nullptr;
135 
136     if(fillIn != nullptr)
137         uset_applyPattern(fillIn, exemplarChars, len,
138                           USET_IGNORE_SPACE | options, status);
139     else
140         fillIn = uset_openPatternOptions(exemplarChars, len,
141                                          USET_IGNORE_SPACE | options, status);
142 
143     return fillIn;
144 
145 }
146 
147 U_CAPI int32_t U_EXPORT2
ulocdata_getDelimiter(ULocaleData * uld,ULocaleDataDelimiterType type,char16_t * result,int32_t resultLength,UErrorCode * status)148 ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
149                       char16_t *result, int32_t resultLength, UErrorCode *status){
150 
151     static const char* const delimiterKeys[] =  {
152         "quotationStart",
153         "quotationEnd",
154         "alternateQuotationStart",
155         "alternateQuotationEnd"
156     };
157 
158     UResourceBundle *delimiterBundle;
159     int32_t len = 0;
160     const char16_t *delimiter = nullptr;
161     UErrorCode localStatus = U_ZERO_ERROR;
162 
163     if (U_FAILURE(*status))
164         return 0;
165 
166     delimiterBundle = ures_getByKey(uld->bundle, "delimiters", nullptr, &localStatus);
167 
168     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
169         localStatus = U_MISSING_RESOURCE_ERROR;
170     }
171 
172     if (localStatus != U_ZERO_ERROR) {
173         *status = localStatus;
174     }
175 
176     if (U_FAILURE(*status)){
177         ures_close(delimiterBundle);
178         return 0;
179     }
180 
181     delimiter = ures_getStringByKeyWithFallback(delimiterBundle, delimiterKeys[type], &len, &localStatus);
182     ures_close(delimiterBundle);
183 
184     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
185         localStatus = U_MISSING_RESOURCE_ERROR;
186     }
187 
188     if (localStatus != U_ZERO_ERROR) {
189         *status = localStatus;
190     }
191 
192     if (U_FAILURE(*status)){
193         return 0;
194     }
195 
196     u_strncpy(result,delimiter, resultLength);
197     return len;
198 }
199 
measurementTypeBundleForLocale(const char * localeID,const char * measurementType,UErrorCode * status)200 static UResourceBundle * measurementTypeBundleForLocale(const char *localeID, const char *measurementType, UErrorCode *status){
201     char region[ULOC_COUNTRY_CAPACITY];
202     UResourceBundle *rb;
203     UResourceBundle *measTypeBundle = nullptr;
204 
205     ulocimp_getRegionForSupplementalData(localeID, true, region, ULOC_COUNTRY_CAPACITY, status);
206 
207     rb = ures_openDirect(nullptr, "supplementalData", status);
208     ures_getByKey(rb, "measurementData", rb, status);
209     if (rb != nullptr) {
210         UResourceBundle *measDataBundle = ures_getByKey(rb, region, nullptr, status);
211         if (U_SUCCESS(*status)) {
212         	measTypeBundle = ures_getByKey(measDataBundle, measurementType, nullptr, status);
213         }
214         if (*status == U_MISSING_RESOURCE_ERROR) {
215             *status = U_ZERO_ERROR;
216             if (measDataBundle != nullptr) {
217                 ures_close(measDataBundle);
218             }
219             measDataBundle = ures_getByKey(rb, "001", nullptr, status);
220             measTypeBundle = ures_getByKey(measDataBundle, measurementType, nullptr, status);
221         }
222         ures_close(measDataBundle);
223     }
224     ures_close(rb);
225     return measTypeBundle;
226 }
227 
228 U_CAPI UMeasurementSystem U_EXPORT2
ulocdata_getMeasurementSystem(const char * localeID,UErrorCode * status)229 ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
230 
231     UResourceBundle* measurement=nullptr;
232     UMeasurementSystem system = UMS_LIMIT;
233 
234     if(status == nullptr || U_FAILURE(*status)){
235         return system;
236     }
237 
238     measurement = measurementTypeBundleForLocale(localeID, MEASUREMENT_SYSTEM, status);
239     int32_t result = ures_getInt(measurement, status);
240     if (U_SUCCESS(*status)) {
241          system = static_cast<UMeasurementSystem>(result);
242     }
243 
244     ures_close(measurement);
245 
246     return system;
247 
248 }
249 
250 U_CAPI void U_EXPORT2
ulocdata_getPaperSize(const char * localeID,int32_t * height,int32_t * width,UErrorCode * status)251 ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
252     UResourceBundle* paperSizeBundle = nullptr;
253     const int32_t* paperSize=nullptr;
254     int32_t len = 0;
255 
256     if(status == nullptr || U_FAILURE(*status)){
257         return;
258     }
259 
260     paperSizeBundle = measurementTypeBundleForLocale(localeID, PAPER_SIZE, status);
261     paperSize = ures_getIntVector(paperSizeBundle, &len,  status);
262 
263     if(U_SUCCESS(*status)){
264         if(len < 2){
265             *status = U_INTERNAL_PROGRAM_ERROR;
266         }else{
267             *height = paperSize[0];
268             *width  = paperSize[1];
269         }
270     }
271 
272     ures_close(paperSizeBundle);
273 
274 }
275 
276 U_CAPI void U_EXPORT2
ulocdata_getCLDRVersion(UVersionInfo versionArray,UErrorCode * status)277 ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) {
278     UResourceBundle *rb = nullptr;
279     rb = ures_openDirect(nullptr, "supplementalData", status);
280     ures_getVersionByKey(rb, "cldrVersion", versionArray, status);
281     ures_close(rb);
282 }
283 
284 U_CAPI int32_t U_EXPORT2
ulocdata_getLocaleDisplayPattern(ULocaleData * uld,char16_t * result,int32_t resultCapacity,UErrorCode * status)285 ulocdata_getLocaleDisplayPattern(ULocaleData *uld,
286                                  char16_t *result,
287                                  int32_t resultCapacity,
288                                  UErrorCode *status) {
289     UResourceBundle *patternBundle;
290     int32_t len = 0;
291     const char16_t *pattern = nullptr;
292     UErrorCode localStatus = U_ZERO_ERROR;
293 
294     if (U_FAILURE(*status))
295         return 0;
296 
297     if (uld->langBundle == nullptr) {
298         *status = U_MISSING_RESOURCE_ERROR;
299         return 0;
300     }
301 
302     patternBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", nullptr, &localStatus);
303 
304     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
305         localStatus = U_MISSING_RESOURCE_ERROR;
306     }
307 
308     if (localStatus != U_ZERO_ERROR) {
309         *status = localStatus;
310     }
311 
312     if (U_FAILURE(*status)){
313         ures_close(patternBundle);
314         return 0;
315     }
316 
317     pattern = ures_getStringByKey(patternBundle, "pattern", &len, &localStatus);
318     ures_close(patternBundle);
319 
320     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
321         localStatus = U_MISSING_RESOURCE_ERROR;
322     }
323 
324     if (localStatus != U_ZERO_ERROR) {
325         *status = localStatus;
326     }
327 
328     if (U_FAILURE(*status)){
329         return 0;
330     }
331 
332     u_strncpy(result, pattern, resultCapacity);
333     return len;
334 }
335 
336 
337 U_CAPI int32_t U_EXPORT2
ulocdata_getLocaleSeparator(ULocaleData * uld,char16_t * result,int32_t resultCapacity,UErrorCode * status)338 ulocdata_getLocaleSeparator(ULocaleData *uld,
339                             char16_t *result,
340                             int32_t resultCapacity,
341                             UErrorCode *status)  {
342     UResourceBundle *separatorBundle;
343     int32_t len = 0;
344     const char16_t *separator = nullptr;
345     UErrorCode localStatus = U_ZERO_ERROR;
346     char16_t *p0, *p1;
347     static const char16_t sub0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 }; /* {0} */
348     static const char16_t sub1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 }; /* {1} */
349     static const int32_t subLen = 3;
350 
351     if (U_FAILURE(*status))
352         return 0;
353 
354     if (uld->langBundle == nullptr) {
355         *status = U_MISSING_RESOURCE_ERROR;
356         return 0;
357     }
358 
359     separatorBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", nullptr, &localStatus);
360 
361     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
362         localStatus = U_MISSING_RESOURCE_ERROR;
363     }
364 
365     if (localStatus != U_ZERO_ERROR) {
366         *status = localStatus;
367     }
368 
369     if (U_FAILURE(*status)){
370         ures_close(separatorBundle);
371         return 0;
372     }
373 
374     separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus);
375     ures_close(separatorBundle);
376 
377     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
378         localStatus = U_MISSING_RESOURCE_ERROR;
379     }
380 
381     if (localStatus != U_ZERO_ERROR) {
382         *status = localStatus;
383     }
384 
385     if (U_FAILURE(*status)){
386         return 0;
387     }
388 
389     /* For backwards compatibility, if we have a pattern, return the portion between {0} and {1} */
390     p0=u_strstr(separator, sub0);
391     p1=u_strstr(separator, sub1);
392     if (p0!=nullptr && p1!=nullptr && p0<=p1) {
393         separator = (const char16_t *)p0 + subLen;
394         len = static_cast<int32_t>(p1 - separator);
395         /* Desired separator is no longer zero-terminated; handle that if necessary */
396         if (len < resultCapacity) {
397             u_strncpy(result, separator, len);
398             result[len] = 0;
399             return len;
400         }
401     }
402 
403     u_strncpy(result, separator, resultCapacity);
404     return len;
405 }
406