49
|
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.
|
|
5
|
|
6 This file is part of GNU Emacs.
|
|
7
|
|
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.
|
|
12
|
|
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.
|
|
17
|
|
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. */
|
|
21
|
|
22
|
|
23 #include <stdio.h>
|
|
24 #include <ctype.h>
|
|
25
|
|
26 doprnt (buffer, bufsize, format, format_end, nargs, args)
|
|
27 char *buffer;
|
|
28 register int bufsize;
|
|
29 char *format;
|
|
30 char *format_end;
|
|
31 int nargs;
|
|
32 char **args;
|
|
33 {
|
|
34 int cnt = 0; /* Number of arg to gobble next */
|
|
35 register char *fmt = format; /* Pointer into format string */
|
|
36 register char *bufptr = buffer; /* Pointer into output buffer.. */
|
|
37 char tembuf[512];
|
|
38 register int tem;
|
|
39 char *string;
|
|
40 char fmtcpy[20];
|
|
41 int minlen;
|
|
42 int size; /* Field width factor; e.g., %90d */
|
|
43
|
|
44 if (format_end == 0)
|
|
45 format_end = format + strlen (format);
|
|
46
|
|
47 bufsize--;
|
|
48 while (fmt != format_end && bufsize > 0) /* Loop until end of format string or buffer full */
|
|
49 {
|
|
50 if (*fmt == '%') /* Check for a '%' character */
|
|
51 {
|
|
52 fmt++;
|
|
53 /* Copy this one %-spec into fmtcopy. */
|
|
54 string = fmtcpy;
|
|
55 *string++ = '%';
|
|
56 while (1)
|
|
57 {
|
|
58 *string++ = *fmt;
|
|
59 if (! (*fmt >= '0' && *fmt <= '9') && *fmt != '-' && *fmt != ' ')
|
|
60 break;
|
|
61 fmt++;
|
|
62 }
|
|
63 *string = 0;
|
|
64 minlen = 0;
|
|
65 switch (*fmt++)
|
|
66 {
|
|
67 default:
|
|
68 error ("Invalid format operation %%%c", fmt[-1]);
|
|
69
|
|
70 /* case 'b': */
|
|
71 case 'd':
|
|
72 case 'o':
|
|
73 case 'x':
|
|
74 if (cnt == nargs)
|
|
75 error ("Format string wants too many arguments");
|
|
76 sprintf (tembuf, fmtcpy, args[cnt++]);
|
|
77 /* Now copy tembuf into final output, truncating as nec. */
|
|
78 string = tembuf;
|
|
79 goto doit;
|
|
80
|
|
81 case 'S':
|
|
82 string[-1] = 's';
|
|
83 case 's':
|
|
84 if (cnt == nargs)
|
|
85 error ("Format string wants too many arguments");
|
|
86 string = args[cnt++];
|
|
87 if (fmtcpy[1] != 's')
|
|
88 minlen = atoi (&fmtcpy[1]);
|
|
89 /* Copy string into final output, truncating if no room. */
|
|
90 doit:
|
|
91 tem = strlen (string);
|
|
92 if (minlen > 0)
|
|
93 {
|
|
94 while (minlen > tem && bufsize > 0)
|
|
95 {
|
|
96 *bufptr++ = ' ';
|
|
97 bufsize--;
|
|
98 minlen--;
|
|
99 }
|
|
100 minlen = 0;
|
|
101 }
|
|
102 if (tem > bufsize)
|
|
103 tem = bufsize;
|
|
104 strncpy (bufptr, string, tem);
|
|
105 bufptr += tem;
|
|
106 bufsize -= tem;
|
|
107 if (minlen < 0)
|
|
108 {
|
|
109 while (minlen < - tem && bufsize > 0)
|
|
110 {
|
|
111 *bufptr++ = ' ';
|
|
112 bufsize--;
|
|
113 minlen++;
|
|
114 }
|
|
115 minlen = 0;
|
|
116 }
|
|
117 continue;
|
|
118
|
|
119 case 'c':
|
|
120 if (cnt == nargs)
|
|
121 error ("Format string wants too many arguments");
|
|
122 *bufptr++ = (int) args[cnt++];
|
|
123 bufsize--;
|
|
124 continue;
|
|
125
|
|
126 case '%':
|
|
127 fmt--; /* Drop thru and this % will be treated as normal */
|
|
128 }
|
|
129 }
|
|
130 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
|
|
131 bufsize--;
|
|
132 };
|
|
133
|
|
134 *bufptr = 0; /* Make sure our string end with a '\0' */
|
|
135 return bufptr - buffer;
|
|
136 }
|