From patchwork Fri Oct 10 03:42:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 569 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760067808; bh=DQck7Q6MwbmfQQ73UhDeLe5TEoshOSqmfWUbttr8En4=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=HA1wC2s441Q8jaQm8EzzF+MqIIm2nBAO88NBOJVbGKZtC7DDMWSYnY0YhvH7KxsgN Dn5eTRYXG0kwcheumPp1bK6NKlegdiC/7CSXN+7g6mkzUgMAR09pj66iUNlO0bmuu8 FwZohoghO0yNnm0KJt6npldi7FWjywkl3vl40a6kXmfnjAVEXKXqDsLHzwruupKjzY G66KjeB1B29flTGa+sGm5u+gB1mDIlkMgJzrVSKg7gxLp9haG4UMeekVQcIik+1Wtz Q1mVOhtDvQZP4ZM3Bse/+4NFMkHsrDIMJHzvPFgVSBMshDmxQlVlKCridzr3NNn1TV 0fu5rPJQ9uxrw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B18BB67F7F for ; Thu, 9 Oct 2025 21:43:28 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id aLKMohFoRxTw for ; Thu, 9 Oct 2025 21:43:28 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760067806; bh=DQck7Q6MwbmfQQ73UhDeLe5TEoshOSqmfWUbttr8En4=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=COeHbi+DInsWk75YmFiudzM52S4x678U1AsktmFwK+HbwdjOpte5sVl6dC6rUmJ7/ j4aFTDincb6noWZKJjjku3XFt14M+y4bkRxPmiZpHtohmixIF7Wto/+9YVXvzePzeM 3CK6kPfQqgaYF7XftgORBGj3mpW2AXd1mKRq/Wzdi5Sa8oHzPB2vcRtjaAqEe+efYn QQ6kRYUV1IlypHguq0p0Wkak9tgU9AScNCtORkr98iP8w1TueLPHzbW+R7Wx821u9f WiES4y8Kl7rwFP3uP8XRTFnWFLID3ImAOXcTPDi7sPyokE/QhQ2WbDzx9Vzm2eGabu nCLRwgByxth7g== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AE9A667FF2 for ; Thu, 9 Oct 2025 21:43:26 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760067803; bh=vtujPPnNgGdL8J5iAutXXo1eJLSZoV2DfuJDly2vIvc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f6TwHYX9xAhE84gH8W5UjD3RapbRpDEYULPXjUkwUrmFxVICCHbVdEqskQ6bZVXtE pFlz96eLP2Lff8hBTiTQdaukaVSg9/RQu75Swl9rpd7J4u+BMve7mh/MEQNdoBJGHk 1lD8WXu6xAMwTVuWGIRhdUPs/h+NR5SDkQ+vPF1lPSOF3Rf5PkGizc0S50h0+Dk4m9 XtCP3zqQi+6/lDg6R4P5b7AJM9vMYjOgoDU48gkkBTeW872gN3a0it7Zjl+kwmU6qB K9xnbXpOLL/LByk19sbs3Lms186a2DTtCQpy2yBIDwnxcPpFUetvLRwM4t1d68DwlT LPBImxMIhaENQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A7BB76806E; Thu, 9 Oct 2025 21:43:23 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id KXLa5EEUxwJO; Thu, 9 Oct 2025 21:43:23 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760067797; bh=hB4X1KA6Czlo0pcz9ajhW66wynyTQusJfVzJnFBJ0go=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZvXTgV/QujBFNL5YHMO3i1PG/Sxdf698rcSQs5nn8N/XkKnt3gexP7xwTlBNDsQkX 1+dcgmncpRJpbDCyRqismtchwnR753SREU4zhX4OR2AGHDQG+HR2LFEIYKrvB1i6Q5 DydvlkFtngUeKDs2EEJJ5oeMzW8R1M4eCUsslEHM0jgp/om+LBi+VHolRzOKet4SS/ Ryex7P1UX/xqj0GWaG46y2n0S3C+ZBz3xHgKpjJMqkdyNYgKc81nMwN+6vwH0CyiKs VHYcHetn4fHmyZZ7ggmmd7MuWyhqGzcfHb+2tH+lO9tJu6ChXiZGcynLulfYPkb0w7 1xUIrfqtrJWRg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id AE96367F7F; Thu, 9 Oct 2025 21:43:16 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Thu, 9 Oct 2025 21:42:30 -0600 Message-ID: <20251010034255.1099728-3-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251010034255.1099728-1-sjg@u-boot.org> References: <20251010034255.1099728-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: KQW5VDXG6536YHE7R6KSJ3WP3QCMH6DQ X-Message-ID-Hash: KQW5VDXG6536YHE7R6KSJ3WP3QCMH6DQ X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 02/18] test: Add a way to printf() into a membuf List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a membuf_printf() function which supports writing a formatted string into a membuf. Co-developed-by: Claude Signed-off-by: Simon Glass --- include/membuf.h | 13 +++++++++++++ lib/membuf.c | 14 ++++++++++++++ test/lib/membuf.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/include/membuf.h b/include/membuf.h index 3352faa0606..17fe721ee3c 100644 --- a/include/membuf.h +++ b/include/membuf.h @@ -268,4 +268,17 @@ int membuf_new(struct membuf *mb, int size); */ void membuf_dispose(struct membuf *mb); +/** + * membuf_printf() - write a formatted string to a membuff + * + * Formats a string and writes it to the membuff. Returns the number of bytes + * written (not including the terminating nul). + * + * @mb: membuff to write to + * @fmt: format string + * @...: arguments for format string + * Return: number of bytes written, or negative error + */ +int membuf_printf(struct membuf *mb, const char *fmt, ...); + #endif diff --git a/lib/membuf.c b/lib/membuf.c index 47a1b06664a..207dff5625b 100644 --- a/lib/membuf.c +++ b/lib/membuf.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "membuf.h" static inline bool is_full(const struct membuf *mb) @@ -435,3 +436,16 @@ void membuf_dispose(struct membuf *mb) free(mb->start); membuf_uninit(mb); } + +int membuf_printf(struct membuf *mb, const char *fmt, ...) +{ + char buf[256]; + va_list args; + int len; + + va_start(args, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + return membuf_put(mb, buf, len); +} diff --git a/test/lib/membuf.c b/test/lib/membuf.c index 2e7de9cdc57..7ae9c079a4c 100644 --- a/test/lib/membuf.c +++ b/test/lib/membuf.c @@ -261,3 +261,43 @@ static int lib_test_membuf_init(struct unit_test_state *uts) return 0; } LIB_TEST(lib_test_membuf_init, 0); + +/* test membuf_printf() */ +static int lib_test_membuf_printf(struct unit_test_state *uts) +{ + struct membuf mb; + int ret, exp_len; + char buf[100]; + char out[100]; + + /* Initialize membuf with a buffer */ + membuf_init(&mb, buf, sizeof(buf)); + + /* Test simple string */ + ret = membuf_printf(&mb, "Hello"); + ut_asserteq(5, ret); + ut_asserteq(5, membuf_get(&mb, out, sizeof(out))); + out[5] = '\0'; + ut_asserteq_str("Hello", out); + + /* Test formatted string with integers */ + exp_len = 9; + membuf_purge(&mb); + ret = membuf_printf(&mb, "Value: %d", 42); + ut_asserteq(exp_len, ret); + ut_asserteq(exp_len, membuf_get(&mb, out, sizeof(out))); + out[exp_len] = '\0'; + ut_asserteq_str("Value: 42", out); + + /* Test formatted string with multiple arguments */ + membuf_purge(&mb); + exp_len = 10; + ret = membuf_printf(&mb, "x=%d y=%d", 10, 200); + ut_asserteq(exp_len, ret); + ut_asserteq(exp_len, membuf_get(&mb, out, sizeof(out))); + out[exp_len] = '\0'; + ut_asserteq_str("x=10 y=200", out); + + return 0; +} +LIB_TEST(lib_test_membuf_printf, 0);