fmtdef.h (3418B)
1 /* 2 * The authors of this software are Rob Pike and Ken Thompson. 3 * Copyright (c) 2002 by Lucent Technologies. 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose without fee is hereby granted, provided that this entire notice 6 * is included in all copies of any software which is or includes a copy 7 * or modification of this software and in all copies of the supporting 8 * documentation for such software. 9 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED 10 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY 11 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY 12 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. 13 */ 14 15 /* 16 * dofmt -- format to a buffer 17 * the number of characters formatted is returned, 18 * or -1 if there was an error. 19 * if the buffer is ever filled, flush is called. 20 * it should reset the buffer and return whether formatting should continue. 21 */ 22 23 typedef int (*Fmts)(Fmt*); 24 25 typedef struct Quoteinfo Quoteinfo; 26 struct Quoteinfo 27 { 28 int quoted; /* if set, string must be quoted */ 29 int nrunesin; /* number of input runes that can be accepted */ 30 int nbytesin; /* number of input bytes that can be accepted */ 31 int nrunesout; /* number of runes that will be generated */ 32 int nbytesout; /* number of bytes that will be generated */ 33 }; 34 35 /* Edit .+1,/^$/ |cfn |grep -v static | grep __ */ 36 double __Inf(int sign); 37 double __NaN(void); 38 int __badfmt(Fmt *f); 39 int __charfmt(Fmt *f); 40 int __countfmt(Fmt *f); 41 int __efgfmt(Fmt *fmt); 42 int __errfmt(Fmt *f); 43 int __flagfmt(Fmt *f); 44 int __fmtFdFlush(Fmt *f); 45 int __fmtcpy(Fmt *f, const void *vm, int n, int sz); 46 void* __fmtdispatch(Fmt *f, const void *fmt, int isrunes); 47 void * __fmtflush(Fmt *f, void *t, int len); 48 void __fmtlock(void); 49 int __fmtpad(Fmt *f, int n); 50 double __fmtpow10(int n); 51 int __fmtrcpy(Fmt *f, const void *vm, int n); 52 void __fmtunlock(void); 53 int __ifmt(Fmt *f); 54 int __isInf(double d, int sign); 55 int __isNaN(double d); 56 int __needsep(int *ndig, char **grouping); 57 int __needsquotes(char *s, int *quotelenp); 58 int __percentfmt(Fmt *f); 59 void __quotesetup(char *s, Rune *r, int nin, int nout, Quoteinfo *q, int sharp, int runesout); 60 int __quotestrfmt(int runesin, Fmt *f); 61 int __rfmtpad(Fmt *f, int n); 62 int __runefmt(Fmt *f); 63 int __runeneedsquotes(Rune *r, int *quotelenp); 64 int __runesfmt(Fmt *f); 65 int __strfmt(Fmt *f); 66 67 #define FMTCHAR(f, t, s, c)\ 68 do{\ 69 if(t + 1 > (char*)s){\ 70 t = __fmtflush(f, t, 1);\ 71 if(t != nil)\ 72 s = f->stop;\ 73 else\ 74 return -1;\ 75 }\ 76 *t++ = c;\ 77 }while(0) 78 79 #define FMTRCHAR(f, t, s, c)\ 80 do{\ 81 if(t + 1 > (Rune*)s){\ 82 t = __fmtflush(f, t, sizeof(Rune));\ 83 if(t != nil)\ 84 s = f->stop;\ 85 else\ 86 return -1;\ 87 }\ 88 *t++ = c;\ 89 }while(0) 90 91 #define FMTRUNE(f, t, s, r)\ 92 do{\ 93 Rune _rune;\ 94 int _runelen;\ 95 if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\ 96 t = __fmtflush(f, t, _runelen);\ 97 if(t != nil)\ 98 s = f->stop;\ 99 else\ 100 return -1;\ 101 }\ 102 if(r < Runeself)\ 103 *t++ = r;\ 104 else{\ 105 _rune = r;\ 106 t += runetochar(t, &_rune);\ 107 }\ 108 }while(0) 109 110 #ifndef va_copy 111 # define va_copy(a,b) (a) = (b) 112 #endif 113