/* NodeLinks.c generated by valac 0.56.19, the Vala compiler
 * generated from NodeLinks.vala, do not modify */

/*
* Copyright (c) 2018-2026 (https://github.com/phase1geo/Minder)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Trevor Williams <phase1geo@gmail.com>
*/

#include <glib-object.h>
#include <gee.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/tree.h>
#include <gobject/gvaluecollector.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_NODE_LINKS (node_links_get_type ())
#define NODE_LINKS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODE_LINKS, NodeLinks))
#define NODE_LINKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODE_LINKS, NodeLinksClass))
#define IS_NODE_LINKS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODE_LINKS))
#define IS_NODE_LINKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODE_LINKS))
#define NODE_LINKS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODE_LINKS, NodeLinksClass))

typedef struct _NodeLinks NodeLinks;
typedef struct _NodeLinksClass NodeLinksClass;
typedef struct _NodeLinksPrivate NodeLinksPrivate;

#define TYPE_NODE_LINK (node_link_get_type ())
#define NODE_LINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODE_LINK, NodeLink))
#define NODE_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODE_LINK, NodeLinkClass))
#define IS_NODE_LINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODE_LINK))
#define IS_NODE_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODE_LINK))
#define NODE_LINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODE_LINK, NodeLinkClass))

typedef struct _NodeLink NodeLink;
typedef struct _NodeLinkClass NodeLinkClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _node_link_unref0(var) ((var == NULL) ? NULL : (var = (node_link_unref (var), NULL)))
typedef struct _Block48Data Block48Data;
#define _node_links_unref0(var) ((var == NULL) ? NULL : (var = (node_links_unref (var), NULL)))
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_NODE (node_get_type ())
#define NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODE, Node))
#define NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODE, NodeClass))
#define IS_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODE))
#define IS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODE))
#define NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODE, NodeClass))

typedef struct _Node Node;
typedef struct _NodeClass NodeClass;

#define TYPE_CONNECTION (connection_get_type ())
#define CONNECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CONNECTION, Connection))
#define CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CONNECTION, ConnectionClass))
#define IS_CONNECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CONNECTION))
#define IS_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CONNECTION))
#define CONNECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CONNECTION, ConnectionClass))

typedef struct _Connection Connection;
typedef struct _ConnectionClass ConnectionClass;
typedef struct _Block49Data Block49Data;
typedef struct _ParamSpecNodeLinks ParamSpecNodeLinks;

struct _NodeLinks {
	GTypeInstance parent_instance;
	volatile int ref_count;
	NodeLinksPrivate * priv;
};

struct _NodeLinksClass {
	GTypeClass parent_class;
	void (*finalize) (NodeLinks *self);
};

struct _NodeLinksPrivate {
	GeeHashMap* _links;
	gint _id;
	GRegex* _link_re;
};

struct _Block48Data {
	int _ref_count_;
	NodeLinks* self;
	gint id;
	NodeLink* link;
};

struct _Block49Data {
	int _ref_count_;
	NodeLinks* self;
	xmlNode* node;
};

struct _ParamSpecNodeLinks {
	GParamSpec parent_instance;
};

static gint NodeLinks_private_offset;
static gpointer node_links_parent_class = NULL;

VALA_EXTERN gpointer node_links_ref (gpointer instance);
VALA_EXTERN void node_links_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_node_links (const gchar* name,
                                   const gchar* nick,
                                   const gchar* blurb,
                                   GType object_type,
                                   GParamFlags flags);
VALA_EXTERN void value_set_node_links (GValue* value,
                           gpointer v_object);
VALA_EXTERN void value_take_node_links (GValue* value,
                            gpointer v_object);
VALA_EXTERN gpointer value_get_node_links (const GValue* value);
VALA_EXTERN GType node_links_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (NodeLinks, node_links_unref)
VALA_EXTERN gpointer node_link_ref (gpointer instance);
VALA_EXTERN void node_link_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_node_link (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_node_link (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_node_link (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_node_link (const GValue* value);
VALA_EXTERN GType node_link_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (NodeLink, node_link_unref)
VALA_EXTERN NodeLinks* node_links_new (void);
VALA_EXTERN NodeLinks* node_links_construct (GType object_type);
VALA_EXTERN gint node_links_add_link (NodeLinks* self,
                          NodeLink* link);
static Block48Data* block48_data_ref (Block48Data* _data48_);
static void block48_data_unref (void * _userdata_);
static gboolean __lambda9_ (Block48Data* _data48_,
                     gint k,
                     NodeLink* v);
VALA_EXTERN gboolean node_link_matches (NodeLink* self,
                            NodeLink* other);
static gboolean ___lambda9__gee_forall_map_func (gconstpointer k,
                                          gconstpointer v,
                                          gpointer self);
VALA_EXTERN NodeLink* node_links_get_node_link (NodeLinks* self,
                                    gint id);
VALA_EXTERN gint node_links_num_links (NodeLinks* self);
static void node_links_get_links_from_note (NodeLinks* self,
                                     const gchar* note,
                                     NodeLinks* doc_links);
VALA_EXTERN GType node_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Node, g_object_unref)
VALA_EXTERN void node_links_get_links_from_node (NodeLinks* self,
                                     Node* node,
                                     NodeLinks* doc_links);
VALA_EXTERN const gchar* node_get_note (Node* self);
VALA_EXTERN GType connection_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Connection, g_object_unref)
VALA_EXTERN void node_links_get_links_from_connection (NodeLinks* self,
                                           Connection* conn,
                                           NodeLinks* doc_links);
VALA_EXTERN const gchar* connection_get_note (Connection* self);
VALA_EXTERN gchar* node_links_set_links_in_note (NodeLinks* self,
                                     const gchar* orig_note,
                                     NodeLinks* doc_links);
VALA_EXTERN void node_links_set_links_in_node (NodeLinks* self,
                                   Node* node,
                                   NodeLinks* doc_links);
VALA_EXTERN void node_set_note (Node* self,
                    const gchar* value);
VALA_EXTERN void node_links_set_links_in_connection (NodeLinks* self,
                                         Connection* conn,
                                         NodeLinks* doc_links);
VALA_EXTERN void connection_set_note (Connection* self,
                          const gchar* value);
VALA_EXTERN xmlNode* node_links_save (NodeLinks* self);
static Block49Data* block49_data_ref (Block49Data* _data49_);
static void block49_data_unref (void * _userdata_);
static gboolean __lambda10_ (Block49Data* _data49_,
                      gint k,
                      NodeLink* v);
VALA_EXTERN xmlNode* node_link_save (NodeLink* self);
static gboolean ___lambda10__gee_forall_map_func (gconstpointer k,
                                           gconstpointer v,
                                           gpointer self);
VALA_EXTERN void node_links_load (NodeLinks* self,
                      xmlNode* node);
VALA_EXTERN NodeLink* node_link_new_from_xml (xmlNode* node);
VALA_EXTERN NodeLink* node_link_construct_from_xml (GType object_type,
                                        xmlNode* node);
static void node_links_finalize (NodeLinks * obj);
static GType node_links_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
node_links_get_instance_private (NodeLinks* self)
{
	return G_STRUCT_MEMBER_P (self, NodeLinks_private_offset);
}

NodeLinks*
node_links_construct (GType object_type)
{
	NodeLinks* self = NULL;
	GeeHashMap* _tmp0_;
	GError* _inner_error0_ = NULL;
	self = (NodeLinks*) g_type_create_instance (object_type);
	_tmp0_ = gee_hash_map_new (G_TYPE_INT, NULL, NULL, TYPE_NODE_LINK, (GBoxedCopyFunc) node_link_ref, (GDestroyNotify) node_link_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->_links);
	self->priv->_links = _tmp0_;
	{
		GRegex* _tmp1_ = NULL;
		GRegex* _tmp2_;
		GRegex* _tmp3_;
		_tmp2_ = g_regex_new ("@Node-(\\d+)", 0, 0, &_inner_error0_);
		_tmp1_ = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp3_ = _tmp1_;
		_tmp1_ = NULL;
		_g_regex_unref0 (self->priv->_link_re);
		self->priv->_link_re = _tmp3_;
		_g_regex_unref0 (_tmp1_);
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		_g_regex_unref0 (self->priv->_link_re);
		self->priv->_link_re = NULL;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	return self;
}

NodeLinks*
node_links_new (void)
{
	return node_links_construct (TYPE_NODE_LINKS);
}

static gpointer
_node_link_ref0 (gpointer self)
{
	return self ? node_link_ref (self) : NULL;
}

static Block48Data*
block48_data_ref (Block48Data* _data48_)
{
	g_atomic_int_inc (&_data48_->_ref_count_);
	return _data48_;
}

static void
block48_data_unref (void * _userdata_)
{
	Block48Data* _data48_;
	_data48_ = (Block48Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data48_->_ref_count_)) {
		NodeLinks* self;
		self = _data48_->self;
		_node_link_unref0 (_data48_->link);
		_node_links_unref0 (self);
		g_slice_free (Block48Data, _data48_);
	}
}

static gboolean
__lambda9_ (Block48Data* _data48_,
            gint k,
            NodeLink* v)
{
	NodeLinks* self;
	gboolean result;
	self = _data48_->self;
	g_return_val_if_fail (v != NULL, FALSE);
	if (node_link_matches (v, _data48_->link)) {
		_data48_->id = k;
		result = FALSE;
		return result;
	}
	result = TRUE;
	return result;
}

static gboolean
___lambda9__gee_forall_map_func (gconstpointer k,
                                 gconstpointer v,
                                 gpointer self)
{
	gboolean result;
	result = __lambda9_ (self, (gint) ((gintptr) k), (NodeLink*) v);
	return result;
}

gint
node_links_add_link (NodeLinks* self,
                     NodeLink* link)
{
	Block48Data* _data48_;
	NodeLink* _tmp0_;
	GeeHashMap* _tmp1_;
	GeeMapIterator* _tmp2_;
	GeeMapIterator* _tmp3_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (link != NULL, -1);
	_data48_ = g_slice_new0 (Block48Data);
	_data48_->_ref_count_ = 1;
	_data48_->self = node_links_ref (self);
	_tmp0_ = _node_link_ref0 (link);
	_node_link_unref0 (_data48_->link);
	_data48_->link = _tmp0_;
	_data48_->id = -1;
	_tmp1_ = self->priv->_links;
	_tmp2_ = gee_abstract_map_map_iterator ((GeeAbstractMap*) _tmp1_);
	_tmp3_ = _tmp2_;
	gee_map_iterator_foreach (_tmp3_, ___lambda9__gee_forall_map_func, _data48_);
	_g_object_unref0 (_tmp3_);
	if (_data48_->id == -1) {
		gint _tmp4_;
		GeeHashMap* _tmp5_;
		_tmp4_ = self->priv->_id;
		self->priv->_id = _tmp4_ + 1;
		_data48_->id = _tmp4_;
		_tmp5_ = self->priv->_links;
		gee_abstract_map_set ((GeeAbstractMap*) _tmp5_, (gpointer) ((gintptr) _data48_->id), _data48_->link);
	}
	result = _data48_->id;
	block48_data_unref (_data48_);
	_data48_ = NULL;
	return result;
}

NodeLink*
node_links_get_node_link (NodeLinks* self,
                          gint id)
{
	GeeHashMap* _tmp0_;
	NodeLink* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_links;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp0_, (gpointer) ((gintptr) id))) {
		GeeHashMap* _tmp1_;
		gpointer _tmp2_;
		_tmp1_ = self->priv->_links;
		_tmp2_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp1_, (gpointer) ((gintptr) id));
		result = (NodeLink*) _tmp2_;
		return result;
	}
	result = NULL;
	return result;
}

gint
node_links_num_links (NodeLinks* self)
{
	GeeHashMap* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = self->priv->_links;
	_tmp1_ = gee_abstract_map_get_size ((GeeAbstractMap*) _tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

static gchar*
string_slice (const gchar* self,
              glong start,
              glong end)
{
	glong string_length = 0L;
	gint _tmp0_;
	gint _tmp1_;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	if (start < ((glong) 0)) {
		start = string_length + start;
	}
	if (end < ((glong) 0)) {
		end = string_length + end;
	}
	if (start >= ((glong) 0)) {
		_tmp2_ = start <= string_length;
	} else {
		_tmp2_ = FALSE;
	}
	g_return_val_if_fail (_tmp2_, NULL);
	if (end >= ((glong) 0)) {
		_tmp3_ = end <= string_length;
	} else {
		_tmp3_ = FALSE;
	}
	g_return_val_if_fail (_tmp3_, NULL);
	g_return_val_if_fail (start <= end, NULL);
	_tmp4_ = g_strndup (((gchar*) self) + start, (gsize) (end - start));
	result = _tmp4_;
	return result;
}

static void
node_links_get_links_from_note (NodeLinks* self,
                                const gchar* note,
                                NodeLinks* doc_links)
{
	GRegex* _tmp0_;
	GMatchInfo* match_info = NULL;
	gint start = 0;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (note != NULL);
	g_return_if_fail (doc_links != NULL);
	_tmp0_ = self->priv->_link_re;
	if (_tmp0_ == NULL) {
		return;
	}
	start = 0;
	{
		while (TRUE) {
			gboolean _tmp1_ = FALSE;
			GRegex* _tmp2_;
			GMatchInfo* _tmp3_ = NULL;
			gboolean _tmp4_;
			gint s = 0;
			gint e = 0;
			GMatchInfo* _tmp5_;
			gint _tmp6_ = 0;
			gint _tmp7_ = 0;
			gint id = 0;
			gchar* _tmp8_;
			gchar* _tmp9_;
			gint _tmp10_;
			NodeLink* link = NULL;
			NodeLink* _tmp11_;
			NodeLink* _tmp12_;
			_tmp2_ = self->priv->_link_re;
			_tmp4_ = g_regex_match_full (_tmp2_, note, (gssize) -1, start, 0, &_tmp3_, &_inner_error0_);
			_g_match_info_unref0 (match_info);
			match_info = _tmp3_;
			_tmp1_ = _tmp4_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				if (_inner_error0_->domain == G_REGEX_ERROR) {
					goto __catch0_g_regex_error;
				}
				_g_match_info_unref0 (match_info);
				g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return;
			}
			if (!_tmp1_) {
				break;
			}
			_tmp5_ = match_info;
			g_match_info_fetch_pos (_tmp5_, 1, &_tmp6_, &_tmp7_);
			s = _tmp6_;
			e = _tmp7_;
			_tmp8_ = string_slice (note, (glong) s, (glong) e);
			_tmp9_ = _tmp8_;
			_tmp10_ = atoi (_tmp9_);
			_g_free0 (_tmp9_);
			id = _tmp10_;
			_tmp11_ = node_links_get_node_link (doc_links, id);
			link = _tmp11_;
			_tmp12_ = link;
			if (_tmp12_ != NULL) {
				GeeHashMap* _tmp13_;
				NodeLink* _tmp14_;
				_tmp13_ = self->priv->_links;
				_tmp14_ = link;
				gee_abstract_map_set ((GeeAbstractMap*) _tmp13_, (gpointer) ((gintptr) id), _tmp14_);
			}
			start = e;
			_node_link_unref0 (link);
		}
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_match_info_unref0 (match_info);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_match_info_unref0 (match_info);
}

void
node_links_get_links_from_node (NodeLinks* self,
                                Node* node,
                                NodeLinks* doc_links)
{
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (node != NULL);
	g_return_if_fail (doc_links != NULL);
	_tmp0_ = node_get_note (node);
	_tmp1_ = _tmp0_;
	node_links_get_links_from_note (self, _tmp1_, doc_links);
}

void
node_links_get_links_from_connection (NodeLinks* self,
                                      Connection* conn,
                                      NodeLinks* doc_links)
{
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (conn != NULL);
	g_return_if_fail (doc_links != NULL);
	_tmp0_ = connection_get_note (conn);
	_tmp1_ = _tmp0_;
	node_links_get_links_from_note (self, _tmp1_, doc_links);
}

static gchar*
string_splice (const gchar* self,
               glong start,
               glong end,
               const gchar* str)
{
	glong string_length = 0L;
	gint _tmp0_;
	gint _tmp1_;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gsize str_size = 0UL;
	const gchar* _result_ = NULL;
	void* _tmp6_;
	gchar* dest = NULL;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp12_;
	const gchar* _tmp13_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	if (start < ((glong) 0)) {
		start = string_length + start;
	}
	if (end < ((glong) 0)) {
		end = string_length + end;
	}
	if (start >= ((glong) 0)) {
		_tmp2_ = start <= string_length;
	} else {
		_tmp2_ = FALSE;
	}
	g_return_val_if_fail (_tmp2_, NULL);
	if (end >= ((glong) 0)) {
		_tmp3_ = end <= string_length;
	} else {
		_tmp3_ = FALSE;
	}
	g_return_val_if_fail (_tmp3_, NULL);
	g_return_val_if_fail (start <= end, NULL);
	if (str == NULL) {
		str_size = (gsize) 0;
	} else {
		gint _tmp4_;
		gint _tmp5_;
		_tmp4_ = strlen ((const gchar*) str);
		_tmp5_ = _tmp4_;
		str_size = (gsize) _tmp5_;
	}
	_tmp6_ = g_malloc0 (((string_length - (end - start)) + str_size) + 1);
	_result_ = _tmp6_;
	_tmp7_ = _result_;
	dest = (gchar*) _tmp7_;
	_tmp8_ = dest;
	memcpy (_tmp8_, self, (gsize) start);
	_tmp9_ = dest;
	dest = _tmp9_ + start;
	if (str != NULL) {
		gchar* _tmp10_;
		gchar* _tmp11_;
		_tmp10_ = dest;
		memcpy (_tmp10_, str, str_size);
		_tmp11_ = dest;
		dest = _tmp11_ + str_size;
	}
	_tmp12_ = dest;
	memcpy (_tmp12_, ((gchar*) self) + end, (gsize) (string_length - end));
	_tmp13_ = _result_;
	_result_ = NULL;
	result = (gchar*) _tmp13_;
	return result;
}

gchar*
node_links_set_links_in_note (NodeLinks* self,
                              const gchar* orig_note,
                              NodeLinks* doc_links)
{
	gchar* note = NULL;
	gchar* _tmp0_;
	GRegex* _tmp1_;
	GMatchInfo* match_info = NULL;
	gint start = 0;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (orig_note != NULL, NULL);
	g_return_val_if_fail (doc_links != NULL, NULL);
	_tmp0_ = g_strdup (orig_note);
	note = _tmp0_;
	_tmp1_ = self->priv->_link_re;
	if (_tmp1_ == NULL) {
		result = NULL;
		_g_free0 (note);
		return result;
	}
	start = 0;
	{
		while (TRUE) {
			gboolean _tmp2_ = FALSE;
			GRegex* _tmp3_;
			const gchar* _tmp4_;
			GMatchInfo* _tmp5_ = NULL;
			gboolean _tmp6_;
			gint s = 0;
			gint e = 0;
			GMatchInfo* _tmp7_;
			gint _tmp8_ = 0;
			gint _tmp9_ = 0;
			gint id = 0;
			const gchar* _tmp10_;
			gchar* _tmp11_;
			gchar* _tmp12_;
			gint _tmp13_;
			NodeLink* link = NULL;
			NodeLink* _tmp14_;
			NodeLink* _tmp15_;
			_tmp3_ = self->priv->_link_re;
			_tmp4_ = note;
			_tmp6_ = g_regex_match_full (_tmp3_, _tmp4_, (gssize) -1, start, 0, &_tmp5_, &_inner_error0_);
			_g_match_info_unref0 (match_info);
			match_info = _tmp5_;
			_tmp2_ = _tmp6_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				if (_inner_error0_->domain == G_REGEX_ERROR) {
					goto __catch0_g_regex_error;
				}
				_g_match_info_unref0 (match_info);
				_g_free0 (note);
				g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return NULL;
			}
			if (!_tmp2_) {
				break;
			}
			_tmp7_ = match_info;
			g_match_info_fetch_pos (_tmp7_, 1, &_tmp8_, &_tmp9_);
			s = _tmp8_;
			e = _tmp9_;
			_tmp10_ = note;
			_tmp11_ = string_slice (_tmp10_, (glong) s, (glong) e);
			_tmp12_ = _tmp11_;
			_tmp13_ = atoi (_tmp12_);
			_g_free0 (_tmp12_);
			id = _tmp13_;
			_tmp14_ = node_links_get_node_link (self, id);
			link = _tmp14_;
			_tmp15_ = link;
			if (_tmp15_ != NULL) {
				NodeLink* _tmp16_;
				const gchar* _tmp17_;
				gchar* _tmp18_;
				gchar* _tmp19_;
				gchar* _tmp20_;
				_tmp16_ = link;
				id = node_links_add_link (doc_links, _tmp16_);
				_tmp17_ = note;
				_tmp18_ = g_strdup_printf ("%i", id);
				_tmp19_ = _tmp18_;
				_tmp20_ = string_splice (_tmp17_, (glong) s, (glong) e, _tmp19_);
				_g_free0 (note);
				note = _tmp20_;
				_g_free0 (_tmp19_);
			}
			start = e;
			_node_link_unref0 (link);
		}
		result = note;
		_g_match_info_unref0 (match_info);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		result = NULL;
		_g_match_info_unref0 (match_info);
		_g_free0 (note);
		return result;
	}
	__finally0:
	_g_match_info_unref0 (match_info);
	_g_free0 (note);
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

void
node_links_set_links_in_node (NodeLinks* self,
                              Node* node,
                              NodeLinks* doc_links)
{
	gchar* note = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (node != NULL);
	g_return_if_fail (doc_links != NULL);
	_tmp0_ = node_get_note (node);
	_tmp1_ = _tmp0_;
	_tmp2_ = node_links_set_links_in_note (self, _tmp1_, doc_links);
	note = _tmp2_;
	_tmp3_ = note;
	if (_tmp3_ != NULL) {
		const gchar* _tmp4_;
		_tmp4_ = note;
		node_set_note (node, _tmp4_);
	}
	_g_free0 (note);
}

void
node_links_set_links_in_connection (NodeLinks* self,
                                    Connection* conn,
                                    NodeLinks* doc_links)
{
	gchar* note = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (conn != NULL);
	g_return_if_fail (doc_links != NULL);
	_tmp0_ = connection_get_note (conn);
	_tmp1_ = _tmp0_;
	_tmp2_ = node_links_set_links_in_note (self, _tmp1_, doc_links);
	note = _tmp2_;
	_tmp3_ = note;
	if (_tmp3_ != NULL) {
		const gchar* _tmp4_;
		_tmp4_ = note;
		connection_set_note (conn, _tmp4_);
	}
	_g_free0 (note);
}

static Block49Data*
block49_data_ref (Block49Data* _data49_)
{
	g_atomic_int_inc (&_data49_->_ref_count_);
	return _data49_;
}

static void
block49_data_unref (void * _userdata_)
{
	Block49Data* _data49_;
	_data49_ = (Block49Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data49_->_ref_count_)) {
		NodeLinks* self;
		self = _data49_->self;
		_node_links_unref0 (self);
		g_slice_free (Block49Data, _data49_);
	}
}

static gboolean
__lambda10_ (Block49Data* _data49_,
             gint k,
             NodeLink* v)
{
	NodeLinks* self;
	xmlNode* link = NULL;
	xmlNode* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gboolean result;
	self = _data49_->self;
	g_return_val_if_fail (v != NULL, FALSE);
	_tmp0_ = node_link_save (v);
	link = _tmp0_;
	_tmp1_ = g_strdup_printf ("%i", k);
	_tmp2_ = _tmp1_;
	xmlSetProp (link, (xmlChar*) "doc-id", (xmlChar*) _tmp2_);
	_g_free0 (_tmp2_);
	xmlAddChild (_data49_->node, link);
	result = TRUE;
	return result;
}

static gboolean
___lambda10__gee_forall_map_func (gconstpointer k,
                                  gconstpointer v,
                                  gpointer self)
{
	gboolean result;
	result = __lambda10_ (self, (gint) ((gintptr) k), (NodeLink*) v);
	return result;
}

xmlNode*
node_links_save (NodeLinks* self)
{
	Block49Data* _data49_;
	xmlNode* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	GeeHashMap* _tmp3_;
	GeeMapIterator* _tmp4_;
	GeeMapIterator* _tmp5_;
	xmlNode* result;
	g_return_val_if_fail (self != NULL, NULL);
	_data49_ = g_slice_new0 (Block49Data);
	_data49_->_ref_count_ = 1;
	_data49_->self = node_links_ref (self);
	_tmp0_ = xmlNewNode (NULL, (xmlChar*) "nodelinks");
	_data49_->node = _tmp0_;
	_tmp1_ = g_strdup_printf ("%i", self->priv->_id);
	_tmp2_ = _tmp1_;
	xmlSetProp (_data49_->node, (xmlChar*) "id", (xmlChar*) _tmp2_);
	_g_free0 (_tmp2_);
	_tmp3_ = self->priv->_links;
	_tmp4_ = gee_abstract_map_map_iterator ((GeeAbstractMap*) _tmp3_);
	_tmp5_ = _tmp4_;
	gee_map_iterator_foreach (_tmp5_, ___lambda10__gee_forall_map_func, _data49_);
	_g_object_unref0 (_tmp5_);
	result = _data49_->node;
	block49_data_unref (_data49_);
	_data49_ = NULL;
	return result;
}

void
node_links_load (NodeLinks* self,
                 xmlNode* node)
{
	gchar* i = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = (gchar*) xmlGetProp (node, (xmlChar*) "id");
	i = _tmp0_;
	_tmp1_ = i;
	if (_tmp1_ != NULL) {
		const gchar* _tmp2_;
		_tmp2_ = i;
		self->priv->_id = atoi (_tmp2_);
	}
	{
		xmlNode* it = NULL;
		xmlNode* _tmp3_;
		_tmp3_ = node->children;
		it = _tmp3_;
		{
			gboolean _tmp4_ = FALSE;
			_tmp4_ = TRUE;
			while (TRUE) {
				xmlNode* _tmp7_;
				gboolean _tmp8_ = FALSE;
				xmlNode* _tmp9_;
				if (!_tmp4_) {
					xmlNode* _tmp5_;
					xmlNode* _tmp6_;
					_tmp5_ = it;
					_tmp6_ = _tmp5_->next;
					it = _tmp6_;
				}
				_tmp4_ = FALSE;
				_tmp7_ = it;
				if (!(_tmp7_ != NULL)) {
					break;
				}
				_tmp9_ = it;
				if (_tmp9_->type == XML_ELEMENT_NODE) {
					xmlNode* _tmp10_;
					const gchar* _tmp11_;
					_tmp10_ = it;
					_tmp11_ = _tmp10_->name;
					_tmp8_ = g_strcmp0 (_tmp11_, "nodelink") == 0;
				} else {
					_tmp8_ = FALSE;
				}
				if (_tmp8_) {
					NodeLink* link = NULL;
					xmlNode* _tmp12_;
					NodeLink* _tmp13_;
					gchar* id = NULL;
					xmlNode* _tmp14_;
					gchar* _tmp15_;
					const gchar* _tmp16_;
					_tmp12_ = it;
					_tmp13_ = node_link_new_from_xml (_tmp12_);
					link = _tmp13_;
					_tmp14_ = it;
					_tmp15_ = (gchar*) xmlGetProp (_tmp14_, (xmlChar*) "doc-id");
					id = _tmp15_;
					_tmp16_ = id;
					if (_tmp16_ != NULL) {
						GeeHashMap* _tmp17_;
						const gchar* _tmp18_;
						NodeLink* _tmp19_;
						_tmp17_ = self->priv->_links;
						_tmp18_ = id;
						_tmp19_ = link;
						gee_abstract_map_set ((GeeAbstractMap*) _tmp17_, (gpointer) ((gintptr) atoi (_tmp18_)), _tmp19_);
					}
					_g_free0 (id);
					_node_link_unref0 (link);
				}
			}
		}
	}
	_g_free0 (i);
}

static void
value_node_links_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
value_node_links_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		node_links_unref (value->data[0].v_pointer);
	}
}

static void
value_node_links_copy_value (const GValue* src_value,
                             GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = node_links_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
value_node_links_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
value_node_links_collect_value (GValue* value,
                                guint n_collect_values,
                                GTypeCValue* collect_values,
                                guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		NodeLinks * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = node_links_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
value_node_links_lcopy_value (const GValue* value,
                              guint n_collect_values,
                              GTypeCValue* collect_values,
                              guint collect_flags)
{
	NodeLinks ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = node_links_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
param_spec_node_links (const gchar* name,
                       const gchar* nick,
                       const gchar* blurb,
                       GType object_type,
                       GParamFlags flags)
{
	ParamSpecNodeLinks* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_NODE_LINKS), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
value_get_node_links (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NODE_LINKS), NULL);
	return value->data[0].v_pointer;
}

void
value_set_node_links (GValue* value,
                      gpointer v_object)
{
	NodeLinks * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NODE_LINKS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_NODE_LINKS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		node_links_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		node_links_unref (old);
	}
}

void
value_take_node_links (GValue* value,
                       gpointer v_object)
{
	NodeLinks * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NODE_LINKS));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_NODE_LINKS));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		node_links_unref (old);
	}
}

static void
node_links_class_init (NodeLinksClass * klass,
                       gpointer klass_data)
{
	node_links_parent_class = g_type_class_peek_parent (klass);
	((NodeLinksClass *) klass)->finalize = node_links_finalize;
	g_type_class_adjust_private_offset (klass, &NodeLinks_private_offset);
}

static void
node_links_instance_init (NodeLinks * self,
                          gpointer klass)
{
	self->priv = node_links_get_instance_private (self);
	self->priv->_id = 0;
	self->ref_count = 1;
}

static void
node_links_finalize (NodeLinks * obj)
{
	NodeLinks * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_NODE_LINKS, NodeLinks);
	g_signal_handlers_destroy (self);
	_g_object_unref0 (self->priv->_links);
	_g_regex_unref0 (self->priv->_link_re);
}

static GType
node_links_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { value_node_links_init, value_node_links_free_value, value_node_links_copy_value, value_node_links_peek_pointer, "p", value_node_links_collect_value, "p", value_node_links_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (NodeLinksClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) node_links_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (NodeLinks), 0, (GInstanceInitFunc) node_links_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType node_links_type_id;
	node_links_type_id = g_type_register_fundamental (g_type_fundamental_next (), "NodeLinks", &g_define_type_info, &g_define_type_fundamental_info, 0);
	NodeLinks_private_offset = g_type_add_instance_private (node_links_type_id, sizeof (NodeLinksPrivate));
	return node_links_type_id;
}

GType
node_links_get_type (void)
{
	static volatile gsize node_links_type_id__once = 0;
	if (g_once_init_enter (&node_links_type_id__once)) {
		GType node_links_type_id;
		node_links_type_id = node_links_get_type_once ();
		g_once_init_leave (&node_links_type_id__once, node_links_type_id);
	}
	return node_links_type_id__once;
}

gpointer
node_links_ref (gpointer instance)
{
	NodeLinks * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
node_links_unref (gpointer instance)
{
	NodeLinks * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		NODE_LINKS_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

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;
}

