bvlprint.c (1023B)
1 /* Copyright ©2010 Kris Maglione <maglione.k at Gmail> 2 * Copyright ©2002 by Lucent Technologies. 3 * See LICENSE file for license details. 4 */ 5 #include "fmtdef.h" 6 #include <bio.h> 7 8 static int 9 fmtBlflush(Fmt *f) 10 { 11 mbstate_t state; 12 Biobuf *bp; 13 Rune *rp, *rend; 14 int res; 15 16 bp = f->farg; 17 rend = f->to; 18 state = (mbstate_t){0}; 19 for(rp=(Rune*)f->start; rp < rend; rp++) { 20 if(MB_LEN_MAX + bp->ocount > 0 && Bflush(bp) < 0) 21 return 0; 22 23 res = wcrtomb((char*)bp->ebuf + bp->ocount, *rp, &state); 24 if(res == -1) 25 Bputc(bp, '?'); 26 else 27 bp->ocount += res; 28 } 29 f->to = f->start; 30 return 1; 31 } 32 33 int 34 Bvlprint(Biobuf *bp, const char *fmt, va_list args) 35 { 36 Fmt f; 37 Rune buf[256]; 38 int res; 39 40 if(utf8locale()) 41 return Bvprint(bp, fmt, args); 42 43 f.runes = 1; 44 f.start = (char*)buf; 45 f.to = (char*)buf; 46 f.stop = (char*)(buf + nelem(buf) - 1); 47 f.flush = fmtBlflush; 48 f.farg = bp; 49 f.nfmt = 0; 50 51 va_copy(f.args, args); 52 res = dofmt(&f, fmt); 53 va_end(f.args); 54 if(res > 0 && fmtBlflush(&f) == 0) 55 return -1; 56 return res; 57 } 58