0
|
1 #ifndef KSTRING_H
|
|
2 #define KSTRING_H
|
|
3
|
|
4 #include <stdlib.h>
|
|
5 #include <string.h>
|
|
6
|
|
7 #ifndef kroundup32
|
|
8 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
|
|
9 #endif
|
|
10
|
|
11 #ifndef KSTRING_T
|
|
12 #define KSTRING_T kstring_t
|
|
13 typedef struct __kstring_t {
|
|
14 size_t l, m;
|
|
15 char *s;
|
|
16 } kstring_t;
|
|
17 #endif
|
|
18
|
|
19 static inline int kputs(const char *p, kstring_t *s)
|
|
20 {
|
|
21 int l = strlen(p);
|
|
22 if (s->l + l + 1 >= s->m) {
|
|
23 s->m = s->l + l + 2;
|
|
24 kroundup32(s->m);
|
|
25 s->s = (char*)realloc(s->s, s->m);
|
|
26 }
|
|
27 strcpy(s->s + s->l, p);
|
|
28 s->l += l;
|
|
29 return l;
|
|
30 }
|
|
31
|
|
32 static inline int kputc(int c, kstring_t *s)
|
|
33 {
|
|
34 if (s->l + 1 >= s->m) {
|
|
35 s->m = s->l + 2;
|
|
36 kroundup32(s->m);
|
|
37 s->s = (char*)realloc(s->s, s->m);
|
|
38 }
|
|
39 s->s[s->l++] = c;
|
|
40 s->s[s->l] = 0;
|
|
41 return c;
|
|
42 }
|
|
43
|
|
44 int ksprintf(kstring_t *s, const char *fmt, ...);
|
|
45
|
|
46 #endif
|