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