]>
Commit | Line | Data |
---|---|---|
812666c8 | 1 | #include "cache.h" |
d1df5743 | 2 | |
b315c5c0 PH |
3 | /* |
4 | * Used as the default ->buf value, so that people can always assume | |
5 | * buf is non NULL and ->buf is NUL terminated even for a freshly | |
6 | * initialized strbuf. | |
7 | */ | |
8 | char strbuf_slopbuf[1]; | |
9 | ||
917c9a71 PH |
10 | void strbuf_init(struct strbuf *sb, size_t hint) |
11 | { | |
b315c5c0 PH |
12 | sb->alloc = sb->len = 0; |
13 | sb->buf = strbuf_slopbuf; | |
f1696ee3 PH |
14 | if (hint) |
15 | strbuf_grow(sb, hint); | |
d1df5743 JH |
16 | } |
17 | ||
917c9a71 PH |
18 | void strbuf_release(struct strbuf *sb) |
19 | { | |
b315c5c0 PH |
20 | if (sb->alloc) { |
21 | free(sb->buf); | |
22 | strbuf_init(sb, 0); | |
23 | } | |
b449f4cf PH |
24 | } |
25 | ||
b315c5c0 | 26 | char *strbuf_detach(struct strbuf *sb, size_t *sz) |
917c9a71 | 27 | { |
b315c5c0 PH |
28 | char *res = sb->alloc ? sb->buf : NULL; |
29 | if (sz) | |
30 | *sz = sb->len; | |
f1696ee3 | 31 | strbuf_init(sb, 0); |
b449f4cf PH |
32 | return res; |
33 | } | |
34 | ||
917c9a71 PH |
35 | void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc) |
36 | { | |
37 | strbuf_release(sb); | |
38 | sb->buf = buf; | |
39 | sb->len = len; | |
40 | sb->alloc = alloc; | |
41 | strbuf_grow(sb, 0); | |
42 | sb->buf[sb->len] = '\0'; | |
43 | } | |
44 | ||
45 | void strbuf_grow(struct strbuf *sb, size_t extra) | |
46 | { | |
b449f4cf PH |
47 | if (sb->len + extra + 1 <= sb->len) |
48 | die("you want to use way too much memory"); | |
b315c5c0 PH |
49 | if (!sb->alloc) |
50 | sb->buf = NULL; | |
b449f4cf PH |
51 | ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); |
52 | } | |
53 | ||
f1696ee3 PH |
54 | void strbuf_rtrim(struct strbuf *sb) |
55 | { | |
56 | while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) | |
57 | sb->len--; | |
58 | sb->buf[sb->len] = '\0'; | |
59 | } | |
60 | ||
45f66f64 PH |
61 | int strbuf_cmp(struct strbuf *a, struct strbuf *b) |
62 | { | |
63 | int cmp; | |
64 | if (a->len < b->len) { | |
65 | cmp = memcmp(a->buf, b->buf, a->len); | |
66 | return cmp ? cmp : -1; | |
67 | } else { | |
68 | cmp = memcmp(a->buf, b->buf, b->len); | |
69 | return cmp ? cmp : a->len != b->len; | |
70 | } | |
71 | } | |
72 | ||
917c9a71 PH |
73 | void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, |
74 | const void *data, size_t dlen) | |
75 | { | |
76 | if (pos + len < pos) | |
77 | die("you want to use way too much memory"); | |
78 | if (pos > sb->len) | |
79 | die("`pos' is too far after the end of the buffer"); | |
80 | if (pos + len > sb->len) | |
81 | die("`pos + len' is too far after the end of the buffer"); | |
82 | ||
83 | if (dlen >= len) | |
84 | strbuf_grow(sb, dlen - len); | |
85 | memmove(sb->buf + pos + dlen, | |
86 | sb->buf + pos + len, | |
87 | sb->len - pos - len); | |
88 | memcpy(sb->buf + pos, data, dlen); | |
89 | strbuf_setlen(sb, sb->len + dlen - len); | |
90 | } | |
91 | ||
c76689df PH |
92 | void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) |
93 | { | |
94 | strbuf_splice(sb, pos, 0, data, len); | |
95 | } | |
96 | ||
97 | void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) | |
98 | { | |
99 | strbuf_splice(sb, pos, len, NULL, 0); | |
100 | } | |
101 | ||
917c9a71 PH |
102 | void strbuf_add(struct strbuf *sb, const void *data, size_t len) |
103 | { | |
b449f4cf PH |
104 | strbuf_grow(sb, len); |
105 | memcpy(sb->buf + sb->len, data, len); | |
106 | strbuf_setlen(sb, sb->len + len); | |
107 | } | |
108 | ||
917c9a71 PH |
109 | void strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
110 | { | |
b449f4cf PH |
111 | int len; |
112 | va_list ap; | |
113 | ||
114 | va_start(ap, fmt); | |
115 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); | |
116 | va_end(ap); | |
117 | if (len < 0) { | |
118 | len = 0; | |
119 | } | |
f1696ee3 | 120 | if (len > strbuf_avail(sb)) { |
b449f4cf PH |
121 | strbuf_grow(sb, len); |
122 | va_start(ap, fmt); | |
123 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); | |
124 | va_end(ap); | |
f1696ee3 | 125 | if (len > strbuf_avail(sb)) { |
b449f4cf PH |
126 | die("this should not happen, your snprintf is broken"); |
127 | } | |
128 | } | |
129 | strbuf_setlen(sb, sb->len + len); | |
d1df5743 JH |
130 | } |
131 | ||
cde75e59 RS |
132 | void strbuf_expand(struct strbuf *sb, const char *format, |
133 | const char **placeholders, expand_fn_t fn, void *context) | |
134 | { | |
135 | for (;;) { | |
136 | const char *percent, **p; | |
137 | ||
138 | percent = strchrnul(format, '%'); | |
139 | strbuf_add(sb, format, percent - format); | |
140 | if (!*percent) | |
141 | break; | |
142 | format = percent + 1; | |
143 | ||
144 | for (p = placeholders; *p; p++) { | |
145 | if (!prefixcmp(format, *p)) | |
146 | break; | |
147 | } | |
148 | if (*p) { | |
149 | fn(sb, *p, context); | |
150 | format += strlen(*p); | |
151 | } else | |
152 | strbuf_addch(sb, '%'); | |
153 | } | |
154 | } | |
155 | ||
917c9a71 PH |
156 | size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) |
157 | { | |
b449f4cf PH |
158 | size_t res; |
159 | ||
160 | strbuf_grow(sb, size); | |
161 | res = fread(sb->buf + sb->len, 1, size, f); | |
162 | if (res > 0) { | |
163 | strbuf_setlen(sb, sb->len + res); | |
d1df5743 | 164 | } |
b449f4cf | 165 | return res; |
d1df5743 JH |
166 | } |
167 | ||
f1696ee3 | 168 | ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) |
b449f4cf PH |
169 | { |
170 | size_t oldlen = sb->len; | |
171 | ||
f1696ee3 | 172 | strbuf_grow(sb, hint ? hint : 8192); |
b449f4cf PH |
173 | for (;;) { |
174 | ssize_t cnt; | |
175 | ||
b449f4cf PH |
176 | cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); |
177 | if (cnt < 0) { | |
178 | strbuf_setlen(sb, oldlen); | |
179 | return -1; | |
180 | } | |
181 | if (!cnt) | |
182 | break; | |
183 | sb->len += cnt; | |
f1696ee3 | 184 | strbuf_grow(sb, 8192); |
b449f4cf PH |
185 | } |
186 | ||
187 | sb->buf[sb->len] = '\0'; | |
188 | return sb->len - oldlen; | |
d1df5743 JH |
189 | } |
190 | ||
e6c019d0 | 191 | int strbuf_getline(struct strbuf *sb, FILE *fp, int term) |
917c9a71 | 192 | { |
d1df5743 | 193 | int ch; |
e6c019d0 PH |
194 | |
195 | strbuf_grow(sb, 0); | |
196 | if (feof(fp)) | |
197 | return EOF; | |
b449f4cf PH |
198 | |
199 | strbuf_reset(sb); | |
d1df5743 JH |
200 | while ((ch = fgetc(fp)) != EOF) { |
201 | if (ch == term) | |
202 | break; | |
b449f4cf PH |
203 | strbuf_grow(sb, 1); |
204 | sb->buf[sb->len++] = ch; | |
d1df5743 | 205 | } |
e6c019d0 PH |
206 | if (ch == EOF && sb->len == 0) |
207 | return EOF; | |
b449f4cf | 208 | |
b449f4cf | 209 | sb->buf[sb->len] = '\0'; |
e6c019d0 | 210 | return 0; |
d1df5743 | 211 | } |
a9390b9f | 212 | |
387e7e19 | 213 | int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) |
a9390b9f KH |
214 | { |
215 | int fd, len; | |
216 | ||
217 | fd = open(path, O_RDONLY); | |
218 | if (fd < 0) | |
219 | return -1; | |
387e7e19 | 220 | len = strbuf_read(sb, fd, hint); |
a9390b9f KH |
221 | close(fd); |
222 | if (len < 0) | |
223 | return -1; | |
224 | ||
225 | return len; | |
226 | } |