From: Rafal Szczekutek Date: Thu, 21 Jul 2016 11:51:40 +0000 (+0200) Subject: [Base-utils][Measure Unit] get_available* functions implementation updated X-Git-Tag: submit/tizen/20160817.024641~1^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f5ac6cc7afe73bbb78bfbafb55030e047f2d9c35;p=platform%2Fcore%2Fapi%2Fbase-utils.git [Base-utils][Measure Unit] get_available* functions implementation updated Change-Id: I0f938f16be32f848bd688a0fe0454ea451c85e23 Signed-off-by: Rafal Szczekutek Signed-off-by: Jakub Siewierski --- diff --git a/src/include/mobile/utils_i18n_measure_unit.h b/src/include/mobile/utils_i18n_measure_unit.h index ae16c42..08170bc 100644 --- a/src/include/mobile/utils_i18n_measure_unit.h +++ b/src/include/mobile/utils_i18n_measure_unit.h @@ -132,16 +132,19 @@ int i18n_measure_unit_get_type(i18n_measure_unit_h measure_unit, char **type); int i18n_measure_unit_get_subtype(i18n_measure_unit_h measure_unit, char **subtype); /** - * @brief Gets all of the available units. - * @details If there are too many units to fit into @a dest_capacity the returned error code - * is #I18N_ERROR_BUFFER_OVERFLOW. + * @brief Gets the available units. + * @details The function creates the array with the results. + * If there are more units available than the given @a max_count + * then the returned array is truncated to the first @a max_count units + * and the returned error code is #I18N_ERROR_BUFFER_OVERFLOW. + * * @since_tizen 3.0 * @remarks The obtained array of measure unit objects should be released by the caller * with the #i18n_measure_unit_array_destroy() function. * - * @param[in] dest_capacity The capacity of the given @a dest_array - * @param[out] dest_array The destination buffer - * @param[out] available The number of available units + * @param[in] max_count The maximal number of units that will be obtained + * @param[out] out_array The output array + * @param[out] available The actual number of available units * * @return @c 0 on success, otherwise a negative error value * @retval #I18N_ERROR_NONE Successful @@ -149,20 +152,23 @@ int i18n_measure_unit_get_subtype(i18n_measure_unit_h measure_unit, char **subty * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory * @retval #I18N_ERROR_BUFFER_OVERFLOW Buffer overflow */ -int i18n_measure_unit_get_available(int32_t dest_capacity, i18n_measure_unit_h **dest_array, int32_t *available); +int i18n_measure_unit_get_available(int32_t max_count, i18n_measure_unit_h **out_array, int32_t *available); /** - * @brief Gets all of the available units for a specific type. - * @details If there are too many units to fit into dest_capacity then the error code - * is set to #I18N_ERROR_BUFFER_OVERFLOW. + * @brief Gets the available units for a specific type. + * @details The function creates the array with the results. + * If there are more units available than the given @a max_count + * then the returned array is truncated to the first @a max_count units + * and the returned error code is #I18N_ERROR_BUFFER_OVERFLOW. + * * @since_tizen 3.0 * @remarks The obtained array of measure unit objects should be released by the caller * with the #i18n_measure_unit_array_destroy() function. * - * @param[in] dest_capacity The capacity of the given @a dest_array - * @param[in] type The type of the obtained units - * @param[out] dest_array The destination buffer - * @param[out] available The number of available units + * @param[in] max_count The maximal number of units that will be obtained + * @param[in] type The type of the obtained units + * @param[out] out_array The output array + * @param[out] available The actual number of available units * * @return @c 0 on success, otherwise a negative error value * @retval #I18N_ERROR_NONE Successful @@ -170,7 +176,8 @@ int i18n_measure_unit_get_available(int32_t dest_capacity, i18n_measure_unit_h * * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory * @retval #I18N_ERROR_BUFFER_OVERFLOW Buffer overflow */ -int i18n_measure_unit_get_available_with_type(int32_t dest_capacity, const char *type, i18n_measure_unit_h **dest_array, int32_t *available); +int i18n_measure_unit_get_available_with_type(int32_t max_count, const char *type, + i18n_measure_unit_h **out_array, int32_t *available); /** * @brief Invokes the given callback function for every available measure unit type. diff --git a/src/include/wearable/utils_i18n_measure_unit.h b/src/include/wearable/utils_i18n_measure_unit.h index 5324c16..0115315 100644 --- a/src/include/wearable/utils_i18n_measure_unit.h +++ b/src/include/wearable/utils_i18n_measure_unit.h @@ -129,19 +129,22 @@ int i18n_measure_unit_get_type(i18n_measure_unit_h measure_unit, char **type); * @retval #I18N_ERROR_INVALID_PARAMETER Invalid function parameter * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory */ -int i18n_measure_unit_get_subtype(i18n_measure_unit_h measure_unit, char **subtype); +int i18n_measure_unit_get_subtype(i18n_measur_unit_h measure_unit, char **subtype); /** - * @brief Gets all of the available units. - * @details If there are too many units to fit into @a dest_capacity the returned error code - * is #I18N_ERROR_BUFFER_OVERFLOW. + * @brief Gets the available units. + * @details The function creates the array with the results. + * If there are more units available than the given @a max_count + * then the returned list is truncated to the first @a max_count units + * and the returned error code is #I18N_ERROR_BUFFER_OVERFLOW. + * * @since_tizen 2.3.2 * @remarks The obtained array of measure unit objects should be released by the caller * with the #i18n_measure_unit_array_destroy() function. * - * @param[in] dest_capacity The capacity of the given @a dest_array - * @param[out] dest_array The destination buffer - * @param[out] available The number of available units + * @param[in] max_count The maximal number of units that will be obtained + * @param[out] out_array The output array + * @param[out] available The actual number of available units * * @return @c 0 on success, otherwise a negative error value * @retval #I18N_ERROR_NONE Successful @@ -149,20 +152,23 @@ int i18n_measure_unit_get_subtype(i18n_measure_unit_h measure_unit, char **subty * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory * @retval #I18N_ERROR_BUFFER_OVERFLOW Buffer overflow */ -int i18n_measure_unit_get_available(int32_t dest_capacity, i18n_measure_unit_h **dest_array, int32_t *available); +int i18n_measure_unit_get_available(int32_t max_count, i18n_measure_unit_h **out_array, int32_t *available); /** - * @brief Gets all of the available units for a specific type. - * @details If there are too many units to fit into dest_capacity then the error code - * is set to #I18N_ERROR_BUFFER_OVERFLOW. + * @brief Gets the available units for a specific type. + * @details The function creates the array with the results. + * If there are more units available than the given @a max_count + * then the returned list is truncated to the first @a max_count units + * and the returned error code is #I18N_ERROR_BUFFER_OVERFLOW. + * * @since_tizen 2.3.2 * @remarks The obtained array of measure unit objects should be released by the caller * with the #i18n_measure_unit_array_destroy() function. * - * @param[in] dest_capacity The capacity of the given @a dest_array - * @param[in] type The type of the obtained units - * @param[out] dest_array The destination buffer - * @param[out] available The number of available units + * @param[in] max_count The maximal number of units that will be obtained + * @param[in] type The type of the obtained units + * @param[out] out_array The output array + * @param[out] available The actual number of available units * * @return @c 0 on success, otherwise a negative error value * @retval #I18N_ERROR_NONE Successful @@ -170,7 +176,8 @@ int i18n_measure_unit_get_available(int32_t dest_capacity, i18n_measure_unit_h * * @retval #I18N_ERROR_OUT_OF_MEMORY Out of memory * @retval #I18N_ERROR_BUFFER_OVERFLOW Buffer overflow */ -int i18n_measure_unit_get_available_with_type(int32_t dest_capacity, const char *type, i18n_measure_unit_h **dest_array, int32_t *available); +int i18n_measure_unit_get_available_with_type(int32_t max_count, const char *type, + i18n_measure_unit_h **out_array, int32_t *available); /** * @brief Invokes the given callback function for every available measure unit type. diff --git a/src/utils_i18n_measure_unit.cpp b/src/utils_i18n_measure_unit.cpp index d2a86d2..f316261 100644 --- a/src/utils_i18n_measure_unit.cpp +++ b/src/utils_i18n_measure_unit.cpp @@ -91,47 +91,72 @@ int i18n_measure_unit_get_subtype(i18n_measure_unit_h measure_unit, char **subty return I18N_ERROR_NONE; } -int i18n_measure_unit_get_available(int32_t dest_capacity, i18n_measure_unit_h **dest_array, int32_t *available) +int i18n_measure_unit_get_available(int32_t max_count, i18n_measure_unit_h **out_array, int32_t *available) { retv_if(available == NULL, I18N_ERROR_INVALID_PARAMETER); - retv_if(dest_array == NULL, I18N_ERROR_INVALID_PARAMETER); - retv_if(dest_capacity < 0, I18N_ERROR_INVALID_PARAMETER); + retv_if(out_array == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(max_count < 0, I18N_ERROR_INVALID_PARAMETER); - MeasureUnit *mu_array = new MeasureUnit[dest_capacity]; UErrorCode status = U_ZERO_ERROR; - *available = MeasureUnit::getAvailable(mu_array, dest_capacity, status); + // Get the number of available units. + *available = MeasureUnit::getAvailable(NULL, 0, status); + retv_if(*available < 0, _i18n_error_mapping(status)); - *dest_array = new i18n_measure_unit_h[dest_capacity]; - for (int i = 0; i < dest_capacity; ++i) { - (*dest_array)[i] = (const i18n_measure_unit_h) mu_array[i].clone(); + // Get the available units. + MeasureUnit *mu_array = new MeasureUnit[*available]; + status = U_ZERO_ERROR; + + MeasureUnit::getAvailable(mu_array, *available, status); + + // If the maximal count given by the user is lower then the available + // number of units, return only the first max_count units. + // Otherwise return all of the available units. + int32_t count = max_count < *available ? max_count : *available; + + *out_array = new i18n_measure_unit_h[count]; + for (int i = 0; i < count; ++i) { + (*out_array)[i] = (i18n_measure_unit_h) mu_array[i].clone(); } delete[] mu_array; - return _i18n_error_mapping(status); + return max_count < *available ? I18N_ERROR_BUFFER_OVERFLOW : _i18n_error_mapping(status); } -int i18n_measure_unit_get_available_with_type(int32_t dest_capacity, const char *type, i18n_measure_unit_h **dest_array, int32_t *available) +int i18n_measure_unit_get_available_with_type(int32_t max_count, const char *type, + i18n_measure_unit_h **out_array, int32_t *available) { retv_if(available == NULL, I18N_ERROR_INVALID_PARAMETER); retv_if(type == NULL, I18N_ERROR_INVALID_PARAMETER); - retv_if(dest_array == NULL, I18N_ERROR_INVALID_PARAMETER); - retv_if(dest_capacity < 0, I18N_ERROR_INVALID_PARAMETER); + retv_if(out_array == NULL, I18N_ERROR_INVALID_PARAMETER); + retv_if(max_count < 0, I18N_ERROR_INVALID_PARAMETER); - MeasureUnit *mu_array = new MeasureUnit[dest_capacity]; UErrorCode status = U_ZERO_ERROR; - *available = MeasureUnit::getAvailable(type, mu_array, dest_capacity, status); + // Get the number of available units for the given type. + *available = MeasureUnit::getAvailable(type, NULL, 0, status); + retv_if(*available < 0, _i18n_error_mapping(status)); - *dest_array = new i18n_measure_unit_h[dest_capacity]; - for (int i = 0; i < dest_capacity; ++i) { - (*dest_array)[i] = (i18n_measure_unit_h) mu_array[i].clone(); + // Get the available units for the given type. + MeasureUnit *mu_array = new MeasureUnit[*available]; + status = U_ZERO_ERROR; + + MeasureUnit::getAvailable(type, mu_array, *available, status); + + // If the maximal count given by the user is lower then the available + // number of units, return only the first max_count units. + // Otherwise return all of the available units for the given type. + int32_t count = max_count < *available ? max_count : *available; + + *out_array = new i18n_measure_unit_h[count]; + for (int i = 0; i < count; ++i) { + (*out_array)[i] = (i18n_measure_unit_h) mu_array[i].clone(); } delete[] mu_array; - return _i18n_error_mapping(status); + return max_count < *available ? I18N_ERROR_BUFFER_OVERFLOW : _i18n_error_mapping(status); } int i18n_measure_unit_foreach_available_type(i18n_measure_unit_types_cb cb, void *user_data)