]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/ulocdata.c
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / i18n / ulocdata.c
CommitLineData
374ca955
A
1/*
2******************************************************************************
3* *
46f4442e 4* Copyright (C) 2003-2007, 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
A
19#include "unicode/ulocdata.h"
20
374ca955
A
21#define MEASUREMENT_SYSTEM "MeasurementSystem"
22#define PAPER_SIZE "PaperSize"
23
73c04bcf
A
24/** A locale data object.
25 * For usage in C programs.
26 * @draft ICU 3.4
27 */
28struct ULocaleData {
29 /**
30 * Controls the "No Substitute" behavior of this locale data object
31 */
32 UBool noSubstitute;
33
34 /**
35 * Pointer to the resource bundle associated with this locale data object
36 */
37 UResourceBundle *bundle;
38};
39
40U_CAPI ULocaleData* U_EXPORT2
41ulocdata_open(const char *localeID, UErrorCode *status)
42{
43 ULocaleData *uld;
44
45 if (U_FAILURE(*status)) {
46 return NULL;
47 }
48
49 uld = (ULocaleData *)uprv_malloc(sizeof(ULocaleData));
50 if (uld == NULL) {
51 *status = U_MEMORY_ALLOCATION_ERROR;
52 return(NULL);
53 }
54
374ca955 55
73c04bcf
A
56 uld->noSubstitute = FALSE;
57 uld->bundle = ures_open(NULL, localeID, status);
58
59 if (U_FAILURE(*status)) {
60 uprv_free(uld);
61 return NULL;
62 }
63
64 return uld;
65}
66
67U_CAPI void U_EXPORT2
68ulocdata_close(ULocaleData *uld)
69{
70 if ( uld != NULL ) {
71 ures_close(uld->bundle);
72 uprv_free(uld);
73 }
74}
75
76U_CAPI void U_EXPORT2
77ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
78{
79 uld->noSubstitute = setting;
80}
81
82U_CAPI UBool U_EXPORT2
83ulocdata_getNoSubstitute(ULocaleData *uld)
84{
85 return uld->noSubstitute;
86}
87
88U_CAPI USet* U_EXPORT2
89ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
90 uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
91
46f4442e 92 static const char* const exemplarSetTypes[] = { "ExemplarCharacters", "AuxExemplarCharacters" };
374ca955
A
93 const UChar *exemplarChars = NULL;
94 int32_t len = 0;
95 UErrorCode localStatus = U_ZERO_ERROR;
96
73c04bcf 97 if (U_FAILURE(*status))
374ca955 98 return NULL;
374ca955 99
73c04bcf
A
100 exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
101 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
102 localStatus = U_MISSING_RESOURCE_ERROR;
374ca955 103 }
73c04bcf
A
104
105 if (localStatus != U_ZERO_ERROR) {
374ca955
A
106 *status = localStatus;
107 }
108
73c04bcf
A
109 if (U_FAILURE(*status))
110 return NULL;
111
112 if(fillIn != NULL)
374ca955
A
113 uset_applyPattern(fillIn, exemplarChars, len,
114 USET_IGNORE_SPACE | options, status);
73c04bcf 115 else
374ca955
A
116 fillIn = uset_openPatternOptions(exemplarChars, len,
117 USET_IGNORE_SPACE | options, status);
374ca955 118
374ca955
A
119 return fillIn;
120
121}
122
73c04bcf
A
123U_CAPI int32_t U_EXPORT2
124ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
125 UChar *result, int32_t resultLength, UErrorCode *status){
126
46f4442e
A
127 static const char* const delimiterKeys[] = {
128 "quotationStart",
129 "quotationEnd",
130 "alternateQuotationStart",
131 "alternateQuotationEnd"
132 };
73c04bcf
A
133
134 UResourceBundle *delimiterBundle;
135 int32_t len = 0;
136 const UChar *delimiter = NULL;
137 UErrorCode localStatus = U_ZERO_ERROR;
138
139 if (U_FAILURE(*status))
140 return 0;
141
142 delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus);
143
144 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
145 localStatus = U_MISSING_RESOURCE_ERROR;
146 }
147
148 if (localStatus != U_ZERO_ERROR) {
149 *status = localStatus;
150 }
151
152 if (U_FAILURE(*status)){
153 ures_close(delimiterBundle);
154 return 0;
155 }
156
157 delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
158 ures_close(delimiterBundle);
159
160 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
161 localStatus = U_MISSING_RESOURCE_ERROR;
162 }
163
164 if (localStatus != U_ZERO_ERROR) {
165 *status = localStatus;
166 }
167
168 if (U_FAILURE(*status)){
169 return 0;
170 }
171
172 u_strncpy(result,delimiter,resultLength);
173 return len;
174}
175
374ca955
A
176U_CAPI UMeasurementSystem U_EXPORT2
177ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
178
179 UResourceBundle* bundle=NULL;
180 UResourceBundle* measurement=NULL;
181 UMeasurementSystem system = UMS_LIMIT;
182
183 if(status == NULL || U_FAILURE(*status)){
184 return system;
185 }
186
187 bundle = ures_open(NULL, localeID, status);
188
189 measurement = ures_getByKey(bundle, MEASUREMENT_SYSTEM, NULL, status);
190
191 system = (UMeasurementSystem) ures_getInt(measurement, status);
192
193 ures_close(bundle);
194 ures_close(measurement);
195
196 return system;
197
198}
199
200U_CAPI void U_EXPORT2
201ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
202 UResourceBundle* bundle=NULL;
203 UResourceBundle* paperSizeBundle = NULL;
204 const int32_t* paperSize=NULL;
205 int32_t len = 0;
206
207 if(status == NULL || U_FAILURE(*status)){
208 return;
209 }
210
211 bundle = ures_open(NULL, localeID, status);
212 paperSizeBundle = ures_getByKey(bundle, PAPER_SIZE, NULL, status);
213 paperSize = ures_getIntVector(paperSizeBundle, &len, status);
214
215 if(U_SUCCESS(*status)){
216 if(len < 2){
217 *status = U_INTERNAL_PROGRAM_ERROR;
218 }else{
219 *height = paperSize[0];
220 *width = paperSize[1];
221 }
222 }
223
224 ures_close(bundle);
225 ures_close(paperSizeBundle);
226
227}