]>
Commit | Line | Data |
---|---|---|
57a6839d A |
1 | /* |
2 | ***************************************************************************************** | |
2ca993e8 | 3 | * Copyright (C) 2014-2016 Apple Inc. All Rights Reserved. |
57a6839d A |
4 | ***************************************************************************************** |
5 | */ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
2ca993e8 | 11 | #include <stdlib.h> |
57a6839d A |
12 | #include "unicode/uameasureformat.h" |
13 | #include "unicode/fieldpos.h" | |
14 | #include "unicode/localpointer.h" | |
15 | #include "unicode/numfmt.h" | |
16 | #include "unicode/measunit.h" | |
17 | #include "unicode/measure.h" | |
18 | #include "unicode/measfmt.h" | |
19 | #include "unicode/unistr.h" | |
20 | #include "unicode/unum.h" | |
21 | #include "unicode/umisc.h" | |
22 | #include "unicode/ures.h" | |
23 | #include "uresimp.h" | |
24 | #include "ustr_imp.h" | |
2ca993e8 A |
25 | #include "cstring.h" |
26 | #include "ulocimp.h" | |
57a6839d A |
27 | |
28 | U_NAMESPACE_USE | |
29 | ||
30 | ||
31 | U_CAPI UAMeasureFormat* U_EXPORT2 | |
32 | uameasfmt_open( const char* locale, | |
33 | UAMeasureFormatWidth width, | |
34 | UNumberFormat* nfToAdopt, | |
35 | UErrorCode* status ) | |
36 | { | |
37 | if (U_FAILURE(*status)) { | |
38 | return NULL; | |
39 | } | |
40 | UMeasureFormatWidth mfWidth = UMEASFMT_WIDTH_WIDE; | |
41 | switch (width) { | |
42 | case UAMEASFMT_WIDTH_WIDE: | |
43 | break; | |
44 | case UAMEASFMT_WIDTH_SHORT: | |
45 | mfWidth = UMEASFMT_WIDTH_SHORT; break; | |
46 | case UAMEASFMT_WIDTH_NARROW: | |
47 | mfWidth = UMEASFMT_WIDTH_NARROW; break; | |
48 | case UAMEASFMT_WIDTH_NUMERIC: | |
49 | mfWidth = UMEASFMT_WIDTH_NUMERIC; break; | |
2ca993e8 A |
50 | case UAMEASFMT_WIDTH_SHORTER: |
51 | mfWidth = UMEASFMT_WIDTH_SHORTER; break; | |
57a6839d A |
52 | default: |
53 | *status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; | |
54 | } | |
55 | LocalPointer<MeasureFormat> measfmt( new MeasureFormat(Locale(locale), mfWidth, (NumberFormat*)nfToAdopt, *status) ); | |
56 | if (U_FAILURE(*status)) { | |
57 | return NULL; | |
58 | } | |
59 | return (UAMeasureFormat*)measfmt.orphan(); | |
60 | } | |
61 | ||
62 | ||
63 | U_CAPI void U_EXPORT2 | |
64 | uameasfmt_close(UAMeasureFormat *measfmt) | |
65 | { | |
66 | delete (MeasureFormat*)measfmt; | |
67 | } | |
68 | ||
69 | static MeasureUnit * createObjectForMeasureUnit(UAMeasureUnit unit, UErrorCode* status ) | |
70 | { | |
71 | MeasureUnit * munit = NULL; | |
72 | switch (unit) { | |
73 | case UAMEASUNIT_ACCELERATION_G_FORCE: munit = MeasureUnit::createGForce(*status); break; | |
b331163b | 74 | case UAMEASUNIT_ACCELERATION_METER_PER_SECOND_SQUARED: munit = MeasureUnit::createMeterPerSecondSquared(*status); break; |
57a6839d A |
75 | |
76 | case UAMEASUNIT_ANGLE_DEGREE: munit = MeasureUnit::createDegree(*status); break; | |
77 | case UAMEASUNIT_ANGLE_ARC_MINUTE: munit = MeasureUnit::createArcMinute(*status); break; | |
78 | case UAMEASUNIT_ANGLE_ARC_SECOND: munit = MeasureUnit::createArcSecond(*status); break; | |
b331163b | 79 | case UAMEASUNIT_ANGLE_RADIAN: munit = MeasureUnit::createRadian(*status); break; |
2ca993e8 | 80 | case UAMEASUNIT_ANGLE_REVOLUTION: munit = MeasureUnit::createRevolutionAngle(*status); break; |
57a6839d A |
81 | |
82 | case UAMEASUNIT_AREA_SQUARE_METER: munit = MeasureUnit::createSquareMeter(*status); break; | |
83 | case UAMEASUNIT_AREA_SQUARE_KILOMETER: munit = MeasureUnit::createSquareKilometer(*status); break; | |
84 | case UAMEASUNIT_AREA_SQUARE_FOOT: munit = MeasureUnit::createSquareFoot(*status); break; | |
85 | case UAMEASUNIT_AREA_SQUARE_MILE: munit = MeasureUnit::createSquareMile(*status); break; | |
86 | case UAMEASUNIT_AREA_ACRE: munit = MeasureUnit::createAcre(*status); break; | |
87 | case UAMEASUNIT_AREA_HECTARE: munit = MeasureUnit::createHectare(*status); break; | |
b331163b A |
88 | case UAMEASUNIT_AREA_SQUARE_CENTIMETER: munit = MeasureUnit::createSquareCentimeter(*status); break; |
89 | case UAMEASUNIT_AREA_SQUARE_INCH: munit = MeasureUnit::createSquareInch(*status); break; | |
90 | case UAMEASUNIT_AREA_SQUARE_YARD: munit = MeasureUnit::createSquareYard(*status); break; | |
57a6839d A |
91 | |
92 | case UAMEASUNIT_DURATION_YEAR: munit = MeasureUnit::createYear(*status); break; | |
93 | case UAMEASUNIT_DURATION_MONTH: munit = MeasureUnit::createMonth(*status); break; | |
2ca993e8 A |
94 | case UAMEASUNIT_DURATION_WEEK: munit = MeasureUnit::createWeek(*status); break; |
95 | case UAMEASUNIT_DURATION_DAY: munit = MeasureUnit::createDay(*status); break; | |
57a6839d A |
96 | case UAMEASUNIT_DURATION_HOUR: munit = MeasureUnit::createHour(*status); break; |
97 | case UAMEASUNIT_DURATION_MINUTE: munit = MeasureUnit::createMinute(*status); break; | |
98 | case UAMEASUNIT_DURATION_SECOND: munit = MeasureUnit::createSecond(*status); break; | |
99 | case UAMEASUNIT_DURATION_MILLISECOND: munit = MeasureUnit::createMillisecond(*status); break; | |
b331163b A |
100 | case UAMEASUNIT_DURATION_MICROSECOND: munit = MeasureUnit::createMicrosecond(*status); break; |
101 | case UAMEASUNIT_DURATION_NANOSECOND: munit = MeasureUnit::createNanosecond(*status); break; | |
2ca993e8 | 102 | case UAMEASUNIT_DURATION_CENTURY: munit = MeasureUnit::createCentury(*status); break; |
57a6839d A |
103 | |
104 | case UAMEASUNIT_LENGTH_METER: munit = MeasureUnit::createMeter(*status); break; | |
105 | case UAMEASUNIT_LENGTH_CENTIMETER: munit = MeasureUnit::createCentimeter(*status); break; | |
106 | case UAMEASUNIT_LENGTH_KILOMETER: munit = MeasureUnit::createKilometer(*status); break; | |
107 | case UAMEASUNIT_LENGTH_MILLIMETER: munit = MeasureUnit::createMillimeter(*status); break; | |
108 | case UAMEASUNIT_LENGTH_PICOMETER: munit = MeasureUnit::createPicometer(*status); break; | |
109 | case UAMEASUNIT_LENGTH_FOOT: munit = MeasureUnit::createFoot(*status); break; | |
110 | case UAMEASUNIT_LENGTH_INCH: munit = MeasureUnit::createInch(*status); break; | |
111 | case UAMEASUNIT_LENGTH_MILE: munit = MeasureUnit::createMile(*status); break; | |
112 | case UAMEASUNIT_LENGTH_YARD: munit = MeasureUnit::createYard(*status); break; | |
113 | case UAMEASUNIT_LENGTH_LIGHT_YEAR: munit = MeasureUnit::createLightYear(*status); break; | |
b331163b A |
114 | case UAMEASUNIT_LENGTH_DECIMETER: munit = MeasureUnit::createDecimeter(*status); break; |
115 | case UAMEASUNIT_LENGTH_MICROMETER: munit = MeasureUnit::createMicrometer(*status); break; | |
116 | case UAMEASUNIT_LENGTH_NANOMETER: munit = MeasureUnit::createNanometer(*status); break; | |
117 | case UAMEASUNIT_LENGTH_NAUTICAL_MILE: munit = MeasureUnit::createNauticalMile(*status); break; | |
118 | case UAMEASUNIT_LENGTH_FATHOM: munit = MeasureUnit::createFathom(*status); break; | |
119 | case UAMEASUNIT_LENGTH_FURLONG: munit = MeasureUnit::createFurlong(*status); break; | |
120 | case UAMEASUNIT_LENGTH_ASTRONOMICAL_UNIT: munit = MeasureUnit::createAstronomicalUnit(*status); break; | |
121 | case UAMEASUNIT_LENGTH_PARSEC: munit = MeasureUnit::createParsec(*status); break; | |
2ca993e8 | 122 | case UAMEASUNIT_LENGTH_MILE_SCANDINAVIAN: munit = MeasureUnit::createMileScandinavian(*status); break; |
f3c0d7a5 | 123 | case UAMEASUNIT_LENGTH_POINT: munit = MeasureUnit::createPoint(*status); break; |
57a6839d A |
124 | |
125 | case UAMEASUNIT_MASS_GRAM: munit = MeasureUnit::createGram(*status); break; | |
126 | case UAMEASUNIT_MASS_KILOGRAM: munit = MeasureUnit::createKilogram(*status); break; | |
127 | case UAMEASUNIT_MASS_OUNCE: munit = MeasureUnit::createOunce(*status); break; | |
128 | case UAMEASUNIT_MASS_POUND: munit = MeasureUnit::createPound(*status); break; | |
129 | case UAMEASUNIT_MASS_STONE: munit = MeasureUnit::createStone(*status); break; | |
b331163b A |
130 | case UAMEASUNIT_MASS_MICROGRAM: munit = MeasureUnit::createMicrogram(*status); break; |
131 | case UAMEASUNIT_MASS_MILLIGRAM: munit = MeasureUnit::createMilligram(*status); break; | |
132 | case UAMEASUNIT_MASS_METRIC_TON: munit = MeasureUnit::createMetricTon(*status); break; | |
133 | case UAMEASUNIT_MASS_TON: munit = MeasureUnit::createTon(*status); break; | |
134 | case UAMEASUNIT_MASS_CARAT: munit = MeasureUnit::createCarat(*status); break; | |
135 | case UAMEASUNIT_MASS_OUNCE_TROY: munit = MeasureUnit::createOunceTroy(*status); break; | |
57a6839d A |
136 | |
137 | case UAMEASUNIT_POWER_WATT: munit = MeasureUnit::createWatt(*status); break; | |
138 | case UAMEASUNIT_POWER_KILOWATT: munit = MeasureUnit::createKilowatt(*status); break; | |
139 | case UAMEASUNIT_POWER_HORSEPOWER: munit = MeasureUnit::createHorsepower(*status); break; | |
b331163b A |
140 | case UAMEASUNIT_POWER_MILLIWATT: munit = MeasureUnit::createMilliwatt(*status); break; |
141 | case UAMEASUNIT_POWER_MEGAWATT: munit = MeasureUnit::createMegawatt(*status); break; | |
142 | case UAMEASUNIT_POWER_GIGAWATT: munit = MeasureUnit::createGigawatt(*status); break; | |
57a6839d A |
143 | |
144 | case UAMEASUNIT_PRESSURE_HECTOPASCAL: munit = MeasureUnit::createHectopascal(*status); break; | |
145 | case UAMEASUNIT_PRESSURE_INCH_HG: munit = MeasureUnit::createInchHg(*status); break; | |
146 | case UAMEASUNIT_PRESSURE_MILLIBAR: munit = MeasureUnit::createMillibar(*status); break; | |
b331163b A |
147 | case UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY: munit = MeasureUnit::createMillimeterOfMercury(*status); break; |
148 | case UAMEASUNIT_PRESSURE_POUND_PER_SQUARE_INCH: munit = MeasureUnit::createPoundPerSquareInch(*status); break; | |
57a6839d A |
149 | |
150 | case UAMEASUNIT_SPEED_METER_PER_SECOND: munit = MeasureUnit::createMeterPerSecond(*status); break; | |
151 | case UAMEASUNIT_SPEED_KILOMETER_PER_HOUR: munit = MeasureUnit::createKilometerPerHour(*status); break; | |
152 | case UAMEASUNIT_SPEED_MILE_PER_HOUR: munit = MeasureUnit::createMilePerHour(*status); break; | |
2ca993e8 | 153 | case UAMEASUNIT_SPEED_KNOT: munit = MeasureUnit::createKnot(*status); break; |
57a6839d A |
154 | |
155 | case UAMEASUNIT_TEMPERATURE_CELSIUS: munit = MeasureUnit::createCelsius(*status); break; | |
156 | case UAMEASUNIT_TEMPERATURE_FAHRENHEIT: munit = MeasureUnit::createFahrenheit(*status); break; | |
b331163b A |
157 | case UAMEASUNIT_TEMPERATURE_KELVIN: munit = MeasureUnit::createKelvin(*status); break; |
158 | case UAMEASUNIT_TEMPERATURE_GENERIC: munit = MeasureUnit::createGenericTemperature(*status); break; | |
57a6839d A |
159 | |
160 | case UAMEASUNIT_VOLUME_LITER: munit = MeasureUnit::createLiter(*status); break; | |
161 | case UAMEASUNIT_VOLUME_CUBIC_KILOMETER: munit = MeasureUnit::createCubicKilometer(*status); break; | |
162 | case UAMEASUNIT_VOLUME_CUBIC_MILE: munit = MeasureUnit::createCubicMile(*status); break; | |
b331163b A |
163 | case UAMEASUNIT_VOLUME_MILLILITER: munit = MeasureUnit::createMilliliter(*status); break; |
164 | case UAMEASUNIT_VOLUME_CENTILITER: munit = MeasureUnit::createCentiliter(*status); break; | |
165 | case UAMEASUNIT_VOLUME_DECILITER: munit = MeasureUnit::createDeciliter(*status); break; | |
166 | case UAMEASUNIT_VOLUME_HECTOLITER: munit = MeasureUnit::createHectoliter(*status); break; | |
167 | case UAMEASUNIT_VOLUME_MEGALITER: munit = MeasureUnit::createMegaliter(*status); break; | |
168 | case UAMEASUNIT_VOLUME_CUBIC_CENTIMETER: munit = MeasureUnit::createCubicCentimeter(*status); break; | |
169 | case UAMEASUNIT_VOLUME_CUBIC_METER: munit = MeasureUnit::createCubicMeter(*status); break; | |
170 | case UAMEASUNIT_VOLUME_CUBIC_INCH: munit = MeasureUnit::createCubicInch(*status); break; | |
171 | case UAMEASUNIT_VOLUME_CUBIC_FOOT: munit = MeasureUnit::createCubicFoot(*status); break; | |
172 | case UAMEASUNIT_VOLUME_CUBIC_YARD: munit = MeasureUnit::createCubicYard(*status); break; | |
173 | case UAMEASUNIT_VOLUME_ACRE_FOOT: munit = MeasureUnit::createAcreFoot(*status); break; | |
174 | case UAMEASUNIT_VOLUME_BUSHEL: munit = MeasureUnit::createBushel(*status); break; | |
175 | case UAMEASUNIT_VOLUME_TEASPOON: munit = MeasureUnit::createTeaspoon(*status); break; | |
176 | case UAMEASUNIT_VOLUME_TABLESPOON: munit = MeasureUnit::createTablespoon(*status); break; | |
177 | case UAMEASUNIT_VOLUME_FLUID_OUNCE: munit = MeasureUnit::createFluidOunce(*status); break; | |
178 | case UAMEASUNIT_VOLUME_CUP: munit = MeasureUnit::createCup(*status); break; | |
179 | case UAMEASUNIT_VOLUME_PINT: munit = MeasureUnit::createPint(*status); break; | |
180 | case UAMEASUNIT_VOLUME_QUART: munit = MeasureUnit::createQuart(*status); break; | |
181 | case UAMEASUNIT_VOLUME_GALLON: munit = MeasureUnit::createGallon(*status); break; | |
2ca993e8 A |
182 | case UAMEASUNIT_VOLUME_CUP_METRIC: munit = MeasureUnit::createCupMetric(*status); break; |
183 | case UAMEASUNIT_VOLUME_PINT_METRIC: munit = MeasureUnit::createPintMetric(*status); break; | |
184 | case UAMEASUNIT_VOLUME_GALLON_IMPERIAL: munit = MeasureUnit::createGallonImperial(*status); break; | |
57a6839d A |
185 | |
186 | case UAMEASUNIT_ENERGY_JOULE: munit = MeasureUnit::createJoule(*status); break; | |
187 | case UAMEASUNIT_ENERGY_KILOJOULE: munit = MeasureUnit::createKilojoule(*status); break; | |
188 | case UAMEASUNIT_ENERGY_CALORIE: munit = MeasureUnit::createCalorie(*status); break; | |
189 | case UAMEASUNIT_ENERGY_KILOCALORIE: munit = MeasureUnit::createKilocalorie(*status); break; | |
190 | case UAMEASUNIT_ENERGY_FOODCALORIE: munit = MeasureUnit::createFoodcalorie(*status); break; | |
b331163b A |
191 | case UAMEASUNIT_ENERGY_KILOWATT_HOUR: munit = MeasureUnit::createKilowattHour(*status); break; |
192 | ||
193 | case UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER: munit = MeasureUnit::createLiterPerKilometer(*status); break; | |
194 | case UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON: munit = MeasureUnit::createMilePerGallon(*status); break; | |
2ca993e8 A |
195 | case UAMEASUNIT_CONSUMPTION_LITER_PER_100_KILOMETERs: munit = MeasureUnit::createLiterPer100Kilometers(*status); break; |
196 | case UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON_IMPERIAL: munit = MeasureUnit::createMilePerGallonImperial(*status); break; | |
b331163b A |
197 | |
198 | case UAMEASUNIT_DIGITAL_BIT: munit = MeasureUnit::createBit(*status); break; | |
199 | case UAMEASUNIT_DIGITAL_BYTE: munit = MeasureUnit::createByte(*status); break; | |
200 | case UAMEASUNIT_DIGITAL_GIGABIT: munit = MeasureUnit::createGigabit(*status); break; | |
201 | case UAMEASUNIT_DIGITAL_GIGABYTE: munit = MeasureUnit::createGigabyte(*status); break; | |
202 | case UAMEASUNIT_DIGITAL_KILOBIT: munit = MeasureUnit::createKilobit(*status); break; | |
203 | case UAMEASUNIT_DIGITAL_KILOBYTE: munit = MeasureUnit::createKilobyte(*status); break; | |
204 | case UAMEASUNIT_DIGITAL_MEGABIT: munit = MeasureUnit::createMegabit(*status); break; | |
205 | case UAMEASUNIT_DIGITAL_MEGABYTE: munit = MeasureUnit::createMegabyte(*status); break; | |
206 | case UAMEASUNIT_DIGITAL_TERABIT: munit = MeasureUnit::createTerabit(*status); break; | |
207 | case UAMEASUNIT_DIGITAL_TERABYTE: munit = MeasureUnit::createTerabyte(*status); break; | |
208 | ||
209 | case UAMEASUNIT_ELECTRIC_AMPERE: munit = MeasureUnit::createAmpere(*status); break; | |
210 | case UAMEASUNIT_ELECTRIC_MILLIAMPERE: munit = MeasureUnit::createMilliampere(*status); break; | |
211 | case UAMEASUNIT_ELECTRIC_OHM: munit = MeasureUnit::createOhm(*status); break; | |
212 | case UAMEASUNIT_ELECTRIC_VOLT: munit = MeasureUnit::createVolt(*status); break; | |
213 | ||
214 | case UAMEASUNIT_FREQUENCY_HERTZ: munit = MeasureUnit::createHertz(*status); break; | |
215 | case UAMEASUNIT_FREQUENCY_KILOHERTZ: munit = MeasureUnit::createKilohertz(*status); break; | |
216 | case UAMEASUNIT_FREQUENCY_MEGAHERTZ: munit = MeasureUnit::createMegahertz(*status); break; | |
217 | case UAMEASUNIT_FREQUENCY_GIGAHERTZ: munit = MeasureUnit::createGigahertz(*status); break; | |
218 | ||
219 | case UAMEASUNIT_LIGHT_LUX: munit = MeasureUnit::createLux(*status); break; | |
220 | ||
2ca993e8 A |
221 | case UAMEASUNIT_CONCENTRATION_KARAT: munit = MeasureUnit::createKarat(*status); break; |
222 | case UAMEASUNIT_CONCENTRATION_MILLIGRAM_PER_DECILITER: munit = MeasureUnit::createMilligramPerDeciliter(*status); break; | |
223 | case UAMEASUNIT_CONCENTRATION_MILLIMOLE_PER_LITER: munit = MeasureUnit::createMillimolePerLiter(*status); break; | |
224 | case UAMEASUNIT_CONCENTRATION_PART_PER_MILLION: munit = MeasureUnit::createPartPerMillion(*status); break; | |
57a6839d A |
225 | |
226 | default: *status = U_ILLEGAL_ARGUMENT_ERROR; break; | |
227 | } | |
228 | return munit; | |
229 | } | |
230 | ||
231 | ||
232 | U_CAPI int32_t U_EXPORT2 | |
233 | uameasfmt_format( const UAMeasureFormat* measfmt, | |
234 | double value, | |
235 | UAMeasureUnit unit, | |
236 | UChar* result, | |
237 | int32_t resultCapacity, | |
238 | UErrorCode* status ) | |
239 | { | |
240 | return uameasfmt_formatGetPosition(measfmt, value, unit, result, | |
241 | resultCapacity, NULL, status); | |
242 | } | |
243 | ||
244 | U_CAPI int32_t U_EXPORT2 | |
245 | uameasfmt_formatGetPosition( const UAMeasureFormat* measfmt, | |
246 | double value, | |
247 | UAMeasureUnit unit, | |
248 | UChar* result, | |
249 | int32_t resultCapacity, | |
250 | UFieldPosition* pos, | |
251 | UErrorCode* status ) | |
252 | { | |
253 | if (U_FAILURE(*status)) { | |
254 | return 0; | |
255 | } | |
256 | if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) { | |
257 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
258 | return 0; | |
259 | } | |
b331163b A |
260 | if ( ((MeasureFormat*)measfmt)->getWidth() == UMEASFMT_WIDTH_NUMERIC && |
261 | (unit == UAMEASUNIT_TEMPERATURE_CELSIUS || unit == UAMEASUNIT_TEMPERATURE_FAHRENHEIT) ) { | |
262 | // Fix here until http://bugs.icu-project.org/trac/ticket/11593 is addressed | |
263 | unit = UAMEASUNIT_TEMPERATURE_GENERIC; | |
264 | } | |
57a6839d A |
265 | MeasureUnit * munit = createObjectForMeasureUnit(unit, status); |
266 | if (U_FAILURE(*status)) { | |
267 | return 0; | |
268 | } | |
269 | LocalPointer<Measure> meas(new Measure(value, munit, *status)); | |
270 | if (U_FAILURE(*status)) { | |
271 | return 0; | |
272 | } | |
273 | FieldPosition fp; | |
274 | if (pos != NULL) { | |
275 | int32_t field = pos->field; | |
276 | if (field < 0 || field >= UNUM_FIELD_COUNT) { | |
277 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
278 | return 0; | |
279 | } | |
280 | fp.setField(field); | |
281 | } else { | |
282 | fp.setField(FieldPosition::DONT_CARE); | |
283 | } | |
284 | Formattable fmt; | |
285 | fmt.adoptObject(meas.orphan()); | |
286 | UnicodeString res; | |
287 | res.setTo(result, 0, resultCapacity); | |
288 | ((MeasureFormat*)measfmt)->format(fmt, res, fp, *status); | |
289 | if (pos != NULL) { | |
290 | pos->beginIndex = fp.getBeginIndex(); | |
291 | pos->endIndex = fp.getEndIndex(); | |
292 | } | |
293 | return res.extract(result, resultCapacity, *status); | |
294 | } | |
295 | ||
296 | enum { kMeasuresMax = 8 }; // temporary limit, will add allocation later as necessary | |
297 | ||
298 | U_CAPI int32_t U_EXPORT2 | |
299 | uameasfmt_formatMultiple( const UAMeasureFormat* measfmt, | |
300 | const UAMeasure* measures, | |
301 | int32_t measureCount, | |
302 | UChar* result, | |
303 | int32_t resultCapacity, | |
304 | UErrorCode* status ) | |
2ca993e8 A |
305 | { |
306 | return uameasfmt_formatMultipleForFields(measfmt, measures, measureCount, | |
307 | result, resultCapacity, NULL, status); | |
308 | } | |
309 | ||
310 | U_CAPI int32_t U_EXPORT2 | |
311 | uameasfmt_formatMultipleForFields( const UAMeasureFormat* measfmt, | |
312 | const UAMeasure* measures, | |
313 | int32_t measureCount, | |
314 | UChar* result, | |
315 | int32_t resultCapacity, | |
316 | UFieldPositionIterator* fpositer, | |
317 | UErrorCode* status ) | |
57a6839d A |
318 | { |
319 | if (U_FAILURE(*status)) { | |
320 | return 0; | |
321 | } | |
322 | if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) || measureCount <= 0 || measureCount > kMeasuresMax ) { | |
323 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
324 | return 0; | |
325 | } | |
326 | int32_t i; | |
327 | Measure * measurePtrs[kMeasuresMax]; | |
328 | for (i = 0; i < kMeasuresMax && U_SUCCESS(*status); i++) { | |
329 | if (i < measureCount) { | |
b331163b A |
330 | UAMeasureUnit unit = measures[i].unit; |
331 | if ( ((MeasureFormat*)measfmt)->getWidth() == UMEASFMT_WIDTH_NUMERIC && | |
332 | (unit == UAMEASUNIT_TEMPERATURE_CELSIUS || unit == UAMEASUNIT_TEMPERATURE_FAHRENHEIT) ) { | |
333 | // Fix here until http://bugs.icu-project.org/trac/ticket/11593 is addressed | |
334 | unit = UAMEASUNIT_TEMPERATURE_GENERIC; | |
335 | } | |
336 | MeasureUnit * munit = createObjectForMeasureUnit(unit, status); | |
57a6839d A |
337 | measurePtrs[i] = new Measure(measures[i].value, munit, *status); |
338 | } else { | |
339 | MeasureUnit * munit = MeasureUnit::createGForce(*status); // any unit will do | |
340 | measurePtrs[i] = new Measure(0, munit, *status); | |
341 | } | |
342 | } | |
343 | if (U_FAILURE(*status)) { | |
344 | while (i-- > 0) { | |
345 | delete measurePtrs[i]; | |
346 | } | |
347 | return 0; | |
348 | } | |
349 | Measure measureObjs[kMeasuresMax] = { *measurePtrs[0], *measurePtrs[1], *measurePtrs[2], *measurePtrs[3], | |
350 | *measurePtrs[4], *measurePtrs[5], *measurePtrs[6], *measurePtrs[7] }; | |
351 | UnicodeString res; | |
352 | res.setTo(result, 0, resultCapacity); | |
2ca993e8 | 353 | ((MeasureFormat*)measfmt)->formatMeasures(measureObjs, measureCount, res, (FieldPositionIterator*)fpositer, *status); |
57a6839d A |
354 | for (i = 0; i < kMeasuresMax; i++) { |
355 | delete measurePtrs[i]; | |
356 | } | |
357 | return res.extract(result, resultCapacity, *status); | |
358 | } | |
359 | ||
2ca993e8 A |
360 | U_CAPI int32_t U_EXPORT2 |
361 | uameasfmt_getUnitName( const UAMeasureFormat* measfmt, | |
362 | UAMeasureUnit unit, | |
363 | UChar* result, | |
364 | int32_t resultCapacity, | |
365 | UErrorCode* status ) | |
366 | { | |
367 | if (U_FAILURE(*status)) { | |
368 | return 0; | |
369 | } | |
370 | if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) { | |
371 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
372 | return 0; | |
373 | } | |
374 | LocalPointer<const MeasureUnit> munit(createObjectForMeasureUnit(unit, status)); | |
375 | if (U_FAILURE(*status)) { | |
376 | return 0; | |
377 | } | |
378 | UnicodeString res; | |
379 | res.setTo(result, 0, resultCapacity); | |
380 | ((MeasureFormat*)measfmt)->getUnitName(munit.getAlias(), res); | |
381 | if (res.isBogus()) { | |
382 | *status = U_MISSING_RESOURCE_ERROR; | |
383 | return 0; | |
384 | } | |
385 | return res.extract(result, resultCapacity, *status); | |
386 | } | |
387 | ||
388 | U_CAPI int32_t U_EXPORT2 | |
389 | uameasfmt_getMultipleUnitNames( const UAMeasureFormat* measfmt, | |
390 | const UAMeasureUnit* units, | |
391 | int32_t unitCount, | |
392 | UAMeasureNameListStyle listStyle, | |
393 | UChar* result, | |
394 | int32_t resultCapacity, | |
395 | UErrorCode* status ) | |
396 | { | |
397 | if (U_FAILURE(*status)) { | |
398 | return 0; | |
399 | } | |
400 | if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) || unitCount <= 0 || unitCount > kMeasuresMax ) { | |
401 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
402 | return 0; | |
403 | } | |
404 | int32_t i; | |
405 | const MeasureUnit * unitPtrs[kMeasuresMax]; | |
406 | for (i = 0; i < unitCount && U_SUCCESS(*status); i++) { | |
407 | unitPtrs[i] = createObjectForMeasureUnit(units[i], status); | |
408 | } | |
409 | if (U_FAILURE(*status)) { | |
410 | while (i-- > 0) { | |
411 | delete unitPtrs[i]; | |
412 | } | |
413 | return 0; | |
414 | } | |
415 | UnicodeString res; | |
416 | res.setTo(result, 0, resultCapacity); | |
417 | ((MeasureFormat*)measfmt)->getMultipleUnitNames(unitPtrs, unitCount, listStyle, res); | |
418 | for (i = 0; i < unitCount; i++) { | |
419 | delete unitPtrs[i]; | |
420 | } | |
421 | if (res.isBogus()) { | |
422 | *status = U_MISSING_RESOURCE_ERROR; | |
423 | return 0; | |
424 | } | |
425 | return res.extract(result, resultCapacity, *status); | |
426 | } | |
427 | ||
428 | // Temporary hack until we can use the forthcoming C++ MeasureUnitPreferences class for this | |
429 | typedef struct { | |
430 | const char* key; | |
431 | int32_t count; | |
432 | UAMeasureUnit units[2]; | |
433 | } KeyToUnits; | |
434 | static const KeyToUnits keyToUnits[] = { | |
435 | { "acre", 1, { UAMEASUNIT_AREA_ACRE } }, | |
436 | { "celsius", 1, { UAMEASUNIT_TEMPERATURE_CELSIUS } }, | |
437 | { "centimeter", 1, { UAMEASUNIT_LENGTH_CENTIMETER } }, | |
438 | { "fahrenheit", 1, { UAMEASUNIT_TEMPERATURE_FAHRENHEIT } }, | |
439 | { "foodcalorie", 1, { UAMEASUNIT_ENERGY_FOODCALORIE } }, | |
440 | { "foot", 1, { UAMEASUNIT_LENGTH_FOOT } }, | |
441 | { "foot inch", 2, { UAMEASUNIT_LENGTH_FOOT, UAMEASUNIT_LENGTH_INCH } }, | |
442 | { "gallon", 1, { UAMEASUNIT_VOLUME_GALLON } }, | |
443 | { "gram", 1, { UAMEASUNIT_MASS_GRAM } }, | |
444 | { "hectare", 1, { UAMEASUNIT_AREA_HECTARE } }, | |
445 | { "hectopascal", 1, { UAMEASUNIT_PRESSURE_HECTOPASCAL } }, | |
446 | { "inch", 1, { UAMEASUNIT_LENGTH_INCH } }, | |
447 | { "inch-hg", 1, { UAMEASUNIT_PRESSURE_INCH_HG } }, | |
448 | { "kilocalorie", 1, { UAMEASUNIT_ENERGY_KILOCALORIE } }, | |
449 | { "kilogram", 1, { UAMEASUNIT_MASS_KILOGRAM } }, | |
450 | { "kilogram gram", 2, { UAMEASUNIT_MASS_KILOGRAM, UAMEASUNIT_MASS_GRAM } }, | |
451 | { "kilometer", 1, { UAMEASUNIT_LENGTH_KILOMETER } }, | |
452 | { "kilometer-per-hour", 1, { UAMEASUNIT_SPEED_KILOMETER_PER_HOUR } }, | |
453 | { "liter-per-100kilometers", 1, { UAMEASUNIT_CONSUMPTION_LITER_PER_100_KILOMETERs } }, | |
454 | { "liter-per-kilometer", 1, { UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER } }, | |
455 | { "liter", 1, { UAMEASUNIT_VOLUME_LITER } }, | |
456 | { "meter", 1, { UAMEASUNIT_LENGTH_METER } }, | |
457 | { "meter centimeter", 2, { UAMEASUNIT_LENGTH_METER, UAMEASUNIT_LENGTH_CENTIMETER } }, | |
458 | { "meter-per-second", 1, { UAMEASUNIT_SPEED_METER_PER_SECOND } }, | |
459 | { "mile", 1, { UAMEASUNIT_LENGTH_MILE } }, | |
460 | { "mile-per-gallon", 1, { UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON } }, | |
461 | { "mile-per-gallon-imperial", 1, { UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON_IMPERIAL } }, | |
462 | { "mile-per-hour", 1, { UAMEASUNIT_SPEED_MILE_PER_HOUR } }, | |
463 | { "mile-scandinavian", 1, { UAMEASUNIT_LENGTH_MILE_SCANDINAVIAN } }, | |
464 | { "millibar", 1, { UAMEASUNIT_PRESSURE_MILLIBAR } }, | |
465 | { "milligram-per-deciliter", 1, { UAMEASUNIT_CONCENTRATION_MILLIGRAM_PER_DECILITER } }, | |
466 | { "millimeter", 1, { UAMEASUNIT_LENGTH_MILLIMETER } }, | |
467 | { "millimeter-of-mercury", 1, { UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY } }, | |
468 | { "millimole-per-liter", 1, { UAMEASUNIT_CONCENTRATION_MILLIMOLE_PER_LITER } }, | |
469 | { "minute second", 2, { UAMEASUNIT_DURATION_MINUTE, UAMEASUNIT_DURATION_SECOND } }, | |
470 | { "pound", 1, { UAMEASUNIT_MASS_POUND } }, | |
471 | { "pound ounce", 2, { UAMEASUNIT_MASS_POUND, UAMEASUNIT_MASS_OUNCE } }, | |
472 | { "stone pound", 2, { UAMEASUNIT_MASS_STONE, UAMEASUNIT_MASS_POUND } }, | |
473 | { "yard", 1, { UAMEASUNIT_LENGTH_YARD } }, | |
474 | { "year-person month-person", 2, { UAMEASUNIT_DURATION_YEAR, UAMEASUNIT_DURATION_MONTH } }, | |
475 | }; | |
476 | enum { kKeyToUnitsCount = UPRV_LENGTHOF(keyToUnits) }; | |
477 | ||
f3c0d7a5 | 478 | enum { kCombinedKeyMax = 64, kKeyValueMax = 15 }; |
2ca993e8 A |
479 | |
480 | static int compareKeyToUnits(const void* searchKey, const void* tableEntry) { | |
481 | return uprv_strncmp(((const KeyToUnits*)searchKey)->key, ((const KeyToUnits*)tableEntry)->key, kCombinedKeyMax); | |
482 | } | |
483 | ||
484 | U_CAPI int32_t U_EXPORT2 | |
485 | uameasfmt_getUnitsForUsage( const char* locale, | |
486 | const char* category, | |
487 | const char* usage, | |
488 | UAMeasureUnit* units, | |
489 | int32_t unitsCapacity, | |
490 | UErrorCode* status ) | |
491 | { | |
492 | if (U_FAILURE(*status)) { | |
493 | return 0; | |
494 | } | |
495 | if ( category==NULL || ((units==NULL)? unitsCapacity!=0: unitsCapacity<0) ) { | |
496 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
497 | return 0; | |
498 | } | |
499 | char combinedKey[kCombinedKeyMax+1] = ""; | |
500 | char* combinedKeyPtr; | |
501 | uprv_strncat(combinedKey, category, kCombinedKeyMax); | |
502 | if (usage != NULL) { | |
503 | uprv_strncat(combinedKey, "-", kCombinedKeyMax-uprv_strlen(combinedKey)); | |
504 | uprv_strncat(combinedKey, usage, kCombinedKeyMax-uprv_strlen(combinedKey)); | |
505 | } | |
506 | ||
507 | // get unitPreferenceData bundle | |
508 | UResourceBundle *prefb = ures_openDirect(NULL, "supplementalData", status); | |
509 | ures_getByKey(prefb, "unitPreferenceData", prefb, status); | |
510 | if (U_FAILURE(*status)) { | |
511 | return 0; | |
512 | } | |
513 | ||
514 | // Get region to use | |
515 | char region[ULOC_COUNTRY_CAPACITY]; | |
f3c0d7a5 A |
516 | UErrorCode localStatus; |
517 | UBool usedOverride = FALSE; | |
518 | // First check for ms overrides, except in certain categories | |
519 | if (uprv_strcmp(category, "concentr") != 0 && uprv_strcmp(category, "duration") != 0) { | |
520 | char msValue[kKeyValueMax + 1]; | |
521 | localStatus = U_ZERO_ERROR; | |
522 | int32_t msValueLen = uloc_getKeywordValue(locale, "ms", msValue, kKeyValueMax, &localStatus); | |
523 | if (U_SUCCESS(localStatus) && msValueLen> 2) { | |
524 | msValue[kKeyValueMax] = 0; // ensure termination | |
525 | if (uprv_strcmp(msValue, "metric") == 0) { | |
526 | uprv_strcpy(region, "001"); | |
527 | usedOverride = TRUE; | |
528 | } else if (uprv_strcmp(msValue, "ussystem") == 0) { | |
529 | uprv_strcpy(region, "US"); | |
530 | usedOverride = TRUE; | |
531 | } else if (uprv_strcmp(msValue, "uksystem") == 0) { | |
532 | uprv_strcpy(region, "GB"); | |
533 | usedOverride = TRUE; | |
534 | } | |
535 | } | |
536 | } | |
537 | if (!usedOverride) { | |
538 | (void)ulocimp_getRegionForSupplementalData(locale, TRUE, region, sizeof(region), status); | |
539 | if (U_FAILURE(*status)) { | |
540 | return 0; | |
541 | } | |
2ca993e8 A |
542 | } |
543 | ||
544 | UResourceBundle *unitb = NULL; | |
f3c0d7a5 | 545 | localStatus = U_ZERO_ERROR; |
2ca993e8 A |
546 | int32_t retval = 0; |
547 | UResourceBundle *regb = ures_getByKey(prefb, region, NULL, &localStatus); | |
548 | if (U_SUCCESS(localStatus)) { | |
549 | unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus); | |
550 | if (U_FAILURE(localStatus)) { | |
551 | combinedKeyPtr = uprv_strstr(combinedKey, "-informal"); | |
552 | if (combinedKeyPtr != NULL) { | |
553 | *combinedKeyPtr = 0; | |
554 | localStatus = U_ZERO_ERROR; | |
555 | unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus); | |
556 | } | |
557 | } | |
558 | } else { | |
559 | combinedKeyPtr = uprv_strstr(combinedKey, "-informal"); | |
560 | if (combinedKeyPtr != NULL) { | |
561 | *combinedKeyPtr = 0; | |
562 | } | |
563 | } | |
564 | if (U_FAILURE(localStatus)) { | |
565 | localStatus = U_ZERO_ERROR; | |
566 | regb = ures_getByKey(prefb, "001", regb, &localStatus); | |
567 | if (U_SUCCESS(localStatus)) { | |
568 | unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus); | |
569 | if (U_FAILURE(localStatus)) { | |
570 | combinedKeyPtr = uprv_strstr(combinedKey, "-small"); | |
571 | if (combinedKeyPtr == NULL) { | |
572 | combinedKeyPtr = uprv_strstr(combinedKey, "-large"); | |
573 | } | |
574 | if (combinedKeyPtr != NULL) { | |
575 | *combinedKeyPtr = 0; | |
576 | localStatus = U_ZERO_ERROR; | |
577 | unitb = ures_getByKey(regb, combinedKey, unitb, &localStatus); | |
578 | } | |
579 | } | |
580 | } | |
581 | } | |
582 | if (U_FAILURE(localStatus)) { | |
583 | *status = localStatus; | |
584 | } else { | |
585 | int32_t keyLen = kCombinedKeyMax; | |
586 | const char* unitsKey = ures_getUTF8String(unitb, combinedKey, &keyLen, FALSE, status); | |
587 | if (U_SUCCESS(*status)) { | |
588 | KeyToUnits searchKey = { unitsKey, 0, { (UAMeasureUnit)0 } }; | |
589 | const KeyToUnits* keyToUnitsPtr = (const KeyToUnits*)bsearch(&searchKey, keyToUnits, kKeyToUnitsCount, | |
590 | sizeof(KeyToUnits), compareKeyToUnits); | |
591 | if (keyToUnitsPtr == NULL) { | |
592 | *status = U_MISSING_RESOURCE_ERROR; | |
593 | } else { | |
594 | retval = keyToUnitsPtr->count; | |
595 | if (units != NULL) { | |
596 | if (retval > unitsCapacity) { | |
597 | *status = U_BUFFER_OVERFLOW_ERROR; | |
598 | } else { | |
599 | units[0] = keyToUnitsPtr->units[0]; | |
600 | if (retval > 1) { | |
601 | units[1] = keyToUnitsPtr->units[1]; | |
602 | } | |
603 | } | |
604 | } | |
605 | } | |
606 | } | |
607 | } | |
608 | ||
609 | ures_close(unitb); | |
610 | ures_close(regb); | |
611 | ures_close(prefb); | |
612 | ||
613 | return retval; | |
614 | } | |
615 | ||
616 | U_CAPI const char * U_EXPORT2 | |
617 | uameasfmt_getUnitCategory(UAMeasureUnit unit, | |
618 | UErrorCode* status ) | |
619 | { | |
620 | if (U_FAILURE(*status)) { | |
621 | return NULL; | |
622 | } | |
623 | LocalPointer<const MeasureUnit> munit(createObjectForMeasureUnit(unit, status)); | |
624 | if (U_FAILURE(*status)) { | |
625 | return NULL; | |
626 | } | |
627 | return munit->getType(); | |
628 | } | |
57a6839d A |
629 | |
630 | #endif /* #if !UCONFIG_NO_FORMATTING */ |