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