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