From d86396f21fa164bd3cb5efc111ef50bd7065beec Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Mon, 25 Nov 2013 23:26:21 +0100 Subject: [PATCH] gio: add g_output_string_[v]printf() which are useful for porting FILE* based output code. --- gio/goutputstream.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ gio/goutputstream.h | 14 +++++++ 2 files changed, 118 insertions(+) diff --git a/gio/goutputstream.c b/gio/goutputstream.c index 492a212..059ff35 100644 --- a/gio/goutputstream.c +++ b/gio/goutputstream.c @@ -21,6 +21,7 @@ */ #include "config.h" +#include #include "goutputstream.h" #include "gcancellable.h" #include "gasyncresult.h" @@ -291,6 +292,109 @@ g_output_stream_write_all (GOutputStream *stream, } /** + * g_output_stream_printf: + * @stream: a #GOutputStream. + * @bytes_written: (out): location to store the number of bytes that was + * written to the stream + * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. + * @error: location to store the error occurring, or %NULL to ignore + * @format: the format string. See the printf() documentation + * @...: the parameters to insert into the format string + * + * This is a utility function around g_output_stream_write_all(). It + * uses g_strdup_vprintf() to turn @format and @... into a string that + * is then written to @stream. + * + * See the documentation of g_output_stream_write_all() about the + * behavior of the actual write operation. + * + * Note that partial writes cannot be properly checked with this + * function due to the variable length of the written string, if you + * need precise control over partial write failures, you need to + * create you own printf()-like wrapper around g_output_stream_write() + * or g_output_stream_write_all(). + * + * Since: 2.40 + * + * Return value: %TRUE on success, %FALSE if there was an error + **/ +gboolean +g_output_stream_printf (GOutputStream *stream, + gsize *bytes_written, + GCancellable *cancellable, + GError **error, + const gchar *format, + ...) +{ + va_list args; + gboolean success; + + g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + g_return_val_if_fail (format != NULL, FALSE); + + va_start (args, format); + success = g_output_stream_vprintf (stream, bytes_written, cancellable, + error, format, args); + va_end (args); + + return success; +} + +/** + * g_output_stream_vprintf: + * @stream: a #GOutputStream. + * @bytes_written: (out): location to store the number of bytes that was + * written to the stream + * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. + * @error: location to store the error occurring, or %NULL to ignore + * @format: the format string. See the printf() documentation + * @args: the parameters to insert into the format string + * + * This is a utility function around g_output_stream_write_all(). It + * uses g_strdup_vprintf() to turn @format and @args into a string that + * is then written to @stream. + * + * See the documentation of g_output_stream_write_all() about the + * behavior of the actual write operation. + * + * Note that partial writes cannot be properly checked with this + * function due to the variable length of the written string, if you + * need precise control over partial write failures, you need to + * create you own printf()-like wrapper around g_output_stream_write() + * or g_output_stream_write_all(). + * + * Since: 2.40 + * + * Return value: %TRUE on success, %FALSE if there was an error + **/ +gboolean +g_output_stream_vprintf (GOutputStream *stream, + gsize *bytes_written, + GCancellable *cancellable, + GError **error, + const gchar *format, + va_list args) +{ + gchar *text; + gboolean success; + + g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + g_return_val_if_fail (format != NULL, FALSE); + + text = g_strdup_vprintf (format, args); + success = g_output_stream_write_all (stream, + text, strlen (text), + bytes_written, cancellable, error); + g_free (text); + + return success; +} + +/** * g_output_stream_write_bytes: * @stream: a #GOutputStream. * @bytes: the #GBytes to write diff --git a/gio/goutputstream.h b/gio/goutputstream.h index dd2cbad..1bc5659 100644 --- a/gio/goutputstream.h +++ b/gio/goutputstream.h @@ -149,6 +149,20 @@ gboolean g_output_stream_write_all (GOutputStream *stream, gsize *bytes_written, GCancellable *cancellable, GError **error); +GLIB_AVAILABLE_IN_2_40 +gboolean g_output_stream_printf (GOutputStream *stream, + gsize *bytes_written, + GCancellable *cancellable, + GError **error, + const gchar *format, + ...) G_GNUC_PRINTF (5, 6); +GLIB_AVAILABLE_IN_2_40 +gboolean g_output_stream_vprintf (GOutputStream *stream, + gsize *bytes_written, + GCancellable *cancellable, + GError **error, + const gchar *format, + va_list args) G_GNUC_PRINTF (5, 0); GLIB_AVAILABLE_IN_2_34 gssize g_output_stream_write_bytes (GOutputStream *stream, GBytes *bytes, -- 2.7.4