]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/ulocdata.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / ulocdata.c
CommitLineData
374ca955
A
1/*
2******************************************************************************
3* *
73c04bcf 4* Copyright (C) 2003-2005, 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
92 const char* 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
127 const char* delimiterKeys[] = { "quotationStart",
128 "quotationEnd",
129 "alternateQuotationStart",
130 "alternateQuotationEnd" };
131
132
133 UResourceBundle *delimiterBundle;
134 int32_t len = 0;
135 const UChar *delimiter = NULL;
136 UErrorCode localStatus = U_ZERO_ERROR;
137
138 if (U_FAILURE(*status))
139 return 0;
140
141 delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus);
142
143 if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
144 localStatus = U_MISSING_RESOURCE_ERROR;
145 }
146
147 if (localStatus != U_ZERO_ERROR) {
148 *status = localStatus;
149 }
150
151 if (U_FAILURE(*status)){
152 ures_close(delimiterBundle);
153 return 0;
154 }
155
156 delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
157 ures_close(delimiterBundle);
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 return 0;
169 }
170
171 u_strncpy(result,delimiter,resultLength);
172 return len;
173}
174
374ca955
A
175U_CAPI UMeasurementSystem U_EXPORT2
176ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
177
178 UResourceBundle* bundle=NULL;
179 UResourceBundle* measurement=NULL;
180 UMeasurementSystem system = UMS_LIMIT;
181
182 if(status == NULL || U_FAILURE(*status)){
183 return system;
184 }
185
186 bundle = ures_open(NULL, localeID, status);
187
188 measurement = ures_getByKey(bundle, MEASUREMENT_SYSTEM, NULL, status);
189
190 system = (UMeasurementSystem) ures_getInt(measurement, status);
191
192 ures_close(bundle);
193 ures_close(measurement);
194
195 return system;
196
197}
198
199U_CAPI void U_EXPORT2
200ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
201 UResourceBundle* bundle=NULL;
202 UResourceBundle* paperSizeBundle = NULL;
203 const int32_t* paperSize=NULL;
204 int32_t len = 0;
205
206 if(status == NULL || U_FAILURE(*status)){
207 return;
208 }
209
210 bundle = ures_open(NULL, localeID, status);
211 paperSizeBundle = ures_getByKey(bundle, PAPER_SIZE, NULL, status);
212 paperSize = ures_getIntVector(paperSizeBundle, &len, status);
213
214 if(U_SUCCESS(*status)){
215 if(len < 2){
216 *status = U_INTERNAL_PROGRAM_ERROR;
217 }else{
218 *height = paperSize[0];
219 *width = paperSize[1];
220 }
221 }
222
223 ures_close(bundle);
224 ures_close(paperSizeBundle);
225
226}