1 /* 2 * dofmt -- format to a buffer 3 * the number of characters formatted is returned, 4 * or -1 if there was an error. 5 * if the buffer is ever filled, flush is called. 6 * it should reset the buffer and return whether formatting should continue. 7 */ 8 9 typedef int (*Fmts)(Fmt*); 10 11 typedef struct Quoteinfo Quoteinfo; 12 struct Quoteinfo 13 { 14 int quoted; /* if set, string must be quoted */ 15 int nrunesin; /* number of input runes that can be accepted */ 16 int nbytesin; /* number of input bytes that can be accepted */ 17 int nrunesout; /* number of runes that will be generated */ 18 int nbytesout; /* number of bytes that will be generated */ 19 }; 20 21 void *_fmtflush(Fmt*, void*, int); 22 void *_fmtdispatch(Fmt*, void*, int); 23 int _floatfmt(Fmt*, double); 24 int _fmtpad(Fmt*, int); 25 int _rfmtpad(Fmt*, int); 26 int _fmtFdFlush(Fmt*); 27 28 int _efgfmt(Fmt*); 29 int _charfmt(Fmt*); 30 int _countfmt(Fmt*); 31 int _flagfmt(Fmt*); 32 int _percentfmt(Fmt*); 33 int _ifmt(Fmt*); 34 int _runefmt(Fmt*); 35 int _runesfmt(Fmt*); 36 int _strfmt(Fmt*); 37 int _badfmt(Fmt*); 38 int _fmtcpy(Fmt*, void*, int, int); 39 int _fmtrcpy(Fmt*, void*, int n); 40 41 void _fmtlock(void); 42 void _fmtunlock(void); 43 44 #define FMTCHAR(f, t, s, c)\ 45 do{\ 46 if(t + 1 > (char*)s){\ 47 t = _fmtflush(f, t, 1);\ 48 if(t != nil)\ 49 s = f->stop;\ 50 else\ 51 return -1;\ 52 }\ 53 *t++ = c;\ 54 }while(0) 55 56 #define FMTRCHAR(f, t, s, c)\ 57 do{\ 58 if(t + 1 > (Rune*)s){\ 59 t = _fmtflush(f, t, sizeof(Rune));\ 60 if(t != nil)\ 61 s = f->stop;\ 62 else\ 63 return -1;\ 64 }\ 65 *t++ = c;\ 66 }while(0) 67 68 #define FMTRUNE(f, t, s, r)\ 69 do{\ 70 Rune _rune;\ 71 int _runelen;\ 72 if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\ 73 t = _fmtflush(f, t, _runelen);\ 74 if(t != nil)\ 75 s = f->stop;\ 76 else\ 77 return -1;\ 78 }\ 79 if(r < Runeself)\ 80 *t++ = r;\ 81 else{\ 82 _rune = r;\ 83 t += runetochar(t, &_rune);\ 84 }\ 85 }while(0) 86