tizen 2.3.1 release
[framework/api/base-utils.git] / src / utils_i18n_unumber.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <unicode/unum.h>
19 #include <utils_i18n_unumber.h>
20 #include <utils_i18n_private.h>
21
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)
23 {
24     retv_if (num_format == NULL, I18N_ERROR_INVALID_PARAMETER);
25
26     UErrorCode icu_error = U_ZERO_ERROR;
27     *num_format = unum_open(style, pattern, pattern_len, locale, (UParseError*)parse_err, &icu_error);
28
29     return _i18n_error_mapping(icu_error);
30 }
31
32 int i18n_unumber_destroy (i18n_unumber_format_h fmt)
33 {
34     retv_if(fmt == NULL, I18N_ERROR_INVALID_PARAMETER);
35
36     unum_close(fmt);
37
38     return I18N_ERROR_NONE;
39 }
40
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)
42 {
43     retv_if(fmt == NULL || len_symbol == NULL, I18N_ERROR_INVALID_PARAMETER);
44
45     UErrorCode icu_error = U_ZERO_ERROR;
46     *len_symbol = unum_getSymbol(fmt, symbol, buffer, size, &icu_error);
47
48     return _i18n_error_mapping(icu_error);
49 }
50
51 // Newly Added APIs
52
53 int i18n_unumber_clone(const i18n_unumber_format_h fmt, i18n_unumber_format_h *fmt_clone)
54 {
55     if(fmt == NULL || NULL == fmt_clone) {
56         return I18N_ERROR_INVALID_PARAMETER;
57     }
58
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);
62
63     i18n_error_code_e i18n_error;
64     ERR_MAPPING(icu_error, i18n_error);
65     return i18n_error;
66 }
67
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)
69 {
70     if(NULL == fmt) {
71         if(NULL != status) {
72             *status = I18N_ERROR_INVALID_PARAMETER;
73         }
74         return 0;
75     }
76     UErrorCode err;
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);
80     if(NULL != status) {
81         ERR_MAPPING(err, *status);
82     }
83     return result_unum_format;
84 }
85
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)
87 {
88     if(fmt == NULL) {
89         set_last_result(I18N_ERROR_INVALID_PARAMETER);
90         return 0;
91     }
92
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);
96
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;
101 }
102
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)
104 {
105     if(fmt == NULL) {
106         set_last_result(I18N_ERROR_INVALID_PARAMETER);
107         return 0;
108     }
109
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);
113
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;
118 }
119
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)
121 {
122     if(fmt == NULL || number == NULL) {
123         set_last_result(I18N_ERROR_INVALID_PARAMETER);
124         return 0;
125     }
126
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);
130
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;
135 }
136
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)
138 {
139     if(fmt == NULL) {
140         set_last_result(I18N_ERROR_INVALID_PARAMETER);
141         return 0;
142     }
143
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);
147
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;
152 }
153
154 int32_t i18n_unumber_parse (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos)
155 {
156     if(fmt == NULL || text == NULL) {
157         set_last_result(I18N_ERROR_INVALID_PARAMETER);
158         return 0;
159     }
160
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);
164
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;
169 }
170
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)
172 {
173     if(fmt == NULL || text == NULL) {
174         set_last_result(I18N_ERROR_INVALID_PARAMETER);
175         return 0;
176     }
177
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);
181
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;
186 }
187
188 double i18n_unumber_parse_double (const i18n_unumber_format_h fmt, const i18n_uchar *text, int32_t text_length, int32_t *parse_pos)
189 {
190     if(fmt == NULL || text == NULL) {
191         set_last_result(I18N_ERROR_INVALID_PARAMETER);
192         return 0;
193     }
194
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);
198
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;
203 }
204
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)
206 {
207     if(fmt == NULL || text == NULL) {
208         set_last_result(I18N_ERROR_INVALID_PARAMETER);
209         return 0;
210     }
211
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);
215
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;
220 }
221
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)
223 {
224     if(fmt == NULL || text == NULL || currency == NULL) {
225         set_last_result(I18N_ERROR_INVALID_PARAMETER);
226         return 0;
227     }
228
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);
232
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;
237 }
238
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)
240 {
241     if(format == NULL || pattern == NULL || pattern_length < -1) {
242         return I18N_ERROR_INVALID_PARAMETER;
243     }
244
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);
248
249     i18n_error_code_e i18n_error;
250     ERR_MAPPING(icu_error, i18n_error);
251     return i18n_error;
252 }
253
254 const char *i18n_unumber_get_available (int32_t locale_index)
255 {
256     if(locale_index < 0) {
257         set_last_result(I18N_ERROR_INVALID_PARAMETER);
258         return NULL;
259     }
260
261     set_last_result(I18N_ERROR_NONE);
262     return unum_getAvailable(locale_index);
263 }
264
265 int32_t i18n_unumber_count_available (void)
266 {
267     set_last_result(I18N_ERROR_NONE);
268     return unum_countAvailable();
269 }
270
271 int32_t i18n_unumber_get_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr)
272 {
273     if(fmt == NULL) {
274         set_last_result(I18N_ERROR_INVALID_PARAMETER);
275         return 0;
276     }
277
278     set_last_result(I18N_ERROR_NONE);
279     return unum_getAttribute(fmt, attr);
280 }
281
282 int i18n_unumber_set_attribute (i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr, int32_t new_value)
283 {
284     if(fmt == NULL) {
285         return I18N_ERROR_INVALID_PARAMETER;
286     }
287
288     unum_setAttribute(fmt, attr, new_value);
289     return I18N_ERROR_NONE;
290 }
291
292 double i18n_unumber_get_double_attribute (const i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr)
293 {
294     if(fmt == NULL) {
295         set_last_result(I18N_ERROR_INVALID_PARAMETER);
296         return 0;
297     }
298
299     set_last_result(I18N_ERROR_NONE);
300     return unum_getDoubleAttribute (fmt, attr);
301 }
302
303 int i18n_unumber_set_double_attribute (i18n_unumber_format_h fmt, i18n_unumber_format_attribute_e attr, double new_value)
304 {
305     if(fmt == NULL) {
306         return I18N_ERROR_INVALID_PARAMETER;
307     }
308
309     unum_setDoubleAttribute(fmt, attr, new_value);
310     return I18N_ERROR_NONE;
311 }
312
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)
314 {
315     if(fmt == NULL) {
316        set_last_result(I18N_ERROR_INVALID_PARAMETER);
317        return 0;
318     }
319
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);
323
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;
328 }
329
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)
331 {
332     if(fmt == NULL) {
333         return I18N_ERROR_INVALID_PARAMETER;
334     }
335
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);
339
340     i18n_error_code_e i18n_error;
341     ERR_MAPPING(icu_error, i18n_error);
342     return i18n_error;
343 }
344
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)
346 {
347     if(fmt == NULL) {
348        set_last_result(I18N_ERROR_INVALID_PARAMETER);
349        return 0;
350     }
351
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);
355
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;
360 }
361
362 int i18n_unumber_set_symbol (i18n_unumber_format_h fmt, i18n_unumber_format_symbol_e symbol, const i18n_uchar *value, int32_t length)
363 {
364     if(fmt == NULL) {
365         return I18N_ERROR_INVALID_PARAMETER;
366     }
367
368     UErrorCode icu_error = U_ZERO_ERROR;
369     unum_setSymbol(fmt, symbol, value, length, &icu_error);
370     ERR("Error code : %d", icu_error);
371
372     i18n_error_code_e i18n_error;
373     ERR_MAPPING(icu_error, i18n_error);
374     return i18n_error;
375 }
376
377 const char *i18n_unumber_get_locale_by_type (const i18n_unumber_format_h fmt, i18n_ulocale_data_locale_type_e type)
378 {
379     if(fmt == NULL) {
380        set_last_result(I18N_ERROR_INVALID_PARAMETER);
381        return 0;
382     }
383
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);
387
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;
392 }