On Mon, Aug 06, 2012 at 06:39:55PM -0400, Ben Walton wrote:

> Excerpts from Jeff King's message of Mon Aug 06 18:31:13 -0400 2012:
> > Looking over the code, I recall now why I used stdio: strbuf_getline
> > requires it. These days we have strbuf_getwholeline_fd. It's
> > slightly less efficient (it has to read() a character at a time),
> > but since we are talking about a few characters of keyboard input,
> > it should be OK.
> I noticed the same requirement.  I'm currently building/testing a
> patch that switches from FILE -> fd and also to
> strbuf_getwholeline_fd.  Personally, I like this approach more than
> calling an open function multiple times.

OK, I'll stop working on the same thing, then. :)

We might want to do this to factor out the extra chomp you'll need by
using strbuf_getwholeline_fd:

-- >8 --
Subject: [PATCH] strbuf: implement strbuf_getline_fd

When reading a line into a strbuf, we have two options:

  1. Read from stdio, or from a descriptor

  2. Chomp the terminator, or leave it in

We already have:

  strbuf_getwholeline:    stdio, nochomp
  strbuf_getline:         stdio,   chomp
  strbuf_getwholeline_fd:    fd, nochomp

This patch adds the missing orthogonal function:

  strbuf_getline_fd:         fd,   chomp
 strbuf.c | 8 ++++++++
 strbuf.h | 1 +
 2 files changed, 9 insertions(+)

diff --git a/strbuf.c b/strbuf.c
index ec88266..a6dffcc 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -399,6 +399,14 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int 
        return 0;
+int strbuf_getline_fd(struct strbuf *sb, int fd, int term)
+       if (strbuf_getwholeline(sb, fd, term))
+               return EOF;
+       if (sb->buf[sb->len-1] == term)
+               strbuf_setlen(sb, sb->len-1);
 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
        int fd, len;
diff --git a/strbuf.h b/strbuf.h
index b888d40..cc1abb2 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -119,6 +119,7 @@ extern int strbuf_readlink(struct strbuf *sb, const char 
*path, size_t hint);
 extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
+extern int strbuf_getline_fd(struct strbuf *, int, int);
 extern void stripspace(struct strbuf *buf, int skip_comments);
 extern int launch_editor(const char *path, struct strbuf *buffer, const char 
*const *env);
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to