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