]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/uameasureformat.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / i18n / uameasureformat.cpp
1 /*
2 *****************************************************************************************
3 * Copyright (C) 2014-2015 Apple Inc. All Rights Reserved.
4 *****************************************************************************************
5 */
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "unicode/uameasureformat.h"
12 #include "unicode/fieldpos.h"
13 #include "unicode/localpointer.h"
14 #include "unicode/numfmt.h"
15 #include "unicode/measunit.h"
16 #include "unicode/measure.h"
17 #include "unicode/measfmt.h"
18 #include "unicode/unistr.h"
19 #include "unicode/unum.h"
20 #include "unicode/umisc.h"
21 #include "unicode/ures.h"
22 #include "uresimp.h"
23 #include "ustr_imp.h"
24
25 U_NAMESPACE_USE
26
27
28 U_CAPI UAMeasureFormat* U_EXPORT2
29 uameasfmt_open( const char* locale,
30 UAMeasureFormatWidth width,
31 UNumberFormat* nfToAdopt,
32 UErrorCode* status )
33 {
34 if (U_FAILURE(*status)) {
35 return NULL;
36 }
37 UMeasureFormatWidth mfWidth = UMEASFMT_WIDTH_WIDE;
38 switch (width) {
39 case UAMEASFMT_WIDTH_WIDE:
40 break;
41 case UAMEASFMT_WIDTH_SHORT:
42 mfWidth = UMEASFMT_WIDTH_SHORT; break;
43 case UAMEASFMT_WIDTH_NARROW:
44 mfWidth = UMEASFMT_WIDTH_NARROW; break;
45 case UAMEASFMT_WIDTH_NUMERIC:
46 mfWidth = UMEASFMT_WIDTH_NUMERIC; break;
47 default:
48 *status = U_ILLEGAL_ARGUMENT_ERROR; return NULL;
49 }
50 LocalPointer<MeasureFormat> measfmt( new MeasureFormat(Locale(locale), mfWidth, (NumberFormat*)nfToAdopt, *status) );
51 if (U_FAILURE(*status)) {
52 return NULL;
53 }
54 return (UAMeasureFormat*)measfmt.orphan();
55 }
56
57
58 U_CAPI void U_EXPORT2
59 uameasfmt_close(UAMeasureFormat *measfmt)
60 {
61 delete (MeasureFormat*)measfmt;
62 }
63
64 static MeasureUnit * createObjectForMeasureUnit(UAMeasureUnit unit, UErrorCode* status )
65 {
66 MeasureUnit * munit = NULL;
67 switch (unit) {
68 case UAMEASUNIT_ACCELERATION_G_FORCE: munit = MeasureUnit::createGForce(*status); break;
69 case UAMEASUNIT_ACCELERATION_METER_PER_SECOND_SQUARED: munit = MeasureUnit::createMeterPerSecondSquared(*status); break;
70
71 case UAMEASUNIT_ANGLE_DEGREE: munit = MeasureUnit::createDegree(*status); break;
72 case UAMEASUNIT_ANGLE_ARC_MINUTE: munit = MeasureUnit::createArcMinute(*status); break;
73 case UAMEASUNIT_ANGLE_ARC_SECOND: munit = MeasureUnit::createArcSecond(*status); break;
74 case UAMEASUNIT_ANGLE_RADIAN: munit = MeasureUnit::createRadian(*status); break;
75
76 case UAMEASUNIT_AREA_SQUARE_METER: munit = MeasureUnit::createSquareMeter(*status); break;
77 case UAMEASUNIT_AREA_SQUARE_KILOMETER: munit = MeasureUnit::createSquareKilometer(*status); break;
78 case UAMEASUNIT_AREA_SQUARE_FOOT: munit = MeasureUnit::createSquareFoot(*status); break;
79 case UAMEASUNIT_AREA_SQUARE_MILE: munit = MeasureUnit::createSquareMile(*status); break;
80 case UAMEASUNIT_AREA_ACRE: munit = MeasureUnit::createAcre(*status); break;
81 case UAMEASUNIT_AREA_HECTARE: munit = MeasureUnit::createHectare(*status); break;
82 case UAMEASUNIT_AREA_SQUARE_CENTIMETER: munit = MeasureUnit::createSquareCentimeter(*status); break;
83 case UAMEASUNIT_AREA_SQUARE_INCH: munit = MeasureUnit::createSquareInch(*status); break;
84 case UAMEASUNIT_AREA_SQUARE_YARD: munit = MeasureUnit::createSquareYard(*status); break;
85
86 case UAMEASUNIT_DURATION_YEAR: munit = MeasureUnit::createYear(*status); break;
87 case UAMEASUNIT_DURATION_MONTH: munit = MeasureUnit::createMonth(*status); break;
88 case UAMEASUNIT_DURATION_WEEK: munit = MeasureUnit::createDay(*status); break;
89 case UAMEASUNIT_DURATION_DAY: munit = MeasureUnit::createWeek(*status); break;
90 case UAMEASUNIT_DURATION_HOUR: munit = MeasureUnit::createHour(*status); break;
91 case UAMEASUNIT_DURATION_MINUTE: munit = MeasureUnit::createMinute(*status); break;
92 case UAMEASUNIT_DURATION_SECOND: munit = MeasureUnit::createSecond(*status); break;
93 case UAMEASUNIT_DURATION_MILLISECOND: munit = MeasureUnit::createMillisecond(*status); break;
94 case UAMEASUNIT_DURATION_MICROSECOND: munit = MeasureUnit::createMicrosecond(*status); break;
95 case UAMEASUNIT_DURATION_NANOSECOND: munit = MeasureUnit::createNanosecond(*status); break;
96
97 case UAMEASUNIT_LENGTH_METER: munit = MeasureUnit::createMeter(*status); break;
98 case UAMEASUNIT_LENGTH_CENTIMETER: munit = MeasureUnit::createCentimeter(*status); break;
99 case UAMEASUNIT_LENGTH_KILOMETER: munit = MeasureUnit::createKilometer(*status); break;
100 case UAMEASUNIT_LENGTH_MILLIMETER: munit = MeasureUnit::createMillimeter(*status); break;
101 case UAMEASUNIT_LENGTH_PICOMETER: munit = MeasureUnit::createPicometer(*status); break;
102 case UAMEASUNIT_LENGTH_FOOT: munit = MeasureUnit::createFoot(*status); break;
103 case UAMEASUNIT_LENGTH_INCH: munit = MeasureUnit::createInch(*status); break;
104 case UAMEASUNIT_LENGTH_MILE: munit = MeasureUnit::createMile(*status); break;
105 case UAMEASUNIT_LENGTH_YARD: munit = MeasureUnit::createYard(*status); break;
106 case UAMEASUNIT_LENGTH_LIGHT_YEAR: munit = MeasureUnit::createLightYear(*status); break;
107 case UAMEASUNIT_LENGTH_DECIMETER: munit = MeasureUnit::createDecimeter(*status); break;
108 case UAMEASUNIT_LENGTH_MICROMETER: munit = MeasureUnit::createMicrometer(*status); break;
109 case UAMEASUNIT_LENGTH_NANOMETER: munit = MeasureUnit::createNanometer(*status); break;
110 case UAMEASUNIT_LENGTH_NAUTICAL_MILE: munit = MeasureUnit::createNauticalMile(*status); break;
111 case UAMEASUNIT_LENGTH_FATHOM: munit = MeasureUnit::createFathom(*status); break;
112 case UAMEASUNIT_LENGTH_FURLONG: munit = MeasureUnit::createFurlong(*status); break;
113 case UAMEASUNIT_LENGTH_ASTRONOMICAL_UNIT: munit = MeasureUnit::createAstronomicalUnit(*status); break;
114 case UAMEASUNIT_LENGTH_PARSEC: munit = MeasureUnit::createParsec(*status); break;
115
116 case UAMEASUNIT_MASS_GRAM: munit = MeasureUnit::createGram(*status); break;
117 case UAMEASUNIT_MASS_KILOGRAM: munit = MeasureUnit::createKilogram(*status); break;
118 case UAMEASUNIT_MASS_OUNCE: munit = MeasureUnit::createOunce(*status); break;
119 case UAMEASUNIT_MASS_POUND: munit = MeasureUnit::createPound(*status); break;
120 case UAMEASUNIT_MASS_STONE: munit = MeasureUnit::createStone(*status); break;
121 case UAMEASUNIT_MASS_MICROGRAM: munit = MeasureUnit::createMicrogram(*status); break;
122 case UAMEASUNIT_MASS_MILLIGRAM: munit = MeasureUnit::createMilligram(*status); break;
123 case UAMEASUNIT_MASS_METRIC_TON: munit = MeasureUnit::createMetricTon(*status); break;
124 case UAMEASUNIT_MASS_TON: munit = MeasureUnit::createTon(*status); break;
125 case UAMEASUNIT_MASS_CARAT: munit = MeasureUnit::createCarat(*status); break;
126 case UAMEASUNIT_MASS_OUNCE_TROY: munit = MeasureUnit::createOunceTroy(*status); break;
127
128 case UAMEASUNIT_POWER_WATT: munit = MeasureUnit::createWatt(*status); break;
129 case UAMEASUNIT_POWER_KILOWATT: munit = MeasureUnit::createKilowatt(*status); break;
130 case UAMEASUNIT_POWER_HORSEPOWER: munit = MeasureUnit::createHorsepower(*status); break;
131 case UAMEASUNIT_POWER_MILLIWATT: munit = MeasureUnit::createMilliwatt(*status); break;
132 case UAMEASUNIT_POWER_MEGAWATT: munit = MeasureUnit::createMegawatt(*status); break;
133 case UAMEASUNIT_POWER_GIGAWATT: munit = MeasureUnit::createGigawatt(*status); break;
134
135 case UAMEASUNIT_PRESSURE_HECTOPASCAL: munit = MeasureUnit::createHectopascal(*status); break;
136 case UAMEASUNIT_PRESSURE_INCH_HG: munit = MeasureUnit::createInchHg(*status); break;
137 case UAMEASUNIT_PRESSURE_MILLIBAR: munit = MeasureUnit::createMillibar(*status); break;
138 case UAMEASUNIT_PRESSURE_MILLIMETER_OF_MERCURY: munit = MeasureUnit::createMillimeterOfMercury(*status); break;
139 case UAMEASUNIT_PRESSURE_POUND_PER_SQUARE_INCH: munit = MeasureUnit::createPoundPerSquareInch(*status); break;
140
141 case UAMEASUNIT_SPEED_METER_PER_SECOND: munit = MeasureUnit::createMeterPerSecond(*status); break;
142 case UAMEASUNIT_SPEED_KILOMETER_PER_HOUR: munit = MeasureUnit::createKilometerPerHour(*status); break;
143 case UAMEASUNIT_SPEED_MILE_PER_HOUR: munit = MeasureUnit::createMilePerHour(*status); break;
144
145 case UAMEASUNIT_TEMPERATURE_CELSIUS: munit = MeasureUnit::createCelsius(*status); break;
146 case UAMEASUNIT_TEMPERATURE_FAHRENHEIT: munit = MeasureUnit::createFahrenheit(*status); break;
147 case UAMEASUNIT_TEMPERATURE_KELVIN: munit = MeasureUnit::createKelvin(*status); break;
148 case UAMEASUNIT_TEMPERATURE_GENERIC: munit = MeasureUnit::createGenericTemperature(*status); break;
149
150 case UAMEASUNIT_VOLUME_LITER: munit = MeasureUnit::createLiter(*status); break;
151 case UAMEASUNIT_VOLUME_CUBIC_KILOMETER: munit = MeasureUnit::createCubicKilometer(*status); break;
152 case UAMEASUNIT_VOLUME_CUBIC_MILE: munit = MeasureUnit::createCubicMile(*status); break;
153 case UAMEASUNIT_VOLUME_MILLILITER: munit = MeasureUnit::createMilliliter(*status); break;
154 case UAMEASUNIT_VOLUME_CENTILITER: munit = MeasureUnit::createCentiliter(*status); break;
155 case UAMEASUNIT_VOLUME_DECILITER: munit = MeasureUnit::createDeciliter(*status); break;
156 case UAMEASUNIT_VOLUME_HECTOLITER: munit = MeasureUnit::createHectoliter(*status); break;
157 case UAMEASUNIT_VOLUME_MEGALITER: munit = MeasureUnit::createMegaliter(*status); break;
158 case UAMEASUNIT_VOLUME_CUBIC_CENTIMETER: munit = MeasureUnit::createCubicCentimeter(*status); break;
159 case UAMEASUNIT_VOLUME_CUBIC_METER: munit = MeasureUnit::createCubicMeter(*status); break;
160 case UAMEASUNIT_VOLUME_CUBIC_INCH: munit = MeasureUnit::createCubicInch(*status); break;
161 case UAMEASUNIT_VOLUME_CUBIC_FOOT: munit = MeasureUnit::createCubicFoot(*status); break;
162 case UAMEASUNIT_VOLUME_CUBIC_YARD: munit = MeasureUnit::createCubicYard(*status); break;
163 case UAMEASUNIT_VOLUME_ACRE_FOOT: munit = MeasureUnit::createAcreFoot(*status); break;
164 case UAMEASUNIT_VOLUME_BUSHEL: munit = MeasureUnit::createBushel(*status); break;
165 case UAMEASUNIT_VOLUME_TEASPOON: munit = MeasureUnit::createTeaspoon(*status); break;
166 case UAMEASUNIT_VOLUME_TABLESPOON: munit = MeasureUnit::createTablespoon(*status); break;
167 case UAMEASUNIT_VOLUME_FLUID_OUNCE: munit = MeasureUnit::createFluidOunce(*status); break;
168 case UAMEASUNIT_VOLUME_CUP: munit = MeasureUnit::createCup(*status); break;
169 case UAMEASUNIT_VOLUME_PINT: munit = MeasureUnit::createPint(*status); break;
170 case UAMEASUNIT_VOLUME_QUART: munit = MeasureUnit::createQuart(*status); break;
171 case UAMEASUNIT_VOLUME_GALLON: munit = MeasureUnit::createGallon(*status); break;
172
173 case UAMEASUNIT_ENERGY_JOULE: munit = MeasureUnit::createJoule(*status); break;
174 case UAMEASUNIT_ENERGY_KILOJOULE: munit = MeasureUnit::createKilojoule(*status); break;
175 case UAMEASUNIT_ENERGY_CALORIE: munit = MeasureUnit::createCalorie(*status); break;
176 case UAMEASUNIT_ENERGY_KILOCALORIE: munit = MeasureUnit::createKilocalorie(*status); break;
177 case UAMEASUNIT_ENERGY_FOODCALORIE: munit = MeasureUnit::createFoodcalorie(*status); break;
178 case UAMEASUNIT_ENERGY_KILOWATT_HOUR: munit = MeasureUnit::createKilowattHour(*status); break;
179
180 case UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER: munit = MeasureUnit::createLiterPerKilometer(*status); break;
181 case UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON: munit = MeasureUnit::createMilePerGallon(*status); break;
182
183 case UAMEASUNIT_DIGITAL_BIT: munit = MeasureUnit::createBit(*status); break;
184 case UAMEASUNIT_DIGITAL_BYTE: munit = MeasureUnit::createByte(*status); break;
185 case UAMEASUNIT_DIGITAL_GIGABIT: munit = MeasureUnit::createGigabit(*status); break;
186 case UAMEASUNIT_DIGITAL_GIGABYTE: munit = MeasureUnit::createGigabyte(*status); break;
187 case UAMEASUNIT_DIGITAL_KILOBIT: munit = MeasureUnit::createKilobit(*status); break;
188 case UAMEASUNIT_DIGITAL_KILOBYTE: munit = MeasureUnit::createKilobyte(*status); break;
189 case UAMEASUNIT_DIGITAL_MEGABIT: munit = MeasureUnit::createMegabit(*status); break;
190 case UAMEASUNIT_DIGITAL_MEGABYTE: munit = MeasureUnit::createMegabyte(*status); break;
191 case UAMEASUNIT_DIGITAL_TERABIT: munit = MeasureUnit::createTerabit(*status); break;
192 case UAMEASUNIT_DIGITAL_TERABYTE: munit = MeasureUnit::createTerabyte(*status); break;
193
194 case UAMEASUNIT_ELECTRIC_AMPERE: munit = MeasureUnit::createAmpere(*status); break;
195 case UAMEASUNIT_ELECTRIC_MILLIAMPERE: munit = MeasureUnit::createMilliampere(*status); break;
196 case UAMEASUNIT_ELECTRIC_OHM: munit = MeasureUnit::createOhm(*status); break;
197 case UAMEASUNIT_ELECTRIC_VOLT: munit = MeasureUnit::createVolt(*status); break;
198
199 case UAMEASUNIT_FREQUENCY_HERTZ: munit = MeasureUnit::createHertz(*status); break;
200 case UAMEASUNIT_FREQUENCY_KILOHERTZ: munit = MeasureUnit::createKilohertz(*status); break;
201 case UAMEASUNIT_FREQUENCY_MEGAHERTZ: munit = MeasureUnit::createMegahertz(*status); break;
202 case UAMEASUNIT_FREQUENCY_GIGAHERTZ: munit = MeasureUnit::createGigahertz(*status); break;
203
204 case UAMEASUNIT_LIGHT_LUX: munit = MeasureUnit::createLux(*status); break;
205
206 //case UAMEASUNIT_PROPORTION_KARAT: munit = MeasureUnit::createKarat(*status); break;
207
208 default: *status = U_ILLEGAL_ARGUMENT_ERROR; break;
209 }
210 return munit;
211 }
212
213
214 U_CAPI int32_t U_EXPORT2
215 uameasfmt_format( const UAMeasureFormat* measfmt,
216 double value,
217 UAMeasureUnit unit,
218 UChar* result,
219 int32_t resultCapacity,
220 UErrorCode* status )
221 {
222 return uameasfmt_formatGetPosition(measfmt, value, unit, result,
223 resultCapacity, NULL, status);
224 }
225
226 U_CAPI int32_t U_EXPORT2
227 uameasfmt_formatGetPosition( const UAMeasureFormat* measfmt,
228 double value,
229 UAMeasureUnit unit,
230 UChar* result,
231 int32_t resultCapacity,
232 UFieldPosition* pos,
233 UErrorCode* status )
234 {
235 if (U_FAILURE(*status)) {
236 return 0;
237 }
238 if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) ) {
239 *status = U_ILLEGAL_ARGUMENT_ERROR;
240 return 0;
241 }
242 if ( ((MeasureFormat*)measfmt)->getWidth() == UMEASFMT_WIDTH_NUMERIC &&
243 (unit == UAMEASUNIT_TEMPERATURE_CELSIUS || unit == UAMEASUNIT_TEMPERATURE_FAHRENHEIT) ) {
244 // Fix here until http://bugs.icu-project.org/trac/ticket/11593 is addressed
245 unit = UAMEASUNIT_TEMPERATURE_GENERIC;
246 }
247 MeasureUnit * munit = createObjectForMeasureUnit(unit, status);
248 if (U_FAILURE(*status)) {
249 return 0;
250 }
251 LocalPointer<Measure> meas(new Measure(value, munit, *status));
252 if (U_FAILURE(*status)) {
253 return 0;
254 }
255 FieldPosition fp;
256 if (pos != NULL) {
257 int32_t field = pos->field;
258 if (field < 0 || field >= UNUM_FIELD_COUNT) {
259 *status = U_ILLEGAL_ARGUMENT_ERROR;
260 return 0;
261 }
262 fp.setField(field);
263 } else {
264 fp.setField(FieldPosition::DONT_CARE);
265 }
266 Formattable fmt;
267 fmt.adoptObject(meas.orphan());
268 UnicodeString res;
269 res.setTo(result, 0, resultCapacity);
270 ((MeasureFormat*)measfmt)->format(fmt, res, fp, *status);
271 if (pos != NULL) {
272 pos->beginIndex = fp.getBeginIndex();
273 pos->endIndex = fp.getEndIndex();
274 }
275 return res.extract(result, resultCapacity, *status);
276 }
277
278 enum { kMeasuresMax = 8 }; // temporary limit, will add allocation later as necessary
279
280 U_CAPI int32_t U_EXPORT2
281 uameasfmt_formatMultiple( const UAMeasureFormat* measfmt,
282 const UAMeasure* measures,
283 int32_t measureCount,
284 UChar* result,
285 int32_t resultCapacity,
286 UErrorCode* status )
287 {
288 if (U_FAILURE(*status)) {
289 return 0;
290 }
291 if ( ((result==NULL)? resultCapacity!=0: resultCapacity<0) || measureCount <= 0 || measureCount > kMeasuresMax ) {
292 *status = U_ILLEGAL_ARGUMENT_ERROR;
293 return 0;
294 }
295 int32_t i;
296 Measure * measurePtrs[kMeasuresMax];
297 for (i = 0; i < kMeasuresMax && U_SUCCESS(*status); i++) {
298 if (i < measureCount) {
299 UAMeasureUnit unit = measures[i].unit;
300 if ( ((MeasureFormat*)measfmt)->getWidth() == UMEASFMT_WIDTH_NUMERIC &&
301 (unit == UAMEASUNIT_TEMPERATURE_CELSIUS || unit == UAMEASUNIT_TEMPERATURE_FAHRENHEIT) ) {
302 // Fix here until http://bugs.icu-project.org/trac/ticket/11593 is addressed
303 unit = UAMEASUNIT_TEMPERATURE_GENERIC;
304 }
305 MeasureUnit * munit = createObjectForMeasureUnit(unit, status);
306 measurePtrs[i] = new Measure(measures[i].value, munit, *status);
307 } else {
308 MeasureUnit * munit = MeasureUnit::createGForce(*status); // any unit will do
309 measurePtrs[i] = new Measure(0, munit, *status);
310 }
311 }
312 if (U_FAILURE(*status)) {
313 while (i-- > 0) {
314 delete measurePtrs[i];
315 }
316 return 0;
317 }
318 Measure measureObjs[kMeasuresMax] = { *measurePtrs[0], *measurePtrs[1], *measurePtrs[2], *measurePtrs[3],
319 *measurePtrs[4], *measurePtrs[5], *measurePtrs[6], *measurePtrs[7] };
320 UnicodeString res;
321 res.setTo(result, 0, resultCapacity);
322 FieldPosition pos(0);
323 ((MeasureFormat*)measfmt)->formatMeasures(measureObjs, measureCount, res, pos, *status);
324 for (i = 0; i < kMeasuresMax; i++) {
325 delete measurePtrs[i];
326 }
327 return res.extract(result, resultCapacity, *status);
328 }
329
330
331 #endif /* #if !UCONFIG_NO_FORMATTING */