/* game-boy-header.c generated by valac 0.35.5, the Vala compiler
 * generated from game-boy-header.vala, do not modify */

/* This file is part of GNOME Games. License: GPLv3*/
/* Documentation: http://gbdev.gg8.se/wiki/articles/The_Cartridge_Header*/

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <string.h>


#define GAMES_TYPE_GAME_BOY_HEADER (games_game_boy_header_get_type ())
#define GAMES_GAME_BOY_HEADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeader))
#define GAMES_GAME_BOY_HEADER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeaderClass))
#define GAMES_IS_GAME_BOY_HEADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GAME_BOY_HEADER))
#define GAMES_IS_GAME_BOY_HEADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GAME_BOY_HEADER))
#define GAMES_GAME_BOY_HEADER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeaderClass))

typedef struct _GamesGameBoyHeader GamesGameBoyHeader;
typedef struct _GamesGameBoyHeaderClass GamesGameBoyHeaderClass;
typedef struct _GamesGameBoyHeaderPrivate GamesGameBoyHeaderPrivate;

#define GAMES_TYPE_GAME_BOY_TYPE (games_game_boy_type_get_type ())
#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _GamesGameBoyHeader {
	GObject parent_instance;
	GamesGameBoyHeaderPrivate * priv;
};

struct _GamesGameBoyHeaderClass {
	GObjectClass parent_class;
};

typedef enum  {
	GAMES_GAME_BOY_TYPE_INVALID,
	GAMES_GAME_BOY_TYPE_CLASSIC,
	GAMES_GAME_BOY_TYPE_SUPER_ENABLED,
	GAMES_GAME_BOY_TYPE_COLOR_ENABLED,
	GAMES_GAME_BOY_TYPE_SUPER_COLOR_ENABLED,
	GAMES_GAME_BOY_TYPE_COLOR_ONLY
} GamesGameBoyType;

struct _GamesGameBoyHeaderPrivate {
	GamesGameBoyType* _game_boy_type;
	GFile* file;
};

typedef enum  {
	GAMES_GAME_BOY_ERROR_INVALID_HEADER
} GamesGameBoyError;
#define GAMES_GAME_BOY_ERROR games_game_boy_error_quark ()

static gpointer games_game_boy_header_parent_class = NULL;
static GType games_game_boy_header_type_id = 0;

GType games_game_boy_header_get_type (void) G_GNUC_CONST;
GType games_game_boy_type_get_type (void) G_GNUC_CONST;
#define GAMES_GAME_BOY_HEADER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeaderPrivate))
enum  {
	GAMES_GAME_BOY_HEADER_DUMMY_PROPERTY,
	GAMES_GAME_BOY_HEADER_GAME_BOY_TYPE
};
#define GAMES_GAME_BOY_HEADER_COLOR_TYPE_OFFSET ((gsize) 0x143)
#define GAMES_GAME_BOY_HEADER_COLOR_ENABLED_VALUE ((guint8) 0x80)
#define GAMES_GAME_BOY_HEADER_COLOR_ONLY_VALUE ((guint8) 0xC0)
#define GAMES_GAME_BOY_HEADER_SUPER_TYPE_OFFSET ((gsize) 0x146)
#define GAMES_GAME_BOY_HEADER_SUPER_DISABLED_VALUE ((guint8) 0x00)
#define GAMES_GAME_BOY_HEADER_SUPER_ENABLED_VALUE ((guint8) 0x03)
GamesGameBoyHeader* games_game_boy_header_new (GFile* file);
GamesGameBoyHeader* games_game_boy_header_construct (GType object_type, GFile* file);
gboolean games_game_boy_header_is_classic (GamesGameBoyHeader* self);
GamesGameBoyType games_game_boy_header_get_game_boy_type (GamesGameBoyHeader* self);
gboolean games_game_boy_header_is_color (GamesGameBoyHeader* self);
static GamesGameBoyType* _games_game_boy_type_dup (GamesGameBoyType* self);
static void games_game_boy_header_finalize (GObject * obj);
static void _vala_games_game_boy_header_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
GQuark games_game_boy_error_quark (void);


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


GamesGameBoyHeader* games_game_boy_header_construct (GType object_type, GFile* file) {
	GamesGameBoyHeader * self = NULL;
	GFile* _tmp0_;
	GFile* _tmp1_;
	g_return_val_if_fail (file != NULL, NULL);
	self = (GamesGameBoyHeader*) g_object_new (object_type, NULL);
	_tmp0_ = file;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->file);
	self->priv->file = _tmp1_;
	return self;
}


GamesGameBoyHeader* games_game_boy_header_new (GFile* file) {
	return games_game_boy_header_construct (GAMES_TYPE_GAME_BOY_HEADER, file);
}


gboolean games_game_boy_header_is_classic (GamesGameBoyHeader* self) {
	gboolean result = FALSE;
	GamesGameBoyType _tmp0_;
	GamesGameBoyType _tmp1_;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = games_game_boy_header_get_game_boy_type (self);
	_tmp1_ = _tmp0_;
	switch (_tmp1_) {
		case GAMES_GAME_BOY_TYPE_SUPER_ENABLED:
		case GAMES_GAME_BOY_TYPE_CLASSIC:
		{
			result = TRUE;
			return result;
		}
		default:
		{
			result = FALSE;
			return result;
		}
	}
}


gboolean games_game_boy_header_is_color (GamesGameBoyHeader* self) {
	gboolean result = FALSE;
	GamesGameBoyType _tmp0_;
	GamesGameBoyType _tmp1_;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = games_game_boy_header_get_game_boy_type (self);
	_tmp1_ = _tmp0_;
	switch (_tmp1_) {
		case GAMES_GAME_BOY_TYPE_SUPER_COLOR_ENABLED:
		case GAMES_GAME_BOY_TYPE_COLOR_ENABLED:
		case GAMES_GAME_BOY_TYPE_COLOR_ONLY:
		{
			result = TRUE;
			return result;
		}
		default:
		{
			result = FALSE;
			return result;
		}
	}
}


static GamesGameBoyType* _games_game_boy_type_dup (GamesGameBoyType* self) {
	GamesGameBoyType* dup;
	dup = g_new0 (GamesGameBoyType, 1);
	memcpy (dup, self, sizeof (GamesGameBoyType));
	return dup;
}


static gpointer __games_game_boy_type_dup0 (gpointer self) {
	return self ? _games_game_boy_type_dup (self) : NULL;
}


GamesGameBoyType games_game_boy_header_get_game_boy_type (GamesGameBoyHeader* self) {
	GamesGameBoyType result;
	GamesGameBoyType* _tmp0_;
	GFileInputStream* stream = NULL;
	guint8 buffer[1] = {0};
	guint8 color_value;
	guint8 _tmp14_;
	guint8 super_value;
	guint8 _tmp20_;
	guint8 _tmp21_;
	GamesGameBoyType* _tmp37_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_game_boy_type;
	if (_tmp0_ != NULL) {
		GamesGameBoyType* _tmp1_;
		_tmp1_ = self->priv->_game_boy_type;
		result = *_tmp1_;
		return result;
	}
	{
		GFileInputStream* _tmp2_;
		GFile* _tmp3_;
		GFileInputStream* _tmp4_;
		GFileInputStream* _tmp5_;
		_tmp3_ = self->priv->file;
		_tmp4_ = g_file_read (_tmp3_, NULL, &_inner_error_);
		_tmp2_ = _tmp4_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp5_ = _tmp2_;
		_tmp2_ = NULL;
		_g_object_unref0 (stream);
		stream = _tmp5_;
		_g_object_unref0 (_tmp2_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GamesGameBoyType _tmp6_;
		GamesGameBoyType* _tmp7_;
		GamesGameBoyType* _tmp8_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp6_ = GAMES_GAME_BOY_TYPE_INVALID;
		_tmp7_ = __games_game_boy_type_dup0 (&_tmp6_);
		_g_free0 (self->priv->_game_boy_type);
		self->priv->_game_boy_type = _tmp7_;
		_tmp8_ = self->priv->_game_boy_type;
		result = *_tmp8_;
		_g_error_free0 (e);
		_g_object_unref0 (stream);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		_g_object_unref0 (stream);
		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 0;
	}
	color_value = (guint8) 0;
	{
		GFileInputStream* _tmp9_;
		GFileInputStream* _tmp10_;
		_tmp9_ = stream;
		G_FILE_INPUT_STREAM_GET_CLASS (_tmp9_)->seek (_tmp9_, (gint64) GAMES_GAME_BOY_HEADER_COLOR_TYPE_OFFSET, G_SEEK_SET, NULL, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch1_g_error;
		}
		_tmp10_ = stream;
		g_input_stream_read ((GInputStream*) _tmp10_, buffer, (gsize) 1, NULL, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch1_g_error;
		}
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError* e = NULL;
		GamesGameBoyType _tmp11_;
		GamesGameBoyType* _tmp12_;
		GamesGameBoyType* _tmp13_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp11_ = GAMES_GAME_BOY_TYPE_INVALID;
		_tmp12_ = __games_game_boy_type_dup0 (&_tmp11_);
		_g_free0 (self->priv->_game_boy_type);
		self->priv->_game_boy_type = _tmp12_;
		_tmp13_ = self->priv->_game_boy_type;
		result = *_tmp13_;
		_g_error_free0 (e);
		_g_object_unref0 (stream);
		return result;
	}
	__finally1:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		_g_object_unref0 (stream);
		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 0;
	}
	_tmp14_ = buffer[0];
	color_value = _tmp14_;
	super_value = (guint8) 0;
	{
		GFileInputStream* _tmp15_;
		GFileInputStream* _tmp16_;
		_tmp15_ = stream;
		G_FILE_INPUT_STREAM_GET_CLASS (_tmp15_)->seek (_tmp15_, (gint64) GAMES_GAME_BOY_HEADER_SUPER_TYPE_OFFSET, G_SEEK_SET, NULL, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch2_g_error;
		}
		_tmp16_ = stream;
		g_input_stream_read ((GInputStream*) _tmp16_, buffer, (gsize) 1, NULL, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch2_g_error;
		}
	}
	goto __finally2;
	__catch2_g_error:
	{
		GError* e = NULL;
		GamesGameBoyType _tmp17_;
		GamesGameBoyType* _tmp18_;
		GamesGameBoyType* _tmp19_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp17_ = GAMES_GAME_BOY_TYPE_INVALID;
		_tmp18_ = __games_game_boy_type_dup0 (&_tmp17_);
		_g_free0 (self->priv->_game_boy_type);
		self->priv->_game_boy_type = _tmp18_;
		_tmp19_ = self->priv->_game_boy_type;
		result = *_tmp19_;
		_g_error_free0 (e);
		_g_object_unref0 (stream);
		return result;
	}
	__finally2:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		_g_object_unref0 (stream);
		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 0;
	}
	_tmp20_ = buffer[0];
	super_value = _tmp20_;
	_tmp21_ = color_value;
	switch (_tmp21_) {
		case GAMES_GAME_BOY_HEADER_COLOR_ONLY_VALUE:
		{
			guint8 _tmp22_;
			_tmp22_ = super_value;
			if (_tmp22_ == GAMES_GAME_BOY_HEADER_SUPER_DISABLED_VALUE) {
				GamesGameBoyType _tmp23_;
				GamesGameBoyType* _tmp24_;
				_tmp23_ = GAMES_GAME_BOY_TYPE_COLOR_ONLY;
				_tmp24_ = __games_game_boy_type_dup0 (&_tmp23_);
				_g_free0 (self->priv->_game_boy_type);
				self->priv->_game_boy_type = _tmp24_;
			}
			break;
		}
		case GAMES_GAME_BOY_HEADER_COLOR_ENABLED_VALUE:
		{
			guint8 _tmp25_;
			guint8 _tmp28_;
			_tmp25_ = super_value;
			if (_tmp25_ == GAMES_GAME_BOY_HEADER_SUPER_ENABLED_VALUE) {
				GamesGameBoyType _tmp26_;
				GamesGameBoyType* _tmp27_;
				_tmp26_ = GAMES_GAME_BOY_TYPE_SUPER_COLOR_ENABLED;
				_tmp27_ = __games_game_boy_type_dup0 (&_tmp26_);
				_g_free0 (self->priv->_game_boy_type);
				self->priv->_game_boy_type = _tmp27_;
			}
			_tmp28_ = super_value;
			if (_tmp28_ == GAMES_GAME_BOY_HEADER_SUPER_DISABLED_VALUE) {
				GamesGameBoyType _tmp29_;
				GamesGameBoyType* _tmp30_;
				_tmp29_ = GAMES_GAME_BOY_TYPE_COLOR_ENABLED;
				_tmp30_ = __games_game_boy_type_dup0 (&_tmp29_);
				_g_free0 (self->priv->_game_boy_type);
				self->priv->_game_boy_type = _tmp30_;
			}
			break;
		}
		default:
		{
			guint8 _tmp31_;
			guint8 _tmp34_;
			_tmp31_ = super_value;
			if (_tmp31_ == GAMES_GAME_BOY_HEADER_SUPER_ENABLED_VALUE) {
				GamesGameBoyType _tmp32_;
				GamesGameBoyType* _tmp33_;
				_tmp32_ = GAMES_GAME_BOY_TYPE_SUPER_ENABLED;
				_tmp33_ = __games_game_boy_type_dup0 (&_tmp32_);
				_g_free0 (self->priv->_game_boy_type);
				self->priv->_game_boy_type = _tmp33_;
			}
			_tmp34_ = super_value;
			if (_tmp34_ == GAMES_GAME_BOY_HEADER_SUPER_DISABLED_VALUE) {
				GamesGameBoyType _tmp35_;
				GamesGameBoyType* _tmp36_;
				_tmp35_ = GAMES_GAME_BOY_TYPE_CLASSIC;
				_tmp36_ = __games_game_boy_type_dup0 (&_tmp35_);
				_g_free0 (self->priv->_game_boy_type);
				self->priv->_game_boy_type = _tmp36_;
			}
			break;
		}
	}
	_tmp37_ = self->priv->_game_boy_type;
	result = *_tmp37_;
	_g_object_unref0 (stream);
	return result;
}


static void games_game_boy_header_class_init (GamesGameBoyHeaderClass * klass) {
	games_game_boy_header_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesGameBoyHeaderPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_games_game_boy_header_get_property;
	G_OBJECT_CLASS (klass)->finalize = games_game_boy_header_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_GAME_BOY_HEADER_GAME_BOY_TYPE, g_param_spec_enum ("game-boy-type", "game-boy-type", "game-boy-type", GAMES_TYPE_GAME_BOY_TYPE, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
}


static void games_game_boy_header_instance_init (GamesGameBoyHeader * self) {
	self->priv = GAMES_GAME_BOY_HEADER_GET_PRIVATE (self);
}


static void games_game_boy_header_finalize (GObject * obj) {
	GamesGameBoyHeader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeader);
	_g_free0 (self->priv->_game_boy_type);
	_g_object_unref0 (self->priv->file);
	G_OBJECT_CLASS (games_game_boy_header_parent_class)->finalize (obj);
}


GType games_game_boy_header_get_type (void) {
	return games_game_boy_header_type_id;
}


GType games_game_boy_header_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (GamesGameBoyHeaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_game_boy_header_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesGameBoyHeader), 0, (GInstanceInitFunc) games_game_boy_header_instance_init, NULL };
	games_game_boy_header_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "GamesGameBoyHeader", &g_define_type_info, 0);
	return games_game_boy_header_type_id;
}


static void _vala_games_game_boy_header_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	GamesGameBoyHeader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_GAME_BOY_HEADER, GamesGameBoyHeader);
	switch (property_id) {
		case GAMES_GAME_BOY_HEADER_GAME_BOY_TYPE:
		g_value_set_enum (value, games_game_boy_header_get_game_boy_type (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


GType games_game_boy_type_get_type (void) {
	static volatile gsize games_game_boy_type_type_id__volatile = 0;
	if (g_once_init_enter (&games_game_boy_type_type_id__volatile)) {
		static const GEnumValue values[] = {{GAMES_GAME_BOY_TYPE_INVALID, "GAMES_GAME_BOY_TYPE_INVALID", "invalid"}, {GAMES_GAME_BOY_TYPE_CLASSIC, "GAMES_GAME_BOY_TYPE_CLASSIC", "classic"}, {GAMES_GAME_BOY_TYPE_SUPER_ENABLED, "GAMES_GAME_BOY_TYPE_SUPER_ENABLED", "super-enabled"}, {GAMES_GAME_BOY_TYPE_COLOR_ENABLED, "GAMES_GAME_BOY_TYPE_COLOR_ENABLED", "color-enabled"}, {GAMES_GAME_BOY_TYPE_SUPER_COLOR_ENABLED, "GAMES_GAME_BOY_TYPE_SUPER_COLOR_ENABLED", "super-color-enabled"}, {GAMES_GAME_BOY_TYPE_COLOR_ONLY, "GAMES_GAME_BOY_TYPE_COLOR_ONLY", "color-only"}, {0, NULL, NULL}};
		GType games_game_boy_type_type_id;
		games_game_boy_type_type_id = g_enum_register_static ("GamesGameBoyType", values);
		g_once_init_leave (&games_game_boy_type_type_id__volatile, games_game_boy_type_type_id);
	}
	return games_game_boy_type_type_id__volatile;
}


GQuark games_game_boy_error_quark (void) {
	return g_quark_from_static_string ("games_game_boy_error-quark");
}



