* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().

        * tests/strfunc-test.c: Add some tests for g_snprintf().
This commit is contained in:
Matthias Clasen 2001-12-12 22:05:55 +00:00
parent 3fa33317b7
commit a99e75d0a8
12 changed files with 123 additions and 60 deletions

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,11 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
This fixes #60543:
* glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
* tests/strfunc-test.c: Add some tests for g_snprintf().
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/gconvert.c, glib/grand.c, glib/ghash.c,

View File

@ -1,3 +1,7 @@
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/tmpl/string_utils.sgml: Correct docs for g_[v]snprintf.
2001-12-12 Matthias Clasen <matthiasc@poet.de>
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,

View File

@ -173,17 +173,28 @@ See also g_strdup_printf().
<note>
<para>
In versions of GLib prior to 1.2.3, this function may return -1 if the output
was truncated, and the truncated string may not be nul-terminated.
was truncated, and the truncated string may not be nul-terminated.
In versions prior to 1.3.12, this function returns the length of the output
string.
</para>
</note>
<note>
<para>
The return value of g_snprintf() conforms to the <function>snprintf()</function>
function as standardized in ISO C99. Note that this is different from
traditional <function>snprintf()</function>, which returns the length of
the output string.
</para>
</note>
@string: the buffer to hold the output.
@n: the maximum number of characters to produce (including the terminating null
@n: the maximum number of characters to produce (including the terminating nul
character).
@format: the format string. See the <function>sprintf()</function>
@format: the format string. See the <function>sprintf()</function>.
documentation.
@Varargs: the arguments to insert in the output.
@Returns: the length of the output string.
@Returns: the number of characters which would be produced if the buffer was
large enough.
<!-- ##### FUNCTION g_vsnprintf ##### -->
@ -200,16 +211,27 @@ See also g_strdup_vprintf().
<para>
In versions of GLib prior to 1.2.3, this function may return -1 if the output
was truncated, and the truncated string may not be nul-terminated.
In versions prior to 1.3.12, this function returns the length of the output
string.
</para>
</note>
<note>
<para>
The return value of g_vsnprintf() conforms to the <function>vsnprintf()</function>
function as standardized in ISO C99. Note that this is different from
traditional <function>vsnprintf()</function>, which returns the length of
the output string.
</para>
</note>
@string: the buffer to hold the output.
@n: the maximum number of characters to produce (including the terminating null
@n: the maximum number of characters to produce (including the terminating nul
character).
@format: the format string. See the <function>sprintf()</function>
documentation.
@args: the list of arguments to insert in the output.
@Returns: the length of the output string.
@Returns: the number of characters which would be produced if the buffer was
large enough.
<!-- ##### FUNCTION g_printf_string_upper_bound ##### -->

View File

@ -341,44 +341,14 @@ g_snprintf (gchar *str,
gchar const *fmt,
...)
{
#ifdef HAVE_VSNPRINTF
va_list args;
gint retval;
g_return_val_if_fail (str != NULL, 0);
g_return_val_if_fail (n > 0, 0);
g_return_val_if_fail (fmt != NULL, 0);
va_start (args, fmt);
retval = vsnprintf (str, n, fmt, args);
retval = g_vsnprintf (str, n, fmt, args);
va_end (args);
if (retval < 0)
{
str[n-1] = '\0';
retval = strlen (str);
}
return retval;
#else /* !HAVE_VSNPRINTF */
gchar *printed;
va_list args;
g_return_val_if_fail (str != NULL, 0);
g_return_val_if_fail (n > 0, 0);
g_return_val_if_fail (fmt != NULL, 0);
va_start (args, fmt);
printed = g_strdup_vprintf (fmt, args);
va_end (args);
strncpy (str, printed, n);
str[n-1] = '\0';
g_free (printed);
return strlen (str);
#endif /* !HAVE_VSNPRINTF */
}
gint
@ -387,37 +357,30 @@ g_vsnprintf (gchar *str,
gchar const *fmt,
va_list args)
{
#ifdef HAVE_VSNPRINTF
gint retval;
g_return_val_if_fail (str != NULL, 0);
g_return_val_if_fail (n > 0, 0);
#ifdef HAVE_VSNPRINTF_C99
g_return_val_if_fail (n == 0 || str != NULL, 0);
g_return_val_if_fail (fmt != NULL, 0);
retval = vsnprintf (str, n, fmt, args);
if (retval < 0)
{
str[n-1] = '\0';
retval = strlen (str);
}
return retval;
#else /* !HAVE_VSNPRINTF */
return vsnprintf (str, n, fmt, args);
#else /* !HAVE_VSNPRINTF_C99 */
gchar *printed;
g_return_val_if_fail (str != NULL, 0);
g_return_val_if_fail (n > 0, 0);
gint retval;
g_return_val_if_fail (n == 0 || str != NULL, 0);
g_return_val_if_fail (fmt != NULL, 0);
printed = g_strdup_vprintf (fmt, args);
strncpy (str, printed, n);
str[n-1] = '\0';
retval = strlen (printed);
if (n > 0)
{
strncpy (str, printed, n - 1);
str[n-1] = '\0';
}
g_free (printed);
return strlen (str);
#endif /* !HAVE_VSNPRINTF */
return retval;
#endif /* !HAVE_VSNPRINTF_C99 */
}
guint

View File

@ -383,5 +383,15 @@ main (int argc,
#undef S
{
gchar buf[5];
TEST (NULL, 3 == g_snprintf (buf, 0, "%s", "abc"));
TEST (NULL, 3 == g_snprintf (NULL,0, "%s", "abc"));
TEST (NULL, 3 == g_snprintf (buf, 5, "%s", "abc"));
TEST (NULL, 4 == g_snprintf (buf, 5, "%s", "abcd"));
TEST (NULL, 9 == g_snprintf (buf, 5, "%s", "abcdefghi"));
}
return any_failed;
}