1 /*
2 ******************************************************************************
3 * *
4 * Copyright (C) 2003-2007, International Business Machines *
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
14 * created by: Ram Viswanadha,John Emmons
15 */
16
17 #include "cmemory.h"
18 #include "unicode/ustring.h"
19 #include "unicode/ulocdata.h"
20
21 #define MEASUREMENT_SYSTEM "MeasurementSystem"
22 #define PAPER_SIZE "PaperSize"
23
24 /** A locale data object.
25 * For usage in C programs.
26 * @draft ICU 3.4
27 */
28 struct 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
40 U_CAPI ULocaleData* U_EXPORT2
ulocdata_open(const char * localeID,UErrorCode * status)41 ulocdata_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
55
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
67 U_CAPI void U_EXPORT2
ulocdata_close(ULocaleData * uld)68 ulocdata_close(ULocaleData *uld)
69 {
70 if ( uld != NULL ) {
71 ures_close(uld->bundle);
72 uprv_free(uld);
73 }
74 }
75
76 U_CAPI void U_EXPORT2
ulocdata_setNoSubstitute(ULocaleData * uld,UBool setting)77 ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
78 {
79 uld->noSubstitute = setting;
80 }
81
82 U_CAPI UBool U_EXPORT2
ulocdata_getNoSubstitute(ULocaleData * uld)83 ulocdata_getNoSubstitute(ULocaleData *uld)
84 {
85 return uld->noSubstitute;
86 }
87
88 U_CAPI USet* U_EXPORT2
ulocdata_getExemplarSet(ULocaleData * uld,USet * fillIn,uint32_t options,ULocaleDataExemplarSetType extype,UErrorCode * status)89 ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
90 uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
91
92 static const char* const exemplarSetTypes[] = { "ExemplarCharacters", "AuxExemplarCharacters" };
93 const UChar *exemplarChars = NULL;
94 int32_t len = 0;
95 UErrorCode localStatus = U_ZERO_ERROR;
96
97 if (U_FAILURE(*status))
98 return NULL;
99
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;
103 }
104
105 if (localStatus != U_ZERO_ERROR) {
106 *status = localStatus;
107 }
108
109 if (U_FAILURE(*status))
110 return NULL;
111
112 if(fillIn != NULL)
113 uset_applyPattern(fillIn, exemplarChars, len,
114 USET_IGNORE_SPACE | options, status);
115 else
116 fillIn = uset_openPatternOptions(exemplarChars, len,
117 USET_IGNORE_SPACE | options, status);
118
119 return fillIn;
120
121 }
122
123 U_CAPI int32_t U_EXPORT2
ulocdata_getDelimiter(ULocaleData * uld,ULocaleDataDelimiterType type,UChar * result,int32_t resultLength,UErrorCode * status)124 ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
125 UChar *result, int32_t resultLength, UErrorCode *status){
126
127 static const char* const delimiterKeys[] = {
128 "quotationStart",
129 "quotationEnd",
130 "alternateQuotationStart",
131 "alternateQuotationEnd"
132 };
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
176 U_CAPI UMeasurementSystem U_EXPORT2
ulocdata_getMeasurementSystem(const char * localeID,UErrorCode * status)177 ulocdata_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
200 U_CAPI void U_EXPORT2
ulocdata_getPaperSize(const char * localeID,int32_t * height,int32_t * width,UErrorCode * status)201 ulocdata_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 }
228