strbuf: give strbuf_getline() to the "most text friendly" variant
Now there is no direct caller to strbuf_getline(), we can demote it to file-scope static that is private to strbuf.c and rename it to strbuf_getdelim(). Rename strbuf_getline_crlf(), which is designed to be the most "text friendly" variant, and allow it to take over this simplest name, strbuf_getline(), so we can add more uses of it without having to type _crlf over and over again in the coming steps. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 13, 2016 at 18:32 UTC
1a0c8dfd89475d6bb09ddee8c019cf0ae5b3bdc2
4 files changed
+20
-21
builtin/am.c
+4
-4
@@ -613,7 +613,7 @@ static int is_mail(FILE *fp)
613
if (regcomp(®ex, header_regex, REG_NOSUB | REG_EXTENDED))
614
die("invalid pattern: %s", header_regex);
615
616
- while (!strbuf_getline_crlf(&sb, fp)) {
616
+ while (!strbuf_getline(&sb, fp)) {
617
if (!sb.len)
618
break; /* End of header */
619
@@ -660,7 +660,7 @@ static int detect_patch_format(const char **paths)
660
661
fp = xfopen(*paths, "r");
662
663
- while (!strbuf_getline_crlf(&l1, fp)) {
663
+ while (!strbuf_getline(&l1, fp)) {
664
if (l1.len)
665
break;
666
}
@@ -681,9 +681,9 @@ static int detect_patch_format(const char **paths)
681
}
682
683
strbuf_reset(&l2);
684
- strbuf_getline_crlf(&l2, fp);
684
+ strbuf_getline(&l2, fp);
685
strbuf_reset(&l3);
686
- strbuf_getline_crlf(&l3, fp);
686
+ strbuf_getline(&l3, fp);
687
688
/*
689
* If the second line is empty and the third is a From, Author or Date
strbuf.c
+4
-4
@@ -501,7 +501,7 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
501
}
502
#endif
503
504
-int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
504
+static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
505
{
506
if (strbuf_getwholeline(sb, fp, term))
507
return EOF;
@@ -510,7 +510,7 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
510
return 0;
511
}
512
513
-int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
513
+int strbuf_getline(struct strbuf *sb, FILE *fp)
514
{
515
if (strbuf_getwholeline(sb, fp, '\n'))
516
return EOF;
@@ -524,12 +524,12 @@ int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
524
525
int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
526
{
527
- return strbuf_getline(sb, fp, '\n');
527
+ return strbuf_getdelim(sb, fp, '\n');
528
}
529
530
int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
531
{
532
- return strbuf_getline(sb, fp, '\0');
532
+ return strbuf_getdelim(sb, fp, '\0');
533
}
534
535
int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
strbuf.h
+10
-12
@@ -354,8 +354,8 @@ extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm
354
*
355
* NOTE: The buffer is rewound if the read fails. If -1 is returned,
356
* `errno` must be consulted, like you would do for `read(3)`.
357
- * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
358
- * same behaviour as well.
357
+ * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
358
+ * family of functions have the same behaviour as well.
359
*/
360
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
361
@@ -379,19 +379,14 @@ extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint
379
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
380
381
/**
382
- * Read a line from a FILE *, overwriting the existing contents
383
- * of the strbuf. The second argument specifies the line
384
- * terminator character, typically `'\n'`.
382
+ * Read a line from a FILE *, overwriting the existing contents of
383
+ * the strbuf. The strbuf_getline*() family of functions share
384
+ * this signature, but have different line termination conventions.
385
+ *
386
* Reading stops after the terminator or at EOF. The terminator
387
* is removed from the buffer before returning. Returns 0 unless
388
* there was nothing left before EOF, in which case it returns `EOF`.
389
*/
389
-extern int strbuf_getline(struct strbuf *, FILE *, int);
390
-
391
-/**
392
- * The strbuf_getline*() family of functions share this signature, but
393
- * have different line termination conventions.
394
- */
390
typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
391
392
/* Uses LF as the line terminator */
@@ -403,8 +398,11 @@ extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
398
/*
399
* Similar to strbuf_getline_lf(), but additionally treats a CR that
400
* comes immediately before the LF as part of the terminator.
401
+ * This is the most friendly version to be used to read "text" files
402
+ * that can come from platforms whose native text format is CRLF
403
+ * terminated.
404
*/
407
-extern int strbuf_getline_crlf(struct strbuf *, FILE *);
405
+extern int strbuf_getline(struct strbuf *, FILE *);
406
407
408
/**
transport-helper.c
+2
-1
@@ -137,7 +137,8 @@ static struct child_process *get_helper(struct transport *transport)
137
data->no_disconnect_req = 0;
138
139
/*
140
- * Open the output as FILE* so strbuf_getline() can be used.
140
+ * Open the output as FILE* so strbuf_getline_*() family of
141
+ * functions can be used.
142
* Do this with duped fd because fclose() will close the fd,
143
* and stuff like taking over will require the fd to remain.
144
*/