summaryrefslogtreecommitdiffstats
path: root/src/strbuf.c
blob: 41c139bd40061e2fc7673471120078507c0948f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include "strbuf.h"

struct chunk {
    struct chunk *next;
    size_t length;
    char text[];
};

struct pa_strbuf {
    size_t length;
    struct chunk *head, *tail;
};

struct pa_strbuf *pa_strbuf_new(void) {
    struct pa_strbuf *sb = malloc(sizeof(struct pa_strbuf));
    assert(sb);
    sb->length = 0;
    sb->head = sb->tail = NULL;
    return sb;
}

void pa_strbuf_free(struct pa_strbuf *sb) {
    assert(sb);
    while (sb->head) {
        struct chunk *c = sb->head;
        sb->head = sb->head->next;
        free(c);
    }

    free(sb);
}

char *pa_strbuf_tostring(struct pa_strbuf *sb) {
    char *t, *e;
    struct chunk *c;
    assert(sb);

    t = malloc(sb->length+1);
    assert(t);

    e = t;
    for (c = sb->head; c; c = c->next) {
        memcpy(e, c->text, c->length);
        e += c->length;
    }

    *e = 0;
    
    return t;
}

char *pa_strbuf_tostring_free(struct pa_strbuf *sb) {
    char *t;
    assert(sb);
    t = pa_strbuf_tostring(sb);
    pa_strbuf_free(sb);
    return t;
}

void pa_strbuf_puts(struct pa_strbuf *sb, const char *t) {
    assert(sb && t);
    pa_strbuf_putsn(sb, t, strlen(t));
} 

void pa_strbuf_putsn(struct pa_strbuf *sb, const char *t, size_t l) {
    struct chunk *c;
    assert(sb && t);
    
    if (!l)
       return;
   
    c = malloc(sizeof(struct chunk)+l);
    assert(c);

    c->next = NULL;
    c->length = l;
    memcpy(c->text, t, l);

    if (sb->tail) {
        assert(sb->head);
        sb->tail->next = c;
    } else {
        assert(!sb->head);
        sb->head = c;
    }

    sb->tail = c;
    sb->length += l;
}

/* The following is based on an example from the GNU libc documentation */

int pa_strbuf_printf(struct pa_strbuf *sb, const char *format, ...) {
    int r, size = 100;
    struct chunk *c = NULL;

    assert(sb);
    
    for(;;) {
        va_list ap;

        c = realloc(c, sizeof(struct chunk)+size);
        assert(c);

        va_start(ap, format);
        r = vsnprintf(c->text, size, format, ap);
        va_end(ap);
        
        if (r > -1 && r < size) {
            c->length = r;
            c->next = NULL;
            
            if (sb->tail) {
                assert(sb->head);
                sb->tail->next = c;
            } else {
                assert(!sb->head);
                sb->head = c;
            }
            
            sb->tail = c;
            sb->length += r;
            
            return r;
        }

        if (r > -1)    /* glibc 2.1 */
            size = r+1; 
        else           /* glibc 2.0 */
            size *= 2;
    }
}