strbuf.con commit Documentation: Allow custom diff tools to be specified in 'diff.tool' (48672b3)
   1#include "cache.h"
   2#include "refs.h"
   3
   4int prefixcmp(const char *str, const char *prefix)
   5{
   6        for (; ; str++, prefix++)
   7                if (!*prefix)
   8                        return 0;
   9                else if (*str != *prefix)
  10                        return (unsigned char)*prefix - (unsigned char)*str;
  11}
  12
  13int suffixcmp(const char *str, const char *suffix)
  14{
  15        int len = strlen(str), suflen = strlen(suffix);
  16        if (len < suflen)
  17                return -1;
  18        else
  19                return strcmp(str + len - suflen, suffix);
  20}
  21
  22/*
  23 * Used as the default ->buf value, so that people can always assume
  24 * buf is non NULL and ->buf is NUL terminated even for a freshly
  25 * initialized strbuf.
  26 */
  27char strbuf_slopbuf[1];
  28
  29void strbuf_init(struct strbuf *sb, size_t hint)
  30{
  31        sb->alloc = sb->len = 0;
  32        sb->buf = strbuf_slopbuf;
  33        if (hint)
  34                strbuf_grow(sb, hint);
  35}
  36
  37void strbuf_release(struct strbuf *sb)
  38{
  39        if (sb->alloc) {
  40                free(sb->buf);
  41                strbuf_init(sb, 0);
  42        }
  43}
  44
  45char *strbuf_detach(struct strbuf *sb, size_t *sz)
  46{
  47        char *res = sb->alloc ? sb->buf : NULL;
  48        if (sz)
  49                *sz = sb->len;
  50        strbuf_init(sb, 0);
  51        return res;
  52}
  53
  54void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  55{
  56        strbuf_release(sb);
  57        sb->buf   = buf;
  58        sb->len   = len;
  59        sb->alloc = alloc;
  60        strbuf_grow(sb, 0);
  61        sb->buf[sb->len] = '\0';
  62}
  63
  64void strbuf_grow(struct strbuf *sb, size_t extra)
  65{
  66        if (unsigned_add_overflows(extra, 1) ||
  67            unsigned_add_overflows(sb->len, extra + 1))
  68                die("you want to use way too much memory");
  69        if (!sb->alloc)
  70                sb->buf = NULL;
  71        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  72}
  73
  74void strbuf_trim(struct strbuf *sb)
  75{
  76        char *b = sb->buf;
  77        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  78                sb->len--;
  79        while (sb->len > 0 && isspace(*b)) {
  80                b++;
  81                sb->len--;
  82        }
  83        memmove(sb->buf, b, sb->len);
  84        sb->buf[sb->len] = '\0';
  85}
  86void strbuf_rtrim(struct strbuf *sb)
  87{
  88        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  89                sb->len--;
  90        sb->buf[sb->len] = '\0';
  91}
  92
  93void strbuf_ltrim(struct strbuf *sb)
  94{
  95        char *b = sb->buf;
  96        while (sb->len > 0 && isspace(*b)) {
  97                b++;
  98                sb->len--;
  99        }
 100        memmove(sb->buf, b, sb->len);
 101        sb->buf[sb->len] = '\0';
 102}
 103
 104struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
 105{
 106        int alloc = 2, pos = 0;
 107        char *n, *p;
 108        struct strbuf **ret;
 109        struct strbuf *t;
 110
 111        ret = xcalloc(alloc, sizeof(struct strbuf *));
 112        p = n = sb->buf;
 113        while (n < sb->buf + sb->len) {
 114                int len;
 115                n = memchr(n, delim, sb->len - (n - sb->buf));
 116                if (pos + 1 >= alloc) {
 117                        alloc = alloc * 2;
 118                        ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
 119                }
 120                if (!n)
 121                        n = sb->buf + sb->len - 1;
 122                len = n - p + 1;
 123                t = xmalloc(sizeof(struct strbuf));
 124                strbuf_init(t, len);
 125                strbuf_add(t, p, len);
 126                ret[pos] = t;
 127                ret[++pos] = NULL;
 128                p = ++n;
 129        }
 130        return ret;
 131}
 132
 133void strbuf_list_free(struct strbuf **sbs)
 134{
 135        struct strbuf **s = sbs;
 136
 137        while (*s) {
 138                strbuf_release(*s);
 139                free(*s++);
 140        }
 141        free(sbs);
 142}
 143
 144int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 145{
 146        int len = a->len < b->len ? a->len: b->len;
 147        int cmp = memcmp(a->buf, b->buf, len);
 148        if (cmp)
 149                return cmp;
 150        return a->len < b->len ? -1: a->len != b->len;
 151}
 152
 153void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 154                                   const void *data, size_t dlen)
 155{
 156        if (unsigned_add_overflows(pos, len))
 157                die("you want to use way too much memory");
 158        if (pos > sb->len)
 159                die("`pos' is too far after the end of the buffer");
 160        if (pos + len > sb->len)
 161                die("`pos + len' is too far after the end of the buffer");
 162
 163        if (dlen >= len)
 164                strbuf_grow(sb, dlen - len);
 165        memmove(sb->buf + pos + dlen,
 166                        sb->buf + pos + len,
 167                        sb->len - pos - len);
 168        memcpy(sb->buf + pos, data, dlen);
 169        strbuf_setlen(sb, sb->len + dlen - len);
 170}
 171
 172void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
 173{
 174        strbuf_splice(sb, pos, 0, data, len);
 175}
 176
 177void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
 178{
 179        strbuf_splice(sb, pos, len, NULL, 0);
 180}
 181
 182void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 183{
 184        strbuf_grow(sb, len);
 185        memcpy(sb->buf + sb->len, data, len);
 186        strbuf_setlen(sb, sb->len + len);
 187}
 188
 189void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
 190{
 191        strbuf_grow(sb, len);
 192        memcpy(sb->buf + sb->len, sb->buf + pos, len);
 193        strbuf_setlen(sb, sb->len + len);
 194}
 195
 196void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 197{
 198        int len;
 199        va_list ap;
 200
 201        if (!strbuf_avail(sb))
 202                strbuf_grow(sb, 64);
 203        va_start(ap, fmt);
 204        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 205        va_end(ap);
 206        if (len < 0)
 207                die("your vsnprintf is broken");
 208        if (len > strbuf_avail(sb)) {
 209                strbuf_grow(sb, len);
 210                va_start(ap, fmt);
 211                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 212                va_end(ap);
 213                if (len > strbuf_avail(sb)) {
 214                        die("this should not happen, your snprintf is broken");
 215                }
 216        }
 217        strbuf_setlen(sb, sb->len + len);
 218}
 219
 220void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 221                   void *context)
 222{
 223        for (;;) {
 224                const char *percent;
 225                size_t consumed;
 226
 227                percent = strchrnul(format, '%');
 228                strbuf_add(sb, format, percent - format);
 229                if (!*percent)
 230                        break;
 231                format = percent + 1;
 232
 233                if (*format == '%') {
 234                        strbuf_addch(sb, '%');
 235                        format++;
 236                        continue;
 237                }
 238
 239                consumed = fn(sb, format, context);
 240                if (consumed)
 241                        format += consumed;
 242                else
 243                        strbuf_addch(sb, '%');
 244        }
 245}
 246
 247size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 248                void *context)
 249{
 250        struct strbuf_expand_dict_entry *e = context;
 251        size_t len;
 252
 253        for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
 254                if (!strncmp(placeholder, e->placeholder, len)) {
 255                        if (e->value)
 256                                strbuf_addstr(sb, e->value);
 257                        return len;
 258                }
 259        }
 260        return 0;
 261}
 262
 263void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 264{
 265        int i, len = src->len;
 266
 267        for (i = 0; i < len; i++) {
 268                if (src->buf[i] == '%')
 269                        strbuf_addch(dst, '%');
 270                strbuf_addch(dst, src->buf[i]);
 271        }
 272}
 273
 274size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 275{
 276        size_t res;
 277        size_t oldalloc = sb->alloc;
 278
 279        strbuf_grow(sb, size);
 280        res = fread(sb->buf + sb->len, 1, size, f);
 281        if (res > 0)
 282                strbuf_setlen(sb, sb->len + res);
 283        else if (oldalloc == 0)
 284                strbuf_release(sb);
 285        return res;
 286}
 287
 288ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 289{
 290        size_t oldlen = sb->len;
 291        size_t oldalloc = sb->alloc;
 292
 293        strbuf_grow(sb, hint ? hint : 8192);
 294        for (;;) {
 295                ssize_t cnt;
 296
 297                cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 298                if (cnt < 0) {
 299                        if (oldalloc == 0)
 300                                strbuf_release(sb);
 301                        else
 302                                strbuf_setlen(sb, oldlen);
 303                        return -1;
 304                }
 305                if (!cnt)
 306                        break;
 307                sb->len += cnt;
 308                strbuf_grow(sb, 8192);
 309        }
 310
 311        sb->buf[sb->len] = '\0';
 312        return sb->len - oldlen;
 313}
 314
 315#define STRBUF_MAXLINK (2*PATH_MAX)
 316
 317int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 318{
 319        size_t oldalloc = sb->alloc;
 320
 321        if (hint < 32)
 322                hint = 32;
 323
 324        while (hint < STRBUF_MAXLINK) {
 325                int len;
 326
 327                strbuf_grow(sb, hint);
 328                len = readlink(path, sb->buf, hint);
 329                if (len < 0) {
 330                        if (errno != ERANGE)
 331                                break;
 332                } else if (len < hint) {
 333                        strbuf_setlen(sb, len);
 334                        return 0;
 335                }
 336
 337                /* .. the buffer was too small - try again */
 338                hint *= 2;
 339        }
 340        if (oldalloc == 0)
 341                strbuf_release(sb);
 342        return -1;
 343}
 344
 345int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 346{
 347        int ch;
 348
 349        strbuf_grow(sb, 0);
 350        if (feof(fp))
 351                return EOF;
 352
 353        strbuf_reset(sb);
 354        while ((ch = fgetc(fp)) != EOF) {
 355                strbuf_grow(sb, 1);
 356                sb->buf[sb->len++] = ch;
 357                if (ch == term)
 358                        break;
 359        }
 360        if (ch == EOF && sb->len == 0)
 361                return EOF;
 362
 363        sb->buf[sb->len] = '\0';
 364        return 0;
 365}
 366
 367int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 368{
 369        if (strbuf_getwholeline(sb, fp, term))
 370                return EOF;
 371        if (sb->buf[sb->len-1] == term)
 372                strbuf_setlen(sb, sb->len-1);
 373        return 0;
 374}
 375
 376int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 377{
 378        int fd, len;
 379
 380        fd = open(path, O_RDONLY);
 381        if (fd < 0)
 382                return -1;
 383        len = strbuf_read(sb, fd, hint);
 384        close(fd);
 385        if (len < 0)
 386                return -1;
 387
 388        return len;
 389}