source: trunk/third/glib2/glib/gprintf.c @ 20721

Revision 20721, 9.3 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20720, which included commits to RCS files with non-trunk default branches.
Line 
1/* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997, 2002  Peter Mattis, Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#define _GNU_SOURCE             /* For vasprintf */
25
26#include <stdarg.h>
27#include <stdlib.h>
28#include <stdio.h>
29
30#include "glib.h"
31#include "gprintf.h"
32#include "gprintfint.h"
33
34/**
35 * g_printf:
36 * @format: a standard printf() format string, but notice
37 *          <link linkend="string-precision">string precision pitfalls</link>.
38 * @Varargs: the arguments to insert in the output.
39 *
40 * An implementation of the standard printf() function which supports
41 * positional parameters, as specified in the Single Unix Specification.
42 *
43 * Returns: the number of characters printed.
44 *
45 * Since: 2.2
46 **/
47gint
48g_printf (gchar const *format,
49          ...)
50{
51  va_list args;
52  gint retval;
53
54  va_start (args, format);
55  retval = g_vprintf (format, args);
56  va_end (args);
57 
58  return retval;
59}
60
61/**
62 * g_fprintf:
63 * @file: the stream to write to.
64 * @format: a standard printf() format string, but notice
65 *          <link linkend="string-precision">string precision pitfalls</link>.
66 * @Varargs: the arguments to insert in the output.
67 *
68 * An implementation of the standard fprintf() function which supports
69 * positional parameters, as specified in the Single Unix Specification.
70 *
71 * Returns: the number of characters printed.
72 *
73 * Since: 2.2
74 **/
75gint
76g_fprintf (FILE        *file,
77           gchar const *format,
78           ...)
79{
80  va_list args;
81  gint retval;
82
83  va_start (args, format);
84  retval = g_vfprintf (file, format, args);
85  va_end (args);
86 
87  return retval;
88}
89
90/**
91 * g_sprintf:
92 * @string: the buffer to hold the output.
93 * @format: a standard printf() format string, but notice
94 *          <link linkend="string-precision">string precision pitfalls</link>.
95 * @Varargs: the arguments to insert in the output.
96 *
97 * An implementation of the standard sprintf() function which supports
98 * positional parameters, as specified in the Single Unix Specification.
99 *
100 * Returns: the number of characters printed.
101 *
102 * Since: 2.2
103 **/
104gint
105g_sprintf (gchar       *string,
106           gchar const *format,
107           ...)
108{
109  va_list args;
110  gint retval;
111
112  va_start (args, format);
113  retval = g_vsprintf (string, format, args);
114  va_end (args);
115 
116  return retval;
117}
118
119/**
120 * g_snprintf:
121 * @string: the buffer to hold the output.
122 * @n: the maximum number of characters to produce (including the
123 *     terminating nul character).
124 * @format: a standard printf() format string, but notice
125 *          <link linkend="string-precision">string precision pitfalls</link>.
126 * @Varargs: the arguments to insert in the output.
127 *
128 * A safer form of the standard sprintf() function. The output is guaranteed
129 * to not exceed @n characters (including the terminating nul character), so
130 * it is easy to ensure that a buffer overflow cannot occur.
131 *
132 * See also g_strdup_printf().
133 *
134 * In versions of GLib prior to 1.2.3, this function may return -1 if the
135 * output was truncated, and the truncated string may not be nul-terminated.
136 * In versions prior to 1.3.12, this function returns the length of the output
137 * string.
138 *
139 * The return value of g_snprintf() conforms to the snprintf()
140 * function as standardized in ISO C99. Note that this is different from
141 * traditional snprintf(), which returns the length of the output string.
142 *
143 * The format string may contain positional parameters, as specified in
144 * the Single Unix Specification.
145 *
146 * Returns: the number of characters which would be produced if the buffer
147 *     was large enough.
148 **/
149gint
150g_snprintf (gchar       *string,
151            gulong       n,
152            gchar const *format,
153            ...)
154{
155  va_list args;
156  gint retval;
157
158  va_start (args, format);
159  retval = g_vsnprintf (string, n, format, args);
160  va_end (args);
161 
162  return retval;
163}
164
165/**
166 * g_vprintf:
167 * @format: a standard printf() format string, but notice
168 *          <link linkend="string-precision">string precision pitfalls</link>.
169 * @args: the list of arguments to insert in the output.
170 *
171 * An implementation of the standard vprintf() function which supports
172 * positional parameters, as specified in the Single Unix Specification.
173 *
174 * Returns: the number of characters printed.
175 *
176 * Since: 2.2
177 **/
178gint
179g_vprintf (gchar const *format,
180           va_list      args)
181{
182  g_return_val_if_fail (format != NULL, -1);
183
184  return _g_vprintf (format, args);
185}
186
187/**
188 * g_vfprintf:
189 * @file: the stream to write to.
190 * @format: a standard printf() format string, but notice
191 *          <link linkend="string-precision">string precision pitfalls</link>.
192 * @args: the list of arguments to insert in the output.
193 *
194 * An implementation of the standard fprintf() function which supports
195 * positional parameters, as specified in the Single Unix Specification.
196 *
197 * Returns: the number of characters printed.
198 *
199 * Since: 2.2
200 **/
201gint
202g_vfprintf (FILE        *file,
203            gchar const *format,
204            va_list      args)
205{
206  g_return_val_if_fail (format != NULL, -1);
207
208  return _g_vfprintf (file, format, args);
209}
210
211/**
212 * g_vsprintf:
213 * @string: the buffer to hold the output.
214 * @format: a standard printf() format string, but notice
215 *          <link linkend="string-precision">string precision pitfalls</link>.
216 * @args: the list of arguments to insert in the output.
217 *
218 * An implementation of the standard vsprintf() function which supports
219 * positional parameters, as specified in the Single Unix Specification.
220 *
221 * Returns: the number of characters printed.
222 *
223 * Since: 2.2
224 **/
225gint
226g_vsprintf (gchar        *string,
227            gchar const *format,
228            va_list      args)
229{
230  g_return_val_if_fail (string != NULL, -1);
231  g_return_val_if_fail (format != NULL, -1);
232
233  return _g_vsprintf (string, format, args);
234}
235
236/**
237 * g_vsnprintf:
238 * @string: the buffer to hold the output.
239 * @n: the maximum number of characters to produce (including the
240 *     terminating nul character).
241 * @format: a standard printf() format string, but notice
242 *          <link linkend="string-precision">string precision pitfalls</link>.
243 * @args: the list of arguments to insert in the output.
244 *
245 * A safer form of the standard vsprintf() function. The output is guaranteed
246 * to not exceed @n characters (including the terminating nul character), so
247 * it is easy to ensure that a buffer overflow cannot occur.
248 *
249 * See also g_strdup_vprintf().
250 *
251 * In versions of GLib prior to 1.2.3, this function may return -1 if the
252 * output was truncated, and the truncated string may not be nul-terminated.
253 * In versions prior to 1.3.12, this function returns the length of the output
254 * string.
255 *
256 * The return value of g_vsnprintf() conforms to the vsnprintf() function
257 * as standardized in ISO C99. Note that this is different from traditional
258 * vsnprintf(), which returns the length of the output string.
259 *
260 * The format string may contain positional parameters, as specified in
261 * the Single Unix Specification.
262 *
263 * Returns: the number of characters which would be produced if the buffer
264 *  was large enough.
265 */
266gint
267g_vsnprintf (gchar       *string,
268             gulong       n,
269             gchar const *format,
270             va_list      args)
271{
272  g_return_val_if_fail (n == 0 || string != NULL, -1);
273  g_return_val_if_fail (format != NULL, -1);
274
275  return _g_vsnprintf (string, n, format, args);
276}
277
278/**
279 * g_vasprintf:
280 * @string: the return location for the newly-allocated string.
281 * @format: a standard printf() format string, but notice
282 *          <link linkend="string-precision">string precision pitfalls</link>.
283 * @args: the list of arguments to insert in the output.
284 *
285 * An implementation of the GNU vasprintf() function which supports
286 * positional parameters, as specified in the Single Unix Specification.
287 * This function is similar to g_vsprintf(), except that it allocates a
288 * string to hold the output, instead of putting the output in a buffer
289 * you allocate in advance.
290 *
291 * Returns: the number of characters printed.
292 *
293 * Since: 2.4
294 **/
295gint
296g_vasprintf (gchar      **string,
297             gchar const *format,
298             va_list      args)
299{
300  gint len;
301  g_return_val_if_fail (string != NULL, -1);
302
303#if !defined(HAVE_GOOD_PRINTF)
304
305  len = _g_gnulib_vasprintf (string, format, args);
306  if (len < 0)
307    *string = NULL;
308
309#elif defined (HAVE_VASPRINTF)
310
311  len = vasprintf (string, format, args);
312  if (len < 0)
313    *string = NULL;
314  else if (!g_mem_is_system_malloc ())
315    {
316      /* vasprintf returns malloc-allocated memory */
317      gchar *string1 = g_strndup (*string, len);
318      free (*string);
319      *string = string1;
320    }
321
322#else
323
324  {
325    va_list args2;
326
327    G_VA_COPY (args2, args);
328
329    *string = g_new (gchar, g_printf_string_upper_bound (format, args));
330
331    len = _g_vsprintf (*string, format, args2);
332    va_end (args2);
333  }
334#endif
335
336  return len;
337}
338
339
340
341
Note: See TracBrowser for help on using the repository browser.