/* xml-doc.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from xml-doc.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib-object.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/xpath.h>
#include "gnome-games.h"

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define GAMES_TYPE_XML_DOC (games_xml_doc_get_type ())
#define GAMES_XML_DOC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_XML_DOC, GamesXmlDoc))
#define GAMES_XML_DOC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_XML_DOC, GamesXmlDocClass))
#define GAMES_IS_XML_DOC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_XML_DOC))
#define GAMES_IS_XML_DOC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_XML_DOC))
#define GAMES_XML_DOC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_XML_DOC, GamesXmlDocClass))

typedef struct _GamesXmlDoc GamesXmlDoc;
typedef struct _GamesXmlDocClass GamesXmlDocClass;
typedef struct _GamesXmlDocPrivate GamesXmlDocPrivate;
enum  {
	GAMES_XML_DOC_0_PROPERTY,
	GAMES_XML_DOC_NUM_PROPERTIES
};
static GParamSpec* games_xml_doc_properties[GAMES_XML_DOC_NUM_PROPERTIES];
#define _xmlFreeDoc0(var) ((var == NULL) ? NULL : (var = (xmlFreeDoc (var), NULL)))
typedef struct _Block33Data Block33Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef void (*GamesXmlDocNodeCallback) (xmlNode* node, gpointer user_data);
#define _xmlXPathFreeContext0(var) ((var == NULL) ? NULL : (var = (xmlXPathFreeContext (var), NULL)))
#define _xmlXPathFreeObject0(var) ((var == NULL) ? NULL : (var = (xmlXPathFreeObject (var), NULL)))

struct _GamesXmlDoc {
	GObject parent_instance;
	GamesXmlDocPrivate * priv;
};

struct _GamesXmlDocClass {
	GObjectClass parent_class;
};

struct _GamesXmlDocPrivate {
	xmlDoc* doc;
};

struct _Block33Data {
	int _ref_count_;
	GamesXmlDoc* self;
	gchar** contents;
	gint contents_length1;
	gint _contents_size_;
};

static gint GamesXmlDoc_private_offset;
static gpointer games_xml_doc_parent_class = NULL;

VALA_EXTERN GType games_xml_doc_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesXmlDoc* games_xml_doc_new_from_data (guint8* data,
                                          gint data_length1,
                                          GError** error);
VALA_EXTERN GamesXmlDoc* games_xml_doc_construct_from_data (GType object_type,
                                                guint8* data,
                                                gint data_length1,
                                                GError** error);
VALA_EXTERN gchar* games_xml_doc_get_content (GamesXmlDoc* self,
                                  const gchar* xpath,
                                  xmlNode* current_node);
static xmlNode* games_xml_doc_get_node (GamesXmlDoc* self,
                                 const gchar* xpath,
                                 xmlNode* from_node);
VALA_EXTERN gchar** games_xml_doc_get_contents (GamesXmlDoc* self,
                                    const gchar* xpath,
                                    xmlNode* current_node,
                                    gint* result_length1);
static Block33Data* block33_data_ref (Block33Data* _data33_);
static void block33_data_unref (void * _userdata_);
static void games_xml_doc_foreach_node (GamesXmlDoc* self,
                                 const gchar* xpath,
                                 xmlNode* from_node,
                                 GamesXmlDocNodeCallback callback,
                                 gpointer callback_target);
static void __lambda78_ (Block33Data* _data33_,
                  xmlNode* node);
static void _vala_array_add48 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void ___lambda78__games_xml_doc_node_callback (xmlNode* node,
                                               gpointer self);
static gchar** _vala_array_dup33 (gchar** self,
                           gssize length);
VALA_EXTERN gint games_xml_doc_count_nodes (GamesXmlDoc* self,
                                const gchar* xpath,
                                xmlNode* from_node);
VALA_EXTERN GamesXmlDoc* games_xml_doc_new (void);
VALA_EXTERN GamesXmlDoc* games_xml_doc_construct (GType object_type);
static void games_xml_doc_finalize (GObject * obj);
static GType games_xml_doc_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
games_xml_doc_get_instance_private (GamesXmlDoc* self)
{
	return G_STRUCT_MEMBER_P (self, GamesXmlDoc_private_offset);
}

GamesXmlDoc*
games_xml_doc_construct_from_data (GType object_type,
                                   guint8* data,
                                   gint data_length1,
                                   GError** error)
{
	GamesXmlDoc * self = NULL;
	xmlDoc* _tmp0_;
	self = (GamesXmlDoc*) g_object_new (object_type, NULL);
	_tmp0_ = xmlParseMemory ((const gchar*) data, data_length1);
	self->priv->doc = _tmp0_;
	return self;
}

GamesXmlDoc*
games_xml_doc_new_from_data (guint8* data,
                             gint data_length1,
                             GError** error)
{
	return games_xml_doc_construct_from_data (GAMES_TYPE_XML_DOC, data, data_length1, error);
}

gchar*
games_xml_doc_get_content (GamesXmlDoc* self,
                           const gchar* xpath,
                           xmlNode* current_node)
{
	xmlNode* node = NULL;
	xmlNode* _tmp0_;
	xmlNode* _tmp1_;
	xmlNode* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (xpath != NULL, NULL);
	_tmp0_ = games_xml_doc_get_node (self, xpath, current_node);
	node = _tmp0_;
	_tmp1_ = node;
	if (_tmp1_ == NULL) {
		result = NULL;
		return result;
	}
	_tmp2_ = node;
	_tmp3_ = (gchar*) xmlNodeGetContent (_tmp2_);
	result = _tmp3_;
	return result;
}

static Block33Data*
block33_data_ref (Block33Data* _data33_)
{
	g_atomic_int_inc (&_data33_->_ref_count_);
	return _data33_;
}

static void
block33_data_unref (void * _userdata_)
{
	Block33Data* _data33_;
	_data33_ = (Block33Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data33_->_ref_count_)) {
		GamesXmlDoc* self;
		self = _data33_->self;
		_data33_->contents = (_vala_array_free (_data33_->contents, _data33_->contents_length1, (GDestroyNotify) g_free), NULL);
		_g_object_unref0 (self);
		g_slice_free (Block33Data, _data33_);
	}
}

static void
_vala_array_add48 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
__lambda78_ (Block33Data* _data33_,
             xmlNode* node)
{
	GamesXmlDoc* self;
	gchar* _tmp0_;
	self = _data33_->self;
	_tmp0_ = (gchar*) xmlNodeGetContent (node);
	_vala_array_add48 (&_data33_->contents, &_data33_->contents_length1, &_data33_->_contents_size_, _tmp0_);
}

static void
___lambda78__games_xml_doc_node_callback (xmlNode* node,
                                          gpointer self)
{
	__lambda78_ (self, node);
}

static gchar**
_vala_array_dup33 (gchar** self,
                   gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

gchar**
games_xml_doc_get_contents (GamesXmlDoc* self,
                            const gchar* xpath,
                            xmlNode* current_node,
                            gint* result_length1)
{
	Block33Data* _data33_;
	gchar** _tmp0_;
	gchar** _tmp1_;
	gint _tmp1__length1;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gchar** result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (xpath != NULL, NULL);
	_data33_ = g_slice_new0 (Block33Data);
	_data33_->_ref_count_ = 1;
	_data33_->self = g_object_ref (self);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	_data33_->contents = _tmp0_;
	_data33_->contents_length1 = 0;
	_data33_->_contents_size_ = _data33_->contents_length1;
	games_xml_doc_foreach_node (self, xpath, current_node, ___lambda78__games_xml_doc_node_callback, _data33_);
	_tmp1_ = (_data33_->contents != NULL) ? _vala_array_dup33 (_data33_->contents, _data33_->contents_length1) : _data33_->contents;
	_tmp1__length1 = _data33_->contents_length1;
	_tmp2_ = _tmp1_;
	_tmp2__length1 = _tmp1__length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	block33_data_unref (_data33_);
	_data33_ = NULL;
	return result;
}

gint
games_xml_doc_count_nodes (GamesXmlDoc* self,
                           const gchar* xpath,
                           xmlNode* from_node)
{
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp3_;
	xmlXPathObject* _tmp4_;
	xmlXPathObject* _tmp5_;
	xmlNodeSet* _tmp6_;
	gint count = 0;
	xmlXPathObject* _tmp7_;
	xmlNodeSet* _tmp8_;
	xmlXPathObject* _tmp9_;
	gint result;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (xpath != NULL, 0);
	_tmp0_ = self->priv->doc;
	_tmp1_ = xmlXPathNewContext (_tmp0_);
	ctx = _tmp1_;
	if (from_node != NULL) {
		xmlXPathContext* _tmp2_;
		_tmp2_ = ctx;
		_tmp2_->node = from_node;
	}
	_tmp3_ = ctx;
	_tmp4_ = xmlXPathEvalExpression ((xmlChar*) xpath, _tmp3_);
	obj = _tmp4_;
	_tmp5_ = obj;
	_tmp6_ = _tmp5_->nodesetval;
	if (_tmp6_ == NULL) {
		result = 0;
		_xmlXPathFreeContext0 (ctx);
		return result;
	}
	_tmp7_ = obj;
	_tmp8_ = _tmp7_->nodesetval;
	count = xmlXPathNodeSetGetLength (_tmp8_);
	_tmp9_ = obj;
	_xmlXPathFreeObject0 (_tmp9_);
	result = count;
	_xmlXPathFreeContext0 (ctx);
	return result;
}

static xmlNode*
games_xml_doc_get_node (GamesXmlDoc* self,
                        const gchar* xpath,
                        xmlNode* from_node)
{
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp3_;
	xmlXPathObject* _tmp4_;
	xmlXPathObject* _tmp5_;
	xmlNodeSet* _tmp6_;
	xmlNode* first_node = NULL;
	xmlXPathObject* _tmp7_;
	xmlNodeSet* _tmp8_;
	xmlNode* _tmp9_;
	xmlXPathObject* _tmp10_;
	xmlNode* _tmp11_;
	xmlNode* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (xpath != NULL, NULL);
	_tmp0_ = self->priv->doc;
	_tmp1_ = xmlXPathNewContext (_tmp0_);
	ctx = _tmp1_;
	if (from_node != NULL) {
		xmlXPathContext* _tmp2_;
		_tmp2_ = ctx;
		_tmp2_->node = from_node;
	}
	_tmp3_ = ctx;
	_tmp4_ = xmlXPathEvalExpression ((xmlChar*) xpath, _tmp3_);
	obj = _tmp4_;
	_tmp5_ = obj;
	_tmp6_ = _tmp5_->nodesetval;
	if (_tmp6_ == NULL) {
		result = NULL;
		_xmlXPathFreeContext0 (ctx);
		return result;
	}
	_tmp7_ = obj;
	_tmp8_ = _tmp7_->nodesetval;
	_tmp9_ = xmlXPathNodeSetItem (_tmp8_, 0);
	first_node = _tmp9_;
	_tmp10_ = obj;
	_xmlXPathFreeObject0 (_tmp10_);
	_tmp11_ = first_node;
	result = _tmp11_;
	_xmlXPathFreeContext0 (ctx);
	return result;
}

static void
games_xml_doc_foreach_node (GamesXmlDoc* self,
                            const gchar* xpath,
                            xmlNode* from_node,
                            GamesXmlDocNodeCallback callback,
                            gpointer callback_target)
{
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp3_;
	xmlXPathObject* _tmp4_;
	xmlXPathObject* _tmp5_;
	xmlNodeSet* _tmp6_;
	xmlXPathObject* _tmp14_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (xpath != NULL);
	_tmp0_ = self->priv->doc;
	_tmp1_ = xmlXPathNewContext (_tmp0_);
	ctx = _tmp1_;
	if (from_node != NULL) {
		xmlXPathContext* _tmp2_;
		_tmp2_ = ctx;
		_tmp2_->node = from_node;
	}
	_tmp3_ = ctx;
	_tmp4_ = xmlXPathEvalExpression ((xmlChar*) xpath, _tmp3_);
	obj = _tmp4_;
	_tmp5_ = obj;
	_tmp6_ = _tmp5_->nodesetval;
	if (_tmp6_ == NULL) {
		_xmlXPathFreeContext0 (ctx);
		return;
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp7_ = FALSE;
			_tmp7_ = TRUE;
			while (TRUE) {
				xmlXPathObject* _tmp9_;
				xmlNodeSet* _tmp10_;
				xmlXPathObject* _tmp11_;
				xmlNodeSet* _tmp12_;
				xmlNode* _tmp13_;
				if (!_tmp7_) {
					gint _tmp8_;
					_tmp8_ = i;
					i = _tmp8_ + 1;
				}
				_tmp7_ = FALSE;
				_tmp9_ = obj;
				_tmp10_ = _tmp9_->nodesetval;
				if (!(i < xmlXPathNodeSetGetLength (_tmp10_))) {
					break;
				}
				_tmp11_ = obj;
				_tmp12_ = _tmp11_->nodesetval;
				_tmp13_ = xmlXPathNodeSetItem (_tmp12_, i);
				callback (_tmp13_, callback_target);
			}
		}
	}
	_tmp14_ = obj;
	_xmlXPathFreeObject0 (_tmp14_);
	_xmlXPathFreeContext0 (ctx);
}

GamesXmlDoc*
games_xml_doc_construct (GType object_type)
{
	GamesXmlDoc * self = NULL;
	self = (GamesXmlDoc*) g_object_new (object_type, NULL);
	return self;
}

GamesXmlDoc*
games_xml_doc_new (void)
{
	return games_xml_doc_construct (GAMES_TYPE_XML_DOC);
}

static void
games_xml_doc_class_init (GamesXmlDocClass * klass,
                          gpointer klass_data)
{
	games_xml_doc_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesXmlDoc_private_offset);
	G_OBJECT_CLASS (klass)->finalize = games_xml_doc_finalize;
}

static void
games_xml_doc_instance_init (GamesXmlDoc * self,
                             gpointer klass)
{
	self->priv = games_xml_doc_get_instance_private (self);
}

static void
games_xml_doc_finalize (GObject * obj)
{
	GamesXmlDoc * self;
	xmlDoc* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_XML_DOC, GamesXmlDoc);
	_tmp0_ = self->priv->doc;
	if (_tmp0_ != NULL) {
		xmlDoc* _tmp1_;
		_tmp1_ = self->priv->doc;
		_xmlFreeDoc0 (_tmp1_);
	}
	G_OBJECT_CLASS (games_xml_doc_parent_class)->finalize (obj);
}

static GType
games_xml_doc_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesXmlDocClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_xml_doc_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesXmlDoc), 0, (GInstanceInitFunc) games_xml_doc_instance_init, NULL };
	GType games_xml_doc_type_id;
	games_xml_doc_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesXmlDoc", &g_define_type_info, 0);
	GamesXmlDoc_private_offset = g_type_add_instance_private (games_xml_doc_type_id, sizeof (GamesXmlDocPrivate));
	return games_xml_doc_type_id;
}

GType
games_xml_doc_get_type (void)
{
	static volatile gsize games_xml_doc_type_id__once = 0;
	if (g_once_init_enter (&games_xml_doc_type_id__once)) {
		GType games_xml_doc_type_id;
		games_xml_doc_type_id = games_xml_doc_get_type_once ();
		g_once_init_leave (&games_xml_doc_type_id__once, games_xml_doc_type_id);
	}
	return games_xml_doc_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

