Now that cache.h needs strbuf.h, remove useless includes.
[git/gitweb.git] / strbuf.c
blobc5f9e2a28f1441e0ae79e3cac3a3640bf943ed52
1 #include "cache.h"
3 void strbuf_init(struct strbuf *sb, size_t hint)
5 memset(sb, 0, sizeof(*sb));
6 if (hint)
7 strbuf_grow(sb, hint);
10 void strbuf_release(struct strbuf *sb)
12 free(sb->buf);
13 memset(sb, 0, sizeof(*sb));
16 void strbuf_reset(struct strbuf *sb)
18 if (sb->len)
19 strbuf_setlen(sb, 0);
20 sb->eof = 0;
23 char *strbuf_detach(struct strbuf *sb)
25 char *res = sb->buf;
26 strbuf_init(sb, 0);
27 return res;
30 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
32 strbuf_release(sb);
33 sb->buf = buf;
34 sb->len = len;
35 sb->alloc = alloc;
36 strbuf_grow(sb, 0);
37 sb->buf[sb->len] = '\0';
40 void strbuf_grow(struct strbuf *sb, size_t extra)
42 if (sb->len + extra + 1 <= sb->len)
43 die("you want to use way too much memory");
44 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
47 void strbuf_rtrim(struct strbuf *sb)
49 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
50 sb->len--;
51 sb->buf[sb->len] = '\0';
54 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
56 strbuf_grow(sb, len);
57 if (pos > sb->len)
58 die("`pos' is too far after the end of the buffer");
59 memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
60 memcpy(sb->buf + pos, data, len);
61 strbuf_setlen(sb, sb->len + len);
64 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
65 const void *data, size_t dlen)
67 if (pos + len < pos)
68 die("you want to use way too much memory");
69 if (pos > sb->len)
70 die("`pos' is too far after the end of the buffer");
71 if (pos + len > sb->len)
72 die("`pos + len' is too far after the end of the buffer");
74 if (dlen >= len)
75 strbuf_grow(sb, dlen - len);
76 memmove(sb->buf + pos + dlen,
77 sb->buf + pos + len,
78 sb->len - pos - len);
79 memcpy(sb->buf + pos, data, dlen);
80 strbuf_setlen(sb, sb->len + dlen - len);
83 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
85 strbuf_grow(sb, len);
86 memcpy(sb->buf + sb->len, data, len);
87 strbuf_setlen(sb, sb->len + len);
90 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
92 int len;
93 va_list ap;
95 va_start(ap, fmt);
96 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
97 va_end(ap);
98 if (len < 0) {
99 len = 0;
101 if (len > strbuf_avail(sb)) {
102 strbuf_grow(sb, len);
103 va_start(ap, fmt);
104 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
105 va_end(ap);
106 if (len > strbuf_avail(sb)) {
107 die("this should not happen, your snprintf is broken");
110 strbuf_setlen(sb, sb->len + len);
113 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
115 size_t res;
117 strbuf_grow(sb, size);
118 res = fread(sb->buf + sb->len, 1, size, f);
119 if (res > 0) {
120 strbuf_setlen(sb, sb->len + res);
122 return res;
125 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
127 size_t oldlen = sb->len;
129 strbuf_grow(sb, hint ? hint : 8192);
130 for (;;) {
131 ssize_t cnt;
133 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
134 if (cnt < 0) {
135 strbuf_setlen(sb, oldlen);
136 return -1;
138 if (!cnt)
139 break;
140 sb->len += cnt;
141 strbuf_grow(sb, 8192);
144 sb->buf[sb->len] = '\0';
145 return sb->len - oldlen;
148 void read_line(struct strbuf *sb, FILE *fp, int term)
150 int ch;
151 if (feof(fp)) {
152 strbuf_release(sb);
153 sb->eof = 1;
154 return;
157 strbuf_reset(sb);
158 while ((ch = fgetc(fp)) != EOF) {
159 if (ch == term)
160 break;
161 strbuf_grow(sb, 1);
162 sb->buf[sb->len++] = ch;
164 if (ch == EOF && sb->len == 0) {
165 strbuf_release(sb);
166 sb->eof = 1;
169 strbuf_grow(sb, 1);
170 sb->buf[sb->len] = '\0';