From 3a1bdff1bda5a8d4b52fcf5c0924097668f2e054 Mon Sep 17 00:00:00 2001 From: Hyunjee Kim Date: Tue, 21 Jan 2020 18:55:08 +0900 Subject: [PATCH] [ACR-1494][Unumsys] Module implementation Change-Id: Ica7385a526482e3fcd8d438412812e5e93a643bf Signed-off-by: Hyunjee Kim --- src/CMakeLists.txt | 2 + src/include/utils_i18n.h | 48 +++++++++++ src/include/utils_i18n_private.h | 1 + src/include/utils_i18n_types.h | 15 ++++ src/include/utils_i18n_unumsys.h | 181 +++++++++++++++++++++++++++++++++++++++ src/utils_i18n_unumsys.c | 112 ++++++++++++++++++++++++ 6 files changed, 359 insertions(+) create mode 100644 src/include/utils_i18n_unumsys.h create mode 100644 src/utils_i18n_unumsys.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 92b3655..38e9e2f 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,7 @@ SET(BASEUTILS_SRCS utils_i18n_ubidi.c utils_i18n_uversion.c utils_i18n_uchar_iter.c + utils_i18n_unumsys.c utils_i18n_plural_rules.cpp utils_i18n_plural_format.cpp utils_i18n_immutable_idx.cpp @@ -101,5 +102,6 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_date_interval.h INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_date_interval_format.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_simple_date_format.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_loc_disp_names.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_unumsys.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${pc_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) diff --git a/src/include/utils_i18n.h b/src/include/utils_i18n.h index 956055b..b1b8761 100644 --- a/src/include/utils_i18n.h +++ b/src/include/utils_i18n.h @@ -52,6 +52,7 @@ #include #include #include +#include /** * @file utils_i18n.h @@ -97,6 +98,7 @@ extern "C" { * - date interval format * - simple date format * - locale display names + * - numbering system * * This module provides flexible generation of number or date format patterns and helps you format and parse dates/number for any locale. * The i18n module provides various features based on data from ICU. The following table shows the version of ICU used in each Tizen platform. @@ -254,9 +256,15 @@ extern "C" { * * @ref CAPI_BASE_UTILS_I18N_SIMPLE_DATE_FORMAT_MODULE * The Simple Date Format module provides API for formatting and parsing dates in language-independent manner. + * + * * @ref CAPI_BASE_UTILS_I18N_LOCALE_DISPLAY_NAMES_MODULE * The locale display names module returns display names of Locales and components of Locales. * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * Numbering System + * * * * @section CAPI_BASE_UTILS_I18N_MODULE_MAPPING_TABLE Mapping Table @@ -4181,6 +4189,46 @@ extern "C" { * #i18n_loc_disp_names_get_key_value_disp_name * keyValueDisplayName * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_create + * unumsys_open + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_create_by_name + * unumsys_openByName + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_destroy + * unumsys_close + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_create_available_names + * unumsys_openAvailableNames + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_get_name + * unumsys_getName + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_is_algorithmic + * unumsys_isAlgorithmic + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_get_radix + * unumsys_getRadix + * + * + * @ref CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * #i18n_unumsys_get_description + * unumsys_getDescription + * * */ diff --git a/src/include/utils_i18n_private.h b/src/include/utils_i18n_private.h index 9b9cd40..2d9c153 100644 --- a/src/include/utils_i18n_private.h +++ b/src/include/utils_i18n_private.h @@ -85,6 +85,7 @@ extern "C" { (dst)[(dst_len) - 1] = '\0'; \ } while (0) +#define I18N_UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) #define ERR_MAPPING(ICU_ERROR, BASE_UTILS_ERROR) BASE_UTILS_ERROR = \ (i18n_error_code_e)_i18n_error_mapping((int)ICU_ERROR) int _i18n_error_mapping(int err); diff --git a/src/include/utils_i18n_types.h b/src/include/utils_i18n_types.h index 150fa74..0559680 100644 --- a/src/include/utils_i18n_types.h +++ b/src/include/utils_i18n_types.h @@ -4476,6 +4476,21 @@ typedef enum { * @} */ +/** +* @addtogroup CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE +* @{ +*/ + +/** + * @brief An i18n_unumsys_h handle. + * @since_tizen 6.0 + */ +typedef void *i18n_unumsys_h; + +/** + * @} + */ + #ifdef __cplusplus } #endif diff --git a/src/include/utils_i18n_unumsys.h b/src/include/utils_i18n_unumsys.h new file mode 100644 index 0000000..cde87d9 --- /dev/null +++ b/src/include/utils_i18n_unumsys.h @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UTILS_I18N_UNUMSYS_H__ +#define __UTILS_I18N_UNUMSYS_H__ + +#include + +/** + * @file utils_i18n_unumsys.h + * @version 0.1 + * @brief utils_i18n_unumsys + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @ingroup CAPI_BASE_UTILS_I18N_MODULE + * @defgroup CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE UNumsys + * @brief C API: UNumberingSystem, information about numbering systems. + * @section CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE_OVERVIEW Overview + * @details Defines numbering systems. A numbering system describes the scheme by which numbers are to be presented to the end user. + * In its simplest form, a numbering system describes the set of digit characters that are to be used to display numbers, + * such as Western digits, Thai digits, Arabic-Indic digits, etc., + * in a positional numbering system with a specified radix (typically 10). + * More complicated numbering systems are algorithmic in nature, and require use of an RBNF formatter (rule based number formatter), + * in order to calculate the characters to be displayed for a given number. + * Examples of algorithmic numbering systems include Roman numerals, Chinese numerals, and Hebrew numerals. + * Formatting rules for many commonly used numbering systems are included in the ICU package, + * based on the numbering system rules defined in CLDR. + * Alternate numbering systems can be specified to a locale by using the numbers locale keyword. + * Formatting numbers is done using the @ref CAPI_BASE_UTILS_I18N_UNUMBER_MODULE API. + * + */ + +/** + * @addtogroup CAPI_BASE_UTILS_I18N_UNUMSYS_MODULE + * @{ + */ + +/** + * @brief Creates an #i18n_unumsys_h using the numbering system for the specified locale. + * @since_tizen 6.0 + * @param[in] locale The locale for which the default numbering system should be created. + * @param[out] unumsys An #i18n_unumsys_h handle for the specified locale, or NULL if an error occurred. + * @remarks The @a unumsys should be released using i18n_unumsys_destroy(). + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_NOT_SUPPORTED A locale such as "en@numbers=xyz" that specifies a numbering system unknown to ICU. + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * + */ +int i18n_unumsys_create(const char *locale, i18n_unumsys_h *unumsys); + +/** + * @brief Creates an #i18n_unumsys_h using the name of one of the predefined numbering systems specified by CLDR and known to ICU, + * such as "latn", "arabext", or "hanidec"; the full list is returned by #i18n_unumsys_create_available_names. + * @since_tizen 6.0 + * @param[in] name The name of the numbering system for which an #i18n_unumsys_h should be created. + * @param[out] unumsys An #i18n_unumsys_h handle for the specified name, or NULL if an error occurred. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_NOT_SUPPORTED A numbering system unknown to ICU. + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * + */ +int i18n_unumsys_create_by_name(const char *name, i18n_unumsys_h *unumsys); + +/** + * @brief Destroys an #i18n_unumsys_h. + * @details Once closed it may no longer be used. + * @since_tizen 6.0 + * @param[in] unumsys The #i18n_unumsys_h to destroy. + * + */ +void i18n_unumsys_destroy(i18n_unumsys_h unumsys); + +/** + * @brief Returns an enumeration over the names of all of the predefined numbering systems known to ICU. + * @details The numbering system names will be in alphabetical (invariant) order. + * @since_tizen 6.0 + * @param[out] uenum A pointer to a #i18n_uenumeration_h that must be closed with #i18n_uenumeration_destroy(), or NULL if an error occurred. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int i18n_unumsys_get_available_names(i18n_uenumeration_h *uenum); + +/** + * @brief Returns the name of the specified #i18n_unumsys_h. + * @since_tizen 6.0 + * @param[in] unumsys The #i18n_unumsys_h whose name is desired. + * @param[out] name A pointer to the name of the specified #i18n_unumsys_h, or NULL if the name is not one of the ICU predefined names. + * The pointer is only valid for the lifetime of the #i18n_unumsys_h. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int i18n_unumsys_get_name(i18n_unumsys_h unumsys, const char **name); + +/** + * @brief Returns whether the given #i18n_unumsys_h is for an algorithmic (not purely positional) system. + * @since_tizen 6.0 + * @param[in] unumsys The #i18n_unumsys_h whose algorithmic status is desired. + * @param[out] is_algorithmic TRUE if the specified #i18n_unumsys_h is for an algorithmic system. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int i18n_unumsys_is_algorithmic(i18n_unumsys_h unumsys, i18n_ubool *is_algorithmic); + +/** + * @brief Returns the radix of the specified #i18n_unumsys_h. + * @details Simple positional numbering systems typically have radix 10, but might have a radix of e.g. 16 for hexadecimal. + * The radix is less well-defined for non-positional algorithmic systems. + * @since_tizen 6.0 + * @param[in] unumsys The #i18n_unumsys_h whose radix is desired. + * @param[out] radix The radix of the specified #i18n_unumsys_h. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int i18n_unumsys_get_radix(i18n_unumsys_h unumsys, int32_t *radix); + +/** + * @brief Gets the description string of the specified #i18n_unumsys_h. + * @details For simple positional systems this is the ordered string of digits (with length matching the radix), + * e.g. "\u3007\u4E00\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D" for "hanidec"; + * it would be "0123456789ABCDEF" for hexadecimal. + * + * For algorithmic systems this is the name of the RBNF ruleset used for formatting, + * e.g. "zh/SpelloutRules/%spellout-cardinal" for "hans" or "%greek-upper" for "grek". + * @since_tizen 6.0 + * @param[in] unumsys The #i18n_unumsys_h whose description string is desired. + * @param[in] result A pointer to a buffer to receive the description string. + * @param[in] result_max_len The maximum size of result. + * @param[out] ulen The total buffer size needed; if greater than result_length, the output was truncated. + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int i18n_unumsys_get_description(i18n_unumsys_h unumsys, i18n_uchar *result, int32_t result_max_len, int32_t *ulen); + + +#ifdef __cplusplus +} +#endif + +/** + * @} + * @} + */ +#endif /* __UTILS_I18N_UNUMSYS_H__*/ diff --git a/src/utils_i18n_unumsys.c b/src/utils_i18n_unumsys.c new file mode 100644 index 0000000..d206372 --- /dev/null +++ b/src/utils_i18n_unumsys.c @@ -0,0 +1,112 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +#include +#include + +int i18n_unumsys_create(const char *locale, i18n_unumsys_h *unumsys) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *unumsys = (i18n_unumsys_h *)unumsys_open(locale, &icu_error); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +int i18n_unumsys_create_by_name(const char *name, i18n_unumsys_h *unumsys) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *unumsys = (i18n_unumsys_h *)unumsys_openByName(name, &icu_error); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +void i18n_unumsys_destroy(i18n_unumsys_h unumsys) +{ + unumsys_close((UNumberingSystem *)unumsys); +} + +int i18n_unumsys_get_available_names(i18n_uenumeration_h *uenum) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *uenum = (i18n_uenumeration_h *)unumsys_openAvailableNames(&icu_error); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +int i18n_unumsys_get_name(i18n_unumsys_h unumsys, const char **name) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *name = unumsys_getName(unumsys); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +int i18n_unumsys_is_algorithmic(i18n_unumsys_h unumsys, i18n_ubool *is_algorithmic) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *is_algorithmic = (UBool)unumsys_isAlgorithmic(unumsys); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +int i18n_unumsys_get_radix(i18n_unumsys_h unumsys, int32_t *radix) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *radix = unumsys_getRadix(unumsys); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; +} + +int i18n_unumsys_get_description(i18n_unumsys_h unumsys, i18n_uchar *result, int32_t result_max_len, int32_t *ulen) +{ + i18n_error_code_e i18n_error; + UErrorCode icu_error = U_ZERO_ERROR; + + *ulen = unumsys_getDescription(unumsys, result, result_max_len, &icu_error); + ERR_MAPPING(icu_error, i18n_error); + I18N_ERR(i18n_error); + + return i18n_error; + +} + + -- 2.7.4