2 ******************************************************************************
4 * Copyright (C) 2003-2013, International Business Machines *
5 * Corporation and others. All Rights Reserved. *
7 ******************************************************************************
8 * file name: ulocdata.c
10 * tab size: 8 (not used)
13 * created on: 2003Oct21
14 * created by: Ram Viswanadha,John Emmons
18 #include "unicode/ustring.h"
19 #include "unicode/ures.h"
20 #include "unicode/uloc.h"
21 #include "unicode/ulocdata.h"
26 #define MEASUREMENT_SYSTEM "MeasurementSystem"
27 #define PAPER_SIZE "PaperSize"
29 /** A locale data object.
30 * For usage in C programs.
35 * Controls the "No Substitute" behavior of this locale data object
40 * Pointer to the resource bundle associated with this locale data object
42 UResourceBundle
*bundle
;
45 * Pointer to the lang resource bundle associated with this locale data object
47 UResourceBundle
*langBundle
;
50 U_CAPI ULocaleData
* U_EXPORT2
51 ulocdata_open(const char *localeID
, UErrorCode
*status
)
55 if (U_FAILURE(*status
)) {
59 uld
= (ULocaleData
*)uprv_malloc(sizeof(ULocaleData
));
61 *status
= U_MEMORY_ALLOCATION_ERROR
;
65 uld
->langBundle
= NULL
;
67 uld
->noSubstitute
= FALSE
;
68 uld
->bundle
= ures_open(NULL
, localeID
, status
);
69 uld
->langBundle
= ures_open(U_ICUDATA_LANG
, localeID
, status
);
71 if (U_FAILURE(*status
)) {
80 ulocdata_close(ULocaleData
*uld
)
83 ures_close(uld
->langBundle
);
84 ures_close(uld
->bundle
);
90 ulocdata_setNoSubstitute(ULocaleData
*uld
, UBool setting
)
92 uld
->noSubstitute
= setting
;
95 U_CAPI UBool U_EXPORT2
96 ulocdata_getNoSubstitute(ULocaleData
*uld
)
98 return uld
->noSubstitute
;
101 U_CAPI USet
* U_EXPORT2
102 ulocdata_getExemplarSet(ULocaleData
*uld
, USet
*fillIn
,
103 uint32_t options
, ULocaleDataExemplarSetType extype
, UErrorCode
*status
){
105 static const char* const exemplarSetTypes
[] = { "ExemplarCharacters",
106 "AuxExemplarCharacters",
107 "ExemplarCharactersIndex",
108 "ExemplarCharactersPunctuation"};
109 const UChar
*exemplarChars
= NULL
;
111 UErrorCode localStatus
= U_ZERO_ERROR
;
113 if (U_FAILURE(*status
))
116 exemplarChars
= ures_getStringByKey(uld
->bundle
, exemplarSetTypes
[extype
], &len
, &localStatus
);
117 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
118 localStatus
= U_MISSING_RESOURCE_ERROR
;
121 if (localStatus
!= U_ZERO_ERROR
) {
122 *status
= localStatus
;
125 if (U_FAILURE(*status
))
129 uset_applyPattern(fillIn
, exemplarChars
, len
,
130 USET_IGNORE_SPACE
| options
, status
);
132 fillIn
= uset_openPatternOptions(exemplarChars
, len
,
133 USET_IGNORE_SPACE
| options
, status
);
139 U_CAPI
int32_t U_EXPORT2
140 ulocdata_getDelimiter(ULocaleData
*uld
, ULocaleDataDelimiterType type
,
141 UChar
*result
, int32_t resultLength
, UErrorCode
*status
){
143 static const char* const delimiterKeys
[] = {
146 "alternateQuotationStart",
147 "alternateQuotationEnd"
150 UResourceBundle
*delimiterBundle
;
152 const UChar
*delimiter
= NULL
;
153 UErrorCode localStatus
= U_ZERO_ERROR
;
155 if (U_FAILURE(*status
))
158 delimiterBundle
= ures_getByKey(uld
->bundle
, "delimiters", NULL
, &localStatus
);
160 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
161 localStatus
= U_MISSING_RESOURCE_ERROR
;
164 if (localStatus
!= U_ZERO_ERROR
) {
165 *status
= localStatus
;
168 if (U_FAILURE(*status
)){
169 ures_close(delimiterBundle
);
173 delimiter
= ures_getStringByKey(delimiterBundle
, delimiterKeys
[type
], &len
, &localStatus
);
174 ures_close(delimiterBundle
);
176 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
177 localStatus
= U_MISSING_RESOURCE_ERROR
;
180 if (localStatus
!= U_ZERO_ERROR
) {
181 *status
= localStatus
;
184 if (U_FAILURE(*status
)){
188 u_strncpy(result
,delimiter
, resultLength
);
192 static UResourceBundle
* measurementDataBundleForLocale(const char *localeID
, UErrorCode
*status
){
193 char fullLoc
[ULOC_FULLNAME_CAPACITY
];
194 char region
[ULOC_COUNTRY_CAPACITY
];
196 UResourceBundle
*measDataBundle
= NULL
;
198 /* The following code is basically copied from Calendar::setWeekData and
199 * Calendar::getCalendarTypeForLocale with adjustments for resource name
201 uloc_addLikelySubtags(localeID
, fullLoc
, ULOC_FULLNAME_CAPACITY
, status
);
202 uloc_getCountry(fullLoc
, region
, ULOC_COUNTRY_CAPACITY
, status
);
204 rb
= ures_openDirect(NULL
, "supplementalData", status
);
205 ures_getByKey(rb
, "measurementData", rb
, status
);
206 measDataBundle
= ures_getByKey(rb
, region
, NULL
, status
);
207 if (*status
== U_MISSING_RESOURCE_ERROR
&& rb
!= NULL
) {
208 *status
= U_ZERO_ERROR
;
209 measDataBundle
= ures_getByKey(rb
, "001", NULL
, status
);
212 return measDataBundle
;
215 U_CAPI UMeasurementSystem U_EXPORT2
216 ulocdata_getMeasurementSystem(const char *localeID
, UErrorCode
*status
){
218 UResourceBundle
* bundle
=NULL
;
219 UResourceBundle
* measurement
=NULL
;
220 UMeasurementSystem system
= UMS_LIMIT
;
222 if(status
== NULL
|| U_FAILURE(*status
)){
226 bundle
= measurementDataBundleForLocale(localeID
, status
);
228 measurement
= ures_getByKeyWithFallback(bundle
, MEASUREMENT_SYSTEM
, NULL
, status
);
230 system
= (UMeasurementSystem
) ures_getInt(measurement
, status
);
233 ures_close(measurement
);
239 U_CAPI
void U_EXPORT2
240 ulocdata_getPaperSize(const char* localeID
, int32_t *height
, int32_t *width
, UErrorCode
*status
){
241 UResourceBundle
* bundle
=NULL
;
242 UResourceBundle
* paperSizeBundle
= NULL
;
243 const int32_t* paperSize
=NULL
;
246 if(status
== NULL
|| U_FAILURE(*status
)){
250 bundle
= measurementDataBundleForLocale(localeID
, status
);
251 paperSizeBundle
= ures_getByKeyWithFallback(bundle
, PAPER_SIZE
, NULL
, status
);
252 paperSize
= ures_getIntVector(paperSizeBundle
, &len
, status
);
254 if(U_SUCCESS(*status
)){
256 *status
= U_INTERNAL_PROGRAM_ERROR
;
258 *height
= paperSize
[0];
259 *width
= paperSize
[1];
264 ures_close(paperSizeBundle
);
268 U_CAPI
void U_EXPORT2
269 ulocdata_getCLDRVersion(UVersionInfo versionArray
, UErrorCode
*status
) {
270 UResourceBundle
*rb
= NULL
;
271 rb
= ures_openDirect(NULL
, "supplementalData", status
);
272 ures_getVersionByKey(rb
, "cldrVersion", versionArray
, status
);
276 U_CAPI
int32_t U_EXPORT2
277 ulocdata_getLocaleDisplayPattern(ULocaleData
*uld
,
279 int32_t resultCapacity
,
280 UErrorCode
*status
) {
281 UResourceBundle
*patternBundle
;
283 const UChar
*pattern
= NULL
;
284 UErrorCode localStatus
= U_ZERO_ERROR
;
286 if (U_FAILURE(*status
))
289 patternBundle
= ures_getByKey(uld
->langBundle
, "localeDisplayPattern", NULL
, &localStatus
);
291 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
292 localStatus
= U_MISSING_RESOURCE_ERROR
;
295 if (localStatus
!= U_ZERO_ERROR
) {
296 *status
= localStatus
;
299 if (U_FAILURE(*status
)){
300 ures_close(patternBundle
);
304 pattern
= ures_getStringByKey(patternBundle
, "pattern", &len
, &localStatus
);
305 ures_close(patternBundle
);
307 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
308 localStatus
= U_MISSING_RESOURCE_ERROR
;
311 if (localStatus
!= U_ZERO_ERROR
) {
312 *status
= localStatus
;
315 if (U_FAILURE(*status
)){
319 u_strncpy(result
, pattern
, resultCapacity
);
324 U_CAPI
int32_t U_EXPORT2
325 ulocdata_getLocaleSeparator(ULocaleData
*uld
,
327 int32_t resultCapacity
,
328 UErrorCode
*status
) {
329 UResourceBundle
*separatorBundle
;
331 const UChar
*separator
= NULL
;
332 UErrorCode localStatus
= U_ZERO_ERROR
;
334 if (U_FAILURE(*status
))
337 separatorBundle
= ures_getByKey(uld
->langBundle
, "localeDisplayPattern", NULL
, &localStatus
);
339 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
340 localStatus
= U_MISSING_RESOURCE_ERROR
;
343 if (localStatus
!= U_ZERO_ERROR
) {
344 *status
= localStatus
;
347 if (U_FAILURE(*status
)){
348 ures_close(separatorBundle
);
352 separator
= ures_getStringByKey(separatorBundle
, "separator", &len
, &localStatus
);
353 ures_close(separatorBundle
);
355 if ( (localStatus
== U_USING_DEFAULT_WARNING
) && uld
->noSubstitute
) {
356 localStatus
= U_MISSING_RESOURCE_ERROR
;
359 if (localStatus
!= U_ZERO_ERROR
) {
360 *status
= localStatus
;
363 if (U_FAILURE(*status
)){
367 u_strncpy(result
, separator
, resultCapacity
);