From 9b9e23be6223e749904de2da14ce6d666403fca4 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 24 Jan 2022 16:10:37 -0600 Subject: [PATCH] a/util: Add a new constructor and a new method to u_string_list --- src/xrt/auxiliary/util/u_string_list.cpp | 33 ++++++++++++++++++++++++ src/xrt/auxiliary/util/u_string_list.h | 27 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/xrt/auxiliary/util/u_string_list.cpp b/src/xrt/auxiliary/util/u_string_list.cpp index cd1bcfcb5..84709e847 100644 --- a/src/xrt/auxiliary/util/u_string_list.cpp +++ b/src/xrt/auxiliary/util/u_string_list.cpp @@ -58,6 +58,22 @@ u_string_list_create_from_list(struct u_string_list *usl) } } +struct u_string_list * +u_string_list_create_from_array(const char *const *arr, uint32_t size) +{ + if (arr == nullptr || size == 0) { + return u_string_list_create(); + } + try { + auto ret = std::make_unique(xrt::auxiliary::util::StringList{size}); + for (uint32_t i = 0; i < size; ++i) { + ret->list.push_back(arr[i]); + } + return ret.release(); + } catch (std::exception const &) { + return nullptr; + } +} uint32_t u_string_list_get_size(const struct u_string_list *usl) @@ -95,6 +111,23 @@ u_string_list_append(struct u_string_list *usl, const char *str) } } +int +u_string_list_append_array(struct u_string_list *usl, const char *const *arr, uint32_t size) +{ + + if (usl == nullptr) { + return -1; + } + try { + for (uint32_t i = 0; i < size; ++i) { + usl->list.push_back(arr[i]); + } + return 1; + } catch (std::exception const &) { + return -1; + } +} + int u_string_list_append_unique(struct u_string_list *usl, const char *str) { diff --git a/src/xrt/auxiliary/util/u_string_list.h b/src/xrt/auxiliary/util/u_string_list.h index af494f382..9319fc110 100644 --- a/src/xrt/auxiliary/util/u_string_list.h +++ b/src/xrt/auxiliary/util/u_string_list.h @@ -48,6 +48,18 @@ u_string_list_create_with_capacity(uint32_t capacity); struct u_string_list * u_string_list_create_from_list(struct u_string_list *usl); +/*! + * @brief Create a new string list from an array of suitable strings. + * + * @param arr an array of zero or more non-null, null-terminated string that must live at least as long as the list, + * preferably string literals. + * @param size the number of elements in the array. + * + * @public @memberof u_string_list + */ +struct u_string_list * +u_string_list_create_from_array(const char *const *arr, uint32_t size); + /*! * @brief Retrieve the number of elements in the list * @@ -77,6 +89,21 @@ u_string_list_get_data(const struct u_string_list *usl); int u_string_list_append(struct u_string_list *usl, const char *str); + +/*! + * @brief Append an array of new string literals to the list. + * + * @param usl self pointer + * @param arr an array of zero or more non-null, null-terminated string that must live at least as long as the list, + * preferably string literals. + * @param size the number of elements in the array. + * @return 1 if successfully added, negative for errors. + * + * @public @memberof u_string_list + */ +int +u_string_list_append_array(struct u_string_list *usl, const char *const *arr, uint32_t size); + /*! * @brief Append a new string literal to the list, if it's not the same as a string already in the list. *