ceph: use i_size_{read,write} to get/set i_size
[linux/fpc-iii.git] / tools / perf / util / strbuf.c
blob25671fa166188413c66758a978082e89a1ba08d2
1 #include "cache.h"
2 #include <linux/kernel.h>
4 int prefixcmp(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 0;
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
14 * Used as the default ->buf value, so that people can always assume
15 * buf is non NULL and ->buf is NUL terminated even for a freshly
16 * initialized strbuf.
18 char strbuf_slopbuf[1];
20 void strbuf_init(struct strbuf *sb, ssize_t hint)
22 sb->alloc = sb->len = 0;
23 sb->buf = strbuf_slopbuf;
24 if (hint)
25 strbuf_grow(sb, hint);
28 void strbuf_release(struct strbuf *sb)
30 if (sb->alloc) {
31 zfree(&sb->buf);
32 strbuf_init(sb, 0);
36 char *strbuf_detach(struct strbuf *sb, size_t *sz)
38 char *res = sb->alloc ? sb->buf : NULL;
39 if (sz)
40 *sz = sb->len;
41 strbuf_init(sb, 0);
42 return res;
45 void strbuf_grow(struct strbuf *sb, size_t extra)
47 if (sb->len + extra + 1 <= sb->len)
48 die("you want to use way too much memory");
49 if (!sb->alloc)
50 sb->buf = NULL;
51 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
54 static void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
55 const void *data, size_t dlen)
57 if (pos + len < pos)
58 die("you want to use way too much memory");
59 if (pos > sb->len)
60 die("`pos' is too far after the end of the buffer");
61 if (pos + len > sb->len)
62 die("`pos + len' is too far after the end of the buffer");
64 if (dlen >= len)
65 strbuf_grow(sb, dlen - len);
66 memmove(sb->buf + pos + dlen,
67 sb->buf + pos + len,
68 sb->len - pos - len);
69 memcpy(sb->buf + pos, data, dlen);
70 strbuf_setlen(sb, sb->len + dlen - len);
73 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
75 strbuf_splice(sb, pos, len, NULL, 0);
78 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
80 strbuf_grow(sb, len);
81 memcpy(sb->buf + sb->len, data, len);
82 strbuf_setlen(sb, sb->len + len);
85 void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
87 int len;
88 va_list ap_saved;
90 if (!strbuf_avail(sb))
91 strbuf_grow(sb, 64);
93 va_copy(ap_saved, ap);
94 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
95 if (len < 0)
96 die("your vsnprintf is broken");
97 if (len > strbuf_avail(sb)) {
98 strbuf_grow(sb, len);
99 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
100 va_end(ap_saved);
101 if (len > strbuf_avail(sb)) {
102 die("this should not happen, your vsnprintf is broken");
105 strbuf_setlen(sb, sb->len + len);
108 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
110 va_list ap;
112 va_start(ap, fmt);
113 strbuf_addv(sb, fmt, ap);
114 va_end(ap);
117 ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
119 size_t oldlen = sb->len;
120 size_t oldalloc = sb->alloc;
122 strbuf_grow(sb, hint ? hint : 8192);
123 for (;;) {
124 ssize_t cnt;
126 cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
127 if (cnt < 0) {
128 if (oldalloc == 0)
129 strbuf_release(sb);
130 else
131 strbuf_setlen(sb, oldlen);
132 return -1;
134 if (!cnt)
135 break;
136 sb->len += cnt;
137 strbuf_grow(sb, 8192);
140 sb->buf[sb->len] = '\0';
141 return sb->len - oldlen;