/* rygel-ruih-filter-entry.c generated by valac 0.30.0.35-2c9e8, the Vala compiler
 * generated from rygel-ruih-filter-entry.vala, do not modify */

/*
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: Neha Shanbhag <N.Shanbhag@cablelabs.com>
 * Contact: http://www.cablelabs.com/
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gobject/gvaluecollector.h>


#define TYPE_FILTER_ENTRY (filter_entry_get_type ())
#define FILTER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FILTER_ENTRY, FilterEntry))
#define FILTER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FILTER_ENTRY, FilterEntryClass))
#define IS_FILTER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FILTER_ENTRY))
#define IS_FILTER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FILTER_ENTRY))
#define FILTER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FILTER_ENTRY, FilterEntryClass))

typedef struct _FilterEntry FilterEntry;
typedef struct _FilterEntryClass FilterEntryClass;
typedef struct _FilterEntryPrivate FilterEntryPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ParamSpecFilterEntry ParamSpecFilterEntry;

struct _FilterEntry {
	GTypeInstance parent_instance;
	volatile int ref_count;
	FilterEntryPrivate * priv;
};

struct _FilterEntryClass {
	GTypeClass parent_class;
	void (*finalize) (FilterEntry *self);
	gboolean (*matches) (FilterEntry* self, const gchar* name, const gchar* value);
};

struct _FilterEntryPrivate {
	gchar* entry_name;
	gchar* entry_value;
};

struct _ParamSpecFilterEntry {
	GParamSpec parent_instance;
};


static gpointer filter_entry_parent_class = NULL;

gpointer filter_entry_ref (gpointer instance);
void filter_entry_unref (gpointer instance);
GParamSpec* param_spec_filter_entry (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_filter_entry (GValue* value, gpointer v_object);
void value_take_filter_entry (GValue* value, gpointer v_object);
gpointer value_get_filter_entry (const GValue* value);
GType filter_entry_get_type (void) G_GNUC_CONST;
#define FILTER_ENTRY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_FILTER_ENTRY, FilterEntryPrivate))
enum  {
	FILTER_ENTRY_DUMMY_PROPERTY
};
#define FILTER_ENTRY_LIFETIME "lifetime"
FilterEntry* filter_entry_new (const gchar* name, const gchar* value);
FilterEntry* filter_entry_construct (GType object_type, const gchar* name, const gchar* value);
gboolean filter_entry_matches (FilterEntry* self, const gchar* name, const gchar* value);
static gboolean filter_entry_real_matches (FilterEntry* self, const gchar* name, const gchar* value);
static void filter_entry_finalize (FilterEntry* obj);


static gchar* string_replace (const gchar* self, const gchar* old, const gchar* replacement) {
	gchar* result = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	{
		GRegex* regex = NULL;
		const gchar* _tmp0_ = NULL;
		gchar* _tmp1_ = NULL;
		gchar* _tmp2_ = NULL;
		GRegex* _tmp3_ = NULL;
		GRegex* _tmp4_ = NULL;
		gchar* _tmp5_ = NULL;
		GRegex* _tmp6_ = NULL;
		const gchar* _tmp7_ = NULL;
		gchar* _tmp8_ = NULL;
		gchar* _tmp9_ = NULL;
		_tmp0_ = old;
		_tmp1_ = g_regex_escape_string (_tmp0_, -1);
		_tmp2_ = _tmp1_;
		_tmp3_ = g_regex_new (_tmp2_, 0, 0, &_inner_error_);
		_tmp4_ = _tmp3_;
		_g_free0 (_tmp2_);
		regex = _tmp4_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_tmp6_ = regex;
		_tmp7_ = replacement;
		_tmp8_ = g_regex_replace_literal (_tmp6_, self, (gssize) -1, 0, _tmp7_, 0, &_inner_error_);
		_tmp5_ = _tmp8_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			_g_regex_unref0 (regex);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
		_tmp9_ = _tmp5_;
		_tmp5_ = NULL;
		result = _tmp9_;
		_g_free0 (_tmp5_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		GError* e = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_assert_not_reached ();
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
}


FilterEntry* filter_entry_construct (GType object_type, const gchar* name, const gchar* value) {
	FilterEntry* self = NULL;
	gchar* temp = NULL;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	const gchar* _tmp4_ = NULL;
	gchar* _tmp5_ = NULL;
	const gchar* _tmp6_ = NULL;
	gchar* _tmp7_ = NULL;
	const gchar* _tmp8_ = NULL;
	gchar* _tmp9_ = NULL;
	const gchar* _tmp10_ = NULL;
	gchar* _tmp11_ = NULL;
	const gchar* _tmp12_ = NULL;
	gchar* _tmp13_ = NULL;
	const gchar* _tmp14_ = NULL;
	gchar* _tmp15_ = NULL;
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (value != NULL, NULL);
	self = (FilterEntry*) g_type_create_instance (object_type);
	_tmp0_ = name;
	_tmp1_ = g_strdup (_tmp0_);
	temp = _tmp1_;
	_tmp2_ = temp;
	_tmp3_ = string_replace (_tmp2_, "\"", "");
	_g_free0 (temp);
	temp = _tmp3_;
	_tmp4_ = temp;
	_tmp5_ = g_strdup (_tmp4_);
	_g_free0 (self->priv->entry_name);
	self->priv->entry_name = _tmp5_;
	_tmp6_ = value;
	_tmp7_ = g_strdup (_tmp6_);
	_g_free0 (temp);
	temp = _tmp7_;
	_tmp8_ = temp;
	_tmp9_ = string_replace (_tmp8_, "\"", "");
	_g_free0 (temp);
	temp = _tmp9_;
	_tmp10_ = temp;
	_tmp11_ = g_regex_escape_string (_tmp10_, -1);
	_g_free0 (temp);
	temp = _tmp11_;
	_tmp12_ = temp;
	_tmp13_ = string_replace (_tmp12_, "\\*", ".*");
	_g_free0 (temp);
	temp = _tmp13_;
	_tmp14_ = temp;
	_tmp15_ = g_strdup (_tmp14_);
	_g_free0 (self->priv->entry_value);
	self->priv->entry_value = _tmp15_;
	_g_free0 (temp);
	return self;
}


FilterEntry* filter_entry_new (const gchar* name, const gchar* value) {
	return filter_entry_construct (TYPE_FILTER_ENTRY, name, value);
}


static gboolean filter_entry_real_matches (FilterEntry* self, const gchar* name, const gchar* value) {
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	const gchar* _tmp1_ = NULL;
	gboolean _tmp3_ = FALSE;
	const gchar* _tmp4_ = NULL;
	const gchar* _tmp5_ = NULL;
	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);
	_tmp1_ = self->priv->entry_name;
	if (_tmp1_ == NULL) {
		const gchar* _tmp2_ = NULL;
		_tmp2_ = self->priv->entry_value;
		_tmp0_ = _tmp2_ == NULL;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		result = FALSE;
		return result;
	}
	_tmp4_ = self->priv->entry_name;
	_tmp5_ = name;
	if (g_strcmp0 (_tmp4_, _tmp5_) == 0) {
		_tmp3_ = TRUE;
	} else {
		const gchar* _tmp6_ = NULL;
		_tmp6_ = self->priv->entry_name;
		_tmp3_ = g_strcmp0 (_tmp6_, "*") == 0;
	}
	if (_tmp3_) {
		const gchar* _tmp7_ = NULL;
		_tmp7_ = self->priv->entry_value;
		if (_tmp7_ != NULL) {
			const gchar* _tmp8_ = NULL;
			gboolean _result_ = FALSE;
			const gchar* _tmp13_ = NULL;
			const gchar* _tmp14_ = NULL;
			gboolean _tmp15_ = FALSE;
			_tmp8_ = self->priv->entry_name;
			if (g_strcmp0 (_tmp8_, FILTER_ENTRY_LIFETIME) == 0) {
				const gchar* _tmp9_ = NULL;
				gint _tmp10_ = 0;
				const gchar* _tmp11_ = NULL;
				gint _tmp12_ = 0;
				_tmp9_ = self->priv->entry_value;
				_tmp10_ = atoi (_tmp9_);
				_tmp11_ = value;
				_tmp12_ = atoi (_tmp11_);
				result = _tmp10_ == _tmp12_;
				return result;
			}
			_tmp13_ = self->priv->entry_value;
			_tmp14_ = value;
			_tmp15_ = g_regex_match_simple (_tmp13_, _tmp14_, G_REGEX_CASELESS, 0);
			_result_ = _tmp15_;
			result = _result_;
			return result;
		}
	}
	result = FALSE;
	return result;
}


gboolean filter_entry_matches (FilterEntry* self, const gchar* name, const gchar* value) {
	g_return_val_if_fail (self != NULL, FALSE);
	return FILTER_ENTRY_GET_CLASS (self)->matches (self, name, value);
}


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


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


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


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


static gchar* value_filter_entry_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		FilterEntry* 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 = filter_entry_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* value_filter_entry_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	FilterEntry** 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 = filter_entry_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* param_spec_filter_entry (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	ParamSpecFilterEntry* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_FILTER_ENTRY), 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_filter_entry (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY), NULL);
	return value->data[0].v_pointer;
}


void value_set_filter_entry (GValue* value, gpointer v_object) {
	FilterEntry* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FILTER_ENTRY));
		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;
		filter_entry_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		filter_entry_unref (old);
	}
}


void value_take_filter_entry (GValue* value, gpointer v_object) {
	FilterEntry* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FILTER_ENTRY));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FILTER_ENTRY));
		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) {
		filter_entry_unref (old);
	}
}


static void filter_entry_class_init (FilterEntryClass * klass) {
	filter_entry_parent_class = g_type_class_peek_parent (klass);
	((FilterEntryClass *) klass)->finalize = filter_entry_finalize;
	g_type_class_add_private (klass, sizeof (FilterEntryPrivate));
	((FilterEntryClass *) klass)->matches = filter_entry_real_matches;
}


static void filter_entry_instance_init (FilterEntry * self) {
	self->priv = FILTER_ENTRY_GET_PRIVATE (self);
	self->priv->entry_name = NULL;
	self->priv->entry_value = NULL;
	self->ref_count = 1;
}


static void filter_entry_finalize (FilterEntry* obj) {
	FilterEntry * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_FILTER_ENTRY, FilterEntry);
	g_signal_handlers_destroy (self);
	_g_free0 (self->priv->entry_name);
	_g_free0 (self->priv->entry_value);
}


GType filter_entry_get_type (void) {
	static volatile gsize filter_entry_type_id__volatile = 0;
	if (g_once_init_enter (&filter_entry_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_filter_entry_init, value_filter_entry_free_value, value_filter_entry_copy_value, value_filter_entry_peek_pointer, "p", value_filter_entry_collect_value, "p", value_filter_entry_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (FilterEntryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) filter_entry_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FilterEntry), 0, (GInstanceInitFunc) filter_entry_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 filter_entry_type_id;
		filter_entry_type_id = g_type_register_fundamental (g_type_fundamental_next (), "FilterEntry", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&filter_entry_type_id__volatile, filter_entry_type_id);
	}
	return filter_entry_type_id__volatile;
}


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


void filter_entry_unref (gpointer instance) {
	FilterEntry* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		FILTER_ENTRY_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}



