1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 15 unchanged lines hidden (view full) --- 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * |
32 * $FreeBSD: head/lib/libc/stdio/printfcommon.h 187354 2009-01-17 05:38:14Z das $ |
33 */ 34 35/* 36 * This file defines common routines used by both printf and wprintf. 37 * You must define CHAR to either char or wchar_t prior to including this. 38 */ 39 40 --- 18 unchanged lines hidden (view full) --- 59static CHAR *__ultoa(u_long, CHAR *, int, int, const char *, int, char, 60 const char *); 61 62#define NIOV 8 63struct io_state { 64 FILE *fp; 65 struct __suio uio; /* output information: summary */ 66 struct __siov iov[NIOV];/* ... and individual io vectors */ |
67}; 68 69static inline void 70io_init(struct io_state *iop, FILE *fp) 71{ 72 |
73 iop->uio.uio_iov = iop->iov; |
74 iop->uio.uio_resid = 0; 75 iop->uio.uio_iovcnt = 0; 76 iop->fp = fp; 77} 78 79/* 80 * WARNING: The buffer passed to io_print() is not copied immediately; it must 81 * remain valid until io_flush() is called. 82 */ 83static inline int 84io_print(struct io_state *iop, const CHAR * __restrict ptr, int len) 85{ 86 |
87 iop->iov[iop->uio.uio_iovcnt].iov_base = (char *)ptr; 88 iop->iov[iop->uio.uio_iovcnt].iov_len = len; |
89 iop->uio.uio_resid += len; |
90 if (++iop->uio.uio_iovcnt >= NIOV) |
91 return (__sprint(iop->fp, &iop->uio)); |
92 else 93 return (0); |
94} 95 96/* 97 * Choose PADSIZE to trade efficiency vs. size. If larger printf 98 * fields occur frequently, increase PADSIZE and make the initialisers 99 * below longer. 100 */ 101#define PADSIZE 16 /* pad chunk size */ --- 4 unchanged lines hidden (view full) --- 106 107/* 108 * Pad with blanks or zeroes. 'with' should point to either the blanks array 109 * or the zeroes array. 110 */ 111static inline int 112io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with) 113{ |
114 int n; |
115 |
116 while (howmany > 0) { 117 n = (howmany >= PADSIZE) ? PADSIZE : howmany; 118 if (io_print(iop, with, n)) |
119 return (-1); |
120 howmany -= n; |
121 } |
122 return (0); 123} 124 125/* 126 * Print exactly len characters of the string spanning p to ep, truncating 127 * or padding with 'with' as necessary. 128 */ 129static inline int 130io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep, 131 int len, const CHAR * __restrict with) 132{ 133 int p_len; 134 135 p_len = ep - p; 136 if (p_len > len) 137 p_len = len; |
138 if (p_len > 0) { 139 if (io_print(iop, p, p_len)) 140 return (-1); 141 } else { 142 p_len = 0; 143 } 144 return (io_pad(iop, len - p_len, with)); |
145} 146 147static inline int 148io_flush(struct io_state *iop) 149{ 150 |
151 return (__sprint(iop->fp, &iop->uio)); 152} 153 154/* 155 * Convert an unsigned long to ASCII for printf purposes, returning 156 * a pointer to the first character of the string representation. 157 * Octal numbers can be forced to have a leading zero; hex numbers 158 * use the given digits. --- 188 unchanged lines hidden --- |