git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/6] Use strbufs to in read_message (imap-send.c), custom buffer--.

From
Pierre Habouzit <madcoder@debian.org>
Date
Sep 9, 2007, 00:04 UTC
Message-ID
<11892962771756-git-send-email-madcoder@debian.org>
In-Reply-To
<11892962771947-git-send-email-madcoder@debian.org>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 imap-send.c |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)
diff --git a/imap-send.c b/imap-send.c
index a5a0696..d3dc5dd 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -23,6 +23,7 @@
  */
 
 #include "cache.h"
+#include "strbuf.h"
 
 typedef struct store_conf {
 	char *name;
@@ -1160,28 +1161,18 @@ imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
 static int
 read_message( FILE *f, msg_data_t *msg )
 {
-	int len, r;
+	struct strbuf buf;
 
-	memset( msg, 0, sizeof *msg );
-	len = CHUNKSIZE;
-	msg->data = xmalloc( len+1 );
-	msg->data[0] = 0;
-
-	while(!feof( f )) {
-		if (msg->len >= len) {
-			void *p;
-			len += CHUNKSIZE;
-			p = xrealloc(msg->data, len+1);
-			if (!p)
-				break;
-			msg->data = p;
-		}
-		r = fread( &msg->data[msg->len], 1, len - msg->len, f );
-		if (r <= 0)
+	memset(msg, 0, sizeof(*msg));
+	strbuf_init(&buf);
+
+	do {
+		if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
 			break;
-		msg->len += r;
-	}
-	msg->data[msg->len] = 0;
+	} while (!feof(f));
+
+	msg->len  = buf.len;
+	msg->data = strbuf_detach(&buf);
 	return msg->len;
 }
 
-- 
1.5.3.1
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 6 of 9 in “Use more strbufs series [on top of next]”
  1. Pierre HabouzitSep 9, 2007
  2. 1/6 Add strbuf_rtrim and strbuf_insert.Pierre Habouzit, Sep 9, 2007
  3. 2/6 Change semantics of interpolate to work like snprintf.Pierre Habouzit, Sep 9, 2007
  4. 3/6 Rework pretty_print_commit to use strbufs instead of custom buffers.Pierre Habouzit, Sep 9, 2007
  5. 4/6 Use strbuf_read in builtin-fetch-tool.c.Pierre Habouzit, Sep 9, 2007
  6. 5/6 Use strbufs to in read_message (imap-send.c), custom buffer--.Pierre Habouzit, Sep 9, 2007
  7. 6/6 Replace all read_fd use with strbuf_read, and get rid of it.Pierre Habouzit, Sep 9, 2007
  8. Pierre HabouzitSep 9, 2007
  9. Linus TorvaldsSep 27, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.