Put the g_return_val_if_fail() in the right place.
[glib.git] / glib / gprintf.c
blob85461644b94560e2f42b3a99d5ea65c9d072f11a
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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include "glib.h"
25 #include "gprintf.h"
26 #include "gprintfint.h"
28 /**
29 * g_printf:
30 * @format: the format string. See the printf() documentation.
31 * @Varargs: the arguments to insert in the output.
33 * An implementation of the standard printf() function which supports
34 * positional parameters, as specified in the Single Unix Specification.
36 * Returns: the number of characters printed.
38 * Since: 2.2
39 **/
40 gint
41 g_printf (gchar const *fmt,
42 ...)
44 va_list args;
45 gint retval;
47 va_start (args, fmt);
48 retval = g_vprintf (fmt, args);
49 va_end (args);
51 return retval;
54 /**
55 * g_fprintf:
56 * @file: the stream to write to.
57 * @format: the format string. See the printf() documentation.
58 * @Varargs: the arguments to insert in the output.
60 * An implementation of the standard fprintf() function which supports
61 * positional parameters, as specified in the Single Unix Specification.
63 * Returns: the number of characters printed.
65 * Since: 2.2
66 **/
67 gint
68 g_fprintf (FILE *file,
69 gchar const *fmt,
70 ...)
72 va_list args;
73 gint retval;
75 va_start (args, fmt);
76 retval = g_vfprintf (file, fmt, args);
77 va_end (args);
79 return retval;
82 /**
83 * g_sprintf:
84 * @string: the buffer to hold the output.
85 * @format: the format string. See the printf() documentation.
86 * @Varargs: the arguments to insert in the output.
88 * An implementation of the standard sprintf() function which supports
89 * positional parameters, as specified in the Single Unix Specification.
91 * Returns: the number of characters printed.
93 * Since: 2.2
94 **/
95 gint
96 g_sprintf (gchar *str,
97 gchar const *fmt,
98 ...)
100 va_list args;
101 gint retval;
103 va_start (args, fmt);
104 retval = g_vsprintf (str, fmt, args);
105 va_end (args);
107 return retval;
111 * g_snprintf:
112 * @string: the buffer to hold the output.
113 * @n: the maximum number of characters to produce (including the
114 * terminating nul character).
115 * @format: the format string. See the printf() documentation.
116 * @Varargs: the arguments to insert in the output.
118 * A safer form of the standard sprintf() function. The output is guaranteed
119 * to not exceed @n characters (including the terminating nul character), so
120 * it is easy to ensure that a buffer overflow cannot occur.
122 * See also g_strdup_printf().
124 * In versions of GLib prior to 1.2.3, this function may return -1 if the
125 * output was truncated, and the truncated string may not be nul-terminated.
126 * In versions prior to 1.3.12, this function returns the length of the output
127 * string.
129 * The return value of g_snprintf() conforms to the snprintf()
130 * function as standardized in ISO C99. Note that this is different from
131 * traditional snprintf(), which returns the length of the output string.
133 * The format string may contain positional parameters, as specified in
134 * the Single Unix Specification.
136 * Returns: the number of characters which would be produced if the buffer
137 * was large enough.
139 gint
140 g_snprintf (gchar *str,
141 gulong n,
142 gchar const *fmt,
143 ...)
145 va_list args;
146 gint retval;
148 va_start (args, fmt);
149 retval = g_vsnprintf (str, n, fmt, args);
150 va_end (args);
152 return retval;
156 * g_vprintf:
157 * @format: the format string. See the printf() documentation.
158 * @args: the list of arguments to insert in the output.
160 * An implementation of the standard vprintf() function which supports
161 * positional parameters, as specified in the Single Unix Specification.
163 * Returns: the number of characters printed.
165 * Since: 2.2
167 gint
168 g_vprintf (gchar const *fmt,
169 va_list args)
171 g_return_val_if_fail (fmt != NULL, 0);
173 return _g_vprintf (fmt, args);
177 * g_vfprintf:
178 * @file: the stream to write to.
179 * @format: the format string. See the printf() documentation.
180 * @args: the list of arguments to insert in the output.
182 * An implementation of the standard fprintf() function which supports
183 * positional parameters, as specified in the Single Unix Specification.
185 * Returns: the number of characters printed.
187 * Since: 2.2
189 gint
190 g_vfprintf (FILE *file,
191 gchar const *fmt,
192 va_list args)
194 g_return_val_if_fail (fmt != NULL, 0);
196 return _g_vfprintf (file, fmt, args);
200 * g_vsprintf:
201 * @string: the buffer to hold the output.
202 * @format: the format string. See the printf() documentation.
203 * @args: the list of arguments to insert in the output.
205 * An implementation of the standard vsprintf() function which supports
206 * positional parameters, as specified in the Single Unix Specification.
208 * Returns: the number of characters printed.
210 * Since: 2.2
212 gint
213 g_vsprintf (gchar *str,
214 gchar const *fmt,
215 va_list args)
217 g_return_val_if_fail (str != NULL, 0);
218 g_return_val_if_fail (fmt != NULL, 0);
220 return _g_vsprintf (str, fmt, args);
223 /**
224 * g_vsnprintf:
225 * @string: the buffer to hold the output.
226 * @n: the maximum number of characters to produce (including the
227 * terminating nul character).
228 * @format: the format string. See the printf() documentation.
229 * @args: the list of arguments to insert in the output.
231 * A safer form of the standard vsprintf() function. The output is guaranteed
232 * to not exceed @n characters (including the terminating nul character), so
233 * it is easy to ensure that a buffer overflow cannot occur.
235 * See also g_strdup_vprintf().
237 * In versions of GLib prior to 1.2.3, this function may return -1 if the
238 * output was truncated, and the truncated string may not be nul-terminated.
239 * In versions prior to 1.3.12, this function returns the length of the output
240 * string.
242 * The return value of g_vsnprintf() conforms to the vsnprintf() function
243 * as standardized in ISO C99. Note that this is different from traditional
244 * vsnprintf(), which returns the length of the output string.
246 * The format string may contain positional parameters, as specified in
247 * the Single Unix Specification.
249 * Returns: the number of characters which would be produced if the buffer
250 * was large enough.
252 gint
253 g_vsnprintf (gchar *str,
254 gulong n,
255 gchar const *fmt,
256 va_list args)
258 g_return_val_if_fail (n == 0 || str != NULL, 0);
259 g_return_val_if_fail (fmt != NULL, 0);
261 return _g_vsnprintf (str, n, fmt, args);