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 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 2, or (at your option)
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; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 extern long *xmalloc (), *xrealloc ();
30 static int doprnt1 ();
32 /* Generate output from a format-spec FORMAT,
33 terminated at position FORMAT_END.
34 Output goes in BUFFER, which has room for BUFSIZE chars.
35 If the output does not fit, truncate it to fit.
36 Returns the number of characters stored into BUFFER.
37 ARGS points to the vector of arguments, and NARGS says how many.
38 A double counts as two arguments.
39 String arguments are passed as C strings.
40 Integers are passed as C integers. */
42 doprnt (buffer
, bufsize
, format
, format_end
, nargs
, args
)
50 return doprnt1 (0, buffer
, bufsize
, format
, format_end
, nargs
, args
);
53 /* Like doprnt except that strings in ARGS are passed
56 doprnt_lisp (buffer
, bufsize
, format
, format_end
, nargs
, args
)
64 return doprnt1 (1, buffer
, bufsize
, format
, format_end
, nargs
, args
);
68 doprnt1 (lispstrings
, buffer
, bufsize
, format
, format_end
, nargs
, args
)
77 int cnt
= 0; /* Number of arg to gobble next */
78 register char *fmt
= format
; /* Pointer into format string */
79 register char *bufptr
= buffer
; /* Pointer into output buffer.. */
81 /* Use this for sprintf unless we need something really big. */
84 /* Size of sprintf_buffer. */
85 int size_allocated
= 100;
87 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
88 char *sprintf_buffer
= tembuf
;
90 /* Buffer we have got with malloc. */
95 char fixed_buffer
[20]; /* Default buffer for small formatting. */
98 int size
; /* Field width factor; e.g., %90d */
99 char charbuf
[2]; /* Used for %c. */
102 format_end
= format
+ strlen (format
);
104 if ((format_end
- format
+ 1) < sizeof (fixed_buffer
))
105 fmtcpy
= fixed_buffer
;
107 fmtcpy
= (char *) alloca (format_end
- format
+ 1);
111 /* Loop until end of format string or buffer full. */
112 while (fmt
!= format_end
&& bufsize
> 0)
114 if (*fmt
== '%') /* Check for a '%' character */
119 /* Copy this one %-spec into fmtcpy. */
125 if (! (*fmt
>= '0' && *fmt
<= '9')
126 && *fmt
!= '-' && *fmt
!= ' '&& *fmt
!= '.')
131 /* Get an idea of how much space we might need. */
132 size_bound
= atoi (&fmtcpy
[1]);
134 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
136 size_bound
= -size_bound
;
139 if (size_bound
> (unsigned) (1 << (BITS_PER_INT
- 1)))
140 error ("Format padding too large");
142 /* Make sure we have that much. */
143 if (size_bound
> size_allocated
)
146 big_buffer
= (char *) xrealloc (big_buffer
, size_bound
);
148 big_buffer
= (char *) xmalloc (size_bound
);
149 sprintf_buffer
= big_buffer
;
150 size_allocated
= size_bound
;
156 error ("Invalid format operation %%%c", fmt
[-1]);
163 error ("Not enough arguments for format string");
164 if (sizeof (int) == sizeof (EMACS_INT
))
166 else if (sizeof (long) == sizeof (EMACS_INT
))
167 /* Insert an `l' the right place. */
168 string
[1] = string
[0],
169 string
[0] = string
[-1],
174 sprintf (sprintf_buffer
, fmtcpy
, args
[cnt
++]);
175 /* Now copy into final output, truncating as nec. */
176 string
= sprintf_buffer
;
183 union { double d
; char *half
[2]; } u
;
184 if (cnt
+ 1 == nargs
)
185 error ("not enough arguments for format string");
186 u
.half
[0] = args
[cnt
++];
187 u
.half
[1] = args
[cnt
++];
188 sprintf (sprintf_buffer
, fmtcpy
, u
.d
);
189 /* Now copy into final output, truncating as nec. */
190 string
= sprintf_buffer
;
198 error ("not enough arguments for format string");
199 if (fmtcpy
[1] != 's')
200 minlen
= atoi (&fmtcpy
[1]);
203 string
= (char *) XSTRING (((Lisp_Object
*) args
)[cnt
])->data
;
204 tem
= XSTRING (((Lisp_Object
*) args
)[cnt
])->size
;
209 string
= args
[cnt
++];
210 tem
= strlen (string
);
214 /* Copy string into final output, truncating if no room. */
216 tem
= strlen (string
);
220 while (minlen
> tem
&& bufsize
> 0)
230 bcopy (string
, bufptr
, tem
);
235 while (minlen
< - tem
&& bufsize
> 0)
247 error ("not enough arguments for format string");
248 *charbuf
= (EMACS_INT
) args
[cnt
++];
251 if (fmtcpy
[1] != 'c')
252 minlen
= atoi (&fmtcpy
[1]);
256 fmt
--; /* Drop thru and this % will be treated as normal */
259 *bufptr
++ = *fmt
++; /* Just some characters; Copy 'em */
263 /* If we had to malloc something, free it. */
267 *bufptr
= 0; /* Make sure our string end with a '\0' */
268 return bufptr
- buffer
;