summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index f19da55b07..2f1a7d3209 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -479,6 +479,23 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
}
}
+#define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;="
+
+void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags)
+{
+ size_t i, len = strlen(src);
+
+ for (i = 0; i < len; i++) {
+ unsigned char ch = src[i];
+ if (ch <= 0x1F || ch >= 0x7F ||
+ (ch == '/' && (flags & STRBUF_ENCODE_SLASH)) ||
+ strchr(URL_UNSAFE_CHARS, ch))
+ strbuf_addf(dst, "%%%02X", (unsigned char)ch);
+ else
+ strbuf_addch(dst, ch);
+ }
+}
+
size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
{
size_t res;
@@ -539,6 +556,10 @@ ssize_t strbuf_write(struct strbuf *sb, FILE *f)
return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
}
+ssize_t strbuf_write_fd(struct strbuf *sb, int fd)
+{
+ return sb->len ? write(fd, sb->buf, sb->len) : 0;
+}
#define STRBUF_MAXLINK (2*PATH_MAX)
@@ -675,6 +696,16 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
}
#endif
+int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
+{
+ struct strbuf line = STRBUF_INIT;
+ if (strbuf_getwholeline(&line, fp, term))
+ return EOF;
+ strbuf_addbuf(sb, &line);
+ strbuf_release(&line);
+ return 0;
+}
+
static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
{
if (strbuf_getwholeline(sb, fp, term))