From b1058f051980710a7fe50690af13f633a84dd877 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20Szczekutek?= Date: Mon, 27 Jun 2016 15:31:38 +0200 Subject: [PATCH] [Base-utils][Format] Complete module added MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I3fc92c80e3278201081e30af4e9c979ce10404c4 Signed-off-by: Beata Stefaniuk Signed-off-by: Rafał Szczekutek --- src/CMakeLists.txt | 2 + src/include/mobile/utils_i18n.h | 32 ++++++- src/include/mobile/utils_i18n_format.h | 148 +++++++++++++++++++++++++++++++ src/include/wearable/utils_i18n.h | 32 ++++++- src/include/wearable/utils_i18n_format.h | 148 +++++++++++++++++++++++++++++++ src/utils_i18n_format.cpp | 105 ++++++++++++++++++++++ 6 files changed, 465 insertions(+), 2 deletions(-) create mode 100644 src/include/mobile/utils_i18n_format.h create mode 100644 src/include/wearable/utils_i18n_format.h create mode 100644 src/utils_i18n_format.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0e58af7..4eb6081 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,7 @@ SET(BASEUTILS_SRCS utils_i18n_formattable.cpp utils_i18n_measure_unit.cpp utils_i18n_measure.cpp + utils_i18n_format.cpp ) ADD_LIBRARY(${target_name} SHARED ${BASEUTILS_SRCS} @@ -75,5 +76,6 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_alpha_idx.h DEST INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_formattable.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_measure_unit.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_measure.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_format.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) \ No newline at end of file diff --git a/src/include/mobile/utils_i18n.h b/src/include/mobile/utils_i18n.h index 507c92e..962ffe1 100644 --- a/src/include/mobile/utils_i18n.h +++ b/src/include/mobile/utils_i18n.h @@ -36,6 +36,7 @@ #include #include #include +#include /** * @file utils_i18n.h @@ -50,7 +51,7 @@ extern "C" { /** * @ingroup CAPI_BASE_UTILS_MODULE * @defgroup CAPI_BASE_UTILS_I18N_MODULE i18n - * @brief The i18n module contains uchar, ucollator, unormalization, usearch, ustring, ucalendar, udate, udatepg, ulocale, unumber, alpha_idx, formattable, measure unit and measure. + * @brief The i18n module contains uchar, ucollator, unormalization, usearch, ustring, ucalendar, udate, udatepg, ulocale, unumber, alpha_idx, formattable, measure unit, measure and format. * 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. * @@ -142,6 +143,10 @@ extern "C" { * * * + * + * + * + * *
@ref CAPI_BASE_UTILS_I18N_MEASURE_MODULEThe Measure module represents an amount of a specified unit, consisting of a number and a unit.
@ref CAPI_BASE_UTILS_I18N_FORMAT_MODULEThe Format module represents the basic format object.
* * @section CAPI_BASE_UTILS_I18N_MODULE_MAPPING_TABLE Mapping Table @@ -2793,6 +2798,31 @@ extern "C" { * #i18n_measure_get_unit * getUnit * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_destroy + * ~Format + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_clone + * clone + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_format + * format + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_parse_object + * parseObject + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_get_locale + * getLocale + * * */ diff --git a/src/include/mobile/utils_i18n_format.h b/src/include/mobile/utils_i18n_format.h new file mode 100644 index 0000000..9545984 --- /dev/null +++ b/src/include/mobile/utils_i18n_format.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2015 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_FORMAT_H__ +#define __UTILS_I18N_FORMAT_H__ + +#include + +/** + * @file utils_i18n_format.h + * @version 0.1 + * @brief utils_i18n_format + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @ingroup CAPI_BASE_UTILS_I18N_MODULE + * @defgroup CAPI_BASE_UTILS_I18N_FORMAT_MODULE Format + * @brief The Format module represents the base class for all formats. + * @section CAPI_BASE_UTILS_I18N_FORMAT_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_BASE_UTILS_I18N_FORMAT_MODULE_OVERVIEW Overview + * @details The Format module specifies the protocol for classes which convert other objects or values,\n + * such as numeric values and dates, and their string representations.\n + * In some cases these representations may be localized or contain localized characters or strings.\n + * Please note, that there is no function for creating an #i18n_format_h object as this module uses a mechanism + * similar to inheritance, known in object-oriented languages. All of the functions that take the + * #i18n_format_h handle as a parameter can actually take the handle to the more specific format object related + * to the corresponding derived classes. + * Please also note that at this point the only derived class that is supported by the base-utils i18n module is the + * ICU's MeasureFormat class, which is why it is possible to use handles of type #i18n_measure_format_h + * in place of the parameters of type #i18n_format_h. + */ + +/** + * @addtogroup CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * @{ + */ + +/** + * @brief Destroys the format object. + * @since_tizen 3.0 + * + * @param[in] format The format object to destroy + * + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + */ +int i18n_format_destroy(i18n_format_h format); + +/** + * @brief Creates a polymorphic clone of the given @a format object. + * @remarks The cloned object should be released by the caller with the + * i18n_format_destroy() function. + * @since_tizen 3.0 + * + * @param[in] format The format object to be cloned + * @param[out] clone The clone of the given @a format object + * + * @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_format_clone(i18n_format_h format, i18n_format_h *clone); + +/** + * @brief Formats an object to produce a string. + * @remarks The @a append_to parameter should be released by the caller with the + * free() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] formattable The object to format + * @param[in/out] append_to An output parameter to receive the result. + * Result is appended to the existing contents. + * + * @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_format_format(i18n_format_h format, i18n_formattable_h formattable, char **append_to); + +/** + * @brief Parses a string to produce an object. + * @remarks The obtained @a result formattable object should be released by the caller with the + * i18n_formattable_destroy() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] source The string to be parsed into an object + * @param[out] result The formattable object to be set to the parse result. + * If parse fails, return contents are undefined. + * + * @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_format_parse_object(i18n_format_h format, const char *source, i18n_formattable_h *result); + +/** + * @brief Gets the locale for the given format object. + * @details You can choose between valid and actual locale. + * @remarks Both @a language and @a country should be released by the caller with the free() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] type The type of the locale we're looking for (valid or actual) + * @param[out] language The obtained locale's ISO-639 language code + * @param[out] country The obtained locale's ISO-3166 country code + * + * @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_format_get_locale(i18n_format_h format, + i18n_uloc_data_locale_type_e type, const char **language, const char **country); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __UTILS_I18N_FORMAT_H__*/ diff --git a/src/include/wearable/utils_i18n.h b/src/include/wearable/utils_i18n.h index 5c288d1..e91a135 100644 --- a/src/include/wearable/utils_i18n.h +++ b/src/include/wearable/utils_i18n.h @@ -36,6 +36,7 @@ #include #include #include +#include /** * @file utils_i18n.h @@ -50,7 +51,7 @@ extern "C" { /** * @ingroup CAPI_BASE_UTILS_MODULE * @defgroup CAPI_BASE_UTILS_I18N_MODULE i18n - * @brief The i18n module contains uchar, ucollator, unormalization, usearch, ustring, ucalendar, udate, udatepg, ulocale, unumber, alpha_idx, formattable and measure. + * @brief The i18n module contains uchar, ucollator, unormalization, usearch, ustring, ucalendar, udate, udatepg, ulocale, unumber, alpha_idx, formattable, measure and format. * 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. * @@ -142,6 +143,10 @@ extern "C" { * * * + * + * + * + * *
@ref CAPI_BASE_UTILS_I18N_MEASURE_MODULEThe Measure module represents an amount of a specified unit, consisting of a number and a unit.
@ref CAPI_BASE_UTILS_I18N_FORMAT_MODULEThe Format module represents the basic format object.
* * @section CAPI_BASE_UTILS_I18N_MODULE_MAPPING_TABLE Mapping Table @@ -2793,6 +2798,31 @@ extern "C" { * #i18n_measure_get_unit * getUnit * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_destroy + * ~Format + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_clone + * clone + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_format + * format + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_parse_object + * parseObject + * + * + * @ref CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * #i18n_format_get_locale + * getLocale + * * */ diff --git a/src/include/wearable/utils_i18n_format.h b/src/include/wearable/utils_i18n_format.h new file mode 100644 index 0000000..9545984 --- /dev/null +++ b/src/include/wearable/utils_i18n_format.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2015 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_FORMAT_H__ +#define __UTILS_I18N_FORMAT_H__ + +#include + +/** + * @file utils_i18n_format.h + * @version 0.1 + * @brief utils_i18n_format + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @ingroup CAPI_BASE_UTILS_I18N_MODULE + * @defgroup CAPI_BASE_UTILS_I18N_FORMAT_MODULE Format + * @brief The Format module represents the base class for all formats. + * @section CAPI_BASE_UTILS_I18N_FORMAT_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_BASE_UTILS_I18N_FORMAT_MODULE_OVERVIEW Overview + * @details The Format module specifies the protocol for classes which convert other objects or values,\n + * such as numeric values and dates, and their string representations.\n + * In some cases these representations may be localized or contain localized characters or strings.\n + * Please note, that there is no function for creating an #i18n_format_h object as this module uses a mechanism + * similar to inheritance, known in object-oriented languages. All of the functions that take the + * #i18n_format_h handle as a parameter can actually take the handle to the more specific format object related + * to the corresponding derived classes. + * Please also note that at this point the only derived class that is supported by the base-utils i18n module is the + * ICU's MeasureFormat class, which is why it is possible to use handles of type #i18n_measure_format_h + * in place of the parameters of type #i18n_format_h. + */ + +/** + * @addtogroup CAPI_BASE_UTILS_I18N_FORMAT_MODULE + * @{ + */ + +/** + * @brief Destroys the format object. + * @since_tizen 3.0 + * + * @param[in] format The format object to destroy + * + * @return @c 0 on success, otherwise a negative error value + * @retval #I18N_ERROR_NONE Successful + * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter + */ +int i18n_format_destroy(i18n_format_h format); + +/** + * @brief Creates a polymorphic clone of the given @a format object. + * @remarks The cloned object should be released by the caller with the + * i18n_format_destroy() function. + * @since_tizen 3.0 + * + * @param[in] format The format object to be cloned + * @param[out] clone The clone of the given @a format object + * + * @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_format_clone(i18n_format_h format, i18n_format_h *clone); + +/** + * @brief Formats an object to produce a string. + * @remarks The @a append_to parameter should be released by the caller with the + * free() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] formattable The object to format + * @param[in/out] append_to An output parameter to receive the result. + * Result is appended to the existing contents. + * + * @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_format_format(i18n_format_h format, i18n_formattable_h formattable, char **append_to); + +/** + * @brief Parses a string to produce an object. + * @remarks The obtained @a result formattable object should be released by the caller with the + * i18n_formattable_destroy() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] source The string to be parsed into an object + * @param[out] result The formattable object to be set to the parse result. + * If parse fails, return contents are undefined. + * + * @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_format_parse_object(i18n_format_h format, const char *source, i18n_formattable_h *result); + +/** + * @brief Gets the locale for the given format object. + * @details You can choose between valid and actual locale. + * @remarks Both @a language and @a country should be released by the caller with the free() function. + * @since_tizen 3.0 + * + * @param[in] format The format object + * @param[in] type The type of the locale we're looking for (valid or actual) + * @param[out] language The obtained locale's ISO-639 language code + * @param[out] country The obtained locale's ISO-3166 country code + * + * @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_format_get_locale(i18n_format_h format, + i18n_uloc_data_locale_type_e type, const char **language, const char **country); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __UTILS_I18N_FORMAT_H__*/ diff --git a/src/utils_i18n_format.cpp b/src/utils_i18n_format.cpp new file mode 100644 index 0000000..327bbeb --- /dev/null +++ b/src/utils_i18n_format.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2015 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 +#include + +int i18n_format_destroy(i18n_format_h format) +{ + retv_if(format == NULL, I18N_ERROR_INVALID_PARAMETER); + + delete((Format *) format); + + return I18N_ERROR_NONE; +} + +int i18n_format_clone(i18n_format_h format, i18n_format_h *clone) +{ + retv_if(format == NULL || clone == NULL, I18N_ERROR_INVALID_PARAMETER); + + *clone = ((Format *) format)->clone(); + retv_if(*clone == NULL, I18N_ERROR_OUT_OF_MEMORY); + + return I18N_ERROR_NONE; +} + +int i18n_format_format(i18n_format_h format, i18n_formattable_h formattable, char **append_to) +{ + retv_if(format == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(formattable == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(append_to == NULL, I18N_ERROR_INVALID_PARAMETER); + UErrorCode status = U_ZERO_ERROR; + + UnicodeString unicode_append_to(*append_to); + + UnicodeString result = ((Format *) format)->format(*((Formattable *)formattable), unicode_append_to, status); + const UChar *uchar_result = result.getTerminatedBuffer(); + + retv_if(uchar_result == NULL, I18N_ERROR_INVALID_PARAMETER); + int32_t ulen = u_strlen(uchar_result); + + retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER); + *append_to = (char *) malloc(ulen + 1); + + retv_if(*append_to == NULL, I18N_ERROR_OUT_OF_MEMORY); + + u_austrcpy(*append_to, uchar_result); + + return _i18n_error_mapping(status); +} + +int i18n_format_parse_object(i18n_format_h format, const char *source, i18n_formattable_h *result) +{ + retv_if(format == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(source == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(result == NULL, I18N_ERROR_INVALID_PARAMETER); + + UErrorCode status = U_ZERO_ERROR; + const UnicodeString unicode_source(source); + + Formattable *f = new Formattable(); + + ((Format *) format)->parseObject(unicode_source, *f, status); + + *result = (i18n_formattable_h)f; + + return _i18n_error_mapping(status); +} + +int i18n_format_get_locale(i18n_format_h format, i18n_uloc_data_locale_type_e type, + const char **language, const char **country) +{ + retv_if(format == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(type < I18N_ULOC_ACTUAL_LOCALE || type > I18N_ULOC_REQUESTED_LOCALE, I18N_ERROR_INVALID_PARAMETER); + retv_if(language == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(country == NULL, I18N_ERROR_INVALID_PARAMETER); + + UErrorCode status = U_ZERO_ERROR; + ULocDataLocaleType icu_type = (ULocDataLocaleType) type; + + Locale locale = ((Format *) format)->getLocale(icu_type, status); + + *language = strdup(locale.getLanguage()); + retv_if(*language == NULL, I18N_ERROR_OUT_OF_MEMORY); + + *country = strdup(locale.getCountry()); + retv_if(*country == NULL, I18N_ERROR_OUT_OF_MEMORY); + + return _i18n_error_mapping(status); +} -- 2.7.4