[sheepdog] [PATCH 1/5] lib: add a helper to strip out strbuf

Liu Yuan namei.unix at gmail.com
Thu Aug 30 08:59:10 CEST 2012


From: Liu Yuan <tailai.ly at taobao.com>

This patch prepares for trace patch set.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 doc/api-strbuf.txt |    6 ++++++
 include/strbuf.h   |    3 ++-
 lib/strbuf.c       |   12 ++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/doc/api-strbuf.txt b/doc/api-strbuf.txt
index c10caef..9f2eb6a 100644
--- a/doc/api-strbuf.txt
+++ b/doc/api-strbuf.txt
@@ -203,3 +203,9 @@ same behaviour as well.
 	Copy the contents of the strbuf to the second argument 'buf'.
 	The number of bytes to be copied is at most the third argument
 	'len'.
+
+`strbuf_stripout`::
+
+	Strip out the contents of the strbuf to the second argument 'buf'.
+	The number of bytes to be copied is at most the third argument
+	'len'.
diff --git a/include/strbuf.h b/include/strbuf.h
index 7ffa903..42794b9 100644
--- a/include/strbuf.h
+++ b/include/strbuf.h
@@ -95,7 +95,8 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
 extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 /* XXX: if read fails, any partial read is undone */
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
-int strbuf_getline(struct strbuf *sb, FILE *fp, int term);
+extern int strbuf_getline(struct strbuf *sb, FILE *fp, int term);
 extern int strbuf_copyout(struct strbuf *sb, void *buf, size_t len);
+extern int strbuf_stripout(struct strbuf *sb, void *buf, size_t len);
 
 #endif
diff --git a/lib/strbuf.c b/lib/strbuf.c
index d0c3c27..f87400e 100644
--- a/lib/strbuf.c
+++ b/lib/strbuf.c
@@ -195,3 +195,15 @@ int strbuf_copyout(struct strbuf *sb, void *buf, size_t len)
 
 	return len;
 }
+
+int strbuf_stripout(struct strbuf *sb, void *buf, size_t len)
+{
+	len = min(len, sb->len);
+	if (len == 0)
+		goto out;
+
+	memcpy(buf, sb->buf, len);
+	strbuf_remove(sb, 0, len);
+out:
+	return len;
+}
-- 
1.7.10.2




More information about the sheepdog mailing list