From 96d6e438cf1dcde0b484fba942660c9d1f77438c Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 8 Aug 2024 11:50:25 -0400 Subject: [PATCH] MT#55283 add strdup method Change-Id: Icf26f301895c94d789902d422cb7907ef3b6025f --- daemon/control_ng.c | 8 ++++++++ daemon/statistics.c | 9 ++++----- include/control_ng.h | 1 + 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/daemon/control_ng.c b/daemon/control_ng.c index aad4060c8..2a0d7bfa8 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -169,6 +169,9 @@ static parser_arg __bencode_list_add_dictionary(bencode_item_t *l) { static str *__bencode_collapse_str(ng_parser_ctx_t *ctx, bencode_item_t *a, str *out) { return bencode_collapse_str(a, out); } +static const char *__bencode_strdup(ng_parser_ctx_t *ctx, const char *s) { + return bencode_strdup(&ctx->ngbuf->buffer, s); +} static bool json_is_dict(JsonNode *n) { return json_node_get_node_type(n) == JSON_NODE_OBJECT; @@ -342,6 +345,9 @@ static int json_strcmp(JsonNode *n, const char *b) { const char *s = json_node_get_string(n); return strcmp(s, b); } +static const char *__json_strdup(ng_parser_ctx_t *ctx, const char *s) { + return s; +} static parser_arg json_dict(ng_parser_ctx_t *c) { JsonObject *o = json_object_new(); JsonNode *n = json_node_init_object(json_node_new(JSON_NODE_OBJECT), o); @@ -423,6 +429,7 @@ const ng_parser_t ng_parser_native = { .list_iter = bencode_list_iter, .get_str = bencode_get_str, .strcmp = bencode_strcmp, + .strdup = __bencode_strdup, .get_int_str = bencode_get_integer_str, .is_int = bencode_is_int, .get_int = bencode_get_int, @@ -454,6 +461,7 @@ const ng_parser_t ng_parser_json = { .list_iter = json_list_iter, .get_str = json_get_str, .strcmp = json_strcmp, + .strdup = __json_strdup, .get_int_str = json_get_int_str, .is_int = json_is_int, .get_int = json_get_int, diff --git a/daemon/statistics.c b/daemon/statistics.c index 045846c5c..be8454103 100644 --- a/daemon/statistics.c +++ b/daemon/statistics.c @@ -958,7 +958,6 @@ const char *statistics_ng(ng_parser_ctx_t *ctx) { parser_arg dict = ctx->resp; const char *sub_label = "statistics"; // top level - bencode_buffer_t *buf = &ctx->ngbuf->buffer; for (__auto_type l = metrics->head; l; l = l->next) { stats_metric *m = l->data; @@ -968,13 +967,13 @@ const char *statistics_ng(ng_parser_ctx_t *ctx) { // key:value entry? if (m->value_short) { if (m->is_int) - ctx->parser->dict_add_int(dict, bencode_strdup(buf, m->label), + ctx->parser->dict_add_int(dict, ctx->parser->strdup(ctx, m->label), m->int_value); else if (m->value_raw) - ctx->parser->dict_add_str_dup(dict, bencode_strdup(buf, m->label), + ctx->parser->dict_add_str_dup(dict, ctx->parser->strdup(ctx, m->label), &STR(m->value_raw)); else - ctx->parser->dict_add_str_dup(dict, bencode_strdup(buf, m->label), + ctx->parser->dict_add_str_dup(dict, ctx->parser->strdup(ctx, m->label), &STR(m->value_short)); continue; } @@ -1005,7 +1004,7 @@ const char *statistics_ng(ng_parser_ctx_t *ctx) { // is this a dictionary? if (ctx->parser->is_dict(dict)) { assert(sub_label != NULL); - ctx->parser->dict_add(dict, bencode_strdup(buf, sub_label), sub); + ctx->parser->dict_add(dict, ctx->parser->strdup(ctx, sub_label), sub); } else if (ctx->parser->is_list(dict)) ctx->parser->list_add(dict, sub); diff --git a/include/control_ng.h b/include/control_ng.h index 95b344e4c..461a53eaf 100644 --- a/include/control_ng.h +++ b/include/control_ng.h @@ -126,6 +126,7 @@ struct ng_parser { helper_arg); str *(*get_str)(parser_arg, str *s); int (*strcmp)(parser_arg, const char *); + const char *(*strdup)(ng_parser_ctx_t *, const char *); long long (*get_int_str)(parser_arg, long long def); bool (*is_int)(parser_arg); long long (*get_int)(parser_arg);