strbuf: provide function to append whole lines
While the strbuf interface already provides functions to read a line into it that completely replaces its current contents, we do not have an interface that allows for appending lines without discarding current contents. Add a new function `strbuf_appendwholeline` that reads a line including its terminating character into a strbuf non-destructively. This is a preparatory step for git-update-ref(1) reading standard input line-wise instead of as a block. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 30, 2020 at 15:46 UTC
bd021f39103fab4c5ae53a4d1a1756f8970d00db
2 files changed
+16
strbuf.c
+10
@@ -690,6 +690,16 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
690
}
691
#endif
692
693
+int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
694
+{
695
+ struct strbuf line = STRBUF_INIT;
696
+ if (strbuf_getwholeline(&line, fp, term))
697
+ return EOF;
698
+ strbuf_addbuf(sb, &line);
699
+ strbuf_release(&line);
700
+ return 0;
701
+}
702
+
703
static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
704
{
705
if (strbuf_getwholeline(sb, fp, term))
strbuf.h
+6
@@ -502,6 +502,12 @@ int strbuf_getline(struct strbuf *sb, FILE *file);
502
*/
503
int strbuf_getwholeline(struct strbuf *sb, FILE *file, int term);
504
505
+/**
506
+ * Like `strbuf_getwholeline`, but appends the line instead of
507
+ * resetting the buffer first.
508
+ */
509
+int strbuf_appendwholeline(struct strbuf *sb, FILE *file, int term);
510
+
511
/**
512
* Like `strbuf_getwholeline`, but operates on a file descriptor.
513
* It reads one character at a time, so it is very slow. Do not