0
|
1 #include <stdarg.h>
|
|
2 #include <stdio.h>
|
|
3 #include "kstring.h"
|
|
4
|
|
5 int ksprintf(kstring_t *s, const char *fmt, ...)
|
|
6 {
|
|
7 va_list ap;
|
|
8 int l;
|
|
9 va_start(ap, fmt);
|
|
10 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
|
|
11 va_end(ap);
|
|
12 if (l + 1 > s->m - s->l) {
|
|
13 s->m = s->l + l + 2;
|
|
14 kroundup32(s->m);
|
|
15 s->s = (char*)realloc(s->s, s->m);
|
|
16 va_start(ap, fmt);
|
|
17 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
|
|
18 }
|
|
19 va_end(ap);
|
|
20 s->l += l;
|
|
21 return l;
|
|
22 }
|
|
23
|
|
24 #ifdef KSTRING_MAIN
|
|
25 #include <stdio.h>
|
|
26 int main()
|
|
27 {
|
|
28 kstring_t *s;
|
|
29 s = (kstring_t*)calloc(1, sizeof(kstring_t));
|
|
30 ksprintf(s, "abcdefg: %d", 100);
|
|
31 printf("%s\n", s->s);
|
|
32 free(s);
|
|
33 return 0;
|
|
34 }
|
|
35 #endif
|