Avoid var declaration after statement.
[platform/upstream/glib.git] / glib / gprintf.c
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: the format string. See the printf() documentation.
37  * @Varargs: the arguments to insert in the output.
38  *
39  * An implementation of the standard printf() function which supports 
40  * positional parameters, as specified in the Single Unix Specification.
41  *
42  * Returns: the number of characters printed.
43  *
44  * Since: 2.2
45  **/
46 gint
47 g_printf (gchar const *format,
48           ...)
49 {
50   va_list args;
51   gint retval;
52
53   va_start (args, format);
54   retval = g_vprintf (format, args);
55   va_end (args);
56   
57   return retval;
58 }
59
60 /**
61  * g_fprintf:
62  * @file: the stream to write to.
63  * @format: the format string. See the printf() documentation.
64  * @Varargs: the arguments to insert in the output.
65  *
66  * An implementation of the standard fprintf() function which supports 
67  * positional parameters, as specified in the Single Unix Specification.
68  *
69  * Returns: the number of characters printed.
70  *
71  * Since: 2.2
72  **/
73 gint
74 g_fprintf (FILE        *file, 
75            gchar const *format,
76            ...)
77 {
78   va_list args;
79   gint retval;
80
81   va_start (args, format);
82   retval = g_vfprintf (file, format, args);
83   va_end (args);
84   
85   return retval;
86 }
87
88 /**
89  * g_sprintf:
90  * @string: the buffer to hold the output.
91  * @format: the format string. See the printf() documentation.
92  * @Varargs: the arguments to insert in the output.
93  *
94  * An implementation of the standard sprintf() function which supports 
95  * positional parameters, as specified in the Single Unix Specification.
96  *
97  * Returns: the number of characters printed.
98  *
99  * Since: 2.2
100  **/
101 gint
102 g_sprintf (gchar       *string,
103            gchar const *format,
104            ...)
105 {
106   va_list args;
107   gint retval;
108
109   va_start (args, format);
110   retval = g_vsprintf (string, format, args);
111   va_end (args);
112   
113   return retval;
114 }
115
116 /**
117  * g_snprintf:
118  * @string: the buffer to hold the output.
119  * @n: the maximum number of characters to produce (including the 
120  *     terminating nul character).
121  * @format: the format string. See the printf() documentation.
122  * @Varargs: the arguments to insert in the output.
123  *
124  * A safer form of the standard sprintf() function. The output is guaranteed
125  * to not exceed @n characters (including the terminating nul character), so 
126  * it is easy to ensure that a buffer overflow cannot occur.
127  * 
128  * See also g_strdup_printf().
129  *
130  * In versions of GLib prior to 1.2.3, this function may return -1 if the 
131  * output was truncated, and the truncated string may not be nul-terminated. 
132  * In versions prior to 1.3.12, this function returns the length of the output 
133  * string.
134  *
135  * The return value of g_snprintf() conforms to the snprintf()
136  * function as standardized in ISO C99. Note that this is different from 
137  * traditional snprintf(), which returns the length of the output string.
138  *
139  * The format string may contain positional parameters, as specified in 
140  * the Single Unix Specification.
141  *
142  * Returns: the number of characters which would be produced if the buffer 
143  *     was large enough.
144  **/
145 gint
146 g_snprintf (gchar       *string,
147             gulong       n,
148             gchar const *format,
149             ...)
150 {
151   va_list args;
152   gint retval;
153
154   va_start (args, format);
155   retval = g_vsnprintf (string, n, format, args);
156   va_end (args);
157   
158   return retval;
159 }
160
161 /**
162  * g_vprintf:
163  * @format: the format string. See the printf() documentation.
164  * @args: the list of arguments to insert in the output.
165  *
166  * An implementation of the standard vprintf() function which supports 
167  * positional parameters, as specified in the Single Unix Specification.
168  *
169  * Returns: the number of characters printed.
170  *
171  * Since: 2.2
172  **/
173 gint
174 g_vprintf (gchar const *format,
175            va_list      args)
176 {
177   g_return_val_if_fail (format != NULL, 0);
178
179   return _g_vprintf (format, args);
180 }
181
182 /**
183  * g_vfprintf:
184  * @file: the stream to write to.
185  * @format: the format string. See the printf() documentation.
186  * @args: the list of arguments to insert in the output.
187  *
188  * An implementation of the standard fprintf() function which supports 
189  * positional parameters, as specified in the Single Unix Specification.
190  *
191  * Returns: the number of characters printed.
192  *
193  * Since: 2.2
194  **/
195 gint
196 g_vfprintf (FILE        *file,
197             gchar const *format,
198             va_list      args)
199 {
200   g_return_val_if_fail (format != NULL, 0);
201
202   return _g_vfprintf (file, format, args);
203 }
204
205 /**
206  * g_vsprintf:
207  * @string: the buffer to hold the output.
208  * @format: the format string. See the printf() documentation.
209  * @args: the list of arguments to insert in the output.
210  *
211  * An implementation of the standard vsprintf() function which supports 
212  * positional parameters, as specified in the Single Unix Specification.
213  *
214  * Returns: the number of characters printed.
215  *
216  * Since: 2.2
217  **/
218 gint
219 g_vsprintf (gchar        *string,
220             gchar const *format,
221             va_list      args)
222 {
223   g_return_val_if_fail (string != NULL, 0);
224   g_return_val_if_fail (format != NULL, 0);
225
226   return _g_vsprintf (string, format, args);
227 }
228
229 /** 
230  * g_vsnprintf:
231  * @string: the buffer to hold the output.
232  * @n: the maximum number of characters to produce (including the 
233  *     terminating nul character).
234  * @format: the format string. See the printf() documentation.
235  * @args: the list of arguments to insert in the output.
236  *
237  * A safer form of the standard vsprintf() function. The output is guaranteed
238  * to not exceed @n characters (including the terminating nul character), so 
239  * it is easy to ensure that a buffer overflow cannot occur.
240  *
241  * See also g_strdup_vprintf().
242  *
243  * In versions of GLib prior to 1.2.3, this function may return -1 if the 
244  * output was truncated, and the truncated string may not be nul-terminated.
245  * In versions prior to 1.3.12, this function returns the length of the output 
246  * string.
247  *
248  * The return value of g_vsnprintf() conforms to the vsnprintf() function 
249  * as standardized in ISO C99. Note that this is different from traditional 
250  * vsnprintf(), which returns the length of the output string.
251  *
252  * The format string may contain positional parameters, as specified in 
253  * the Single Unix Specification.
254  *
255  * Returns: the number of characters which would be produced if the buffer 
256  *  was large enough.
257  */
258 gint
259 g_vsnprintf (gchar       *string,
260              gulong       n,
261              gchar const *format,
262              va_list      args)
263 {
264   g_return_val_if_fail (n == 0 || string != NULL, 0);
265   g_return_val_if_fail (format != NULL, 0);
266
267   return _g_vsnprintf (string, n, format, args);
268 }
269
270 /**
271  * g_vasprintf:
272  * @string: the return location for the newly-allocated string.
273  * @format: the format string. See the printf() documentation.
274  * @args: the list of arguments to insert in the output.
275  *
276  * An implementation of the GNU vasprintf() function which supports 
277  * positional parameters, as specified in the Single Unix Specification.
278  * This function is similar to g_vsprintf(), except that it allocates a 
279  * string to hold the output, instead of putting the output in a buffer 
280  * you allocate in advance.
281  *
282  * Returns: the number of characters printed.
283  *
284  * Since: 2.4
285  **/
286 gint 
287 g_vasprintf (gchar      **string,
288              gchar const *format,
289              va_list      args)
290 {
291   gint len;
292   g_return_val_if_fail (string != NULL, -1);
293
294 #ifdef HAVE_VASPRINTF
295   len = _g_vasprintf (string, format, args);
296   if (len < 0)
297     *string = NULL;
298   else if (!g_mem_is_system_malloc ()) 
299     {
300       gchar *string1 = g_strndup (*string, len);
301       free (*string);
302       *string = string1;
303     }
304 #else
305   {
306     va_list args2;
307
308     G_VA_COPY (args2, args);
309
310     *string = g_new (gchar, g_printf_string_upper_bound (format, args));
311
312     len = _g_vsprintf (*string, format, args2);
313     va_end (args2);
314   }
315 #endif
316
317   return len;
318 }
319
320
321