From 2d0866cac82e3ab70922f8901f2e8e5bf6da6895 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 21 Aug 2015 15:12:46 +0200 Subject: [PATCH] gvdb: Add gvdb_table_get_content This is useful if you want to apply outstanding changes to a table withough immediately persisting it to disk. --- lib/gvdb/gvdb-builder.c | 32 ++++++++++++++++++++++++-------- lib/gvdb/gvdb-builder.h | 5 +++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/gvdb/gvdb-builder.c b/lib/gvdb/gvdb-builder.c index 90ea50b71..ded770862 100644 --- a/lib/gvdb/gvdb-builder.c +++ b/lib/gvdb/gvdb-builder.c @@ -499,23 +499,39 @@ file_builder_serialise (FileBuilder *fb, return result; } +GBytes * +gvdb_table_get_content (GHashTable *table, + gboolean byteswap) +{ + struct gvdb_pointer root; + FileBuilder *fb; + GString *str; + GBytes *res; + + fb = file_builder_new (byteswap); + file_builder_add_hash (fb, table, &root); + str = file_builder_serialise (fb, root); + + res = g_bytes_new_take (str->str, str->len); + g_string_free (str, FALSE); + + return res; +} + gboolean gvdb_table_write_contents (GHashTable *table, const gchar *filename, gboolean byteswap, GError **error) { - struct gvdb_pointer root; + GBytes *content; gboolean status; - FileBuilder *fb; - GString *str; - fb = file_builder_new (byteswap); - file_builder_add_hash (fb, table, &root); - str = file_builder_serialise (fb, root); + content = gvdb_table_get_content (table, byteswap); - status = g_file_set_contents (filename, str->str, str->len, error); - g_string_free (str, TRUE); + status = g_file_set_contents (filename, g_bytes_get_data (content, NULL), g_bytes_get_size (content), error); + + g_bytes_unref (content); return status; } diff --git a/lib/gvdb/gvdb-builder.h b/lib/gvdb/gvdb-builder.h index 8ec05c8be..ad361dc62 100644 --- a/lib/gvdb/gvdb-builder.h +++ b/lib/gvdb/gvdb-builder.h @@ -52,4 +52,9 @@ gboolean gvdb_table_write_contents (GHashTa gboolean byteswap, GError **error); +G_GNUC_INTERNAL +GBytes * gvdb_table_get_content (GHashTable *table, + gboolean byteswap); + + #endif /* __gvdb_builder_h__ */