@@ -29,6 +29,8 @@ bool qapi_bool_parse(const char *name, const char *value, bool *obj,
Error **errp);
int parse_qapi_name(const char *name, bool complete);
+struct strList *strList_from_string(const char *in, char c);
+int qemu_string_count_delim(const char *str, char delim);
/*
* For any GenericList @list, insert @element at the front.
@@ -56,4 +58,14 @@ int parse_qapi_name(const char *name, bool complete);
(tail) = &(*(tail))->next; \
} while (0)
+/* provides the length of any type of list */
+#define QAPI_LIST_LENGTH(list) ({ \
+ size_t _len = 0; \
+ typeof(list) _elem; \
+ for (_elem = list; _elem != NULL; _elem = _elem->next) { \
+ _len++; \
+ } \
+ _len; \
+})
+
#endif
@@ -43,6 +43,7 @@
#include "qapi/qapi-commands-stats.h"
#include "qapi/qapi-commands-tpm.h"
#include "qapi/qapi-commands-ui.h"
+#include "qapi/util.h"
#include "qapi/qapi-visit-net.h"
#include "qapi/qapi-visit-migration.h"
#include "qapi/qmp/qdict.h"
@@ -71,32 +72,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)
return false;
}
-/*
- * Produce a strList from a comma separated list.
- * A NULL or empty input string return NULL.
- */
-static strList *strList_from_comma_list(const char *in)
-{
- strList *res = NULL;
- strList **tail = &res;
-
- while (in && in[0]) {
- char *comma = strchr(in, ',');
- char *value;
-
- if (comma) {
- value = g_strndup(in, comma - in);
- in = comma + 1; /* skip the , */
- } else {
- value = g_strdup(in);
- in = NULL;
- }
- QAPI_LIST_APPEND(tail, value);
- }
-
- return res;
-}
-
void hmp_info_name(Monitor *mon, const QDict *qdict)
{
NameInfo *info;
@@ -1121,7 +1096,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict)
migrate_announce_params());
qapi_free_strList(params->interfaces);
- params->interfaces = strList_from_comma_list(interfaces_str);
+ params->interfaces = strList_from_string(interfaces_str, ',');
params->has_interfaces = params->interfaces != NULL;
params->id = g_strdup(id);
params->has_id = !!params->id;
@@ -2399,7 +2374,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names,
request->provider = provider_idx;
if (names && !g_str_equal(names, "*")) {
request->has_names = true;
- request->names = strList_from_comma_list(names);
+ request->names = strList_from_string(names, ',');
}
QAPI_LIST_PREPEND(request_list, request);
}
@@ -152,3 +152,38 @@ int parse_qapi_name(const char *str, bool complete)
}
return p - str;
}
+
+/*
+ * Produce a strList from a delimiter separated list.
+ * A NULL or empty input string return NULL.
+ */
+strList *strList_from_string(const char *in, char c)
+{
+ strList *res = NULL;
+ strList **tail = &res;
+
+ while (in && in[0]) {
+ char *ch = strchr(in, c);
+ char *value;
+
+ if (ch) {
+ value = g_strndup(in, ch - in);
+ in = ch + 1; /* skip the , */
+ } else {
+ value = g_strdup(in);
+ in = NULL;
+ }
+ QAPI_LIST_APPEND(tail, value);
+ }
+
+ return res;
+}
+
+int qemu_string_count_delim(const char *str, char delim)
+{
+ int count = 0;
+ for (int i = 0; i < strlen(str); i++) {
+ count += (str[i] == delim);
+ }
+ return count;
+}
i) strList_from_string() shifted from hm-cmds.c -> qapi-util.c ii) Adding qemu_string_count_delim() helper func. in qapi-util.c and QAPI_LIST_LENGTH() macro defined func. in util.h Suggested-by: Manish Mishra <manish.mishra@nutanix.com> Signed-off-by: Het Gala <het.gala@nutanix.com> --- include/qapi/util.h | 12 ++++++++++++ monitor/hmp-cmds.c | 31 +++---------------------------- qapi/qapi-util.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 28 deletions(-)