4bd47a34fd9200f34f909ff4db431ca20bad24a1
[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 #include "config.h"
21
22 #define _GNU_SOURCE             /* For vasprintf */
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include "glib.h"
29 #include "gprintf.h"
30 #include "gprintfint.h"
31
32 #include "galias.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  **/
47 gint
48 g_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  **/
75 gint
76 g_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: A pointer to a memory buffer to contain the resulting string. It 
93  *          is up to the caller to ensure that the allocated buffer is large 
94  *          enough to hold the formatted result
95  * @format: a standard printf() format string, but notice 
96  *          <link linkend="string-precision">string precision pitfalls</link>.
97  * @Varargs: the arguments to insert in the output.
98  *
99  * An implementation of the standard sprintf() function which supports 
100  * positional parameters, as specified in the Single Unix Specification.
101  *
102  * Returns: the number of characters printed.
103  *
104  * Since: 2.2
105  **/
106 gint
107 g_sprintf (gchar       *string,
108            gchar const *format,
109            ...)
110 {
111   va_list args;
112   gint retval;
113
114   va_start (args, format);
115   retval = g_vsprintf (string, format, args);
116   va_end (args);
117   
118   return retval;
119 }
120
121 /**
122  * g_snprintf:
123  * @string: the buffer to hold the output.
124  * @n: the maximum number of characters to produce (including the 
125  *     terminating nul character).
126  * @format: a standard printf() format string, but notice 
127  *          <link linkend="string-precision">string precision pitfalls</link>.
128  * @Varargs: the arguments to insert in the output.
129  *
130  * A safer form of the standard sprintf() function. The output is guaranteed
131  * to not exceed @n characters (including the terminating nul character), so 
132  * it is easy to ensure that a buffer overflow cannot occur.
133  * 
134  * See also g_strdup_printf().
135  *
136  * In versions of GLib prior to 1.2.3, this function may return -1 if the 
137  * output was truncated, and the truncated string may not be nul-terminated. 
138  * In versions prior to 1.3.12, this function returns the length of the output 
139  * string.
140  *
141  * The return value of g_snprintf() conforms to the snprintf()
142  * function as standardized in ISO C99. Note that this is different from 
143  * traditional snprintf(), which returns the length of the output string.
144  *
145  * The format string may contain positional parameters, as specified in 
146  * the Single Unix Specification.
147  *
148  * Returns: the number of bytes which would be produced if the buffer 
149  *     was large enough.
150  **/
151 gint
152 g_snprintf (gchar       *string,
153             gulong       n,
154             gchar const *format,
155             ...)
156 {
157   va_list args;
158   gint retval;
159
160   va_start (args, format);
161   retval = g_vsnprintf (string, n, format, args);
162   va_end (args);
163   
164   return retval;
165 }
166
167 /**
168  * g_vprintf:
169  * @format: a standard printf() format string, but notice 
170  *          <link linkend="string-precision">string precision pitfalls</link>.
171  * @args: the list of arguments to insert in the output.
172  *
173  * An implementation of the standard vprintf() function which supports 
174  * positional parameters, as specified in the Single Unix Specification.
175  *
176  * Returns: the number of characters printed.
177  *
178  * Since: 2.2
179  **/
180 gint
181 g_vprintf (gchar const *format,
182            va_list      args)
183 {
184   g_return_val_if_fail (format != NULL, -1);
185
186   return _g_vprintf (format, args);
187 }
188
189 /**
190  * g_vfprintf:
191  * @file: the stream to write to.
192  * @format: a standard printf() format string, but notice 
193  *          <link linkend="string-precision">string precision pitfalls</link>.
194  * @args: the list of arguments to insert in the output.
195  *
196  * An implementation of the standard fprintf() function which supports 
197  * positional parameters, as specified in the Single Unix Specification.
198  *
199  * Returns: the number of characters printed.
200  *
201  * Since: 2.2
202  **/
203 gint
204 g_vfprintf (FILE        *file,
205             gchar const *format,
206             va_list      args)
207 {
208   g_return_val_if_fail (format != NULL, -1);
209
210   return _g_vfprintf (file, format, args);
211 }
212
213 /**
214  * g_vsprintf:
215  * @string: the buffer to hold the output.
216  * @format: a standard printf() format string, but notice 
217  *          <link linkend="string-precision">string precision pitfalls</link>.
218  * @args: the list of arguments to insert in the output.
219  *
220  * An implementation of the standard vsprintf() function which supports 
221  * positional parameters, as specified in the Single Unix Specification.
222  *
223  * Returns: the number of characters printed.
224  *
225  * Since: 2.2
226  **/
227 gint
228 g_vsprintf (gchar        *string,
229             gchar const *format,
230             va_list      args)
231 {
232   g_return_val_if_fail (string != NULL, -1);
233   g_return_val_if_fail (format != NULL, -1);
234
235   return _g_vsprintf (string, format, args);
236 }
237
238 /** 
239  * g_vsnprintf:
240  * @string: the buffer to hold the output.
241  * @n: the maximum number of characters to produce (including the 
242  *     terminating nul character).
243  * @format: a standard printf() format string, but notice 
244  *          <link linkend="string-precision">string precision pitfalls</link>.
245  * @args: the list of arguments to insert in the output.
246  *
247  * A safer form of the standard vsprintf() function. The output is guaranteed
248  * to not exceed @n characters (including the terminating nul character), so 
249  * it is easy to ensure that a buffer overflow cannot occur.
250  *
251  * See also g_strdup_vprintf().
252  *
253  * In versions of GLib prior to 1.2.3, this function may return -1 if the 
254  * output was truncated, and the truncated string may not be nul-terminated.
255  * In versions prior to 1.3.12, this function returns the length of the output 
256  * string.
257  *
258  * The return value of g_vsnprintf() conforms to the vsnprintf() function 
259  * as standardized in ISO C99. Note that this is different from traditional 
260  * vsnprintf(), which returns the length of the output string.
261  *
262  * The format string may contain positional parameters, as specified in 
263  * the Single Unix Specification.
264  *
265  * Returns: the number of characters which would be produced if the buffer 
266  *  was large enough.
267  */
268 gint
269 g_vsnprintf (gchar       *string,
270              gulong       n,
271              gchar const *format,
272              va_list      args)
273 {
274   g_return_val_if_fail (n == 0 || string != NULL, -1);
275   g_return_val_if_fail (format != NULL, -1);
276
277   return _g_vsnprintf (string, n, format, args);
278 }
279
280 /**
281  * g_vasprintf:
282  * @string: the return location for the newly-allocated string.
283  * @format: a standard printf() format string, but notice
284  *          <link linkend="string-precision">string precision pitfalls</link>.
285  * @args: the list of arguments to insert in the output.
286  *
287  * An implementation of the GNU vasprintf() function which supports 
288  * positional parameters, as specified in the Single Unix Specification.
289  * This function is similar to g_vsprintf(), except that it allocates a 
290  * string to hold the output, instead of putting the output in a buffer 
291  * you allocate in advance.
292  *
293  * Returns: the number of characters printed.
294  *
295  * Since: 2.4
296  **/
297 gint 
298 g_vasprintf (gchar      **string,
299              gchar const *format,
300              va_list      args)
301 {
302   gint len;
303   g_return_val_if_fail (string != NULL, -1);
304
305 #if !defined(HAVE_GOOD_PRINTF)
306
307   len = _g_gnulib_vasprintf (string, format, args);
308   if (len < 0)
309     *string = NULL;
310
311 #elif defined (HAVE_VASPRINTF)
312
313   len = vasprintf (string, format, args);
314   if (len < 0)
315     *string = NULL;
316   else if (!g_mem_is_system_malloc ()) 
317     {
318       /* vasprintf returns malloc-allocated memory */
319       gchar *string1 = g_strndup (*string, len);
320       free (*string);
321       *string = string1;
322     }
323
324 #else
325
326   {
327     va_list args2;
328
329     G_VA_COPY (args2, args);
330
331     *string = g_new (gchar, g_printf_string_upper_bound (format, args));
332
333     len = _g_vsprintf (*string, format, args2);
334     va_end (args2);
335   }
336 #endif
337
338   return len;
339 }
340
341 #define __G_PRINTF_C__
342 #include "galiasdef.c"