From 68cce01556c07acd1b6b0b3ef9ac76ecebb1e31c Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 31 Mar 2012 10:32:26 -0400 Subject: [PATCH] core: Add stream->variant helper, as well as clear macros for ptrarray/hashtable --- src/libotutil/ot-variant-utils.c | 45 ++++++++++++++++++++++++++++++++ src/libotutil/ot-variant-utils.h | 19 ++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/libotutil/ot-variant-utils.c b/src/libotutil/ot-variant-utils.c index cb616f35..73cadca1 100644 --- a/src/libotutil/ot-variant-utils.c +++ b/src/libotutil/ot-variant-utils.c @@ -88,6 +88,12 @@ ot_util_variant_take_ref (GVariant *variant) #endif } +/** + * Return in @out_variant the result of memory-mapping the entire + * contents of file @src. + * + * Note the returned @out_variant is not floating. + */ gboolean ot_util_variant_map (GFile *src, const GVariantType *type, @@ -121,3 +127,42 @@ ot_util_variant_map (GFile *src, g_mapped_file_unref (mfile); return ret; } + +/** + * Read all input from @src, allocating a new #GVariant from it into + * output variable @out_variant. @src will be closed as a result. + * + * Note the returned @out_variant is not floating. + */ +gboolean +ot_util_variant_from_stream (GInputStream *src, + const GVariantType *type, + gboolean trusted, + GVariant **out_variant, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + GMemoryOutputStream *data_stream = NULL; + GVariant *ret_variant = NULL; + + data_stream = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free); + + if (!g_output_stream_splice ((GOutputStream*)data_stream, src, + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, + cancellable, error)) + goto out; + + ret_variant = g_variant_new_from_data (type, g_memory_output_stream_get_data (data_stream), + g_memory_output_stream_get_data_size (data_stream), + trusted, (GDestroyNotify) g_object_unref, data_stream); + data_stream = NULL; /* Transfer ownership */ + g_variant_ref_sink (ret_variant); + + ret = TRUE; + ot_transfer_out_value (out_variant, &ret_variant); + out: + g_clear_object (&data_stream); + ot_clear_gvariant (&ret_variant); + return ret; +} diff --git a/src/libotutil/ot-variant-utils.h b/src/libotutil/ot-variant-utils.h index 69f31112..e443a749 100644 --- a/src/libotutil/ot-variant-utils.h +++ b/src/libotutil/ot-variant-utils.h @@ -33,6 +33,18 @@ G_BEGIN_DECLS *a_v = NULL; \ } while (0); +#define ot_clear_ptrarray(a_v) do { \ + if (*a_v) \ + g_ptr_array_unref (*a_v); \ + *a_v = NULL; \ + } while (0); + +#define ot_clear_hashtable(a_v) do { \ + if (*a_v) \ + g_hash_table_unref (*a_v); \ + *a_v = NULL; \ + } while (0); + GHashTable *ot_util_variant_asv_to_hash_table (GVariant *variant); GVariant * ot_util_variant_take_ref (GVariant *variant); @@ -47,6 +59,13 @@ gboolean ot_util_variant_map (GFile *src, GVariant **out_variant, GError **error); +gboolean ot_util_variant_from_stream (GInputStream *src, + const GVariantType *type, + gboolean trusted, + GVariant **out_variant, + GCancellable *cancellable, + GError **error); + G_END_DECLS #endif