]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/ulocdata.c
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / i18n / ulocdata.c
CommitLineData
374ca955
A
1/*
2******************************************************************************
3* *
729e4ab9 4* Copyright (C) 2003-2009, 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"
374ca955 19#include "unicode/ulocdata.h"
729e4ab9
A
20#include "umutex.h"
21#include "uresimp.h"
22#include "ureslocs.h"
374ca955 23
374ca955
A
24#define MEASUREMENT_SYSTEM "MeasurementSystem"
25#define PAPER_SIZE "PaperSize"
26
73c04bcf
A
27/** A locale data object.
28 * For usage in C programs.
29 * @draft ICU 3.4
30 */
31struct ULocaleData {
32 /**
33 * Controls the "No Substitute" behavior of this locale data object
34 */
35 UBool noSubstitute;
36
37 /**
38 * Pointer to the resource bundle associated with this locale data object
39 */
40 UResourceBundle *bundle;
729e4ab9
A
41
42 /**
43 * Pointer to the lang resource bundle associated with this locale data object
44 */
45 UResourceBundle *langBundle;
73c04bcf
A
46};
47
48U_CAPI ULocaleData* U_EXPORT2
49ulocdata_open(const char *localeID, UErrorCode *status)
50{
51 ULocaleData *uld;
729e4ab9 52
73c04bcf
A
53 if (U_FAILURE(*status)) {
54 return NULL;
55 }
56
57 uld = (ULocaleData *)uprv_malloc(sizeof(ULocaleData));
58 if (uld == NULL) {
59 *status = U_MEMORY_ALLOCATION_ERROR;
60 return(NULL);
61 }
62
729e4ab9
A
63 uld->langBundle = NULL;
64
73c04bcf
A
65 uld->noSubstitute = FALSE;
66 uld->bundle = ures_open(NULL, localeID, status);
729e4ab9 67 uld->langBundle = ures_open(U_ICUDATA_LANG, localeID, status);
73c04bcf
A
68
69 if (U_FAILURE(*status)) {
70 uprv_free(uld);
71 return NULL;
72 }
73
74 return uld;
75}
76
77U_CAPI void U_EXPORT2
78ulocdata_close(ULocaleData *uld)
79{
80 if ( uld != NULL ) {
729e4ab9 81 ures_close(uld->langBundle);
73c04bcf
A
82 ures_close(uld->bundle);
83 uprv_free(uld);
729e4ab9 84 }
73c04bcf
A
85}
86
87U_CAPI void U_EXPORT2
88ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
89{
90 uld->noSubstitute = setting;
91}
92
93U_CAPI UBool U_EXPORT2
94ulocdata_getNoSubstitute(ULocaleData *uld)
95{
96 return uld->noSubstitute;
97}
98
99U_CAPI USet* U_EXPORT2
729e4ab9 100ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
73c04bcf 101 uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
729e4ab9 102
46f4442e 103 static const char* const exemplarSetTypes[] = { "ExemplarCharacters", "AuxExemplarCharacters" };
374ca955
A
104 const UChar *exemplarChars = NULL;
105 int32_t len = 0;
106 UErrorCode localStatus = U_ZERO_ERROR;
107
73c04bcf 108 if (U_FAILURE(*status))
374ca955 109 return NULL;
729e4ab9 110
73c04bcf
A
111 exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
112 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
113 localStatus = U_MISSING_RESOURCE_ERROR;
374ca955 114 }
729e4ab9 115
73c04bcf 116 if (localStatus != U_ZERO_ERROR) {
374ca955
A
117 *status = localStatus;
118 }
729e4ab9 119
73c04bcf
A
120 if (U_FAILURE(*status))
121 return NULL;
729e4ab9 122
73c04bcf 123 if(fillIn != NULL)
729e4ab9 124 uset_applyPattern(fillIn, exemplarChars, len,
374ca955 125 USET_IGNORE_SPACE | options, status);
73c04bcf 126 else
374ca955
A
127 fillIn = uset_openPatternOptions(exemplarChars, len,
128 USET_IGNORE_SPACE | options, status);
729e4ab9 129
374ca955
A
130 return fillIn;
131
132}
133
73c04bcf 134U_CAPI int32_t U_EXPORT2
729e4ab9 135ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
73c04bcf
A
136 UChar *result, int32_t resultLength, UErrorCode *status){
137
46f4442e
A
138 static const char* const delimiterKeys[] = {
139 "quotationStart",
140 "quotationEnd",
141 "alternateQuotationStart",
142 "alternateQuotationEnd"
143 };
729e4ab9 144
73c04bcf
A
145 UResourceBundle *delimiterBundle;
146 int32_t len = 0;
147 const UChar *delimiter = NULL;
148 UErrorCode localStatus = U_ZERO_ERROR;
149
150 if (U_FAILURE(*status))
151 return 0;
152
153 delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus);
154
155 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
156 localStatus = U_MISSING_RESOURCE_ERROR;
157 }
158
159 if (localStatus != U_ZERO_ERROR) {
160 *status = localStatus;
161 }
162
163 if (U_FAILURE(*status)){
164 ures_close(delimiterBundle);
165 return 0;
166 }
167
168 delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
169 ures_close(delimiterBundle);
170
171 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
172 localStatus = U_MISSING_RESOURCE_ERROR;
173 }
174
175 if (localStatus != U_ZERO_ERROR) {
176 *status = localStatus;
177 }
729e4ab9 178
73c04bcf
A
179 if (U_FAILURE(*status)){
180 return 0;
181 }
729e4ab9
A
182
183 u_strncpy(result,delimiter, resultLength);
73c04bcf
A
184 return len;
185}
186
374ca955
A
187U_CAPI UMeasurementSystem U_EXPORT2
188ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
729e4ab9 189
374ca955
A
190 UResourceBundle* bundle=NULL;
191 UResourceBundle* measurement=NULL;
729e4ab9
A
192 UMeasurementSystem system = UMS_LIMIT;
193
374ca955
A
194 if(status == NULL || U_FAILURE(*status)){
195 return system;
196 }
729e4ab9 197
374ca955
A
198 bundle = ures_open(NULL, localeID, status);
199
729e4ab9 200 measurement = ures_getByKeyWithFallback(bundle, MEASUREMENT_SYSTEM, NULL, status);
374ca955
A
201
202 system = (UMeasurementSystem) ures_getInt(measurement, status);
203
204 ures_close(bundle);
205 ures_close(measurement);
206
207 return system;
208
209}
210
211U_CAPI void U_EXPORT2
212ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
213 UResourceBundle* bundle=NULL;
214 UResourceBundle* paperSizeBundle = NULL;
215 const int32_t* paperSize=NULL;
216 int32_t len = 0;
217
218 if(status == NULL || U_FAILURE(*status)){
219 return;
220 }
729e4ab9 221
374ca955 222 bundle = ures_open(NULL, localeID, status);
729e4ab9 223 paperSizeBundle = ures_getByKeyWithFallback(bundle, PAPER_SIZE, NULL, status);
374ca955 224 paperSize = ures_getIntVector(paperSizeBundle, &len, status);
729e4ab9 225
374ca955
A
226 if(U_SUCCESS(*status)){
227 if(len < 2){
228 *status = U_INTERNAL_PROGRAM_ERROR;
229 }else{
230 *height = paperSize[0];
231 *width = paperSize[1];
232 }
233 }
729e4ab9 234
374ca955
A
235 ures_close(bundle);
236 ures_close(paperSizeBundle);
729e4ab9
A
237
238}
239
240U_DRAFT void U_EXPORT2
241ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) {
242 UResourceBundle *rb = NULL;
243 rb = ures_openDirect(NULL, "supplementalData", status);
244 ures_getVersionByKey(rb, "cldrVersion", versionArray, status);
245 ures_close(rb);
246}
247
248U_DRAFT int32_t U_EXPORT2
249ulocdata_getLocaleDisplayPattern(ULocaleData *uld,
250 UChar *result,
251 int32_t resultCapacity,
252 UErrorCode *status) {
253 UResourceBundle *patternBundle;
254 int32_t len = 0;
255 const UChar *pattern = NULL;
256 UErrorCode localStatus = U_ZERO_ERROR;
257
258 if (U_FAILURE(*status))
259 return 0;
260
261 patternBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
262
263 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
264 localStatus = U_MISSING_RESOURCE_ERROR;
265 }
266
267 if (localStatus != U_ZERO_ERROR) {
268 *status = localStatus;
269 }
270
271 if (U_FAILURE(*status)){
272 ures_close(patternBundle);
273 return 0;
274 }
275
276 pattern = ures_getStringByKey(patternBundle, "pattern", &len, &localStatus);
277 ures_close(patternBundle);
278
279 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
280 localStatus = U_MISSING_RESOURCE_ERROR;
281 }
282
283 if (localStatus != U_ZERO_ERROR) {
284 *status = localStatus;
285 }
286
287 if (U_FAILURE(*status)){
288 return 0;
289 }
290
291 u_strncpy(result, pattern, resultCapacity);
292 return len;
293}
294
295
296U_DRAFT int32_t U_EXPORT2
297ulocdata_getLocaleSeparator(ULocaleData *uld,
298 UChar *result,
299 int32_t resultCapacity,
300 UErrorCode *status) {
301 UResourceBundle *separatorBundle;
302 int32_t len = 0;
303 const UChar *separator = NULL;
304 UErrorCode localStatus = U_ZERO_ERROR;
305
306 if (U_FAILURE(*status))
307 return 0;
308
309 separatorBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
310
311 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
312 localStatus = U_MISSING_RESOURCE_ERROR;
313 }
314
315 if (localStatus != U_ZERO_ERROR) {
316 *status = localStatus;
317 }
318
319 if (U_FAILURE(*status)){
320 ures_close(separatorBundle);
321 return 0;
322 }
323
324 separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus);
325 ures_close(separatorBundle);
326
327 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
328 localStatus = U_MISSING_RESOURCE_ERROR;
329 }
330
331 if (localStatus != U_ZERO_ERROR) {
332 *status = localStatus;
333 }
334
335 if (U_FAILURE(*status)){
336 return 0;
337 }
338
339 u_strncpy(result, separator, resultCapacity);
340 return len;
374ca955 341}