PengZheng commented on code in PR #721:
URL: https://github.com/apache/celix/pull/721#discussion_r1464768306


##########
libs/utils/include/celix_convert_utils.h:
##########
@@ -71,13 +77,150 @@ CELIX_UTILS_EXPORT long 
celix_utils_convertStringToLong(const char* val, long de
  *
  * @param[in] val The string to convert.
  * @param[in] defaultValue The default value if the string is not a valid 
celix_version_t.
- * @param[out] converted If not NULL, will be set to true if the string is a 
valid celix_version_t, otherwise false.
- * @return A new celix_version_t* if the string is a valid version, otherwise 
NULL.
+ * @param[out] version The converted version. If the string is not a valid 
version, the version will be set to a copy of
+ *                     the defaultValue.
+ * @return CELIX_SUCCESS if the string is a valid version, 
CELIX_ILLEGAL_ARGUMENT if the string is not a valid version
+ * and CELIX_ENOMEM if memory could not be allocated. Note that on a 
CELIX_ILLEGAL_ARGUMENT the version will be set to a
+ * copy of the defaultValue.
+ */
+CELIX_UTILS_EXPORT celix_status_t celix_utils_convertStringToVersion(const 
char* val,
+                                                                     const 
celix_version_t* defaultValue,
+                                                                     
celix_version_t** version);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with long entries.
+ *
+ * The expected format of the string is a "," separated list of longs. 
Whitespace is ignored.
+ * Long entries are created using celix_utils_convertStringToLong.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of longs.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToLongArrayList(const char* val,
+                                                        const 
celix_array_list_t* defaultValue,
+                                                        celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with long entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_longArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with double entries.
+ *
+ * The expected format of the string is a "," separated list of doubles. 
Whitespace is ignored.
+ * Double entries are created using celix_utils_convertStringToDouble.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of doubles.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToDoubleArrayList(const char* val,
+                                                          const 
celix_array_list_t* defaultValue,
+                                                          celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with double entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_doubleArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with boolean entries.
+ *
+ * The expected format of the string is a "," separated list of booleans. 
Whitespace is ignored.
+ * Boolean entries are converted using celix_utils_convertStringToBool.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of booleans.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToBoolArrayList(const char* val,
+                                                        const 
celix_array_list_t* defaultValue,
+                                                        celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with boolean entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_boolArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with string entries.
+ *
+ * The expected format of the string is a "," separated list of strings. 
Whitespace is preserved.
+ * String entries are copied and the returned list will be configured to call 
free when entries are removed.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of strings.
+ *                         Note that the defaultValue is copied if the string 
is not a valid list of string entries
+ *                         and the defaultValue is expected to be configured 
with a removed entry callback so the
+ * strings are freed.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToStringArrayList(const char* val,
+                                                          const 
celix_array_list_t* defaultValue,
+                                                          celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with string entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_stringArrayListToString(const celix_array_list_t* list);

Review Comment:
   Without proper escaping mechanism, `celix_utils_stringArrayListToString` and 
`celix_utils_convertStringToStringArrayList` are not exactly reverse to each 
other.



##########
libs/utils/src/celix_convert_utils.c:
##########
@@ -107,25 +110,242 @@ long celix_utils_convertStringToLong(const char* val, 
long defaultValue, bool* c
     return result;
 }
 
-celix_version_t* celix_utils_convertStringToVersion(const char* val, const 
celix_version_t* defaultValue, bool* converted) {
-    celix_version_t* result = NULL;
-    if (val != NULL) {
-        //check if string has two dots ('.'), and only try to create string if 
it has two dots
-        char* firstDot = strchr(val, '.');
-        char* lastDot = strrchr(val, '.');
-        if (firstDot != NULL && lastDot != NULL && firstDot != lastDot) {
-            char buf[64];
-            char* valCopy = celix_utils_writeOrCreateString(buf, sizeof(buf), 
"%s", val);
-            char *trimmed = celix_utils_trimInPlace(valCopy);
-            result = celix_version_createVersionFromString(trimmed);
-            celix_utils_freeStringIfNotEqual(buf, valCopy);
+celix_status_t
+celix_utils_convertStringToVersion(const char* val, const celix_version_t* 
defaultValue, celix_version_t** version) {
+    assert(version != NULL);
+    if (!val && defaultValue) {
+        *version = celix_version_copy(defaultValue);
+        return *version ? CELIX_ILLEGAL_ARGUMENT : CELIX_ENOMEM;
+    } else if (!val) {
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    celix_status_t status = celix_version_parse(val, version);
+    if (status == CELIX_ILLEGAL_ARGUMENT) {
+        if (defaultValue) {
+            *version = celix_version_copy(defaultValue);
+            return *version ? status : CELIX_ENOMEM;
         }
+        return status;
+    }
+    return status;
+}
+
+/**
+ * @brief Convert the provided string to an array list using the provided 
addEntry callback to convert the string
+ * to a specific type and add it to the list.
+ */
+static celix_status_t celix_utils_convertStringToArrayList(const char* val,
+                                                           const 
celix_array_list_t* defaultValue,
+                                                           
celix_array_list_t** list,
+                                                           void 
(*freeCb)(void*),
+                                                           celix_status_t 
(*addEntry)(celix_array_list_t*,
+                                                                               
       const char*)) {
+    assert(list != NULL);
+    *list = NULL;
+
+    if (!val && defaultValue) {
+        *list = celix_arrayList_copy(defaultValue);
+        return *list ? CELIX_ILLEGAL_ARGUMENT : CELIX_ENOMEM;
+    } else if (!val) {
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    celix_array_list_create_options_t opts = 
CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS;
+    opts.simpleRemovedCallback = freeCb;
+    celix_autoptr(celix_array_list_t) result = 
celix_arrayList_createWithOptions(&opts);
+    if (!result) {
+        return CELIX_ENOMEM;
+    }
+
+    char buf[256];
+    char* valCopy = celix_utils_writeOrCreateString(buf, sizeof(buf), "%s", 
val);
+    if (!valCopy) {
+        return CELIX_ENOMEM;
     }
-    if (converted) {
-        *converted = result != NULL;
+
+    celix_status_t status = CELIX_SUCCESS;
+    char* savePtr = NULL;
+    char* token = strtok_r(valCopy, ",", &savePtr);

Review Comment:
   This won't work for StringArrayList. What if a string contains ','?



##########
libs/utils/include/celix_convert_utils.h:
##########
@@ -71,13 +77,150 @@ CELIX_UTILS_EXPORT long 
celix_utils_convertStringToLong(const char* val, long de
  *
  * @param[in] val The string to convert.
  * @param[in] defaultValue The default value if the string is not a valid 
celix_version_t.
- * @param[out] converted If not NULL, will be set to true if the string is a 
valid celix_version_t, otherwise false.
- * @return A new celix_version_t* if the string is a valid version, otherwise 
NULL.
+ * @param[out] version The converted version. If the string is not a valid 
version, the version will be set to a copy of
+ *                     the defaultValue.
+ * @return CELIX_SUCCESS if the string is a valid version, 
CELIX_ILLEGAL_ARGUMENT if the string is not a valid version
+ * and CELIX_ENOMEM if memory could not be allocated. Note that on a 
CELIX_ILLEGAL_ARGUMENT the version will be set to a
+ * copy of the defaultValue.
+ */
+CELIX_UTILS_EXPORT celix_status_t celix_utils_convertStringToVersion(const 
char* val,
+                                                                     const 
celix_version_t* defaultValue,
+                                                                     
celix_version_t** version);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with long entries.
+ *
+ * The expected format of the string is a "," separated list of longs. 
Whitespace is ignored.
+ * Long entries are created using celix_utils_convertStringToLong.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of longs.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToLongArrayList(const char* val,
+                                                        const 
celix_array_list_t* defaultValue,
+                                                        celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with long entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_longArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with double entries.
+ *
+ * The expected format of the string is a "," separated list of doubles. 
Whitespace is ignored.
+ * Double entries are created using celix_utils_convertStringToDouble.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of doubles.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToDoubleArrayList(const char* val,
+                                                          const 
celix_array_list_t* defaultValue,
+                                                          celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with double entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_doubleArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with boolean entries.
+ *
+ * The expected format of the string is a "," separated list of booleans. 
Whitespace is ignored.
+ * Boolean entries are converted using celix_utils_convertStringToBool.
+ *
+ * @param[in] val The string to convert.
+ * @param[in] defaultValue The default value if the string is not a valid "," 
separated list of booleans.
+ * @param[out] list The converted list. If the string is not a valid list, the 
list will be set to a copy of the
+ * defaultValue.
+ */
+CELIX_UTILS_EXPORT
+celix_status_t celix_utils_convertStringToBoolArrayList(const char* val,
+                                                        const 
celix_array_list_t* defaultValue,
+                                                        celix_array_list_t** 
list);
+
+/**
+ * @brief Convert a celix_array_list_t* with boolean entries to a string.
+ *
+ * @param[in] list The list to convert.
+ * @return The string representation of the list. The returned string is 
allocated and should be freed.
+ */
+CELIX_UTILS_EXPORT
+char* celix_utils_boolArrayListToString(const celix_array_list_t* list);
+
+/**
+ * @brief Convert a string to a celix_array_list_t* with string entries.
+ *
+ * The expected format of the string is a "," separated list of strings. 
Whitespace is preserved.

Review Comment:
   I think this is harder to define than other colon separated list, since 
proper escaping is needed to accommodate for colon-containing strings.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to