2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <unicode/unum.h>
19 #include <utils_i18n_unumber.h>
20 #include <utils_i18n_private.h>
22 int i18n_unumber_create (i18n_unumber_format_style_e style, const i18n_uchar *pattern, int32_t pattern_len, const char *locale, i18n_uparse_error_s *parse_err, i18n_unumber_format_h *num_format)
24 retv_if (num_format == NULL, I18N_ERROR_INVALID_PARAMETER);
26 UErrorCode icu_error = U_ZERO_ERROR;
27 *num_format = unum_open(style, pattern, pattern_len, locale, (UParseError*)parse_err, &icu_error);
29 return _i18n_error_mapping(icu_error);
32 int i18n_unumber_destroy (i18n_unumber_format_h fmt)
34 retv_if(fmt == NULL, I18N_ERROR_INVALID_PARAMETER);
38 return I18N_ERROR_NONE;
41 int i18n_unumber_get_symbol (const i18n_unumber_format_h fmt, i18n_unumber_format_symbol_e symbol, i18n_uchar *buffer, int32_t size, int32_t *len_symbol)
43 retv_if(fmt == NULL || len_symbol == NULL, I18N_ERROR_INVALID_PARAMETER);
45 UErrorCode icu_error = U_ZERO_ERROR;
46 *len_symbol = unum_getSymbol(fmt, symbol, buffer, size, &icu_error);
48 return _i18n_error_mapping(icu_error);
53 int i18n_unumber_clone(const i18n_unumber_format_h fmt, i18n_unumber_format_h *fmt_clone)
55 if(fmt == NULL || NULL == fmt_clone) {
56 return I18N_ERROR_INVALID_PARAMETER;
59 UErrorCode icu_error = U_ZERO_ERROR;
60 *fmt_clone = (i18n_unumber_format_h)unum_clone(fmt, &icu_error);
61 ERR("Error code : %d", icu_error);
63 i18n_error_code_e i18n_error;
64 ERR_MAPPING(icu_error, i18n_error);
68 int32_t i18n_unumber_format (const i18n_unumber_format_h fmt, int32_t number, i18n_uchar *result, int32_t result_length, i18n_ufield_position_s *pos, i18n_error_code_e *status)
72 *status = I18N_ERROR_INVALID_PARAMETER;
77 ERR_MAPPING_REVERSE(*status, err);
78 int32_t result_unum_format= unum_format (fmt, number, (UChar*)result, result_length, (UFieldPosition*)pos, &err);
79 ERR("pErrorCode : %d", err);
81 ERR_MAPPING(err, *status);
83 return result_unum_format;
86 int32_t i18n_unumber_format_int64 (const i18n_unumber_format_h fmt, int64_t number, i18n_uchar *result, int32_t result_length, i18n_ufield_position_h pos)
89 set_last_result(I18N_ERROR_INVALID_PARAMETER);
93 UErrorCode icu_error = U_ZERO_ERROR;
94 int32_t result_unum_formatInt64 = unum_formatInt64 (fmt, number, result, result_length, (UFieldPosition*)pos, &icu_error);
95 ERR("Error code : %d", icu_error);
97 i18n_error_code_e i18n_error;
98 ERR_MAPPING(icu_error, i18n_error);
99 set_last_result(i18n_error);
100 return result_unum_formatInt64;
103 int32_t i18n_unumber_format_double (const i18n_unumber_format_h fmt, double number, i18n_uchar *result, int32_t result_length, i18n_ufield_position_h pos)
106 set_last_result(I18N_ERROR_INVALID_PARAMETER);
110 UErrorCode icu_error = U_ZERO_ERROR;
111 int32_t result_unum_formatDouble = unum_formatDouble(fmt, number, result, result_length, (UFieldPosition*)pos, &icu_error);
112 ERR("Error code : %d", icu_error);
114 i18n_error_code_e i18n_error;
115 ERR_MAPPING(icu_error, i18n_error);
116 set_last_result(i18n_error);
117 return result_unum_formatDouble;
120 int32_t i18n_unumber_format_decimal (const i18n_unumber_format_h fmt, const char *number, int32_t length, i18n_uchar *result, int32_t result_length, i18n_ufield_position_h pos)
122 if(fmt == NULL || number == NULL) {
123 set_last_result(I18N_ERROR_INVALID_PARAMETER);
127 UErrorCode icu_error = U_ZERO_ERROR;
128 int32_t result_unum_formatDecimal = unum_formatDecimal (fmt, number, length, result, result_length, (UFieldPosition*)pos, &icu_error);
129 ERR("Error code : %d", icu_error);
131 i18n_error_code_e i18n_error;
132 ERR_MAPPING(icu_error, i18n_error);
133 set_last_result(i18n_error);
134 return result_unum_formatDecimal;
137 int32_t i18n_unumber_format_double_currency (const i18n_unumber_format_h fmt, double number, i18n_uchar *currency, i18n_uchar *result, int32_t result_length, i18n_ufield_position_h pos)
140 set_last_result(I18N_ERROR_INVALID_PARAMETER);
144 UErrorCode icu_error = U_ZERO_ERROR;
145 int32_t result_unum_formatDoubleCurrency = unum_formatDoubleCurrency (fmt, number, currency, result, result_length, (UFieldPosition*)pos, &icu_error);
146 ERR("Error code : %d", icu_error);
148 i18n_error_code_e i18n_error;
149 ERR_MAPPING(icu_error, i18n_error);
150 set_last_result(i18n_error);
151 return result_unum_formatDoubleCurrency;
154 int32_t i18n_unumber_parse (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos)
156 if(fmt == NULL || text == NULL) {
157 set_last_result(I18N_ERROR_INVALID_PARAMETER);
161 UErrorCode icu_error = U_ZERO_ERROR;
162 int32_t result_unum_parse = unum_parse (fmt, text, text_length, parse_pos, &icu_error);
163 ERR("Error code : %d", icu_error);
165 i18n_error_code_e i18n_error;
166 ERR_MAPPING(icu_error, i18n_error);
167 set_last_result(i18n_error);
168 return result_unum_parse;
171 int64_t i18n_unumber_parse_int64 (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos)
173 if(fmt == NULL || text == NULL) {
174 set_last_result(I18N_ERROR_INVALID_PARAMETER);
178 UErrorCode icu_error = U_ZERO_ERROR;
179 int64_t result_unum_parseInt64 = unum_parseInt64 (fmt, text, text_length, parse_pos, &icu_error);
180 ERR("Error code : %d", icu_error);
182 i18n_error_code_e i18n_error;
183 ERR_MAPPING(icu_error, i18n_error);
184 set_last_result(i18n_error);
185 return result_unum_parseInt64;
188 double i18n_unumber_parse_double (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos)
190 if(fmt == NULL || text == NULL) {
191 set_last_result(I18N_ERROR_INVALID_PARAMETER);
195 UErrorCode icu_error = U_ZERO_ERROR;
196 double result_unum_parseDouble = unum_parseDouble (fmt, text, text_length, parse_pos, &icu_error);
197 ERR("Error code : %d", icu_error);
199 i18n_error_code_e i18n_error;
200 ERR_MAPPING(icu_error, i18n_error);
201 set_last_result(i18n_error);
202 return result_unum_parseDouble;
205 int32_t i18n_unumber_parse_decimal (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos, char *out_buf, int32_t out_buf_length)
207 if(fmt == NULL || text == NULL) {
208 set_last_result(I18N_ERROR_INVALID_PARAMETER);
212 UErrorCode icu_error = U_ZERO_ERROR;
213 int32_t result_unum_parseDecimal = unum_parseDecimal (fmt, text, text_length, parse_pos, out_buf, out_buf_length, &icu_error);
214 ERR("Error code : %d", icu_error);
216 i18n_error_code_e i18n_error;
217 ERR_MAPPING(icu_error, i18n_error);
218 set_last_result(i18n_error);
219 return result_unum_parseDecimal;
222 double i18n_unumber_parse_double_currency (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos, i18n_uchar *currency)
224 if(fmt == NULL || text == NULL || currency == NULL) {
225 set_last_result(I18N_ERROR_INVALID_PARAMETER);
229 UErrorCode icu_error = U_ZERO_ERROR;
230 int32_t result_unum_parseDoubleCurrency = unum_parseDoubleCurrency (fmt, text, text_length, parse_pos, currency, &icu_error);
231 ERR("Error code : %d", icu_error);
233 i18n_error_code_e i18n_error;
234 ERR_MAPPING(icu_error, i18n_error);
235 set_last_result(i18n_error);
236 return result_unum_parseDoubleCurrency;
239 int i18n_unumber_apply_pattern (i18n_unumber_format_h format, i18n_ubool localized, const i18n_uchar *pattern, int32_t pattern_length, i18n_uparse_error_s* parse_error)
241 if(format == NULL || pattern == NULL || pattern_length < -1) {
242 return I18N_ERROR_INVALID_PARAMETER;
245 UErrorCode icu_error = U_ZERO_ERROR;
246 unum_applyPattern (format, localized, pattern, pattern_length, (UParseError*)parse_error, &icu_error);
247 ERR("Error code : %d", icu_error);
249 i18n_error_code_e i18n_error;
250 ERR_MAPPING(icu_error, i18n_error);
254 const char *i18n_unumber_get_available (int32_t locale_index)
256 if(locale_index < 0) {
257 set_last_result(I18N_ERROR_INVALID_PARAMETER);
261 set_last_result(I18N_ERROR_NONE);
262 return unum_getAvailable(locale_index);
265 int32_t i18n_unumber_count_available (void)
267 set_last_result(I18N_ERROR_NONE);
268 return unum_countAvailable();
271 int32_t i18n_unumber_get_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr)
274 set_last_result(I18N_ERROR_INVALID_PARAMETER);
278 set_last_result(I18N_ERROR_NONE);
279 return unum_getAttribute(fmt, attr);
282 int i18n_unumber_set_attribute (i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr, int32_t new_value)
285 return I18N_ERROR_INVALID_PARAMETER;
288 unum_setAttribute(fmt, attr, new_value);
289 return I18N_ERROR_NONE;
292 double i18n_unumber_get_double_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr)
295 set_last_result(I18N_ERROR_INVALID_PARAMETER);
299 set_last_result(I18N_ERROR_NONE);
300 return unum_getDoubleAttribute (fmt, attr);
303 int i18n_unumber_set_double_attribute (i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr, double new_value)
306 return I18N_ERROR_INVALID_PARAMETER;
309 unum_setDoubleAttribute(fmt, attr, new_value);
310 return I18N_ERROR_NONE;
313 int32_t i18n_unumber_get_text_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_text_attribute_e tag, i18n_uchar *result, int32_t result_length)
316 set_last_result(I18N_ERROR_INVALID_PARAMETER);
320 UErrorCode icu_error = U_ZERO_ERROR;
321 int32_t result_unum_getTextAttribute = unum_getTextAttribute (fmt, tag, result, result_length, &icu_error);
322 ERR("Error code : %d", icu_error);
324 i18n_error_code_e i18n_error;
325 ERR_MAPPING(icu_error, i18n_error);
326 set_last_result(i18n_error);
327 return result_unum_getTextAttribute;
330 int i18n_unumber_set_text_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_text_attribute_e tag, const i18n_uchar *new_value, int32_t new_value_length)
333 return I18N_ERROR_INVALID_PARAMETER;
336 UErrorCode icu_error = U_ZERO_ERROR;
337 unum_setTextAttribute((UNumberFormat*)fmt, tag, new_value, new_value_length, &icu_error);
338 ERR("Error code : %d", icu_error);
340 i18n_error_code_e i18n_error;
341 ERR_MAPPING(icu_error, i18n_error);
345 int32_t i18n_unumber_to_pattern (const i18n_unumber_format_h fmt, i18n_ubool is_pattern_localized, i18n_uchar *result, int32_t result_length)
348 set_last_result(I18N_ERROR_INVALID_PARAMETER);
352 UErrorCode icu_error = U_ZERO_ERROR;
353 int32_t result_unum_toPattern = unum_toPattern(fmt, is_pattern_localized, result, result_length, &icu_error);
354 ERR("Error code : %d", icu_error);
356 i18n_error_code_e i18n_error;
357 ERR_MAPPING(icu_error, i18n_error);
358 set_last_result(i18n_error);
359 return result_unum_toPattern;
362 int i18n_unumber_set_symbol (i18n_unumber_format_h fmt, i18n_unumber_format_symbol_e symbol, const i18n_uchar *value, int32_t length)
365 return I18N_ERROR_INVALID_PARAMETER;
368 UErrorCode icu_error = U_ZERO_ERROR;
369 unum_setSymbol(fmt, symbol, value, length, &icu_error);
370 ERR("Error code : %d", icu_error);
372 i18n_error_code_e i18n_error;
373 ERR_MAPPING(icu_error, i18n_error);
377 const char *i18n_unumber_get_locale_by_type (const i18n_unumber_format_h fmt, i18n_ulocale_data_locale_type_e type)
380 set_last_result(I18N_ERROR_INVALID_PARAMETER);
384 UErrorCode icu_error = U_ZERO_ERROR;
385 const char *result_unum_getLocaleByType = unum_getLocaleByType (fmt, type, &icu_error);
386 ERR("Error code : %d", icu_error);
388 i18n_error_code_e i18n_error;
389 ERR_MAPPING(icu_error, i18n_error);
390 set_last_result(i18n_error);
391 return result_unum_getLocaleByType;