• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    uld->langBundle = ures_open(U_ICUDATA_LANG, localeID, status);
72 
73    if (U_FAILURE(*status)) {
74       uprv_free(uld);
75       return nullptr;
76    }
77 
78    return uld;
79 }
80 
81 U_CAPI void U_EXPORT2
ulocdata_close(ULocaleData * uld)82 ulocdata_close(ULocaleData *uld)
83 {
84     if ( uld != nullptr ) {
85        ures_close(uld->langBundle);
86        ures_close(uld->bundle);
87        uprv_free(uld);
88     }
89 }
90 
91 U_CAPI void U_EXPORT2
ulocdata_setNoSubstitute(ULocaleData * uld,UBool setting)92 ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
93 {
94    uld->noSubstitute = setting;
95 }
96 
97 U_CAPI UBool U_EXPORT2
ulocdata_getNoSubstitute(ULocaleData * uld)98 ulocdata_getNoSubstitute(ULocaleData *uld)
99 {
100    return uld->noSubstitute;
101 }
102 
103 U_CAPI USet* U_EXPORT2
ulocdata_getExemplarSet(ULocaleData * uld,USet * fillIn,uint32_t options,ULocaleDataExemplarSetType extype,UErrorCode * status)104 ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
105                         uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
106 
107     static const char* const exemplarSetTypes[] = { "ExemplarCharacters",
108                                                     "AuxExemplarCharacters",
109                                                     "ExemplarCharactersIndex",
110                                                     "ExemplarCharactersPunctuation"};
111     const char16_t *exemplarChars = nullptr;
112     int32_t len = 0;
113     UErrorCode localStatus = U_ZERO_ERROR;
114 
115     if (U_FAILURE(*status))
116         return nullptr;
117 
118     exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
119     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
120         localStatus = U_MISSING_RESOURCE_ERROR;
121     }
122 
123     if (localStatus != U_ZERO_ERROR) {
124         *status = localStatus;
125     }
126 
127     if (U_FAILURE(*status))
128         return nullptr;
129 
130     if(fillIn != nullptr)
131         uset_applyPattern(fillIn, exemplarChars, len,
132                           USET_IGNORE_SPACE | options, status);
133     else
134         fillIn = uset_openPatternOptions(exemplarChars, len,
135                                          USET_IGNORE_SPACE | options, status);
136 
137     return fillIn;
138 
139 }
140 
141 U_CAPI int32_t U_EXPORT2
ulocdata_getDelimiter(ULocaleData * uld,ULocaleDataDelimiterType type,char16_t * result,int32_t resultLength,UErrorCode * status)142 ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
143                       char16_t *result, int32_t resultLength, UErrorCode *status){
144 
145     static const char* const delimiterKeys[] =  {
146         "quotationStart",
147         "quotationEnd",
148         "alternateQuotationStart",
149         "alternateQuotationEnd"
150     };
151 
152     UResourceBundle *delimiterBundle;
153     int32_t len = 0;
154     const char16_t *delimiter = nullptr;
155     UErrorCode localStatus = U_ZERO_ERROR;
156 
157     if (U_FAILURE(*status))
158         return 0;
159 
160     delimiterBundle = ures_getByKey(uld->bundle, "delimiters", nullptr, &localStatus);
161 
162     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
163         localStatus = U_MISSING_RESOURCE_ERROR;
164     }
165 
166     if (localStatus != U_ZERO_ERROR) {
167         *status = localStatus;
168     }
169 
170     if (U_FAILURE(*status)){
171         ures_close(delimiterBundle);
172         return 0;
173     }
174 
175     delimiter = ures_getStringByKeyWithFallback(delimiterBundle, delimiterKeys[type], &len, &localStatus);
176     ures_close(delimiterBundle);
177 
178     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
179         localStatus = U_MISSING_RESOURCE_ERROR;
180     }
181 
182     if (localStatus != U_ZERO_ERROR) {
183         *status = localStatus;
184     }
185 
186     if (U_FAILURE(*status)){
187         return 0;
188     }
189 
190     u_strncpy(result,delimiter, resultLength);
191     return len;
192 }
193 
measurementTypeBundleForLocale(const char * localeID,const char * measurementType,UErrorCode * status)194 static UResourceBundle * measurementTypeBundleForLocale(const char *localeID, const char *measurementType, UErrorCode *status){
195     char region[ULOC_COUNTRY_CAPACITY];
196     UResourceBundle *rb;
197     UResourceBundle *measTypeBundle = nullptr;
198 
199     ulocimp_getRegionForSupplementalData(localeID, true, region, ULOC_COUNTRY_CAPACITY, status);
200 
201     rb = ures_openDirect(nullptr, "supplementalData", status);
202     ures_getByKey(rb, "measurementData", rb, status);
203     if (rb != nullptr) {
204         UResourceBundle *measDataBundle = ures_getByKey(rb, region, nullptr, status);
205         if (U_SUCCESS(*status)) {
206         	measTypeBundle = ures_getByKey(measDataBundle, measurementType, nullptr, status);
207         }
208         if (*status == U_MISSING_RESOURCE_ERROR) {
209             *status = U_ZERO_ERROR;
210             if (measDataBundle != nullptr) {
211                 ures_close(measDataBundle);
212             }
213             measDataBundle = ures_getByKey(rb, "001", nullptr, status);
214             measTypeBundle = ures_getByKey(measDataBundle, measurementType, nullptr, status);
215         }
216         ures_close(measDataBundle);
217     }
218     ures_close(rb);
219     return measTypeBundle;
220 }
221 
222 U_CAPI UMeasurementSystem U_EXPORT2
ulocdata_getMeasurementSystem(const char * localeID,UErrorCode * status)223 ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
224 
225     UResourceBundle* measurement=nullptr;
226     UMeasurementSystem system = UMS_LIMIT;
227 
228     if(status == nullptr || U_FAILURE(*status)){
229         return system;
230     }
231 
232     measurement = measurementTypeBundleForLocale(localeID, MEASUREMENT_SYSTEM, status);
233     int32_t result = ures_getInt(measurement, status);
234     if (U_SUCCESS(*status)) {
235          system = static_cast<UMeasurementSystem>(result);
236     }
237 
238     ures_close(measurement);
239 
240     return system;
241 
242 }
243 
244 U_CAPI void U_EXPORT2
ulocdata_getPaperSize(const char * localeID,int32_t * height,int32_t * width,UErrorCode * status)245 ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
246     UResourceBundle* paperSizeBundle = nullptr;
247     const int32_t* paperSize=nullptr;
248     int32_t len = 0;
249 
250     if(status == nullptr || U_FAILURE(*status)){
251         return;
252     }
253 
254     paperSizeBundle = measurementTypeBundleForLocale(localeID, PAPER_SIZE, status);
255     paperSize = ures_getIntVector(paperSizeBundle, &len,  status);
256 
257     if(U_SUCCESS(*status)){
258         if(len < 2){
259             *status = U_INTERNAL_PROGRAM_ERROR;
260         }else{
261             *height = paperSize[0];
262             *width  = paperSize[1];
263         }
264     }
265 
266     ures_close(paperSizeBundle);
267 
268 }
269 
270 U_CAPI void U_EXPORT2
ulocdata_getCLDRVersion(UVersionInfo versionArray,UErrorCode * status)271 ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) {
272     UResourceBundle *rb = nullptr;
273     rb = ures_openDirect(nullptr, "supplementalData", status);
274     ures_getVersionByKey(rb, "cldrVersion", versionArray, status);
275     ures_close(rb);
276 }
277 
278 U_CAPI int32_t U_EXPORT2
ulocdata_getLocaleDisplayPattern(ULocaleData * uld,char16_t * result,int32_t resultCapacity,UErrorCode * status)279 ulocdata_getLocaleDisplayPattern(ULocaleData *uld,
280                                  char16_t *result,
281                                  int32_t resultCapacity,
282                                  UErrorCode *status) {
283     UResourceBundle *patternBundle;
284     int32_t len = 0;
285     const char16_t *pattern = nullptr;
286     UErrorCode localStatus = U_ZERO_ERROR;
287 
288     if (U_FAILURE(*status))
289         return 0;
290 
291     patternBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", nullptr, &localStatus);
292 
293     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
294         localStatus = U_MISSING_RESOURCE_ERROR;
295     }
296 
297     if (localStatus != U_ZERO_ERROR) {
298         *status = localStatus;
299     }
300 
301     if (U_FAILURE(*status)){
302         ures_close(patternBundle);
303         return 0;
304     }
305 
306     pattern = ures_getStringByKey(patternBundle, "pattern", &len, &localStatus);
307     ures_close(patternBundle);
308 
309     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
310         localStatus = U_MISSING_RESOURCE_ERROR;
311     }
312 
313     if (localStatus != U_ZERO_ERROR) {
314         *status = localStatus;
315     }
316 
317     if (U_FAILURE(*status)){
318         return 0;
319     }
320 
321     u_strncpy(result, pattern, resultCapacity);
322     return len;
323 }
324 
325 
326 U_CAPI int32_t U_EXPORT2
ulocdata_getLocaleSeparator(ULocaleData * uld,char16_t * result,int32_t resultCapacity,UErrorCode * status)327 ulocdata_getLocaleSeparator(ULocaleData *uld,
328                             char16_t *result,
329                             int32_t resultCapacity,
330                             UErrorCode *status)  {
331     UResourceBundle *separatorBundle;
332     int32_t len = 0;
333     const char16_t *separator = nullptr;
334     UErrorCode localStatus = U_ZERO_ERROR;
335     char16_t *p0, *p1;
336     static const char16_t sub0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 }; /* {0} */
337     static const char16_t sub1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 }; /* {1} */
338     static const int32_t subLen = 3;
339 
340     if (U_FAILURE(*status))
341         return 0;
342 
343     separatorBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", nullptr, &localStatus);
344 
345     if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
346         localStatus = U_MISSING_RESOURCE_ERROR;
347     }
348 
349     if (localStatus != U_ZERO_ERROR) {
350         *status = localStatus;
351     }
352 
353     if (U_FAILURE(*status)){
354         ures_close(separatorBundle);
355         return 0;
356     }
357 
358     separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus);
359     ures_close(separatorBundle);
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         return 0;
371     }
372 
373     /* For backwards compatibility, if we have a pattern, return the portion between {0} and {1} */
374     p0=u_strstr(separator, sub0);
375     p1=u_strstr(separator, sub1);
376     if (p0!=nullptr && p1!=nullptr && p0<=p1) {
377         separator = (const char16_t *)p0 + subLen;
378         len = static_cast<int32_t>(p1 - separator);
379         /* Desired separator is no longer zero-terminated; handle that if necessary */
380         if (len < resultCapacity) {
381             u_strncpy(result, separator, len);
382             result[len] = 0;
383             return len;
384         }
385     }
386 
387     u_strncpy(result, separator, resultCapacity);
388     return len;
389 }
390