From f8a486884b8b203a3949d21a2bc309560b201919 Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Sun, 22 May 2022 00:47:34 +1000 Subject: [PATCH] u/u_json: Implement u_json_get_int_array() --- src/xrt/auxiliary/util/u_json.c | 33 +++++++++++++++++++++++++++++++++ src/xrt/auxiliary/util/u_json.h | 8 ++++++++ 2 files changed, 41 insertions(+) diff --git a/src/xrt/auxiliary/util/u_json.c b/src/xrt/auxiliary/util/u_json.c index cb831f07a..e1efa7a6a 100644 --- a/src/xrt/auxiliary/util/u_json.c +++ b/src/xrt/auxiliary/util/u_json.c @@ -328,6 +328,39 @@ u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_s return i; } +size_t +u_json_get_int_array(const cJSON *json_array, int *out_array, size_t max_size) +{ + assert(out_array != NULL); + + if (!json_array) { + return 0; + } + if (!cJSON_IsArray(json_array)) { + return 0; + } + + size_t i = 0; + const cJSON *elt; + cJSON_ArrayForEach(elt, json_array) + { + if (i >= max_size) { + break; + } + + if (!u_json_get_int(elt, &out_array[i])) { + U_LOG_W( + "u_json_get_int got a non-number in a " + "numeric array"); + return i; + } + + i++; + } + + return i; +} + bool u_json_get_matrix_3x3(const cJSON *json, struct xrt_matrix_3x3 *out_matrix) { diff --git a/src/xrt/auxiliary/util/u_json.h b/src/xrt/auxiliary/util/u_json.h index 8dc69ab66..bac339854 100644 --- a/src/xrt/auxiliary/util/u_json.h +++ b/src/xrt/auxiliary/util/u_json.h @@ -117,6 +117,14 @@ u_json_get_float_array(const cJSON *json_array, float *out_array, size_t max_siz size_t u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_size); +/*! + * @brief Parse up to max_size int from a JSON array. + * + * @return the number of elements set. + */ +size_t +u_json_get_int_array(const cJSON *json_array, int *out_array, size_t max_size); + /*! * @brief Parse a matrix_3x3 from a JSON object. *