Blame glib/gprintf.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997, 2002  Peter Mattis, Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdarg.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
#include "gprintf.h"
Packit ae235b
#include "gprintfint.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_printf:
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @...: the arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard printf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * As with the standard printf(), this does not automatically append a trailing
Packit ae235b
 * new-line character to the message, so typically @format should end with its
Packit ae235b
 * own new-line character.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_printf (gchar const *format,
Packit ae235b
	  ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = g_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_fprintf:
Packit ae235b
 * @file: (not nullable): the stream to write to.
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @...: the arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard fprintf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_fprintf (FILE        *file, 
Packit ae235b
           gchar const *format,
Packit ae235b
	   ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = g_vfprintf (file, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_sprintf:
Packit ae235b
 * @string: A pointer to a memory buffer to contain the resulting string. It
Packit ae235b
 *          is up to the caller to ensure that the allocated buffer is large
Packit ae235b
 *          enough to hold the formatted result
Packit ae235b
 * @format: a standard printf() format string, but notice
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @...: the arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard sprintf() function which supports
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * Note that it is usually better to use g_snprintf(), to avoid the
Packit ae235b
 * risk of buffer overflow.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * See also g_strdup_printf().
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_sprintf (gchar       *string,
Packit ae235b
	   gchar const *format,
Packit ae235b
	   ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = g_vsprintf (string, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_snprintf:
Packit ae235b
 * @string: the buffer to hold the output.
Packit ae235b
 * @n: the maximum number of bytes to produce (including the
Packit ae235b
 *     terminating nul character).
Packit ae235b
 * @format: a standard printf() format string, but notice
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @...: the arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * A safer form of the standard sprintf() function. The output is guaranteed
Packit ae235b
 * to not exceed @n characters (including the terminating nul character), so
Packit ae235b
 * it is easy to ensure that a buffer overflow cannot occur.
Packit ae235b
 *
Packit ae235b
 * See also g_strdup_printf().
Packit ae235b
 *
Packit ae235b
 * In versions of GLib prior to 1.2.3, this function may return -1 if the
Packit ae235b
 * output was truncated, and the truncated string may not be nul-terminated.
Packit ae235b
 * In versions prior to 1.3.12, this function returns the length of the output
Packit ae235b
 * string.
Packit ae235b
 *
Packit ae235b
 * The return value of g_snprintf() conforms to the snprintf()
Packit ae235b
 * function as standardized in ISO C99. Note that this is different from
Packit ae235b
 * traditional snprintf(), which returns the length of the output string.
Packit ae235b
 *
Packit ae235b
 * The format string may contain positional parameters, as specified in
Packit ae235b
 * the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes which would be produced if the buffer 
Packit ae235b
 *     was large enough.
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_snprintf (gchar	*string,
Packit ae235b
	    gulong	 n,
Packit ae235b
	    gchar const *format,
Packit ae235b
	    ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  retval = g_vsnprintf (string, n, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_vprintf:
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @args: the list of arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard vprintf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_vprintf (gchar const *format,
Packit ae235b
	   va_list      args)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (format != NULL, -1);
Packit ae235b
Packit ae235b
  return _g_vprintf (format, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_vfprintf:
Packit ae235b
 * @file: (not nullable): the stream to write to.
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @args: the list of arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard fprintf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_vfprintf (FILE        *file,
Packit ae235b
            gchar const *format,
Packit ae235b
	    va_list      args)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (format != NULL, -1);
Packit ae235b
Packit ae235b
  return _g_vfprintf (file, format, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_vsprintf:
Packit ae235b
 * @string: the buffer to hold the output.
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @args: the list of arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the standard vsprintf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_vsprintf (gchar	 *string,
Packit ae235b
	    gchar const *format,
Packit ae235b
	    va_list      args)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (string != NULL, -1);
Packit ae235b
  g_return_val_if_fail (format != NULL, -1);
Packit ae235b
Packit ae235b
  return _g_vsprintf (string, format, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/** 
Packit ae235b
 * g_vsnprintf:
Packit ae235b
 * @string: the buffer to hold the output.
Packit ae235b
 * @n: the maximum number of bytes to produce (including the 
Packit ae235b
 *     terminating nul character).
Packit ae235b
 * @format: a standard printf() format string, but notice 
Packit ae235b
 *          string precision pitfalls][string-precision]
Packit ae235b
 * @args: the list of arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * A safer form of the standard vsprintf() function. The output is guaranteed
Packit ae235b
 * to not exceed @n characters (including the terminating nul character), so 
Packit ae235b
 * it is easy to ensure that a buffer overflow cannot occur.
Packit ae235b
 *
Packit ae235b
 * See also g_strdup_vprintf().
Packit ae235b
 *
Packit ae235b
 * In versions of GLib prior to 1.2.3, this function may return -1 if the 
Packit ae235b
 * output was truncated, and the truncated string may not be nul-terminated.
Packit ae235b
 * In versions prior to 1.3.12, this function returns the length of the output 
Packit ae235b
 * string.
Packit ae235b
 *
Packit ae235b
 * The return value of g_vsnprintf() conforms to the vsnprintf() function 
Packit ae235b
 * as standardized in ISO C99. Note that this is different from traditional 
Packit ae235b
 * vsnprintf(), which returns the length of the output string.
Packit ae235b
 *
Packit ae235b
 * The format string may contain positional parameters, as specified in 
Packit ae235b
 * the Single Unix Specification.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes which would be produced if the buffer 
Packit ae235b
 *  was large enough.
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_vsnprintf (gchar	 *string,
Packit ae235b
	     gulong	  n,
Packit ae235b
	     gchar const *format,
Packit ae235b
	     va_list      args)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (n == 0 || string != NULL, -1);
Packit ae235b
  g_return_val_if_fail (format != NULL, -1);
Packit ae235b
Packit ae235b
  return _g_vsnprintf (string, n, format, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_vasprintf:
Packit ae235b
 * @string: the return location for the newly-allocated string.
Packit ae235b
 * @format: a standard printf() format string, but notice
Packit ae235b
 *          [string precision pitfalls][string-precision]
Packit ae235b
 * @args: the list of arguments to insert in the output.
Packit ae235b
 *
Packit ae235b
 * An implementation of the GNU vasprintf() function which supports 
Packit ae235b
 * positional parameters, as specified in the Single Unix Specification.
Packit ae235b
 * This function is similar to g_vsprintf(), except that it allocates a 
Packit ae235b
 * string to hold the output, instead of putting the output in a buffer 
Packit ae235b
 * you allocate in advance.
Packit ae235b
 *
Packit ae235b
 * `glib/gprintf.h` must be explicitly included in order to use this function.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes printed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 **/
Packit ae235b
gint 
Packit ae235b
g_vasprintf (gchar      **string,
Packit ae235b
	     gchar const *format,
Packit ae235b
	     va_list      args)
Packit ae235b
{
Packit ae235b
  gint len;
Packit ae235b
  g_return_val_if_fail (string != NULL, -1);
Packit ae235b
Packit ae235b
#if !defined(HAVE_GOOD_PRINTF)
Packit ae235b
Packit ae235b
  len = _g_gnulib_vasprintf (string, format, args);
Packit ae235b
  if (len < 0)
Packit ae235b
    *string = NULL;
Packit ae235b
Packit ae235b
#elif defined (HAVE_VASPRINTF)
Packit ae235b
Packit ae235b
  len = vasprintf (string, format, args);
Packit ae235b
  if (len < 0)
Packit ae235b
    *string = NULL;
Packit ae235b
Packit ae235b
#else
Packit ae235b
Packit ae235b
  {
Packit ae235b
    va_list args2;
Packit ae235b
Packit ae235b
    G_VA_COPY (args2, args);
Packit ae235b
Packit ae235b
    *string = g_new (gchar, g_printf_string_upper_bound (format, args));
Packit ae235b
Packit ae235b
    len = _g_vsprintf (*string, format, args2);
Packit ae235b
    va_end (args2);
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return len;
Packit ae235b
}