Skip to content

Commit

Permalink
trivial: Split out XbSiloNode to a new source file
Browse files Browse the repository at this point in the history
Add some accessors for XbSiloNode so that we can easily experiment with
silo format changes without changing dozens of source files.

Also, rename some internal symbols for consistency.
  • Loading branch information
hughsie authored and pwithnall committed Jan 25, 2021
1 parent 64a968a commit 5efecd0
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 140 deletions.
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ libxmlb = library(
'xb-query-context.c',
'xb-silo.c',
'xb-silo-export.c',
'xb-silo-node.c',
'xb-silo-query.c',
'xb-stack.c',
'xb-string.c',
Expand Down Expand Up @@ -218,6 +219,7 @@ if get_option('tests')
'xb-query-context.c',
'xb-silo.c',
'xb-silo-export.c',
'xb-silo-node.c',
'xb-silo-query.c',
'xb-stack.c',
'xb-string.c',
Expand Down
8 changes: 4 additions & 4 deletions src/xb-builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ static void
xb_builder_nodetab_write_sentinel (XbBuilderNodetabHelper *helper)
{
XbSiloNode sn = {
.is_node = FALSE,
.nr_attrs = 0,
.flags = XB_SILO_NODE_FLAG_NONE,
.attr_count = 0,
};
// g_debug ("SENT @%u", (guint) helper->buf->len);
XB_SILO_APPENDBUF (helper->buf, &sn, xb_silo_node_get_size (&sn));
Expand All @@ -447,8 +447,8 @@ xb_builder_nodetab_write_node (XbBuilderNodetabHelper *helper, XbBuilderNode *bn
{
GPtrArray *attrs = xb_builder_node_get_attrs (bn);
XbSiloNode sn = {
.is_node = TRUE,
.nr_attrs = (attrs != NULL) ? attrs->len : 0,
.flags = XB_SILO_NODE_FLAG_IS_ELEMENT,
.attr_count = (attrs != NULL) ? attrs->len : 0,
.element_name = xb_builder_node_get_element_idx (bn),
.next = 0x0,
.parent = 0x0,
Expand Down
4 changes: 2 additions & 2 deletions src/xb-node-query.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ xb_node_query_text (XbNode *self, const gchar *xpath, GError **error)
return NULL;
sn = g_ptr_array_index (results, 0);

tmp = xb_silo_node_get_text (silo, sn);
tmp = xb_silo_get_node_text (silo, sn);
if (tmp == NULL) {
g_set_error_literal (error,
G_IO_ERROR,
Expand Down Expand Up @@ -274,7 +274,7 @@ xb_node_query_attr (XbNode *self, const gchar *xpath, const gchar *name, GError
return NULL;
sn = g_ptr_array_index (results, 0);

a = xb_silo_node_get_attr_by_str (silo, sn, name);
a = xb_silo_get_node_attr_by_str (silo, sn, name);
if (a == NULL) {
g_set_error_literal (error,
G_IO_ERROR,
Expand Down
28 changes: 14 additions & 14 deletions src/xb-node.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ xb_node_get_root (XbNode *self)

g_return_val_if_fail (XB_IS_NODE (self), NULL);

sn = xb_silo_get_sroot (priv->silo);
sn = xb_silo_get_root_node (priv->silo);
if (sn == NULL)
return NULL;
return xb_silo_node_create (priv->silo, sn, FALSE);
return xb_silo_create_node (priv->silo, sn, FALSE);
}

/**
Expand All @@ -153,10 +153,10 @@ xb_node_get_parent (XbNode *self)

g_return_val_if_fail (XB_IS_NODE (self), NULL);

sn = xb_silo_node_get_parent (priv->silo, priv->sn);
sn = xb_silo_get_parent_node (priv->silo, priv->sn);
if (sn == NULL)
return NULL;
return xb_silo_node_create (priv->silo, sn, FALSE);
return xb_silo_create_node (priv->silo, sn, FALSE);
}

/**
Expand All @@ -177,10 +177,10 @@ xb_node_get_next (XbNode *self)

g_return_val_if_fail (XB_IS_NODE (self), NULL);

sn = xb_silo_node_get_next (priv->silo, priv->sn);
sn = xb_silo_get_next_node (priv->silo, priv->sn);
if (sn == NULL)
return NULL;
return xb_silo_node_create (priv->silo, sn, FALSE);
return xb_silo_create_node (priv->silo, sn, FALSE);
}

/**
Expand All @@ -201,10 +201,10 @@ xb_node_get_child (XbNode *self)

g_return_val_if_fail (XB_IS_NODE (self), NULL);

sn = xb_silo_node_get_child (priv->silo, priv->sn);
sn = xb_silo_get_child_node (priv->silo, priv->sn);
if (sn == NULL)
return NULL;
return xb_silo_node_create (priv->silo, sn, FALSE);
return xb_silo_create_node (priv->silo, sn, FALSE);
}

/**
Expand Down Expand Up @@ -247,7 +247,7 @@ xb_node_get_text (XbNode *self)
{
XbNodePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (XB_IS_NODE (self), NULL);
return xb_silo_node_get_text (priv->silo, priv->sn);
return xb_silo_get_node_text (priv->silo, priv->sn);
}

/**
Expand All @@ -268,7 +268,7 @@ xb_node_get_text_as_uint (XbNode *self)

g_return_val_if_fail (XB_IS_NODE (self), G_MAXUINT64);

tmp = xb_silo_node_get_text (priv->silo, priv->sn);;
tmp = xb_silo_get_node_text (priv->silo, priv->sn);;
if (tmp == NULL)
return G_MAXUINT64;
if (g_str_has_prefix (tmp, "0x"))
Expand All @@ -291,7 +291,7 @@ xb_node_get_tail (XbNode *self)
{
XbNodePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (XB_IS_NODE (self), NULL);
return xb_silo_node_get_tail (priv->silo, priv->sn);
return xb_silo_get_node_tail (priv->silo, priv->sn);
}

/**
Expand All @@ -309,7 +309,7 @@ xb_node_get_element (XbNode *self)
{
XbNodePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (XB_IS_NODE (self), NULL);
return xb_silo_node_get_element (priv->silo, priv->sn);
return xb_silo_get_node_element (priv->silo, priv->sn);
}

/**
Expand All @@ -332,7 +332,7 @@ xb_node_get_attr (XbNode *self, const gchar *name)
g_return_val_if_fail (XB_IS_NODE (self), NULL);
g_return_val_if_fail (name != NULL, NULL);

a = xb_silo_node_get_attr_by_str (priv->silo, priv->sn, name);
a = xb_silo_get_node_attr_by_str (priv->silo, priv->sn, name);
if (a == NULL)
return NULL;
return xb_silo_from_strtab (priv->silo, a->attr_value);
Expand Down Expand Up @@ -380,7 +380,7 @@ xb_node_get_depth (XbNode *self)
{
XbNodePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (XB_IS_NODE (self), 0);
return xb_silo_node_get_depth (priv->silo, priv->sn);
return xb_silo_get_node_depth (priv->silo, priv->sn);
}

/**
Expand Down
31 changes: 16 additions & 15 deletions src/xb-silo-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "xb-node-private.h"
#include "xb-silo-export-private.h"
#include "xb-silo-private.h"
#include "xb-silo-node.h"
#include "xb-string-private.h"

typedef struct {
Expand All @@ -38,22 +38,22 @@ xb_silo_export_node (XbSilo *self, XbSiloExportHelper *helper, XbSiloNode *sn, G
xb_silo_from_strtab (self, sn->element_name));

/* add any attributes */
for (guint8 i = 0; i < sn->nr_attrs; i++) {
XbSiloNodeAttr *a = xb_silo_get_attr (self, helper->off, i);
for (guint8 i = 0; i < xb_silo_node_get_attr_count (sn); i++) {
XbSiloNodeAttr *a = xb_silo_node_get_attr (sn, i);
g_autofree gchar *key = xb_string_xml_escape (xb_silo_from_strtab (self, a->attr_name));
g_autofree gchar *val = xb_string_xml_escape (xb_silo_from_strtab (self, a->attr_value));
g_string_append_printf (helper->xml, " %s=\"%s\"", key, val);
}

/* collapse open/close tags together if no text or children */
if (helper->flags & XB_NODE_EXPORT_FLAG_COLLAPSE_EMPTY &&
sn->text == XB_SILO_UNSET &&
xb_silo_node_get_child (self, sn) == NULL) {
xb_silo_node_get_text_idx (sn) == XB_SILO_UNSET &&
xb_silo_get_child_node (self, sn) == NULL) {
g_string_append (helper->xml, " />");
} else {
/* finish the opening tag and add any text if it exists */
if (sn->text != XB_SILO_UNSET) {
g_autofree gchar *text = xb_string_xml_escape (xb_silo_from_strtab (self, sn->text));
if (xb_silo_node_get_text_idx (sn) != XB_SILO_UNSET) {
g_autofree gchar *text = xb_string_xml_escape (xb_silo_get_node_text (self, sn));
g_string_append (helper->xml, ">");
g_string_append (helper->xml, text);
} else {
Expand All @@ -64,7 +64,8 @@ xb_silo_export_node (XbSilo *self, XbSiloExportHelper *helper, XbSiloNode *sn, G
helper->off += xb_silo_node_get_size (sn);

/* recurse deeper */
while (xb_silo_get_node(self, helper->off)->is_node) {
while (xb_silo_node_has_flag (xb_silo_get_node (self, helper->off),
XB_SILO_NODE_FLAG_IS_ELEMENT)) {
XbSiloNode *child = xb_silo_get_node (self, helper->off);
helper->level++;
if (!xb_silo_export_node (self, helper, child, error))
Expand All @@ -74,7 +75,7 @@ xb_silo_export_node (XbSilo *self, XbSiloExportHelper *helper, XbSiloNode *sn, G

/* check for the single byte sentinel */
sn2 = xb_silo_get_node (self, helper->off);
if (sn2->is_node) {
if (xb_silo_node_has_flag (sn2, XB_SILO_NODE_FLAG_IS_ELEMENT)) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
Expand All @@ -86,7 +87,7 @@ xb_silo_export_node (XbSilo *self, XbSiloExportHelper *helper, XbSiloNode *sn, G

/* add closing tag */
if ((helper->flags & XB_NODE_EXPORT_FLAG_FORMAT_INDENT) > 0 &&
sn->text == XB_SILO_UNSET) {
xb_silo_node_get_text_idx (sn) == XB_SILO_UNSET) {
for (guint i = 0; i < helper->level; i++)
g_string_append (helper->xml, " ");
}
Expand All @@ -95,8 +96,8 @@ xb_silo_export_node (XbSilo *self, XbSiloExportHelper *helper, XbSiloNode *sn, G
}

/* add any optional tail */
if (sn->tail != XB_SILO_UNSET) {
g_autofree gchar *tail = xb_string_xml_escape (xb_silo_from_strtab (self, sn->tail));
if (xb_silo_node_get_tail_idx (sn) != XB_SILO_UNSET) {
g_autofree gchar *tail = xb_string_xml_escape (xb_silo_get_node_tail (self, sn));
g_string_append (helper->xml, tail);
}

Expand Down Expand Up @@ -127,9 +128,9 @@ xb_silo_export_with_root (XbSilo *self, XbSiloNode *sroot, XbNodeExportFlags fla
if (sroot != NULL) {
sn = sroot;
if (sn != NULL && flags & XB_NODE_EXPORT_FLAG_ONLY_CHILDREN)
sn = xb_silo_node_get_child (self, sn);
sn = xb_silo_get_child_node (self, sn);
} else {
sn = xb_silo_get_sroot (self);
sn = xb_silo_get_root_node (self);
}

/* no root */
Expand All @@ -152,7 +153,7 @@ xb_silo_export_with_root (XbSilo *self, XbSiloNode *sroot, XbNodeExportFlags fla
}
if ((flags & XB_NODE_EXPORT_FLAG_INCLUDE_SIBLINGS) == 0)
break;
sn = xb_silo_node_get_next (self, sn);
sn = xb_silo_get_next_node (self, sn);
} while (sn != NULL);

/* success */
Expand Down
67 changes: 67 additions & 0 deletions src/xb-silo-node.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2021 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/

#define G_LOG_DOMAIN "XbSilo"

#include <string.h>

#include "xb-silo-node.h"

/* private */
guint32
xb_silo_node_get_size (XbSiloNode *self)
{
/* sentinel */
if ((self->flags & XB_SILO_NODE_FLAG_IS_ELEMENT) == 0)
return sizeof(guint8);

/* element */
return sizeof(XbSiloNode) + sizeof(XbSiloNodeAttr) * self->attr_count;
}

/* private */
guint8
xb_silo_node_get_flags (XbSiloNode *self)
{
return self->flags;
}

/* private */
gboolean
xb_silo_node_has_flag (XbSiloNode *self, XbSiloNodeFlag flag)
{
return (self->flags & flag) > 0;
}

/* private */
guint32
xb_silo_node_get_text_idx (XbSiloNode *self)
{
return self->text;
}

/* private */
guint32
xb_silo_node_get_tail_idx (XbSiloNode *self)
{
return self->tail;
}

/* private */
guint8
xb_silo_node_get_attr_count (XbSiloNode *self)
{
return self->attr_count;
}

/* private */
XbSiloNodeAttr *
xb_silo_node_get_attr (XbSiloNode *self, guint8 idx)
{
guint32 off = sizeof(XbSiloNode);
off += sizeof(XbSiloNodeAttr) * idx;
return (XbSiloNodeAttr *) (((guint8 *) self) + off);
}
41 changes: 41 additions & 0 deletions src/xb-silo-node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/

#pragma once

#include <glib-object.h>

#define XB_SILO_UNSET 0xffffffff

typedef enum {
XB_SILO_NODE_FLAG_NONE = 0,
XB_SILO_NODE_FLAG_IS_ELEMENT = 1 << 0,
} XbSiloNodeFlag;

typedef struct __attribute__ ((packed)) {
guint8 flags:1;
guint8 attr_count:7;
guint32 element_name; /* ONLY when is_node: from strtab */
guint32 parent; /* ONLY when is_node: from 0 */
guint32 next; /* ONLY when is_node: from 0 */
guint32 text; /* ONLY when is_node: from strtab */
guint32 tail; /* ONLY when is_node: from strtab */
} XbSiloNode;

typedef struct __attribute__ ((packed)) {
guint32 attr_name; /* from strtab */
guint32 attr_value; /* from strtab */
} XbSiloNodeAttr;

guint32 xb_silo_node_get_size (XbSiloNode *self);
guint32 xb_silo_node_get_text_idx (XbSiloNode *self);
guint32 xb_silo_node_get_tail_idx (XbSiloNode *self);
guint8 xb_silo_node_get_flags (XbSiloNode *self);
gboolean xb_silo_node_has_flag (XbSiloNode *self,
XbSiloNodeFlag flags);
guint8 xb_silo_node_get_attr_count (XbSiloNode *self);
XbSiloNodeAttr *xb_silo_node_get_attr (XbSiloNode *self,
guint8 idx);
Loading

0 comments on commit 5efecd0

Please sign in to comment.