1 /* Output like sprintf to a buffer of specified size.
2 Also takes args differently: pass one pointer to an array of strings
3 in addition to the format string which is separate.
4 Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
35 /* Since we use the macro CHAR_HEAD_P, we have to include this, but
36 don't have to include others because CHAR_HEAD_P does not contains
38 #include "character.h"
40 #ifndef DBL_MAX_10_EXP
41 #define DBL_MAX_10_EXP 308 /* IEEE double */
44 /* Generate output from a format-spec FORMAT,
45 terminated at position FORMAT_END.
46 Output goes in BUFFER, which has room for BUFSIZE chars.
47 If the output does not fit, truncate it to fit.
48 Returns the number of bytes stored into BUFFER.
49 ARGS points to the vector of arguments, and NARGS says how many.
50 A double counts as two arguments.
51 String arguments are passed as C strings.
52 Integers are passed as C integers. */
55 doprnt (char *buffer
, register int bufsize
, const char *format
,
56 const char *format_end
, va_list ap
)
58 const char *fmt
= format
; /* Pointer into format string */
59 register char *bufptr
= buffer
; /* Pointer into output buffer.. */
61 /* Use this for sprintf unless we need something really big. */
62 char tembuf
[DBL_MAX_10_EXP
+ 100];
64 /* Size of sprintf_buffer. */
65 unsigned size_allocated
= sizeof (tembuf
);
67 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
68 char *sprintf_buffer
= tembuf
;
70 /* Buffer we have got with malloc. */
75 char fixed_buffer
[20]; /* Default buffer for small formatting. */
78 char charbuf
[MAX_MULTIBYTE_LENGTH
+ 1]; /* Used for %c. */
81 format_end
= format
+ strlen (format
);
83 if ((format_end
- format
+ 1) < sizeof (fixed_buffer
))
84 fmtcpy
= fixed_buffer
;
86 fmtcpy
= (char *) alloca (format_end
- format
+ 1);
90 /* Loop until end of format string or buffer full. */
91 while (fmt
!= format_end
&& bufsize
> 0)
93 if (*fmt
== '%') /* Check for a '%' character */
95 unsigned size_bound
= 0;
96 EMACS_INT width
; /* Columns occupied by STRING. */
99 /* Copy this one %-spec into fmtcpy. */
105 if ('0' <= *fmt
&& *fmt
<= '9')
107 /* Get an idea of how much space we might need.
108 This might be a field width or a precision; e.g.
109 %1.1000f and %1000.1f both might need 1000+ bytes.
110 Parse the width or precision, checking for overflow. */
111 unsigned n
= *fmt
- '0';
112 while ('0' <= fmt
[1] && fmt
[1] <= '9')
114 if (n
* 10 + fmt
[1] - '0' < n
)
115 error ("Format width or precision too large");
116 n
= n
* 10 + fmt
[1] - '0';
123 else if (*fmt
== '-' || *fmt
== ' ' || *fmt
== '.' || *fmt
== '+')
131 /* Make the size bound large enough to handle floating point formats
132 with large numbers. */
133 if (size_bound
+ DBL_MAX_10_EXP
+ 50 < size_bound
)
134 error ("Format width or precision too large");
135 size_bound
+= DBL_MAX_10_EXP
+ 50;
137 /* Make sure we have that much. */
138 if (size_bound
> size_allocated
)
141 big_buffer
= (char *) xrealloc (big_buffer
, size_bound
);
143 big_buffer
= (char *) xmalloc (size_bound
);
144 sprintf_buffer
= big_buffer
;
145 size_allocated
= size_bound
;
151 error ("Invalid format operation %%%c", fmt
[-1]);
157 if (sizeof (int) == sizeof (EMACS_INT
))
159 else if (sizeof (long) == sizeof (EMACS_INT
))
160 /* Insert an `l' the right place. */
161 string
[1] = string
[0],
162 string
[0] = string
[-1],
167 sprintf (sprintf_buffer
, fmtcpy
, va_arg(ap
, char *));
168 /* Now copy into final output, truncating as nec. */
169 string
= sprintf_buffer
;
176 double d
= va_arg(ap
, double);
177 sprintf (sprintf_buffer
, fmtcpy
, d
);
178 /* Now copy into final output, truncating as nec. */
179 string
= sprintf_buffer
;
186 if (fmtcpy
[1] != 's')
187 minlen
= atoi (&fmtcpy
[1]);
188 string
= va_arg (ap
, char *);
189 tem
= strlen (string
);
190 width
= strwidth (string
, tem
);
193 /* Copy string into final output, truncating if no room. */
195 /* Coming here means STRING contains ASCII only. */
196 width
= tem
= strlen (string
);
198 /* We have already calculated:
199 TEM -- length of STRING,
200 WIDTH -- columns occupied by STRING when displayed, and
201 MINLEN -- minimum columns of the output. */
204 while (minlen
> width
&& bufsize
> 0)
214 /* Truncate the string at character boundary. */
216 while (!CHAR_HEAD_P (string
[tem
- 1])) tem
--;
217 memcpy (bufptr
, string
, tem
);
218 /* We must calculate WIDTH again. */
219 width
= strwidth (bufptr
, tem
);
222 memcpy (bufptr
, string
, tem
);
227 while (minlen
< - width
&& bufsize
> 0)
239 /* Sometimes for %c we pass a char, which would widen
240 to int. Sometimes we pass XFASTINT() or XINT()
241 values, which would be EMACS_INT. Let's hope that
242 both are passed the same way, otherwise we'll need
243 to rewrite callers. */
244 EMACS_INT chr
= va_arg(ap
, EMACS_INT
);
245 tem
= CHAR_STRING ((int) chr
, (unsigned char *) charbuf
);
248 width
= strwidth (string
, tem
);
249 if (fmtcpy
[1] != 'c')
250 minlen
= atoi (&fmtcpy
[1]);
255 fmt
--; /* Drop thru and this % will be treated as normal */
260 /* Just some character; Copy it if the whole multi-byte form
261 fit in the buffer. */
262 char *save_bufptr
= bufptr
;
264 do { *bufptr
++ = *fmt
++; }
265 while (--bufsize
> 0 && !CHAR_HEAD_P (*fmt
));
266 if (!CHAR_HEAD_P (*fmt
))
268 bufptr
= save_bufptr
;
274 /* If we had to malloc something, free it. */
277 *bufptr
= 0; /* Make sure our string end with a '\0' */
278 return bufptr
- buffer
;