From 79bba926a886c034152b02eebe4ee44a4e73e463 Mon Sep 17 00:00:00 2001 From: Lukasz Pik Date: Thu, 16 Nov 2017 15:20:34 +0100 Subject: [PATCH] [Base-utils][ImmutableIdx][ACR-1118] Module added Change-Id: I8a8a36f4f4d4282c7dbb317fa2dcb40ee3f45476 Signed-off-by: Lukasz Pik --- src/CMakeLists.txt | 2 + src/include/utils_i18n.h | 36 ++++++++ src/include/utils_i18n_immutable_idx.h | 145 +++++++++++++++++++++++++++++++++ src/include/utils_i18n_types.h | 16 ++++ src/utils_i18n_immutable_idx.cpp | 100 +++++++++++++++++++++++ 5 files changed, 299 insertions(+) create mode 100644 src/include/utils_i18n_immutable_idx.h create mode 100644 src/utils_i18n_immutable_idx.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8aeeff..3c8df4e 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,6 +42,7 @@ SET(BASEUTILS_SRCS utils_i18n_uchar_iter.c utils_i18n_plural_rules.cpp utils_i18n_plural_format.cpp + utils_i18n_immutable_idx.cpp ) ADD_LIBRARY(${target_name} SHARED ${BASEUTILS_SRCS} @@ -91,5 +92,6 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_uversion.h DESTI INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_uchar_iter.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_plural_rules.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_plural_format.h DESTINATION ${INCLUDE_INSTALL_DIR}/base) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${INC_DIR}/utils_i18n_immutable_idx.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 e718ef1..3c2976b 100644 --- a/src/include/utils_i18n.h +++ b/src/include/utils_i18n.h @@ -47,6 +47,7 @@ #include #include #include +#include /** * @file utils_i18n.h @@ -87,6 +88,7 @@ extern "C" { * - uchar iter * - plural rules * - plural format + * - immutable_idx * * 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. @@ -229,6 +231,10 @@ extern "C" { * @ref CAPI_BASE_UTILS_I18N_PLURAL_FORMAT_MODULE * The Plural format module supports the creation of internationalized messages with plural inflection. * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * The Immutable_idx is immutable, thread-safe version of Alpha_idx. + * * * * @section CAPI_BASE_UTILS_I18N_MODULE_MAPPING_TABLE Mapping Table @@ -3875,6 +3881,36 @@ extern "C" { * #i18n_plural_format_to_pattern * toPattern * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_create + * buildImmutableIndex + * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_destroy + * ~ImmutableIndex + * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_get_bucket_count + * getBucketCount + * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_get_bucket_index + * getBucketIndex + * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_get_bucket_label + * getLabel + * + * + * @ref CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * #i18n_immutable_idx_get_bucket_label_type + * getLabelType + * * */ diff --git a/src/include/utils_i18n_immutable_idx.h b/src/include/utils_i18n_immutable_idx.h new file mode 100644 index 0000000..c9c7d35 --- /dev/null +++ b/src/include/utils_i18n_immutable_idx.h @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2017 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_IMMUTABLE_IDX_H__ +#define __UTILS_I18N_IMMUTABLE_IDX_H__ + +#include + +/** + * @file utils_i18n_immutable_idx.h + * @version 0.1 + * @brief utils_i18n_immutable_idx + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @ingroup CAPI_BASE_UTILS_I18N_MODULE + * @defgroup CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE Immutable Index + * @brief Immutable, thread-safe version of Alphabetic Index. + * @section CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE_OVERVIEW Overview + * @details This module provides thread-safe methods for bucketing, and random access to buckets + * and their properties, but does not offer adding records to the index. + */ + +/** + * @addtogroup CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE + * @{ + */ + +/** + * @brief Creates an immutable index object. + * @since_tizen 5.0 + * @remarks The created object should be released by the caller with the + * i18n_immutable_idx_destroy() function. + * + * @param[in] index The alphabetic index the thread-safe version of which is to be created + * @param[out] immutable_index The created immutable index 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_immutable_idx_create(i18n_alpha_idx_h index, i18n_immutable_idx_h *immutable_index); + +/** + * @brief Destroys the immutable index object. + * @since_tizen 5.0 + * + * @param[in] index The immutable index to be destroyed + * + * @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_immutable_idx_destroy(i18n_immutable_idx_h index); + +/** + * @brief Returns the number of index buckets and labels, including underflow/inflow/overflow. + * @since_tizen 5.0 + * + * @param[in] index The immutable index object + * @param[out] count The number of index buckets + * + * @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_immutable_idx_get_bucket_count(i18n_immutable_idx_h index, int32_t *count); + +/** + * @brief Finds the index bucket for the given name and returns the number of that bucket. + * @details Use i18n_immutable_idx_get_bucket_label() or i18n_immutable_idx_get_bucket_label_type() + * to get bucket properties. + * @since_tizen 5.0 + * + * @param[in] index The immutable index object + * @param[in] name The string to be sorted into an index bucket + * @param[out] number The bucket number for the @a name + * + * @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_immutable_idx_get_bucket_index(i18n_immutable_idx_h index, char *name, int32_t *number); + +/** + * @brief Returns the label string of the index-th bucket. + * @since_tizen 5.0 + * @remarks The returned string should be freed by the caller with free() function. + * + * @param[in] index The immutable index object + * @param[in] bucket_index The bucket number + * @param[out] label The label string for the bucket + * + * @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_INDEX_OUTOFBOUNDS Desired position is out of bounds + */ +int i18n_immutable_idx_get_bucket_label(i18n_immutable_idx_h index, int32_t bucket_index, char **label); + +/** + * @brief Returns the label type of the index-th bucket. + * @since_tizen 5.0 + * + * @param[in] index The immutable index object + * @param[in] bucket_index The bucket number + * @param[out] type The alphabetic index label type + * + * @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_INDEX_OUTOFBOUNDS Desired position is out of bounds + */ +int i18n_immutable_idx_get_bucket_label_type(i18n_immutable_idx_h index, int32_t bucket_index, i18n_alpha_idx_label_type_e *type); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __UTILS_I18N_IMMUTABLE_IDX_H__*/ diff --git a/src/include/utils_i18n_types.h b/src/include/utils_i18n_types.h index bbc536f..d7b761c 100644 --- a/src/include/utils_i18n_types.h +++ b/src/include/utils_i18n_types.h @@ -4321,6 +4321,22 @@ typedef void *i18n_plural_format_h; */ /** +* @addtogroup CAPI_BASE_UTILS_I18N_IMMUTABLE_IDX_MODULE +* @{ +*/ + +/** + * @brief An #i18n_immutable_idx_h handle. + * @details Use i18n_immutable_idx_* functions to operate on #i18n_immutable_idx_h objects. + * @since_tizen 5.0 + */ +typedef void *i18n_immutable_idx_h; + +/** + * @} + */ + +/** * @brief This value is intended for sentinel values for APIs that (take or) return * single code points (#i18n_uchar32). * @details It is outside of the Unicode code point range 0..0x10ffff. For example, diff --git a/src/utils_i18n_immutable_idx.cpp b/src/utils_i18n_immutable_idx.cpp new file mode 100644 index 0000000..0d24e92 --- /dev/null +++ b/src/utils_i18n_immutable_idx.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2017 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 +#include + +int i18n_immutable_idx_create(i18n_alpha_idx_h index, i18n_immutable_idx_h *immutable_index) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(immutable_index == NULL, I18N_ERROR_INVALID_PARAMETER); + + UErrorCode status = U_ZERO_ERROR; + + *immutable_index = ((AlphabeticIndex *)index)->buildImmutableIndex(status); + retv_if(*immutable_index == NULL, I18N_ERROR_OUT_OF_MEMORY); + + return _i18n_error_mapping(status); +} + +int i18n_immutable_idx_destroy(i18n_immutable_idx_h index) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + + delete ((AlphabeticIndex::ImmutableIndex *) index); + + return I18N_ERROR_NONE; +} + +int i18n_immutable_idx_get_bucket_count(i18n_immutable_idx_h index, int32_t *count) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(count == NULL, I18N_ERROR_INVALID_PARAMETER); + + *count = ((AlphabeticIndex::ImmutableIndex *) index)->getBucketCount(); + + return I18N_ERROR_NONE; +} + +int i18n_immutable_idx_get_bucket_index(i18n_immutable_idx_h index, char *name, int32_t *number) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(name == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(number == NULL, I18N_ERROR_INVALID_PARAMETER); + + UErrorCode status = U_ZERO_ERROR; + UnicodeString unicode_name(name); + + *number = ((AlphabeticIndex::ImmutableIndex *) index)->getBucketIndex(unicode_name, status); + + return _i18n_error_mapping(status); +} + +int i18n_immutable_idx_get_bucket_label(i18n_immutable_idx_h index, int32_t position, char **label) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(label == NULL, I18N_ERROR_INVALID_PARAMETER); + + const AlphabeticIndex::Bucket *bucket = ((AlphabeticIndex::ImmutableIndex *) index)->getBucket(position); + retv_if(bucket == NULL, I18N_ERROR_INDEX_OUTOFBOUNDS); + + UnicodeString result = bucket->getLabel(); + + std::string string_result; + result.toUTF8String(string_result); + + *label = strdup(string_result.c_str()); + retv_if(*label == NULL, I18N_ERROR_OUT_OF_MEMORY); + + return I18N_ERROR_NONE; +} + +int i18n_immutable_idx_get_bucket_label_type(i18n_immutable_idx_h index, int32_t position, i18n_alpha_idx_label_type_e *type) +{ + retv_if(index == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(type == NULL, I18N_ERROR_INVALID_PARAMETER); + + const AlphabeticIndex::Bucket *bucket = ((AlphabeticIndex::ImmutableIndex *) index)->getBucket(position); + retv_if(bucket == NULL, I18N_ERROR_INDEX_OUTOFBOUNDS); + + *type =(i18n_alpha_idx_label_type_e) bucket->getLabelType(); + + return I18N_ERROR_NONE; +} -- 2.7.4