/* xml-doc.c generated by valac 0.36.12.13-3ae08, the Vala compiler
 * generated from xml-doc.vala, do not modify */

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

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


#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;
typedef struct _Block13Data Block13Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _xmlXPathFreeContext0(var) ((var == NULL) ? NULL : (var = (xmlXPathFreeContext (var), NULL)))

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

struct _GamesXmlDocClass {
	GObjectClass parent_class;
};

struct _GamesXmlDocPrivate {
	xmlDoc* doc;
};

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

typedef void (*GamesXmlDocNodeCallback) (xmlNode* node, void* user_data);

static gpointer games_xml_doc_parent_class = NULL;

GType games_xml_doc_get_type (void) G_GNUC_CONST;
#define GAMES_XML_DOC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_XML_DOC, GamesXmlDocPrivate))
enum  {
	GAMES_XML_DOC_0_PROPERTY
};
GamesXmlDoc* games_xml_doc_new_from_data (guint8* data, int data_length1, GError** error);
GamesXmlDoc* games_xml_doc_construct_from_data (GType object_type, guint8* data, int data_length1, GError** error);
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);
gchar** games_xml_doc_get_contents (GamesXmlDoc* self, const gchar* xpath, xmlNode* current_node, int* result_length1);
static Block13Data* block13_data_ref (Block13Data* _data13_);
static void block13_data_unref (void * _userdata_);
static void games_xml_doc_foreach_node (GamesXmlDoc* self, const gchar* xpath, xmlNode* from_node, GamesXmlDocNodeCallback callback, void* callback_target);
static void __lambda6_ (Block13Data* _data13_, xmlNode* node);
static void _vala_array_add24 (gchar** * array, int* length, int* size, gchar* value);
static void ___lambda6__games_xml_doc_node_callback (xmlNode* node, gpointer self);
static gchar** _vala_array_dup17 (gchar** self, int length);
gint games_xml_doc_count_nodes (GamesXmlDoc* self, const gchar* xpath, xmlNode* from_node);
GamesXmlDoc* games_xml_doc_new (void);
GamesXmlDoc* games_xml_doc_construct (GType object_type);
static void games_xml_doc_finalize (GObject * obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


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


GamesXmlDoc* games_xml_doc_new_from_data (guint8* data, int 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) {
	gchar* result = NULL;
	xmlNode* node = NULL;
	const gchar* _tmp0_;
	xmlNode* _tmp1_;
	xmlNode* _tmp2_;
	xmlNode* _tmp3_;
	xmlNode* _tmp4_;
	gchar* _tmp5_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (xpath != NULL, NULL);
	_tmp0_ = xpath;
	_tmp1_ = current_node;
	_tmp2_ = games_xml_doc_get_node (self, _tmp0_, _tmp1_);
	node = _tmp2_;
	_tmp3_ = node;
	if (_tmp3_ == NULL) {
		result = NULL;
		return result;
	}
	_tmp4_ = node;
	_tmp5_ = (gchar*) xmlNodeGetContent (_tmp4_);
	result = _tmp5_;
	return result;
}


static Block13Data* block13_data_ref (Block13Data* _data13_) {
	g_atomic_int_inc (&_data13_->_ref_count_);
	return _data13_;
}


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


static void _vala_array_add24 (gchar** * array, int* length, int* 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 __lambda6_ (Block13Data* _data13_, xmlNode* node) {
	GamesXmlDoc* self;
	xmlNode* _tmp0_;
	gchar* _tmp1_;
	self = _data13_->self;
	_tmp0_ = node;
	_tmp1_ = (gchar*) xmlNodeGetContent (_tmp0_);
	_vala_array_add24 (&_data13_->contents, &_data13_->contents_length1, &_data13_->_contents_size_, _tmp1_);
}


static void ___lambda6__games_xml_doc_node_callback (xmlNode* node, gpointer self) {
	__lambda6_ (self, node);
}


static gchar** _vala_array_dup17 (gchar** self, int length) {
	gchar** result;
	int 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;
}


gchar** games_xml_doc_get_contents (GamesXmlDoc* self, const gchar* xpath, xmlNode* current_node, int* result_length1) {
	gchar** result = NULL;
	Block13Data* _data13_;
	gchar** _tmp0_;
	const gchar* _tmp1_;
	xmlNode* _tmp2_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	gchar** _tmp4_;
	gint _tmp4__length1;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (xpath != NULL, NULL);
	_data13_ = g_slice_new0 (Block13Data);
	_data13_->_ref_count_ = 1;
	_data13_->self = g_object_ref (self);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	_data13_->contents = _tmp0_;
	_data13_->contents_length1 = 0;
	_data13_->_contents_size_ = _data13_->contents_length1;
	_tmp1_ = xpath;
	_tmp2_ = current_node;
	games_xml_doc_foreach_node (self, _tmp1_, _tmp2_, ___lambda6__games_xml_doc_node_callback, _data13_);
	_tmp3_ = (_data13_->contents != NULL) ? _vala_array_dup17 (_data13_->contents, _data13_->contents_length1) : ((gpointer) _data13_->contents);
	_tmp3__length1 = _data13_->contents_length1;
	_tmp4_ = _tmp3_;
	_tmp4__length1 = _tmp3__length1;
	if (result_length1) {
		*result_length1 = _tmp4__length1;
	}
	result = _tmp4_;
	block13_data_unref (_data13_);
	_data13_ = NULL;
	return result;
}


gint games_xml_doc_count_nodes (GamesXmlDoc* self, const gchar* xpath, xmlNode* from_node) {
	gint result = 0;
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlNode* _tmp2_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp5_;
	const gchar* _tmp6_;
	xmlXPathObject* _tmp7_;
	xmlXPathObject* _tmp8_;
	xmlNodeSet* _tmp9_;
	gint count = 0;
	xmlXPathObject* _tmp10_;
	xmlNodeSet* _tmp11_;
	gint _tmp12_;
	xmlXPathObject* _tmp13_;
	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_;
	_tmp2_ = from_node;
	if (_tmp2_ != NULL) {
		xmlXPathContext* _tmp3_;
		xmlNode* _tmp4_;
		_tmp3_ = ctx;
		_tmp4_ = from_node;
		_tmp3_->node = _tmp4_;
	}
	_tmp5_ = ctx;
	_tmp6_ = xpath;
	_tmp7_ = xmlXPathEvalExpression ((xmlChar*) _tmp6_, _tmp5_);
	obj = _tmp7_;
	_tmp8_ = obj;
	_tmp9_ = _tmp8_->nodesetval;
	if (_tmp9_ == NULL) {
		result = 0;
		_xmlXPathFreeContext0 (ctx);
		return result;
	}
	_tmp10_ = obj;
	_tmp11_ = _tmp10_->nodesetval;
	_tmp12_ = xmlXPathNodeSetGetLength (_tmp11_);
	count = _tmp12_;
	_tmp13_ = obj;
	xmlXPathFreeObject (_tmp13_);
	result = count;
	_xmlXPathFreeContext0 (ctx);
	return result;
}


static xmlNode* games_xml_doc_get_node (GamesXmlDoc* self, const gchar* xpath, xmlNode* from_node) {
	xmlNode* result = NULL;
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlNode* _tmp2_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp5_;
	const gchar* _tmp6_;
	xmlXPathObject* _tmp7_;
	xmlXPathObject* _tmp8_;
	xmlNodeSet* _tmp9_;
	xmlNode* first_node = NULL;
	xmlXPathObject* _tmp10_;
	xmlNodeSet* _tmp11_;
	xmlNode* _tmp12_;
	xmlXPathObject* _tmp13_;
	xmlNode* _tmp14_;
	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_;
	_tmp2_ = from_node;
	if (_tmp2_ != NULL) {
		xmlXPathContext* _tmp3_;
		xmlNode* _tmp4_;
		_tmp3_ = ctx;
		_tmp4_ = from_node;
		_tmp3_->node = _tmp4_;
	}
	_tmp5_ = ctx;
	_tmp6_ = xpath;
	_tmp7_ = xmlXPathEvalExpression ((xmlChar*) _tmp6_, _tmp5_);
	obj = _tmp7_;
	_tmp8_ = obj;
	_tmp9_ = _tmp8_->nodesetval;
	if (_tmp9_ == NULL) {
		result = NULL;
		_xmlXPathFreeContext0 (ctx);
		return result;
	}
	_tmp10_ = obj;
	_tmp11_ = _tmp10_->nodesetval;
	_tmp12_ = xmlXPathNodeSetItem (_tmp11_, 0);
	first_node = _tmp12_;
	_tmp13_ = obj;
	xmlXPathFreeObject (_tmp13_);
	_tmp14_ = first_node;
	result = _tmp14_;
	_xmlXPathFreeContext0 (ctx);
	return result;
}


static void games_xml_doc_foreach_node (GamesXmlDoc* self, const gchar* xpath, xmlNode* from_node, GamesXmlDocNodeCallback callback, void* callback_target) {
	xmlXPathContext* ctx = NULL;
	xmlDoc* _tmp0_;
	xmlXPathContext* _tmp1_;
	xmlNode* _tmp2_;
	xmlXPathObject* obj = NULL;
	xmlXPathContext* _tmp5_;
	const gchar* _tmp6_;
	xmlXPathObject* _tmp7_;
	xmlXPathObject* _tmp8_;
	xmlNodeSet* _tmp9_;
	xmlXPathObject* _tmp21_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (xpath != NULL);
	_tmp0_ = self->priv->doc;
	_tmp1_ = xmlXPathNewContext (_tmp0_);
	ctx = _tmp1_;
	_tmp2_ = from_node;
	if (_tmp2_ != NULL) {
		xmlXPathContext* _tmp3_;
		xmlNode* _tmp4_;
		_tmp3_ = ctx;
		_tmp4_ = from_node;
		_tmp3_->node = _tmp4_;
	}
	_tmp5_ = ctx;
	_tmp6_ = xpath;
	_tmp7_ = xmlXPathEvalExpression ((xmlChar*) _tmp6_, _tmp5_);
	obj = _tmp7_;
	_tmp8_ = obj;
	_tmp9_ = _tmp8_->nodesetval;
	if (_tmp9_ == NULL) {
		_xmlXPathFreeContext0 (ctx);
		return;
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp10_ = FALSE;
			_tmp10_ = TRUE;
			while (TRUE) {
				gint _tmp12_;
				xmlXPathObject* _tmp13_;
				xmlNodeSet* _tmp14_;
				gint _tmp15_;
				GamesXmlDocNodeCallback _tmp16_;
				void* _tmp16__target;
				xmlXPathObject* _tmp17_;
				xmlNodeSet* _tmp18_;
				gint _tmp19_;
				xmlNode* _tmp20_;
				if (!_tmp10_) {
					gint _tmp11_;
					_tmp11_ = i;
					i = _tmp11_ + 1;
				}
				_tmp10_ = FALSE;
				_tmp12_ = i;
				_tmp13_ = obj;
				_tmp14_ = _tmp13_->nodesetval;
				_tmp15_ = xmlXPathNodeSetGetLength (_tmp14_);
				if (!(_tmp12_ < _tmp15_)) {
					break;
				}
				_tmp16_ = callback;
				_tmp16__target = callback_target;
				_tmp17_ = obj;
				_tmp18_ = _tmp17_->nodesetval;
				_tmp19_ = i;
				_tmp20_ = xmlXPathNodeSetItem (_tmp18_, _tmp19_);
				_tmp16_ (_tmp20_, _tmp16__target);
			}
		}
	}
	_tmp21_ = obj;
	xmlXPathFreeObject (_tmp21_);
	_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) {
	games_xml_doc_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesXmlDocPrivate));
	G_OBJECT_CLASS (klass)->finalize = games_xml_doc_finalize;
}


static void games_xml_doc_instance_init (GamesXmlDoc * self) {
	self->priv = GAMES_XML_DOC_GET_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;
		xmlFreeDoc (_tmp1_);
	}
	G_OBJECT_CLASS (games_xml_doc_parent_class)->finalize (obj);
}


GType games_xml_doc_get_type (void) {
	static volatile gsize games_xml_doc_type_id__volatile = 0;
	if (g_once_init_enter (&games_xml_doc_type_id__volatile)) {
		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);
		g_once_init_leave (&games_xml_doc_type_id__volatile, games_xml_doc_type_id);
	}
	return games_xml_doc_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int 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, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



