fmt.h (5705B)
1 #ifndef _FMT_H_ 2 #define _FMT_H_ 1 3 /* 4 * The authors of this software are Rob Pike and Ken Thompson. 5 * Copyright (c) 2002 by Lucent Technologies. 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose without fee is hereby granted, provided that this entire notice 8 * is included in all copies of any software which is or includes a copy 9 * or modification of this software and in all copies of the supporting 10 * documentation for such software. 11 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED 12 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY 13 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY 14 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. 15 */ 16 17 #include <stdarg.h> 18 #include <utf.h> 19 20 typedef struct Fmt Fmt; 21 struct Fmt{ 22 unsigned char runes; /* output buffer is runes or chars? */ 23 void *start; /* of buffer */ 24 void *to; /* current place in the buffer */ 25 void *stop; /* end of the buffer; overwritten if flush fails */ 26 int (*flush)(Fmt *); /* called when to == stop */ 27 void *farg; /* to make flush a closure */ 28 int nfmt; /* num chars formatted so far */ 29 va_list args; /* args passed to dofmt */ 30 Rune r; /* % format Rune */ 31 int width; 32 int prec; 33 unsigned long flags; 34 char *decimal; /* decimal point; cannot be "" */ 35 36 /* For %'d */ 37 char *thousands; /* separator for thousands */ 38 39 /* 40 * Each char is an integer indicating #digits before next separator. Values: 41 * \xFF: no more grouping (or \x7F; defined to be CHAR_MAX in POSIX) 42 * \x00: repeat previous indefinitely 43 * \x**: count that many 44 */ 45 char *grouping; /* descriptor of separator placement */ 46 }; 47 48 enum{ 49 FmtWidth = 1, 50 FmtLeft = FmtWidth << 1, 51 FmtPrec = FmtLeft << 1, 52 FmtSharp = FmtPrec << 1, 53 FmtSpace = FmtSharp << 1, 54 FmtSign = FmtSpace << 1, 55 FmtApost = FmtSign << 1, 56 FmtZero = FmtApost << 1, 57 FmtUnsigned = FmtZero << 1, 58 FmtShort = FmtUnsigned << 1, 59 FmtLong = FmtShort << 1, 60 FmtVLong = FmtLong << 1, 61 FmtComma = FmtVLong << 1, 62 FmtByte = FmtComma << 1, 63 FmtLDouble = FmtByte << 1, 64 65 FmtFlag = FmtLDouble << 1 66 }; 67 68 extern int (*fmtdoquote)(int); 69 70 #ifdef VARARGCK 71 /* *sigh* */ 72 typedef unsigned char _fmt_uchar; 73 typedef unsigned short _fmt_ushort; 74 typedef unsigned int _fmt_uint; 75 typedef unsigned long _fmt_ulong; 76 typedef unsigned long long _fmt_uvlong; 77 typedef long long _fmt_vlong; 78 # pragma varargck argpos fmtprint 2 79 # pragma varargck argpos fprint 2 80 # pragma varargck argpos print 1 81 # pragma varargck argpos runeseprint 3 82 # pragma varargck argpos runesmprint 1 83 # pragma varargck argpos runesnprint 3 84 # pragma varargck argpos runesprint 2 85 # pragma varargck argpos seprint 3 86 # pragma varargck argpos smprint 1 87 # pragma varargck argpos snprint 3 88 # pragma varargck argpos sprint 2 89 90 # pragma varargck type "lld" _fmt_vlong 91 # pragma varargck type "llx" _fmt_vlong 92 # pragma varargck type "lld" _fmt_uvlong 93 # pragma varargck type "llx" _fmt_uvlong 94 # pragma varargck type "ld" long 95 # pragma varargck type "lx" long 96 # pragma varargck type "lb" long 97 # pragma varargck type "ld" _fmt_ulong 98 # pragma varargck type "lx" _fmt_ulong 99 # pragma varargck type "lb" _fmt_ulong 100 # pragma varargck type "d" int 101 # pragma varargck type "x" int 102 # pragma varargck type "c" int 103 # pragma varargck type "C" int 104 # pragma varargck type "b" int 105 # pragma varargck type "d" _fmt_uint 106 # pragma varargck type "x" _fmt_uint 107 # pragma varargck type "c" _fmt_uint 108 # pragma varargck type "C" _fmt_uint 109 # pragma varargck type "b" _fmt_uint 110 # pragma varargck type "f" double 111 # pragma varargck type "e" double 112 # pragma varargck type "g" double 113 # pragma varargck type "s" char* 114 # pragma varargck type "q" char* 115 # pragma varargck type "S" Rune* 116 # pragma varargck type "Q" Rune* 117 # pragma varargck type "r" void 118 # pragma varargck type "%" void 119 # pragma varargck type "n" int* 120 # pragma varargck type "p" uintptr_t 121 # pragma varargck type "p" void* 122 # pragma varargck flag ',' 123 # pragma varargck flag 'h' 124 # pragma varargck type "<" void* 125 # pragma varargck type "[" void* 126 # pragma varargck type "H" void* 127 # pragma varargck type "lH" void* 128 #endif 129 130 /* Edit .+1,/^$/ | cfn $PLAN9/src/lib9/fmt/?*.c | grep -v static |grep -v __ */ 131 int dofmt(Fmt*, const char *fmt); 132 int dorfmt(Fmt*, const Rune *fmt); 133 double fmtcharstod(int(*f)(void*), void*); 134 int fmtfdflush(Fmt*); 135 int fmtfdinit(Fmt*, int fd, char *buf, int size); 136 int fmtinstall(int, int (*f)(Fmt*)); 137 void fmtlocaleinit(Fmt*, char *decimal, char *thousands, char *grouping); 138 int fmtprint(Fmt*, const char*, ...); 139 int fmtrune(Fmt*, int); 140 int fmtrunestrcpy(Fmt*, Rune*); 141 int fmtstrcpy(Fmt*, const char*); 142 char* fmtstrflush(Fmt*); 143 int fmtstrinit(Fmt*); 144 double fmtstrtod(const char*, char**); 145 int fmtvprint(Fmt*, const char*, va_list); 146 int fprint(int, const char*, ...); 147 int print(const char*, ...); 148 void quotefmtinstall(void); 149 int quoterunestrfmt(Fmt*); 150 int quotestrfmt(Fmt*); 151 Rune* runefmtstrflush(Fmt*); 152 int runefmtstrinit(Fmt*); 153 Rune* runeseprint(Rune*,Rune*, const char*, ...); 154 Rune* runesmprint(const char*, ...); 155 int runesnprint(Rune*, int, const char*, ...); 156 int runesprint(Rune*, const char*, ...); 157 Rune* runevseprint(Rune*, Rune *, const char*, va_list); 158 Rune* runevsmprint(const char*, va_list); 159 int runevsnprint(Rune*, int, const char*, va_list); 160 char* seprint(char*, char*, const char*, ...); 161 char* smprint(const char*, ...); 162 int snprint(char*, int, const char *, ...); 163 int sprint(char*, const char*, ...); 164 int vfprint(int, const char*, va_list); 165 char* vseprint(char*, char*, const char*, va_list); 166 char* vsmprint(const char*, va_list); 167 int vsnprint(char*, int, const char*, va_list); 168 169 #endif