]>
Commit | Line | Data |
---|---|---|
57a6839d A |
1 | /* |
2 | ***************************************************************************************** | |
3 | * Copyright (C) 2014 Apple Inc. All Rights Reserved. | |
4 | ***************************************************************************************** | |
5 | */ | |
6 | ||
7 | #ifndef UAMEASUREFORMAT_H | |
8 | #define UAMEASUREFORMAT_H | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | #ifndef U_HIDE_DRAFT_API | |
14 | ||
15 | #include "unicode/localpointer.h" | |
16 | #include "unicode/unum.h" | |
17 | #include "unicode/umisc.h" | |
18 | ||
19 | /** | |
20 | * \file | |
21 | * \brief C API: Format combinations of measurement units and numeric values. | |
22 | * | |
23 | * This is a somewhat temporary Apple-specific wrapper for using C++ MeasureFormat | |
24 | * to format Measure objects, until the official ICU C API is available. | |
25 | */ | |
26 | ||
27 | /** | |
28 | * Opaque UAMeasureFormat object for use in C programs. | |
29 | * @draft ICU 53 | |
30 | */ | |
31 | struct UAMeasureFormat; | |
32 | typedef struct UAMeasureFormat UAMeasureFormat; /**< C typedef for struct UAMeasureFormat. @draft ICU 53 */ | |
33 | ||
34 | /** | |
35 | * Constants for various widths. | |
36 | * @draft ICU 53 | |
37 | */ | |
38 | typedef enum UAMeasureFormatWidth { | |
39 | /** | |
40 | * Full unit names, e.g. "5 hours, 37 minutes" | |
41 | * @draft ICU 53 | |
42 | */ | |
43 | UAMEASFMT_WIDTH_WIDE, | |
44 | ||
45 | /** | |
46 | * Abbreviated unit names, e.g. "5 hrs, 37 mins" | |
47 | * @draft ICU 53 | |
48 | */ | |
49 | UAMEASFMT_WIDTH_SHORT, | |
50 | ||
51 | /** | |
52 | * Use unit symbols if possible, e.g. "5 h, 37 m" | |
53 | * @draft ICU 53 | |
54 | */ | |
55 | UAMEASFMT_WIDTH_NARROW, | |
56 | ||
57 | /** | |
58 | * Completely omit unit designatins if possible, e.g. "5:37" | |
59 | * @draft ICU 53 | |
60 | */ | |
61 | UAMEASFMT_WIDTH_NUMERIC, | |
62 | ||
63 | /** | |
64 | * Count of values in this enum. | |
65 | * @draft ICU 53 | |
66 | */ | |
67 | UAMEASFMT_WIDTH_COUNT | |
68 | } UAMeasureFormatWidth; | |
69 | ||
70 | /** | |
71 | * Measurement units | |
72 | * @draft ICU 54 | |
73 | */ | |
74 | typedef enum UAMeasureUnit { | |
75 | UAMEASUNIT_ACCELERATION_G_FORCE = (0 << 8) + 0, | |
76 | // | |
77 | UAMEASUNIT_ANGLE_DEGREE = (1 << 8) + 0, | |
78 | UAMEASUNIT_ANGLE_ARC_MINUTE = (1 << 8) + 1, | |
79 | UAMEASUNIT_ANGLE_ARC_SECOND = (1 << 8) + 2, | |
80 | // | |
81 | UAMEASUNIT_AREA_SQUARE_METER = (2 << 8) + 0, | |
82 | UAMEASUNIT_AREA_SQUARE_KILOMETER = (2 << 8) + 1, | |
83 | UAMEASUNIT_AREA_SQUARE_FOOT = (2 << 8) + 2, | |
84 | UAMEASUNIT_AREA_SQUARE_MILE = (2 << 8) + 3, | |
85 | UAMEASUNIT_AREA_ACRE = (2 << 8) + 4, | |
86 | UAMEASUNIT_AREA_HECTARE = (2 << 8) + 5, | |
87 | // | |
88 | // (currency category 3 handled separately) | |
89 | // | |
90 | UAMEASUNIT_DURATION_YEAR = (4 << 8) + 0, | |
91 | UAMEASUNIT_DURATION_MONTH = (4 << 8) + 1, | |
92 | UAMEASUNIT_DURATION_WEEK = (4 << 8) + 2, | |
93 | UAMEASUNIT_DURATION_DAY = (4 << 8) + 3, | |
94 | UAMEASUNIT_DURATION_HOUR = (4 << 8) + 4, | |
95 | UAMEASUNIT_DURATION_MINUTE = (4 << 8) + 5, | |
96 | UAMEASUNIT_DURATION_SECOND = (4 << 8) + 6, | |
97 | UAMEASUNIT_DURATION_MILLISECOND = (4 << 8) + 7, | |
98 | // | |
99 | UAMEASUNIT_LENGTH_METER = (5 << 8) + 0, | |
100 | UAMEASUNIT_LENGTH_CENTIMETER = (5 << 8) + 1, | |
101 | UAMEASUNIT_LENGTH_KILOMETER = (5 << 8) + 2, | |
102 | UAMEASUNIT_LENGTH_MILLIMETER = (5 << 8) + 3, | |
103 | UAMEASUNIT_LENGTH_PICOMETER = (5 << 8) + 4, | |
104 | UAMEASUNIT_LENGTH_FOOT = (5 << 8) + 5, | |
105 | UAMEASUNIT_LENGTH_INCH = (5 << 8) + 6, | |
106 | UAMEASUNIT_LENGTH_MILE = (5 << 8) + 7, | |
107 | UAMEASUNIT_LENGTH_YARD = (5 << 8) + 8, | |
108 | UAMEASUNIT_LENGTH_LIGHT_YEAR = (5 << 8) + 9, | |
109 | // | |
110 | UAMEASUNIT_MASS_GRAM = (6 << 8) + 0, | |
111 | UAMEASUNIT_MASS_KILOGRAM = (6 << 8) + 1, | |
112 | UAMEASUNIT_MASS_OUNCE = (6 << 8) + 2, | |
113 | UAMEASUNIT_MASS_POUND = (6 << 8) + 3, | |
114 | UAMEASUNIT_MASS_STONE = (6 << 8) + 4, // = 14 pounds / 6.35 kg, abbr "st", used in UK/Ireland for body weight (proposed CLDR 26) | |
115 | // | |
116 | UAMEASUNIT_POWER_WATT = (7 << 8) + 0, | |
117 | UAMEASUNIT_POWER_KILOWATT = (7 << 8) + 1, | |
118 | UAMEASUNIT_POWER_HORSEPOWER = (7 << 8) + 2, | |
119 | // | |
120 | UAMEASUNIT_PRESSURE_HECTOPASCAL = (8 << 8) + 0, | |
121 | UAMEASUNIT_PRESSURE_INCH_HG = (8 << 8) + 1, | |
122 | UAMEASUNIT_PRESSURE_MILLIBAR = (8 << 8) + 2, | |
123 | // | |
124 | UAMEASUNIT_SPEED_METER_PER_SECOND = (9 << 8) + 0, | |
125 | UAMEASUNIT_SPEED_KILOMETER_PER_HOUR = (9 << 8) + 1, | |
126 | UAMEASUNIT_SPEED_MILE_PER_HOUR = (9 << 8) + 2, | |
127 | // | |
128 | UAMEASUNIT_TEMPERATURE_CELSIUS = (10 << 8) + 0, | |
129 | UAMEASUNIT_TEMPERATURE_FAHRENHEIT = (10 << 8) + 1, | |
130 | // | |
131 | UAMEASUNIT_VOLUME_LITER = (11 << 8) + 0, | |
132 | UAMEASUNIT_VOLUME_CUBIC_KILOMETER = (11 << 8) + 1, | |
133 | UAMEASUNIT_VOLUME_CUBIC_MILE = (11 << 8) + 2, | |
134 | // | |
135 | // new categories proposed for CLDR 26 | |
136 | UAMEASUNIT_ENERGY_JOULE = (12 << 8) + 2, // (proposed CLDR 26) | |
137 | UAMEASUNIT_ENERGY_KILOJOULE = (12 << 8) + 4, // (proposed CLDR 26) | |
138 | UAMEASUNIT_ENERGY_CALORIE = (12 << 8) + 0, // chemistry "calories", abbr "cal" (proposed CLDR 26) | |
139 | UAMEASUNIT_ENERGY_KILOCALORIE = (12 << 8) + 3, // kilocalories in general (chemistry, food), abbr "kcal" (proposed CLDR 26) | |
140 | UAMEASUNIT_ENERGY_FOODCALORIE = (12 << 8) + 1, // kilocalories specifically for food; in US/UK/? "Calories" abbr "C", elsewhere same as "kcal" (proposed CLDR 26) | |
141 | } UAMeasureUnit; | |
142 | ||
143 | /** | |
144 | * Structure that combines value and UAMeasureUnit, | |
145 | * for use with uameasfmt_formatMultiple to specify a | |
146 | * list of value/unit combinations to format. | |
147 | * @draft ICU 54 | |
148 | */ | |
149 | typedef struct UAMeasure { | |
150 | double value; | |
151 | UAMeasureUnit unit; | |
152 | } UAMeasure; | |
153 | ||
154 | ||
155 | /** | |
156 | * Open a new UAMeasureFormat object for a given locale using the specified width, | |
157 | * along with a number formatter (if desired) to override the default formatter | |
158 | * that would be used for the numeric part of the unit in uameasfmt_format, or the | |
159 | * numeric part of the *last unit* (only) in uameasfmt_formatMultiple. The default | |
160 | * formatter uses zero decimal places and rounds toward 0; an alternate number formatter | |
161 | * can be used to produce (e.g.) "37.2 mins" instead of "37 mins", or | |
162 | * "5 hours, 37.2 minutes" instead of "5 hours, 37 minutes". | |
163 | * | |
164 | * @param locale | |
165 | * The locale | |
166 | * @param style | |
167 | * The width - wide, short, narrow, etc. | |
168 | * @param nfToAdopt | |
169 | * A number formatter to set for this UAMeasureFormat object (instead of | |
170 | * the default decimal formatter). Ownership of this UNumberFormat object | |
171 | * will pass to the UAMeasureFormat object (the UAMeasureFormat adopts the | |
172 | * UNumberFormat), which becomes responsible for closing it. If the caller | |
173 | * wishes to retain ownership of the UNumberFormat object, the caller must | |
174 | * clone it (with unum_clone) and pass the clone to | |
175 | * uatmufmt_openWithNumberFormat. May be NULL to use the default decimal | |
176 | * formatter. | |
177 | * @param status | |
178 | * A pointer to a UErrorCode to receive any errors. | |
179 | * @return | |
180 | * A pointer to a UAMeasureFormat object for the specified locale, | |
181 | * or NULL if an error occurred. | |
182 | * @draft ICU 54 | |
183 | */ | |
184 | U_DRAFT UAMeasureFormat* U_EXPORT2 | |
185 | uameasfmt_open( const char* locale, | |
186 | UAMeasureFormatWidth width, | |
187 | UNumberFormat* nfToAdopt, | |
188 | UErrorCode* status ); | |
189 | ||
190 | /** | |
191 | * Close a UAMeasureFormat object. Once closed it may no longer be used. | |
192 | * @param measfmt | |
193 | * The UATimeUnitFormat object to close. | |
194 | * @draft ICU 54 | |
195 | */ | |
196 | U_DRAFT void U_EXPORT2 | |
197 | uameasfmt_close(UAMeasureFormat *measfmt); | |
198 | ||
199 | #if U_SHOW_CPLUSPLUS_API | |
200 | ||
201 | U_NAMESPACE_BEGIN | |
202 | ||
203 | /** | |
204 | * \class LocalUAMeasureFormatPointer | |
205 | * "Smart pointer" class, closes a UAMeasureFormat via uameasfmt_close(). | |
206 | * For most methods see the LocalPointerBase base class. | |
207 | * | |
208 | * @see LocalPointerBase | |
209 | * @see LocalPointer | |
210 | * @draft ICU 54 | |
211 | */ | |
212 | U_DEFINE_LOCAL_OPEN_POINTER(LocalUAMeasureFormatPointer, UAMeasureFormat, uameasfmt_close); | |
213 | ||
214 | U_NAMESPACE_END | |
215 | ||
216 | #endif | |
217 | ||
218 | ||
219 | /** | |
220 | * Format a value like 1.0 and a field like UAMEASUNIT_DURATION_MINUTE to e.g. "1.0 minutes". | |
221 | * | |
222 | * @param measfmt | |
223 | * The UAMeasureFormat object specifying the format conventions. | |
224 | * @param value | |
225 | * The numeric value to format | |
226 | * @param unit | |
227 | * The unit to format with the specified numeric value | |
228 | * @param result | |
229 | * A pointer to a buffer to receive the formatted result. | |
230 | * @param resultCapacity | |
231 | * The maximum size of result. | |
232 | * @param status | |
233 | * A pointer to a UErrorCode to receive any errors. In case of error status, | |
234 | * the contents of result are undefined. | |
235 | * @return | |
236 | * The length of the formatted result; may be greater than resultCapacity, | |
237 | * in which case an error is returned. | |
238 | * @draft ICU 54 | |
239 | */ | |
240 | U_DRAFT int32_t U_EXPORT2 | |
241 | uameasfmt_format( const UAMeasureFormat* measfmt, | |
242 | double value, | |
243 | UAMeasureUnit unit, | |
244 | UChar* result, | |
245 | int32_t resultCapacity, | |
246 | UErrorCode* status ); | |
247 | ||
248 | /** | |
249 | * Format a value like 1.0 and a field like UAMEASUNIT_DURATION_MINUTE to e.g. "1.0 minutes", | |
250 | * and get the position in the formatted result for certain types for fields. | |
251 | * | |
252 | * @param measfmt | |
253 | * The UAMeasureFormat object specifying the format conventions. | |
254 | * @param value | |
255 | * The numeric value to format | |
256 | * @param unit | |
257 | * The unit to format with the specified numeric value | |
258 | * @param result | |
259 | * A pointer to a buffer to receive the formatted result. | |
260 | * @param resultCapacity | |
261 | * The maximum size of result. | |
262 | * @param pos | |
263 | * A pointer to a UFieldPosition. On input, pos->field is read; this should | |
264 | * be a value from the UNumberFormatFields enum in unum.h. On output, | |
265 | * pos->beginIndex and pos->endIndex indicate the beginning and ending offsets | |
266 | * of that field in the formatted output, if relevant. This parameter may be | |
267 | * NULL if no position information is desired. | |
268 | * @param status | |
269 | * A pointer to a UErrorCode to receive any errors. In case of error status, | |
270 | * the contents of result are undefined. | |
271 | * @return | |
272 | * The length of the formatted result; may be greater than resultCapacity, | |
273 | * in which case an error is returned. | |
274 | * @draft ICU 54 | |
275 | */ | |
276 | U_DRAFT int32_t U_EXPORT2 | |
277 | uameasfmt_formatGetPosition( const UAMeasureFormat* measfmt, | |
278 | double value, | |
279 | UAMeasureUnit unit, | |
280 | UChar* result, | |
281 | int32_t resultCapacity, | |
282 | UFieldPosition* pos, | |
283 | UErrorCode* status ); | |
284 | ||
285 | /** | |
286 | * Format a list of value and unit combinations, using locale-appropriate | |
287 | * conventions for the list. Each combination is represented by a UAMeasure | |
288 | * that combines a value and unit, such as 5.3 + UAMEASUNIT_DURATION_HOUR or | |
289 | * 37.2 + UAMEASUNIT_DURATION_MINUTE. For all except the last UAMeasure in the | |
290 | * list, the numeric part will be formatted using the default formatter (zero | |
291 | * decimal places, rounds toward 0); for the last UAMeasure, the default may | |
292 | * be overriden by passing a number formatter in uameasfmt_open. The result | |
293 | * can thus be something like "5 hours, 37.2 minutes" or "5 hrs, 37.2 mins". | |
294 | * | |
295 | * @param measfmt | |
296 | * The UAMeasureFormat object specifying the format conventions. | |
297 | * @param measures | |
298 | * A list of UAMeasure structs each specifying a numeric value | |
299 | * and a UAMeasureUnit. | |
300 | * @param measureCount | |
301 | * The count of UAMeasureUnits in measures. Currently this has a limit of 8. | |
302 | * @param result | |
303 | * A pointer to a buffer to receive the formatted result. | |
304 | * @param resultCapacity | |
305 | * The maximum size of result. | |
306 | * @param status | |
307 | * A pointer to a UErrorCode to receive any errors. In case of error status, | |
308 | * the contents of result are undefined. | |
309 | * @return | |
310 | * The length of the formatted result; may be greater than resultCapacity, | |
311 | * in which case an error is returned. | |
312 | * @draft ICU 54 | |
313 | */ | |
314 | U_DRAFT int32_t U_EXPORT2 | |
315 | uameasfmt_formatMultiple( const UAMeasureFormat* measfmt, | |
316 | const UAMeasure* measures, | |
317 | int32_t measureCount, | |
318 | UChar* result, | |
319 | int32_t resultCapacity, | |
320 | UErrorCode* status ); | |
321 | ||
322 | ||
323 | #endif /* U_HIDE_DRAFT_API */ | |
324 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
325 | ||
326 | #endif /* #ifndef UAMEASUREFORMAT_H */ |