strbuf.con commit move setting of object->type to alloc_* functions (d36f51c)
   1#include "cache.h"
   2#include "refs.h"
   3#include "utf8.h"
   4
   5int starts_with(const char *str, const char *prefix)
   6{
   7        for (; ; str++, prefix++)
   8                if (!*prefix)
   9                        return 1;
  10                else if (*str != *prefix)
  11                        return 0;
  12}
  13
  14int ends_with(const char *str, const char *suffix)
  15{
  16        int len = strlen(str), suflen = strlen(suffix);
  17        if (len < suflen)
  18                return 0;
  19        else
  20                return !strcmp(str + len - suflen, suffix);
  21}
  22
  23/*
  24 * Used as the default ->buf value, so that people can always assume
  25 * buf is non NULL and ->buf is NUL terminated even for a freshly
  26 * initialized strbuf.
  27 */
  28char strbuf_slopbuf[1];
  29
  30void strbuf_init(struct strbuf *sb, size_t hint)
  31{
  32        sb->alloc = sb->len = 0;
  33        sb->buf = strbuf_slopbuf;
  34        if (hint)
  35                strbuf_grow(sb, hint);
  36}
  37
  38void strbuf_release(struct strbuf *sb)
  39{
  40        if (sb->alloc) {
  41                free(sb->buf);
  42                strbuf_init(sb, 0);
  43        }
  44}
  45
  46char *strbuf_detach(struct strbuf *sb, size_t *sz)
  47{
  48        char *res;
  49        strbuf_grow(sb, 0);
  50        res = sb->buf;
  51        if (sz)
  52                *sz = sb->len;
  53        strbuf_init(sb, 0);
  54        return res;
  55}
  56
  57void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  58{
  59        strbuf_release(sb);
  60        sb->buf   = buf;
  61        sb->len   = len;
  62        sb->alloc = alloc;
  63        strbuf_grow(sb, 0);
  64        sb->buf[sb->len] = '\0';
  65}
  66
  67void strbuf_grow(struct strbuf *sb, size_t extra)
  68{
  69        int new_buf = !sb->alloc;
  70        if (unsigned_add_overflows(extra, 1) ||
  71            unsigned_add_overflows(sb->len, extra + 1))
  72                die("you want to use way too much memory");
  73        if (new_buf)
  74                sb->buf = NULL;
  75        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  76        if (new_buf)
  77                sb->buf[0] = '\0';
  78}
  79
  80void strbuf_trim(struct strbuf *sb)
  81{
  82        strbuf_rtrim(sb);
  83        strbuf_ltrim(sb);
  84}
  85void strbuf_rtrim(struct strbuf *sb)
  86{
  87        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  88                sb->len--;
  89        sb->buf[sb->len] = '\0';
  90}
  91
  92void strbuf_ltrim(struct strbuf *sb)
  93{
  94        char *b = sb->buf;
  95        while (sb->len > 0 && isspace(*b)) {
  96                b++;
  97                sb->len--;
  98        }
  99        memmove(sb->buf, b, sb->len);
 100        sb->buf[sb->len] = '\0';
 101}
 102
 103int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
 104{
 105        char *out;
 106        int len;
 107
 108        if (same_encoding(from, to))
 109                return 0;
 110
 111        out = reencode_string_len(sb->buf, sb->len, to, from, &len);
 112        if (!out)
 113                return -1;
 114
 115        strbuf_attach(sb, out, len, len);
 116        return 0;
 117}
 118
 119void strbuf_tolower(struct strbuf *sb)
 120{
 121        char *p = sb->buf, *end = sb->buf + sb->len;
 122        for (; p < end; p++)
 123                *p = tolower(*p);
 124}
 125
 126struct strbuf **strbuf_split_buf(const char *str, size_t slen,
 127                                 int terminator, int max)
 128{
 129        struct strbuf **ret = NULL;
 130        size_t nr = 0, alloc = 0;
 131        struct strbuf *t;
 132
 133        while (slen) {
 134                int len = slen;
 135                if (max <= 0 || nr + 1 < max) {
 136                        const char *end = memchr(str, terminator, slen);
 137                        if (end)
 138                                len = end - str + 1;
 139                }
 140                t = xmalloc(sizeof(struct strbuf));
 141                strbuf_init(t, len);
 142                strbuf_add(t, str, len);
 143                ALLOC_GROW(ret, nr + 2, alloc);
 144                ret[nr++] = t;
 145                str += len;
 146                slen -= len;
 147        }
 148        ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
 149        ret[nr] = NULL;
 150        return ret;
 151}
 152
 153void strbuf_list_free(struct strbuf **sbs)
 154{
 155        struct strbuf **s = sbs;
 156
 157        while (*s) {
 158                strbuf_release(*s);
 159                free(*s++);
 160        }
 161        free(sbs);
 162}
 163
 164int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 165{
 166        int len = a->len < b->len ? a->len: b->len;
 167        int cmp = memcmp(a->buf, b->buf, len);
 168        if (cmp)
 169                return cmp;
 170        return a->len < b->len ? -1: a->len != b->len;
 171}
 172
 173void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 174                                   const void *data, size_t dlen)
 175{
 176        if (unsigned_add_overflows(pos, len))
 177                die("you want to use way too much memory");
 178        if (pos > sb->len)
 179                die("`pos' is too far after the end of the buffer");
 180        if (pos + len > sb->len)
 181                die("`pos + len' is too far after the end of the buffer");
 182
 183        if (dlen >= len)
 184                strbuf_grow(sb, dlen - len);
 185        memmove(sb->buf + pos + dlen,
 186                        sb->buf + pos + len,
 187                        sb->len - pos - len);
 188        memcpy(sb->buf + pos, data, dlen);
 189        strbuf_setlen(sb, sb->len + dlen - len);
 190}
 191
 192void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
 193{
 194        strbuf_splice(sb, pos, 0, data, len);
 195}
 196
 197void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
 198{
 199        strbuf_splice(sb, pos, len, NULL, 0);
 200}
 201
 202void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 203{
 204        strbuf_grow(sb, len);
 205        memcpy(sb->buf + sb->len, data, len);
 206        strbuf_setlen(sb, sb->len + len);
 207}
 208
 209void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
 210{
 211        strbuf_grow(sb, len);
 212        memcpy(sb->buf + sb->len, sb->buf + pos, len);
 213        strbuf_setlen(sb, sb->len + len);
 214}
 215
 216void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 217{
 218        va_list ap;
 219        va_start(ap, fmt);
 220        strbuf_vaddf(sb, fmt, ap);
 221        va_end(ap);
 222}
 223
 224static void add_lines(struct strbuf *out,
 225                        const char *prefix1,
 226                        const char *prefix2,
 227                        const char *buf, size_t size)
 228{
 229        while (size) {
 230                const char *prefix;
 231                const char *next = memchr(buf, '\n', size);
 232                next = next ? (next + 1) : (buf + size);
 233
 234                prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
 235                strbuf_addstr(out, prefix);
 236                strbuf_add(out, buf, next - buf);
 237                size -= next - buf;
 238                buf = next;
 239        }
 240        strbuf_complete_line(out);
 241}
 242
 243void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
 244{
 245        static char prefix1[3];
 246        static char prefix2[2];
 247
 248        if (prefix1[0] != comment_line_char) {
 249                sprintf(prefix1, "%c ", comment_line_char);
 250                sprintf(prefix2, "%c", comment_line_char);
 251        }
 252        add_lines(out, prefix1, prefix2, buf, size);
 253}
 254
 255void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 256{
 257        va_list params;
 258        struct strbuf buf = STRBUF_INIT;
 259        int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
 260
 261        va_start(params, fmt);
 262        strbuf_vaddf(&buf, fmt, params);
 263        va_end(params);
 264
 265        strbuf_add_commented_lines(sb, buf.buf, buf.len);
 266        if (incomplete_line)
 267                sb->buf[--sb->len] = '\0';
 268
 269        strbuf_release(&buf);
 270}
 271
 272void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
 273{
 274        int len;
 275        va_list cp;
 276
 277        if (!strbuf_avail(sb))
 278                strbuf_grow(sb, 64);
 279        va_copy(cp, ap);
 280        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
 281        va_end(cp);
 282        if (len < 0)
 283                die("BUG: your vsnprintf is broken (returned %d)", len);
 284        if (len > strbuf_avail(sb)) {
 285                strbuf_grow(sb, len);
 286                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 287                if (len > strbuf_avail(sb))
 288                        die("BUG: your vsnprintf is broken (insatiable)");
 289        }
 290        strbuf_setlen(sb, sb->len + len);
 291}
 292
 293void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 294                   void *context)
 295{
 296        for (;;) {
 297                const char *percent;
 298                size_t consumed;
 299
 300                percent = strchrnul(format, '%');
 301                strbuf_add(sb, format, percent - format);
 302                if (!*percent)
 303                        break;
 304                format = percent + 1;
 305
 306                if (*format == '%') {
 307                        strbuf_addch(sb, '%');
 308                        format++;
 309                        continue;
 310                }
 311
 312                consumed = fn(sb, format, context);
 313                if (consumed)
 314                        format += consumed;
 315                else
 316                        strbuf_addch(sb, '%');
 317        }
 318}
 319
 320size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 321                void *context)
 322{
 323        struct strbuf_expand_dict_entry *e = context;
 324        size_t len;
 325
 326        for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
 327                if (!strncmp(placeholder, e->placeholder, len)) {
 328                        if (e->value)
 329                                strbuf_addstr(sb, e->value);
 330                        return len;
 331                }
 332        }
 333        return 0;
 334}
 335
 336void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 337{
 338        int i, len = src->len;
 339
 340        for (i = 0; i < len; i++) {
 341                if (src->buf[i] == '%')
 342                        strbuf_addch(dst, '%');
 343                strbuf_addch(dst, src->buf[i]);
 344        }
 345}
 346
 347size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 348{
 349        size_t res;
 350        size_t oldalloc = sb->alloc;
 351
 352        strbuf_grow(sb, size);
 353        res = fread(sb->buf + sb->len, 1, size, f);
 354        if (res > 0)
 355                strbuf_setlen(sb, sb->len + res);
 356        else if (oldalloc == 0)
 357                strbuf_release(sb);
 358        return res;
 359}
 360
 361ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 362{
 363        size_t oldlen = sb->len;
 364        size_t oldalloc = sb->alloc;
 365
 366        strbuf_grow(sb, hint ? hint : 8192);
 367        for (;;) {
 368                ssize_t cnt;
 369
 370                cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 371                if (cnt < 0) {
 372                        if (oldalloc == 0)
 373                                strbuf_release(sb);
 374                        else
 375                                strbuf_setlen(sb, oldlen);
 376                        return -1;
 377                }
 378                if (!cnt)
 379                        break;
 380                sb->len += cnt;
 381                strbuf_grow(sb, 8192);
 382        }
 383
 384        sb->buf[sb->len] = '\0';
 385        return sb->len - oldlen;
 386}
 387
 388#define STRBUF_MAXLINK (2*PATH_MAX)
 389
 390int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 391{
 392        size_t oldalloc = sb->alloc;
 393
 394        if (hint < 32)
 395                hint = 32;
 396
 397        while (hint < STRBUF_MAXLINK) {
 398                int len;
 399
 400                strbuf_grow(sb, hint);
 401                len = readlink(path, sb->buf, hint);
 402                if (len < 0) {
 403                        if (errno != ERANGE)
 404                                break;
 405                } else if (len < hint) {
 406                        strbuf_setlen(sb, len);
 407                        return 0;
 408                }
 409
 410                /* .. the buffer was too small - try again */
 411                hint *= 2;
 412        }
 413        if (oldalloc == 0)
 414                strbuf_release(sb);
 415        return -1;
 416}
 417
 418int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 419{
 420        int ch;
 421
 422        if (feof(fp))
 423                return EOF;
 424
 425        strbuf_reset(sb);
 426        while ((ch = fgetc(fp)) != EOF) {
 427                strbuf_grow(sb, 1);
 428                sb->buf[sb->len++] = ch;
 429                if (ch == term)
 430                        break;
 431        }
 432        if (ch == EOF && sb->len == 0)
 433                return EOF;
 434
 435        sb->buf[sb->len] = '\0';
 436        return 0;
 437}
 438
 439int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 440{
 441        if (strbuf_getwholeline(sb, fp, term))
 442                return EOF;
 443        if (sb->buf[sb->len-1] == term)
 444                strbuf_setlen(sb, sb->len-1);
 445        return 0;
 446}
 447
 448int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
 449{
 450        strbuf_reset(sb);
 451
 452        while (1) {
 453                char ch;
 454                ssize_t len = xread(fd, &ch, 1);
 455                if (len <= 0)
 456                        return EOF;
 457                strbuf_addch(sb, ch);
 458                if (ch == term)
 459                        break;
 460        }
 461        return 0;
 462}
 463
 464int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 465{
 466        int fd, len;
 467
 468        fd = open(path, O_RDONLY);
 469        if (fd < 0)
 470                return -1;
 471        len = strbuf_read(sb, fd, hint);
 472        close(fd);
 473        if (len < 0)
 474                return -1;
 475
 476        return len;
 477}
 478
 479void strbuf_add_lines(struct strbuf *out, const char *prefix,
 480                      const char *buf, size_t size)
 481{
 482        add_lines(out, prefix, NULL, buf, size);
 483}
 484
 485void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
 486{
 487        while (*s) {
 488                size_t len = strcspn(s, "\"<>&");
 489                strbuf_add(buf, s, len);
 490                s += len;
 491                switch (*s) {
 492                case '"':
 493                        strbuf_addstr(buf, "&quot;");
 494                        break;
 495                case '<':
 496                        strbuf_addstr(buf, "&lt;");
 497                        break;
 498                case '>':
 499                        strbuf_addstr(buf, "&gt;");
 500                        break;
 501                case '&':
 502                        strbuf_addstr(buf, "&amp;");
 503                        break;
 504                case 0:
 505                        return;
 506                }
 507                s++;
 508        }
 509}
 510
 511static int is_rfc3986_reserved(char ch)
 512{
 513        switch (ch) {
 514                case '!': case '*': case '\'': case '(': case ')': case ';':
 515                case ':': case '@': case '&': case '=': case '+': case '$':
 516                case ',': case '/': case '?': case '#': case '[': case ']':
 517                        return 1;
 518        }
 519        return 0;
 520}
 521
 522static int is_rfc3986_unreserved(char ch)
 523{
 524        return isalnum(ch) ||
 525                ch == '-' || ch == '_' || ch == '.' || ch == '~';
 526}
 527
 528static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
 529                                 int reserved)
 530{
 531        strbuf_grow(sb, len);
 532        while (len--) {
 533                char ch = *s++;
 534                if (is_rfc3986_unreserved(ch) ||
 535                    (!reserved && is_rfc3986_reserved(ch)))
 536                        strbuf_addch(sb, ch);
 537                else
 538                        strbuf_addf(sb, "%%%02x", ch);
 539        }
 540}
 541
 542void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
 543                             int reserved)
 544{
 545        strbuf_add_urlencode(sb, s, strlen(s), reserved);
 546}
 547
 548void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
 549{
 550        if (bytes > 1 << 30) {
 551                strbuf_addf(buf, "%u.%2.2u GiB",
 552                            (int)(bytes >> 30),
 553                            (int)(bytes & ((1 << 30) - 1)) / 10737419);
 554        } else if (bytes > 1 << 20) {
 555                int x = bytes + 5243;  /* for rounding */
 556                strbuf_addf(buf, "%u.%2.2u MiB",
 557                            x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
 558        } else if (bytes > 1 << 10) {
 559                int x = bytes + 5;  /* for rounding */
 560                strbuf_addf(buf, "%u.%2.2u KiB",
 561                            x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
 562        } else {
 563                strbuf_addf(buf, "%u bytes", (int)bytes);
 564        }
 565}
 566
 567int printf_ln(const char *fmt, ...)
 568{
 569        int ret;
 570        va_list ap;
 571        va_start(ap, fmt);
 572        ret = vprintf(fmt, ap);
 573        va_end(ap);
 574        if (ret < 0 || putchar('\n') == EOF)
 575                return -1;
 576        return ret + 1;
 577}
 578
 579int fprintf_ln(FILE *fp, const char *fmt, ...)
 580{
 581        int ret;
 582        va_list ap;
 583        va_start(ap, fmt);
 584        ret = vfprintf(fp, fmt, ap);
 585        va_end(ap);
 586        if (ret < 0 || putc('\n', fp) == EOF)
 587                return -1;
 588        return ret + 1;
 589}
 590
 591char *xstrdup_tolower(const char *string)
 592{
 593        char *result;
 594        size_t len, i;
 595
 596        len = strlen(string);
 597        result = xmalloc(len + 1);
 598        for (i = 0; i < len; i++)
 599                result[i] = tolower(string[i]);
 600        result[i] = '\0';
 601        return result;
 602}