Updated British English translation
[glib.git] / glib / gprintf.c
blob346fd956ecce31e9f1606b84b5d7068e33143276
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997, 2002 Peter Mattis, Red Hat, Inc.
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.
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.
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.
20 #include "config.h"
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include "gprintf.h"
27 #include "gprintfint.h"
30 /**
31 * g_printf:
32 * @format: a standard printf() format string, but notice
33 * <link linkend="string-precision">string precision pitfalls</link>.
34 * @...: the arguments to insert in the output.
36 * An implementation of the standard printf() function which supports
37 * positional parameters, as specified in the Single Unix Specification.
39 * Returns: the number of bytes printed.
41 * Since: 2.2
42 **/
43 gint
44 g_printf (gchar const *format,
45 ...)
47 va_list args;
48 gint retval;
50 va_start (args, format);
51 retval = g_vprintf (format, args);
52 va_end (args);
54 return retval;
57 /**
58 * g_fprintf:
59 * @file: the stream to write to.
60 * @format: a standard printf() format string, but notice
61 * <link linkend="string-precision">string precision pitfalls</link>.
62 * @...: the arguments to insert in the output.
64 * An implementation of the standard fprintf() function which supports
65 * positional parameters, as specified in the Single Unix Specification.
67 * Returns: the number of bytes printed.
69 * Since: 2.2
70 **/
71 gint
72 g_fprintf (FILE *file,
73 gchar const *format,
74 ...)
76 va_list args;
77 gint retval;
79 va_start (args, format);
80 retval = g_vfprintf (file, format, args);
81 va_end (args);
83 return retval;
86 /**
87 * g_sprintf:
88 * @string: A pointer to a memory buffer to contain the resulting string. It
89 * is up to the caller to ensure that the allocated buffer is large
90 * enough to hold the formatted result
91 * @format: a standard printf() format string, but notice
92 * <link linkend="string-precision">string precision pitfalls</link>.
93 * @...: the arguments to insert in the output.
95 * An implementation of the standard sprintf() function which supports
96 * positional parameters, as specified in the Single Unix Specification.
98 * Note that it is usually better to use g_snprintf(), to avoid the
99 * risk of buffer overflow.
101 * See also g_strdup_printf().
103 * Returns: the number of bytes printed.
105 * Since: 2.2
107 gint
108 g_sprintf (gchar *string,
109 gchar const *format,
110 ...)
112 va_list args;
113 gint retval;
115 va_start (args, format);
116 retval = g_vsprintf (string, format, args);
117 va_end (args);
119 return retval;
123 * g_snprintf:
124 * @string: the buffer to hold the output.
125 * @n: the maximum number of bytes to produce (including the
126 * terminating nul character).
127 * @format: a standard printf() format string, but notice
128 * <link linkend="string-precision">string precision pitfalls</link>.
129 * @...: the arguments to insert in the output.
131 * A safer form of the standard sprintf() function. The output is guaranteed
132 * to not exceed @n characters (including the terminating nul character), so
133 * it is easy to ensure that a buffer overflow cannot occur.
135 * See also g_strdup_printf().
137 * In versions of GLib prior to 1.2.3, this function may return -1 if the
138 * output was truncated, and the truncated string may not be nul-terminated.
139 * In versions prior to 1.3.12, this function returns the length of the output
140 * string.
142 * The return value of g_snprintf() conforms to the snprintf()
143 * function as standardized in ISO C99. Note that this is different from
144 * traditional snprintf(), which returns the length of the output string.
146 * The format string may contain positional parameters, as specified in
147 * the Single Unix Specification.
149 * Returns: the number of bytes which would be produced if the buffer
150 * was large enough.
152 gint
153 g_snprintf (gchar *string,
154 gulong n,
155 gchar const *format,
156 ...)
158 va_list args;
159 gint retval;
161 va_start (args, format);
162 retval = g_vsnprintf (string, n, format, args);
163 va_end (args);
165 return retval;
169 * g_vprintf:
170 * @format: a standard printf() format string, but notice
171 * <link linkend="string-precision">string precision pitfalls</link>.
172 * @args: the list of arguments to insert in the output.
174 * An implementation of the standard vprintf() function which supports
175 * positional parameters, as specified in the Single Unix Specification.
177 * Returns: the number of bytes printed.
179 * Since: 2.2
181 gint
182 g_vprintf (gchar const *format,
183 va_list args)
185 g_return_val_if_fail (format != NULL, -1);
187 return _g_vprintf (format, args);
191 * g_vfprintf:
192 * @file: the stream to write to.
193 * @format: a standard printf() format string, but notice
194 * <link linkend="string-precision">string precision pitfalls</link>.
195 * @args: the list of arguments to insert in the output.
197 * An implementation of the standard fprintf() function which supports
198 * positional parameters, as specified in the Single Unix Specification.
200 * Returns: the number of bytes printed.
202 * Since: 2.2
204 gint
205 g_vfprintf (FILE *file,
206 gchar const *format,
207 va_list args)
209 g_return_val_if_fail (format != NULL, -1);
211 return _g_vfprintf (file, format, args);
215 * g_vsprintf:
216 * @string: the buffer to hold the output.
217 * @format: a standard printf() format string, but notice
218 * <link linkend="string-precision">string precision pitfalls</link>.
219 * @args: the list of arguments to insert in the output.
221 * An implementation of the standard vsprintf() function which supports
222 * positional parameters, as specified in the Single Unix Specification.
224 * Returns: the number of bytes printed.
226 * Since: 2.2
228 gint
229 g_vsprintf (gchar *string,
230 gchar const *format,
231 va_list args)
233 g_return_val_if_fail (string != NULL, -1);
234 g_return_val_if_fail (format != NULL, -1);
236 return _g_vsprintf (string, format, args);
239 /**
240 * g_vsnprintf:
241 * @string: the buffer to hold the output.
242 * @n: the maximum number of bytes to produce (including the
243 * terminating nul character).
244 * @format: a standard printf() format string, but notice
245 * <link linkend="string-precision">string precision pitfalls</link>.
246 * @args: the list of arguments to insert in the output.
248 * A safer form of the standard vsprintf() function. The output is guaranteed
249 * to not exceed @n characters (including the terminating nul character), so
250 * it is easy to ensure that a buffer overflow cannot occur.
252 * See also g_strdup_vprintf().
254 * In versions of GLib prior to 1.2.3, this function may return -1 if the
255 * output was truncated, and the truncated string may not be nul-terminated.
256 * In versions prior to 1.3.12, this function returns the length of the output
257 * string.
259 * The return value of g_vsnprintf() conforms to the vsnprintf() function
260 * as standardized in ISO C99. Note that this is different from traditional
261 * vsnprintf(), which returns the length of the output string.
263 * The format string may contain positional parameters, as specified in
264 * the Single Unix Specification.
266 * Returns: the number of bytes which would be produced if the buffer
267 * was large enough.
269 gint
270 g_vsnprintf (gchar *string,
271 gulong n,
272 gchar const *format,
273 va_list args)
275 g_return_val_if_fail (n == 0 || string != NULL, -1);
276 g_return_val_if_fail (format != NULL, -1);
278 return _g_vsnprintf (string, n, format, args);
282 * g_vasprintf:
283 * @string: the return location for the newly-allocated string.
284 * @format: a standard printf() format string, but notice
285 * <link linkend="string-precision">string precision pitfalls</link>.
286 * @args: the list of arguments to insert in the output.
288 * An implementation of the GNU vasprintf() function which supports
289 * positional parameters, as specified in the Single Unix Specification.
290 * This function is similar to g_vsprintf(), except that it allocates a
291 * string to hold the output, instead of putting the output in a buffer
292 * you allocate in advance.
294 * Returns: the number of bytes printed.
296 * Since: 2.4
298 gint
299 g_vasprintf (gchar **string,
300 gchar const *format,
301 va_list args)
303 gint len;
304 g_return_val_if_fail (string != NULL, -1);
306 #if !defined(HAVE_GOOD_PRINTF)
308 len = _g_gnulib_vasprintf (string, format, args);
309 if (len < 0)
310 *string = NULL;
312 #elif defined (HAVE_VASPRINTF)
314 len = vasprintf (string, format, args);
315 if (len < 0)
316 *string = NULL;
317 else if (!g_mem_is_system_malloc ())
319 /* vasprintf returns malloc-allocated memory */
320 gchar *string1 = g_strndup (*string, len);
321 free (*string);
322 *string = string1;
325 #else
328 va_list args2;
330 G_VA_COPY (args2, args);
332 *string = g_new (gchar, g_printf_string_upper_bound (format, args));
334 len = _g_vsprintf (*string, format, args2);
335 va_end (args2);
337 #endif
339 return len;