runevsmprint.c (1494B)
1 /* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ 2 /* 3 * Plan 9 port version must include libc.h in order to 4 * get Plan 9 debugging malloc, which sometimes returns 5 * different pointers than the standard malloc. 6 */ 7 #ifdef PLAN9PORT 8 #include <u.h> 9 #include <libc.h> 10 #include "fmtdef.h" 11 #else 12 #include <stdlib.h> 13 #include <string.h> 14 #include "plan9.h" 15 #include "fmt.h" 16 #include "fmtdef.h" 17 #endif 18 19 static int 20 runeFmtStrFlush(Fmt *f) 21 { 22 Rune *s; 23 int n; 24 25 if(f->start == nil) 26 return 0; 27 n = (uintptr)f->farg; 28 n *= 2; 29 s = (Rune*)f->start; 30 f->start = realloc(s, sizeof(Rune)*n); 31 if(f->start == nil){ 32 f->farg = nil; 33 f->to = nil; 34 f->stop = nil; 35 free(s); 36 return 0; 37 } 38 f->farg = (void*)(uintptr)n; 39 f->to = (Rune*)f->start + ((Rune*)f->to - s); 40 f->stop = (Rune*)f->start + n - 1; 41 return 1; 42 } 43 44 int 45 runefmtstrinit(Fmt *f) 46 { 47 int n; 48 49 memset(f, 0, sizeof *f); 50 f->runes = 1; 51 n = 32; 52 f->start = malloc(sizeof(Rune)*n); 53 if(f->start == nil) 54 return -1; 55 f->to = f->start; 56 f->stop = (Rune*)f->start + n - 1; 57 f->flush = runeFmtStrFlush; 58 f->farg = (void*)(uintptr)n; 59 f->nfmt = 0; 60 fmtlocaleinit(f, nil, nil, nil); 61 return 0; 62 } 63 64 /* 65 * print into an allocated string buffer 66 */ 67 Rune* 68 runevsmprint(char *fmt, va_list args) 69 { 70 Fmt f; 71 int n; 72 73 if(runefmtstrinit(&f) < 0) 74 return nil; 75 VA_COPY(f.args,args); 76 n = dofmt(&f, fmt); 77 VA_END(f.args); 78 if(f.start == nil) 79 return nil; 80 if(n < 0){ 81 free(f.start); 82 return nil; 83 } 84 *(Rune*)f.to = '\0'; 85 return (Rune*)f.start; 86 }