xref: /plan9/sys/src/ape/lib/fmt/fmtdef.h (revision 781103c4074deb8af160e8a0da2742ba6b29dc2b)
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  * dofmt -- format to a buffer
16  * the number of characters formatted is returned,
17  * or -1 if there was an error.
18  * if the buffer is ever filled, flush is called.
19  * it should reset the buffer and return whether formatting should continue.
20  */
21 #define uchar _fmtuchar
22 #define ushort _fmtushort
23 #define uint _fmtuint
24 #define ulong _fmtulong
25 #define vlong _fmtvlong
26 #define uvlong _fmtuvlong
27 
28 // #define USED(x) if(x);else
29 
30 typedef unsigned char		uchar;
31 typedef unsigned short		ushort;
32 typedef unsigned int		uint;
33 typedef unsigned long		ulong;
34 
35 #ifndef NOVLONGS
36 typedef unsigned long long	uvlong;
37 typedef long long		vlong;
38 #endif
39 
40 /* #define nil		0	/* cannot be ((void*)0) because used for function pointers */
41 
42 typedef int (*Fmts)(Fmt*);
43 
44 typedef struct Quoteinfo Quoteinfo;
45 struct Quoteinfo
46 {
47 	int	quoted;		/* if set, string must be quoted */
48 	int	nrunesin;	/* number of input runes that can be accepted */
49 	int	nbytesin;	/* number of input bytes that can be accepted */
50 	int	nrunesout;	/* number of runes that will be generated */
51 	int	nbytesout;	/* number of bytes that will be generated */
52 };
53 
54 void	*__fmtflush(Fmt*, void*, int);
55 void	*__fmtdispatch(Fmt*, void*, int);
56 int	__floatfmt(Fmt*, double);
57 int	__fmtpad(Fmt*, int);
58 int	__rfmtpad(Fmt*, int);
59 int	__fmtFdFlush(Fmt*);
60 
61 int	__efgfmt(Fmt*);
62 int	__charfmt(Fmt*);
63 int	__runefmt(Fmt*);
64 int	__runesfmt(Fmt*);
65 int	__countfmt(Fmt*);
66 int	__flagfmt(Fmt*);
67 int	__percentfmt(Fmt*);
68 int	__ifmt(Fmt*);
69 int	__strfmt(Fmt*);
70 int	__badfmt(Fmt*);
71 int	__fmtcpy(Fmt*, const void*, int, int);
72 int	__fmtrcpy(Fmt*, const void*, int n);
73 int	__errfmt(Fmt *f);
74 
75 double	__fmtpow10(int);
76 
77 void	__fmtlock(void);
78 void	__fmtunlock(void);
79 
80 #define FMTCHAR(f, t, s, c)\
81 	do{\
82 	if(t + 1 > (char*)s){\
83 		t = __fmtflush(f, t, 1);\
84 		if(t != nil)\
85 			s = f->stop;\
86 		else\
87 			return -1;\
88 	}\
89 	*t++ = c;\
90 	}while(0)
91 
92 #define FMTRCHAR(f, t, s, c)\
93 	do{\
94 	if(t + 1 > (Rune*)s){\
95 		t = __fmtflush(f, t, sizeof(Rune));\
96 		if(t != nil)\
97 			s = f->stop;\
98 		else\
99 			return -1;\
100 	}\
101 	*t++ = c;\
102 	}while(0)
103 
104 #define FMTRUNE(f, t, s, r)\
105 	do{\
106 	Rune _rune;\
107 	int _runelen;\
108 	if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\
109 		t = __fmtflush(f, t, _runelen);\
110 		if(t != nil)\
111 			s = f->stop;\
112 		else\
113 			return -1;\
114 	}\
115 	if(r < Runeself)\
116 		*t++ = r;\
117 	else{\
118 		_rune = r;\
119 		t += runetochar(t, &_rune);\
120 	}\
121 	}while(0)
122