*** empty log message ***
[emacs.git] / src / doprnt.c
blob731afe400cc4054e9d5fbc3bac996600c10398d5
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 1, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <stdio.h>
24 #include <ctype.h>
26 /* Generate output from a format-spec FORMAT,
27 terminated at position FORMAT_END.
28 Output goes in BUFFER, which has room for BUFSIZE chars.
29 If the output does not fit, truncate it to fit.
30 Returns the number of characters stored into BUFFER.
31 ARGS points to the vector of arguments, and NARGS says how many.
32 A double counts as two arguments. */
34 doprnt (buffer, bufsize, format, format_end, nargs, args)
35 char *buffer;
36 register int bufsize;
37 char *format;
38 char *format_end;
39 int nargs;
40 char **args;
42 int cnt = 0; /* Number of arg to gobble next */
43 register char *fmt = format; /* Pointer into format string */
44 register char *bufptr = buffer; /* Pointer into output buffer.. */
45 /* Use this for sprintf unless we need something really big. */
46 char tembuf[100];
47 /* Size of sprintf_buffer. */
48 int size_allocated = 100;
49 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
50 char *sprintf_buffer = tembuf;
51 /* Buffer we have got with malloc. */
52 char *big_buffer = 0;
53 register int tem;
54 char *string;
55 char fmtcpy[20];
56 int minlen;
57 int size; /* Field width factor; e.g., %90d */
59 if (format_end == 0)
60 format_end = format + strlen (format);
62 bufsize--;
63 while (fmt != format_end && bufsize > 0) /* Loop until end of format
64 string or buffer full */
66 if (*fmt == '%') /* Check for a '%' character */
68 int size_bound;
70 fmt++;
71 /* Copy this one %-spec into fmtcopy. */
72 string = fmtcpy;
73 *string++ = '%';
74 while (1)
76 *string++ = *fmt;
77 if (! (*fmt >= '0' && *fmt <= '9') && *fmt != '-' && *fmt != ' ')
78 break;
79 fmt++;
81 *string = 0;
82 /* Get an idea of how much space we might need. */
83 size_bound = atoi (&fmtcpy[1]) + 50;
84 /* Make sure we have that much. */
85 if (size_bound > size_allocated)
87 if (big_buffer)
88 big_buffer = (char *) xrealloc (big_buffer, size_bound);
89 else
90 big_buffer = (char *) xmalloc (size_bound);
91 sprintf_buffer = big_buffer;
92 size_allocated = size_bound;
94 minlen = 0;
95 switch (*fmt++)
97 default:
98 error ("Invalid format operation %%%c", fmt[-1]);
100 /* case 'b': */
101 case 'd':
102 case 'o':
103 case 'x':
104 if (cnt == nargs)
105 error ("Format string wants too many arguments");
106 sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
107 /* Now copy into final output, truncating as nec. */
108 string = sprintf_buffer;
109 goto doit;
111 case 'f':
112 case 'e':
113 case 'g':
115 union { double d; char *half[2]; } u;
116 if (cnt + 1 == nargs)
117 error ("Format string wants too many arguments");
118 u.half[0] = args[cnt++];
119 u.half[1] = args[cnt++];
120 sprintf (sprintf_buffer, fmtcpy, u.d);
121 /* Now copy into final output, truncating as nec. */
122 string = sprintf_buffer;
123 goto doit;
126 case 'S':
127 string[-1] = 's';
128 case 's':
129 if (cnt == nargs)
130 error ("Format string wants too many arguments");
131 string = args[cnt++];
132 if (fmtcpy[1] != 's')
133 minlen = atoi (&fmtcpy[1]);
134 /* Copy string into final output, truncating if no room. */
135 doit:
136 tem = strlen (string);
137 if (minlen > 0)
139 while (minlen > tem && bufsize > 0)
141 *bufptr++ = ' ';
142 bufsize--;
143 minlen--;
145 minlen = 0;
147 if (tem > bufsize)
148 tem = bufsize;
149 strncpy (bufptr, string, tem);
150 bufptr += tem;
151 bufsize -= tem;
152 if (minlen < 0)
154 while (minlen < - tem && bufsize > 0)
156 *bufptr++ = ' ';
157 bufsize--;
158 minlen++;
160 minlen = 0;
162 continue;
164 case 'c':
165 if (cnt == nargs)
166 error ("Format string wants too many arguments");
167 *bufptr++ = (int) args[cnt++];
168 bufsize--;
169 continue;
171 case '%':
172 fmt--; /* Drop thru and this % will be treated as normal */
175 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
176 bufsize--;
179 /* If we had to malloc something, free it. */
180 if (big_buffer)
181 free (big_buffer);
183 *bufptr = 0; /* Make sure our string end with a '\0' */
184 return bufptr - buffer;