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