2 *****************************************************************************************
3 * Copyright (C) 2014-2015 Apple Inc. All Rights Reserved.
4 *****************************************************************************************
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
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"
28 U_CAPI UAMeasureFormat
* U_EXPORT2
29 uameasfmt_open( const char* locale
,
30 UAMeasureFormatWidth width
,
31 UNumberFormat
* nfToAdopt
,
34 if (U_FAILURE(*status
)) {
37 UMeasureFormatWidth mfWidth
= UMEASFMT_WIDTH_WIDE
;
39 case UAMEASFMT_WIDTH_WIDE
:
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;
48 *status
= U_ILLEGAL_ARGUMENT_ERROR
; return NULL
;
50 LocalPointer
<MeasureFormat
> measfmt( new MeasureFormat(Locale(locale
), mfWidth
, (NumberFormat
*)nfToAdopt
, *status
) );
51 if (U_FAILURE(*status
)) {
54 return (UAMeasureFormat
*)measfmt
.orphan();
59 uameasfmt_close(UAMeasureFormat
*measfmt
)
61 delete (MeasureFormat
*)measfmt
;
64 static MeasureUnit
* createObjectForMeasureUnit(UAMeasureUnit unit
, UErrorCode
* status
)
66 MeasureUnit
* munit
= NULL
;
68 case UAMEASUNIT_ACCELERATION_G_FORCE
: munit
= MeasureUnit::createGForce(*status
); break;
69 case UAMEASUNIT_ACCELERATION_METER_PER_SECOND_SQUARED
: munit
= MeasureUnit::createMeterPerSecondSquared(*status
); break;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
180 case UAMEASUNIT_CONSUMPTION_LITER_PER_KILOMETER
: munit
= MeasureUnit::createLiterPerKilometer(*status
); break;
181 case UAMEASUNIT_CONSUMPTION_MILE_PER_GALLON
: munit
= MeasureUnit::createMilePerGallon(*status
); break;
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;
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;
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;
204 case UAMEASUNIT_LIGHT_LUX
: munit
= MeasureUnit::createLux(*status
); break;
206 //case UAMEASUNIT_PROPORTION_KARAT: munit = MeasureUnit::createKarat(*status); break;
208 default: *status
= U_ILLEGAL_ARGUMENT_ERROR
; break;
214 U_CAPI
int32_t U_EXPORT2
215 uameasfmt_format( const UAMeasureFormat
* measfmt
,
219 int32_t resultCapacity
,
222 return uameasfmt_formatGetPosition(measfmt
, value
, unit
, result
,
223 resultCapacity
, NULL
, status
);
226 U_CAPI
int32_t U_EXPORT2
227 uameasfmt_formatGetPosition( const UAMeasureFormat
* measfmt
,
231 int32_t resultCapacity
,
235 if (U_FAILURE(*status
)) {
238 if ( ((result
==NULL
)? resultCapacity
!=0: resultCapacity
<0) ) {
239 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
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
;
247 MeasureUnit
* munit
= createObjectForMeasureUnit(unit
, status
);
248 if (U_FAILURE(*status
)) {
251 LocalPointer
<Measure
> meas(new Measure(value
, munit
, *status
));
252 if (U_FAILURE(*status
)) {
257 int32_t field
= pos
->field
;
258 if (field
< 0 || field
>= UNUM_FIELD_COUNT
) {
259 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
264 fp
.setField(FieldPosition::DONT_CARE
);
267 fmt
.adoptObject(meas
.orphan());
269 res
.setTo(result
, 0, resultCapacity
);
270 ((MeasureFormat
*)measfmt
)->format(fmt
, res
, fp
, *status
);
272 pos
->beginIndex
= fp
.getBeginIndex();
273 pos
->endIndex
= fp
.getEndIndex();
275 return res
.extract(result
, resultCapacity
, *status
);
278 enum { kMeasuresMax
= 8 }; // temporary limit, will add allocation later as necessary
280 U_CAPI
int32_t U_EXPORT2
281 uameasfmt_formatMultiple( const UAMeasureFormat
* measfmt
,
282 const UAMeasure
* measures
,
283 int32_t measureCount
,
285 int32_t resultCapacity
,
288 if (U_FAILURE(*status
)) {
291 if ( ((result
==NULL
)? resultCapacity
!=0: resultCapacity
<0) || measureCount
<= 0 || measureCount
> kMeasuresMax
) {
292 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
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
;
305 MeasureUnit
* munit
= createObjectForMeasureUnit(unit
, status
);
306 measurePtrs
[i
] = new Measure(measures
[i
].value
, munit
, *status
);
308 MeasureUnit
* munit
= MeasureUnit::createGForce(*status
); // any unit will do
309 measurePtrs
[i
] = new Measure(0, munit
, *status
);
312 if (U_FAILURE(*status
)) {
314 delete measurePtrs
[i
];
318 Measure measureObjs
[kMeasuresMax
] = { *measurePtrs
[0], *measurePtrs
[1], *measurePtrs
[2], *measurePtrs
[3],
319 *measurePtrs
[4], *measurePtrs
[5], *measurePtrs
[6], *measurePtrs
[7] };
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
];
327 return res
.extract(result
, resultCapacity
, *status
);
331 #endif /* #if !UCONFIG_NO_FORMATTING */