[glib/new-gsettings] gvdb changes
- From: Ryan Lortie <ryanl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/new-gsettings] gvdb changes
- Date: Thu, 15 Apr 2010 20:36:40 +0000 (UTC)
commit d0b4953423b66b23a9fe91095b2cb8410f7a04e6
Author: Ryan Lortie <desrt desrt ca>
Date: Thu Apr 15 15:51:43 2010 -0400
gvdb changes
gio/gvdb/gvdb-builder.c | 52 +++++++++++++++++++++++++--------------------
gio/gvdb/gvdb-format.h | 2 +
gio/gvdb/gvdb-reader.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++
gio/gvdb/gvdb-reader.h | 5 ++++
4 files changed, 90 insertions(+), 23 deletions(-)
---
diff --git a/gio/gvdb/gvdb-builder.c b/gio/gvdb/gvdb-builder.c
index 290309c..45c5f4b 100644
--- a/gio/gvdb/gvdb-builder.c
+++ b/gio/gvdb/gvdb-builder.c
@@ -172,9 +172,6 @@ gvdb_item_set_parent (GvdbItem *item,
*node = item;
}
-
-
-
typedef struct
{
GvdbItem **buckets;
@@ -219,10 +216,9 @@ item_to_index (GvdbItem *item)
typedef struct
{
- GString *strings;
GQueue *chunks;
guint64 offset;
-
+ gboolean byteswap;
} FileBuilder;
typedef struct
@@ -264,7 +260,15 @@ file_builder_add_value (FileBuilder *fb,
gpointer data;
gsize size;
- variant = g_variant_new_variant (value);
+ if (fb->byteswap)
+ {
+ value = g_variant_byteswap (value);
+ variant = g_variant_new_variant (value);
+ g_variant_unref (value);
+ }
+ else
+ variant = g_variant_new_variant (value);
+
normal = g_variant_get_normal_form (variant);
g_variant_unref (variant);
@@ -283,19 +287,22 @@ file_builder_add_options (FileBuilder *fb,
gpointer data;
gsize size;
- if (options != NULL)
+ if (options)
{
- normal = g_variant_get_normal_form (options);
+ if (fb->byteswap)
+ {
+ options = g_variant_byteswap (options);
+ normal = g_variant_get_normal_form (options);
+ g_variant_unref (options);
+ }
+ else
+ normal = g_variant_get_normal_form (options);
+
size = g_variant_get_size (normal);
data = file_builder_allocate (fb, 8, size, pointer);
g_variant_store (normal, data);
g_variant_unref (normal);
}
- else
- {
- pointer->start = guint32_to_le (0);
- pointer->end = guint32_to_le (0);
- }
}
static void
@@ -451,30 +458,29 @@ file_builder_add_hash (FileBuilder *fb,
}
static FileBuilder *
-file_builder_new (void)
+file_builder_new (gboolean byteswap)
{
FileBuilder *builder;
builder = g_slice_new (FileBuilder);
- builder->strings = g_string_new ("");
builder->chunks = g_queue_new ();
builder->offset = sizeof (struct gvdb_header);
+ builder->byteswap = byteswap;
return builder;
}
static GString *
file_builder_serialise (FileBuilder *fb,
- struct gvdb_pointer root,
- gboolean byteswap)
+ struct gvdb_pointer root)
{
- GString *result = g_string_new (NULL);
struct gvdb_header header;
+ GString *result;
- if (byteswap)
+ if (fb->byteswap)
{
- header.signature[0] = GUINT32_TO_LE (GVDB_SIGNATURE0);
- header.signature[1] = GUINT32_TO_LE (GVDB_SIGNATURE1);
+ header.signature[0] = GVDB_SWAPPED_SIGNATURE0;
+ header.signature[1] = GVDB_SWAPPED_SIGNATURE1;
}
else
{
@@ -523,9 +529,9 @@ gvdb_table_write_contents (GHashTable *table,
FileBuilder *fb;
GString *str;
- fb = file_builder_new ();
+ fb = file_builder_new (byteswap);
file_builder_add_hash (fb, table, &root);
- str = file_builder_serialise (fb, root, byteswap);
+ str = file_builder_serialise (fb, root);
status = g_file_set_contents (filename, str->str, str->len, error);
g_string_free (str, TRUE);
diff --git a/gio/gvdb/gvdb-format.h b/gio/gvdb/gvdb-format.h
index e90a4bc..41c8254 100644
--- a/gio/gvdb/gvdb-format.h
+++ b/gio/gvdb/gvdb-format.h
@@ -83,5 +83,7 @@ static inline guint16 guint16_from_le (guint16_le value) {
#define GVDB_SIGNATURE0 1918981703
#define GVDB_SIGNATURE1 1953390953
+#define GVDB_SWAPPED_SIGNATURE0 GUINT32_SWAP_LE_BE (GVDB_SIGNATURE0)
+#define GVDB_SWAPPED_SIGNATURE1 GUINT32_SWAP_LE_BE (GVDB_SIGNATURE1)
#endif /* __gvdb_format_h__ */
diff --git a/gio/gvdb/gvdb-reader.c b/gio/gvdb/gvdb-reader.c
index 7fe3d46..9a7a8d5 100644
--- a/gio/gvdb/gvdb-reader.c
+++ b/gio/gvdb/gvdb-reader.c
@@ -31,6 +31,7 @@ struct _GvdbTable {
gsize size;
GMappedFile *mapped;
+ gboolean byteswapped;
gboolean trusted;
const guint32 *bloom_words;
@@ -165,6 +166,27 @@ gvdb_table_new (const gchar *filename,
if (sizeof (struct gvdb_header) <= file->size)
{
const struct gvdb_header *header = (gpointer) file->data;
+
+ if (header->signature[0] == GVDB_SIGNATURE0 &&
+ header->signature[1] == GVDB_SIGNATURE1 &&
+ guint32_from_le (header->version) == 0)
+ file->byteswapped = FALSE;
+
+ else if (header->signature[0] == GVDB_SWAPPED_SIGNATURE0 &&
+ header->signature[1] == GVDB_SWAPPED_SIGNATURE1 &&
+ guint32_from_le (header->version) == 0)
+ file->byteswapped = TRUE;
+
+ else
+ {
+ g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
+ "%s: invalid header", filename);
+ g_slice_free (GvdbTable, file);
+ g_mapped_file_unref (mapped);
+
+ return NULL;
+ }
+
gvdb_table_setup_root (file, &header->root);
}
@@ -329,6 +351,24 @@ gvdb_table_list (GvdbTable *file,
}
/**
+ * gvdb_table_has_value:
+ * @file: a #GvdbTable
+ * @key: a string
+ * @returns: %TRUE if @key is in the table
+ *
+ * Checks for a value named @key in @file.
+ *
+ * Note: this function does not consider non-value nodes (other hash
+ * tables, for example).
+ **/
+gboolean
+gvdb_table_has_value (GvdbTable *file,
+ const gchar *key)
+{
+ return gvdb_table_lookup (file, key, 'v') != NULL;
+}
+
+/**
* gvdb_table_get_value:
* @file: a #GvdbTable
* @key: a string
@@ -373,6 +413,13 @@ gvdb_table_get_value (GvdbTable *file,
value = g_variant_get_variant (variant);
g_variant_unref (variant);
+ if (file->byteswapped)
+ {
+ GVariant *tmp = g_variant_byteswap (value);
+ g_variant_unref (value);
+ value = tmp;
+ }
+
if (options != NULL)
{
data = gvdb_table_dereference (file, &item->options, 8, &size);
@@ -384,6 +431,13 @@ gvdb_table_get_value (GvdbTable *file,
(GDestroyNotify) g_mapped_file_unref,
g_mapped_file_ref (file->mapped));
g_variant_ref_sink (*options);
+
+ if (file->byteswapped)
+ {
+ GVariant *tmp = g_variant_byteswap (*options);
+ g_variant_unref (*options);
+ *options = tmp;
+ }
}
else
*options = NULL;
diff --git a/gio/gvdb/gvdb-reader.h b/gio/gvdb/gvdb-reader.h
index 03ef97a..0e4b79e 100644
--- a/gio/gvdb/gvdb-reader.h
+++ b/gio/gvdb/gvdb-reader.h
@@ -46,4 +46,9 @@ GVariant * gvdb_table_get_value (GvdbTab
const gchar *key,
GVariant **options);
+G_GNUC_INTERNAL
+gboolean gvdb_table_has_value (GvdbTable *table,
+ const gchar *key);
+
+
#endif /* __gvdb_reader_h__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]