1 #include <u.h> 2 #include <libc.h> 3 #include "fmtdef.h" 4 5 static int runeFmtStrFlush(Fmt * f)6runeFmtStrFlush(Fmt *f) 7 { 8 Rune *s; 9 int n; 10 11 if(f->start == nil) 12 return 0; 13 n = (uintptr)f->farg; 14 n *= 2; 15 s = (Rune*)f->start; 16 f->start = realloc(s, sizeof(Rune)*n); 17 if(f->start == nil){ 18 f->farg = nil; 19 f->to = nil; 20 f->stop = nil; 21 free(s); 22 return 0; 23 } 24 f->farg = (void*)(uintptr)n; 25 f->to = (Rune*)f->start + ((Rune*)f->to - s); 26 f->stop = (Rune*)f->start + n - 1; 27 return 1; 28 } 29 30 int runefmtstrinit(Fmt * f)31runefmtstrinit(Fmt *f) 32 { 33 int n; 34 35 memset(f, 0, sizeof *f); 36 f->runes = 1; 37 n = 32; 38 f->start = malloc(sizeof(Rune)*n); 39 if(f->start == nil) 40 return -1; 41 f->to = f->start; 42 f->stop = (Rune*)f->start + n - 1; 43 f->flush = runeFmtStrFlush; 44 f->farg = (void*)(uintptr)n; 45 f->nfmt = 0; 46 return 0; 47 } 48 49 /* 50 * print into an allocated string buffer 51 */ 52 Rune* runevsmprint(char * fmt,va_list args)53runevsmprint(char *fmt, va_list args) 54 { 55 Fmt f; 56 int n; 57 58 if(runefmtstrinit(&f) < 0) 59 return nil; 60 VA_COPY(f.args,args); 61 n = dofmt(&f, fmt); 62 VA_END(f.args); 63 if(f.start == nil) 64 return nil; 65 if(n < 0){ 66 free(f.start); 67 return nil; 68 } 69 *(Rune*)f.to = '\0'; 70 return (Rune*)f.start; 71 } 72