[Base-utils][Measure Unit] get_available* functions implementation updated 31/81031/7
authorRafal Szczekutek <r.szczekutek@samsung.com>
Thu, 21 Jul 2016 11:51:40 +0000 (13:51 +0200)
committerSzczekutek <r.szczekutek@samsung.com>
Tue, 26 Jul 2016 14:08:03 +0000 (07:08 -0700)
Change-Id: I0f938f16be32f848bd688a0fe0454ea451c85e23
Signed-off-by: Rafal Szczekutek <r.szczekutek@samsung.com>
Signed-off-by: Jakub Siewierski <j.siewierski@samsung.com>
src/include/mobile/utils_i18n_measure_unit.h
src/include/wearable/utils_i18n_measure_unit.h
src/utils_i18n_measure_unit.cpp

index ae16c42743b1f9cfd1fab97324d88e28e8a26775..08170bc7d4851cbb58628dac6131bc2998550638 100644 (file)
@@ -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.
index 5324c16a97741611e9db0def1900a3cec022afbb..011531556d42323aaa92aba0c7e8a2e72ee4a0d2 100644 (file)
@@ -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.
index d2a86d272a86a6e3ae579eb82828a01cb64c88f9..f316261eb7f7436f1ee9715410920c9ec8cf1830 100644 (file)
@@ -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)