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

/* This file is part of GNOME Games. License: GPLv3*/

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


#define GAMES_TYPE_MAME_GAME_INFO (games_mame_game_info_get_type ())
typedef struct _GamesMameGameInfo GamesMameGameInfo;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _g_bytes_unref0(var) ((var == NULL) ? NULL : (var = (g_bytes_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
typedef struct _GamesMameGameInfoGetSupportedGamesData GamesMameGameInfoGetSupportedGamesData;

struct _GamesMameGameInfo {
	gchar* id;
	gchar* name;
};

struct _GamesMameGameInfoGetSupportedGamesData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	gboolean _task_complete_;
	GHashTable* result;
	GHashTable* _tmp0_;
	GHashTable* _tmp1_;
	GHashTable* _tmp2_;
	GHashFunc _tmp3_;
	GEqualFunc _tmp4_;
	GHashTable* _tmp5_;
	GBytes* bytes;
	GBytes* _tmp6_;
	gchar* text;
	GBytes* _tmp7_;
	gsize _tmp8_;
	guint8* _tmp9_;
	gchar* _tmp10_;
	GRegex* _tmp11_;
	gchar* simple;
	gchar* _tmp12_;
	gchar* quoted;
	gchar* _tmp13_;
	gchar* pattern;
	const gchar* _tmp14_;
	const gchar* _tmp15_;
	const gchar* _tmp16_;
	const gchar* _tmp17_;
	const gchar* _tmp18_;
	const gchar* _tmp19_;
	const gchar* _tmp20_;
	const gchar* _tmp21_;
	const gchar* _tmp22_;
	const gchar* _tmp23_;
	const gchar* _tmp24_;
	const gchar* _tmp25_;
	const gchar* _tmp26_;
	const gchar* _tmp27_;
	const gchar* _tmp28_;
	const gchar* _tmp29_;
	const gchar* _tmp30_;
	const gchar* _tmp31_;
	const gchar* _tmp32_;
	const gchar* _tmp33_;
	const gchar* _tmp34_;
	const gchar* _tmp35_;
	const gchar* _tmp36_;
	const gchar* _tmp37_;
	gchar* _tmp38_;
	GRegex* _tmp39_;
	const gchar* _tmp40_;
	gchar* _tmp41_;
	gchar* _tmp42_;
	gchar* _tmp43_;
	gchar* _tmp44_;
	GRegex* _tmp45_;
	GRegex* _tmp46_;
	GRegex* _tmp47_;
	const gchar* _tmp48_;
	gchar** _tmp49_;
	gchar** _tmp50_;
	gchar** line_collection;
	gint line_collection_length1;
	gint _line_collection_size_;
	gint line_it;
	gchar* _tmp51_;
	gchar* line;
	GMatchInfo* match_info;
	GRegex* _tmp52_;
	const gchar* _tmp53_;
	GMatchInfo* _tmp54_;
	gboolean _tmp55_;
	GamesMameGameInfo game_info;
	GMatchInfo* _tmp56_;
	gchar* _tmp57_;
	gchar* _tmp58_;
	gchar* _tmp59_;
	GMatchInfo* _tmp60_;
	gchar* _tmp61_;
	gchar* _tmp62_;
	gchar* _tmp63_;
	GamesMameGameInfo _tmp64_;
	GamesMameGameInfo _tmp65_;
	GHashTable* _tmp66_;
	GamesMameGameInfo _tmp67_;
	const gchar* _tmp68_;
	gchar* _tmp69_;
	GamesMameGameInfo _tmp70_;
	GamesMameGameInfo _tmp71_;
	GamesMameGameInfo* _tmp72_;
	GHashTable* _tmp73_;
	GHashTable* _tmp74_;
	GError * _inner_error_;
};


extern GHashTable* games_mame_game_info_supported_games;
GHashTable* games_mame_game_info_supported_games = NULL;
extern GRegex* games_mame_game_info_game_regex;
GRegex* games_mame_game_info_game_regex = NULL;
extern GRegex* games_mame_game_info_cleanup_string_regex;
GRegex* games_mame_game_info_cleanup_string_regex = NULL;

GType games_mame_game_info_get_type (void) G_GNUC_CONST;
GamesMameGameInfo* games_mame_game_info_dup (const GamesMameGameInfo* self);
void games_mame_game_info_free (GamesMameGameInfo* self);
void games_mame_game_info_copy (const GamesMameGameInfo* self, GamesMameGameInfo* dest);
void games_mame_game_info_destroy (GamesMameGameInfo* self);
static void games_mame_game_info_get_supported_games_data_free (gpointer _data);
void games_mame_game_info_get_supported_games (GAsyncReadyCallback _callback_, gpointer _user_data_);
GHashTable* games_mame_game_info_get_supported_games_finish (GAsyncResult* _res_, GError** error);
static gboolean games_mame_game_info_get_supported_games_co (GamesMameGameInfoGetSupportedGamesData* _data_);
static void _g_free0_ (gpointer var);
static void _games_mame_game_info_free0_ (gpointer var);
static gboolean _games_mame_game_info_get_supported_games_co_gsource_func (gpointer self);
static gchar* games_mame_game_info_cleanup_string (const gchar* text);
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);
static gint _vala_array_length (gpointer array);

static GRegex* _tmp_regex_0 = NULL;

static void games_mame_game_info_get_supported_games_data_free (gpointer _data) {
	GamesMameGameInfoGetSupportedGamesData* _data_;
	_data_ = _data;
	_g_hash_table_unref0 (_data_->result);
	g_slice_free (GamesMameGameInfoGetSupportedGamesData, _data_);
}


void games_mame_game_info_get_supported_games (GAsyncReadyCallback _callback_, gpointer _user_data_) {
	GamesMameGameInfoGetSupportedGamesData* _data_;
	_data_ = g_slice_new0 (GamesMameGameInfoGetSupportedGamesData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, games_mame_game_info_get_supported_games_data_free);
	games_mame_game_info_get_supported_games_co (_data_);
}


GHashTable* games_mame_game_info_get_supported_games_finish (GAsyncResult* _res_, GError** error) {
	GHashTable* result;
	GamesMameGameInfoGetSupportedGamesData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	_data_->_task_complete_ = TRUE;
	result = _data_->result;
	_data_->result = NULL;
	return result;
}


static gpointer _g_hash_table_ref0 (gpointer self) {
	return self ? g_hash_table_ref (self) : NULL;
}


static void _g_free0_ (gpointer var) {
	var = (g_free (var), NULL);
}


static void _games_mame_game_info_free0_ (gpointer var) {
	(var == NULL) ? NULL : (var = (games_mame_game_info_free (var), NULL));
}


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


static gboolean _games_mame_game_info_get_supported_games_co_gsource_func (gpointer self) {
	gboolean result;
	result = games_mame_game_info_get_supported_games_co (self);
	return result;
}


static gpointer _games_mame_game_info_dup0 (gpointer self) {
	return self ? games_mame_game_info_dup (self) : NULL;
}


static gboolean games_mame_game_info_get_supported_games_co (GamesMameGameInfoGetSupportedGamesData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = games_mame_game_info_supported_games;
	if (_data_->_tmp0_ != NULL) {
		_data_->_tmp1_ = games_mame_game_info_supported_games;
		_data_->_tmp2_ = _g_hash_table_ref0 (_data_->_tmp1_);
		_data_->result = _data_->_tmp2_;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (_data_->_task_complete_ != TRUE) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp3_ = g_str_hash;
	_data_->_tmp4_ = g_str_equal;
	_data_->_tmp5_ = g_hash_table_new_full (_data_->_tmp3_, _data_->_tmp4_, _g_free0_, _games_mame_game_info_free0_);
	_g_hash_table_unref0 (games_mame_game_info_supported_games);
	games_mame_game_info_supported_games = _data_->_tmp5_;
	_data_->_tmp6_ = g_resources_lookup_data ("/org/gnome/Games/plugins/mame/supported-games", G_RESOURCE_LOOKUP_FLAGS_NONE, &_data_->_inner_error_);
	_data_->bytes = _data_->_tmp6_;
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp7_ = _data_->bytes;
	_data_->_tmp9_ = g_bytes_get_data (_data_->_tmp7_, &_data_->_tmp8_);
	_data_->_tmp10_ = g_strdup ((const gchar*) _data_->_tmp9_);
	_data_->text = _data_->_tmp10_;
	_data_->_tmp11_ = games_mame_game_info_game_regex;
	if (_data_->_tmp11_ == NULL) {
		_data_->_tmp12_ = g_strdup (" *([^,]+) *");
		_data_->simple = _data_->_tmp12_;
		_data_->_tmp13_ = g_strdup (" *\" *(.*?) *\" *");
		_data_->quoted = _data_->_tmp13_;
		_data_->_tmp14_ = _data_->simple;
		_data_->_tmp15_ = string_to_string (_data_->_tmp14_);
		_data_->_tmp16_ = _data_->simple;
		_data_->_tmp17_ = string_to_string (_data_->_tmp16_);
		_data_->_tmp18_ = _data_->simple;
		_data_->_tmp19_ = string_to_string (_data_->_tmp18_);
		_data_->_tmp20_ = _data_->simple;
		_data_->_tmp21_ = string_to_string (_data_->_tmp20_);
		_data_->_tmp22_ = _data_->simple;
		_data_->_tmp23_ = string_to_string (_data_->_tmp22_);
		_data_->_tmp24_ = _data_->simple;
		_data_->_tmp25_ = string_to_string (_data_->_tmp24_);
		_data_->_tmp26_ = _data_->simple;
		_data_->_tmp27_ = string_to_string (_data_->_tmp26_);
		_data_->_tmp28_ = _data_->simple;
		_data_->_tmp29_ = string_to_string (_data_->_tmp28_);
		_data_->_tmp30_ = _data_->quoted;
		_data_->_tmp31_ = string_to_string (_data_->_tmp30_);
		_data_->_tmp32_ = _data_->quoted;
		_data_->_tmp33_ = string_to_string (_data_->_tmp32_);
		_data_->_tmp34_ = _data_->simple;
		_data_->_tmp35_ = string_to_string (_data_->_tmp34_);
		_data_->_tmp36_ = _data_->simple;
		_data_->_tmp37_ = string_to_string (_data_->_tmp36_);
		_data_->_tmp38_ = g_strconcat (_data_->_tmp15_, ",", _data_->_tmp17_, ",", _data_->_tmp19_, ",", _data_->_tmp21_, ",", _data_->_tmp23_, ",", _data_->_tmp25_, ",", _data_->_tmp27_, ",", _data_->_tmp29_, ",", _data_->_tmp31_, ",", _data_->_tmp33_, ",", _data_->_tmp35_, "(?:,", _data_->_tmp37_, ")?", NULL);
		_data_->pattern = _data_->_tmp38_;
		_data_->_tmp40_ = _data_->pattern;
		_data_->_tmp41_ = g_strconcat ("^GAMEL?\\(", _data_->_tmp40_, NULL);
		_data_->_tmp42_ = _data_->_tmp41_;
		_data_->_tmp43_ = g_strconcat (_data_->_tmp42_, "\\) *$", NULL);
		_data_->_tmp44_ = _data_->_tmp43_;
		_data_->_tmp45_ = g_regex_new (_data_->_tmp44_, 0, 0, &_data_->_inner_error_);
		_data_->_tmp46_ = _data_->_tmp45_;
		_g_free0 (_data_->_tmp44_);
		_g_free0 (_data_->_tmp42_);
		_data_->_tmp39_ = _data_->_tmp46_;
		if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error_);
			_g_free0 (_data_->pattern);
			_g_free0 (_data_->quoted);
			_g_free0 (_data_->simple);
			_g_free0 (_data_->text);
			_g_bytes_unref0 (_data_->bytes);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_tmp47_ = _data_->_tmp39_;
		_data_->_tmp39_ = NULL;
		_g_regex_unref0 (games_mame_game_info_game_regex);
		games_mame_game_info_game_regex = _data_->_tmp47_;
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _games_mame_game_info_get_supported_games_co_gsource_func, _data_, NULL);
		_data_->_state_ = 1;
		return FALSE;
		_state_1:
		;
		_g_regex_unref0 (_data_->_tmp39_);
		_g_free0 (_data_->pattern);
		_g_free0 (_data_->quoted);
		_g_free0 (_data_->simple);
	}
	_data_->_tmp48_ = _data_->text;
	_data_->_tmp50_ = _data_->_tmp49_ = g_strsplit (_data_->_tmp48_, "\n", 0);
	{
		_data_->line_collection_length1 = 0;
		_data_->_line_collection_size_ = 0;
		_data_->line_collection = _data_->_tmp50_;
		_data_->line_collection_length1 = _vala_array_length (_data_->_tmp49_);
		for (_data_->line_it = 0; _data_->line_it < _vala_array_length (_data_->_tmp49_); _data_->line_it = _data_->line_it + 1) {
			_data_->_tmp51_ = g_strdup (_data_->line_collection[_data_->line_it]);
			_data_->line = _data_->_tmp51_;
			{
				_data_->_tmp52_ = games_mame_game_info_game_regex;
				_data_->_tmp53_ = _data_->line;
				_data_->_tmp54_ = NULL;
				_data_->_tmp55_ = g_regex_match (_data_->_tmp52_, _data_->_tmp53_, 0, &_data_->_tmp54_);
				_g_match_info_unref0 (_data_->match_info);
				_data_->match_info = _data_->_tmp54_;
				if (!_data_->_tmp55_) {
					_g_match_info_unref0 (_data_->match_info);
					_g_free0 (_data_->line);
					continue;
				}
				_data_->_tmp56_ = _data_->match_info;
				_data_->_tmp57_ = g_match_info_fetch (_data_->_tmp56_, 2);
				_data_->_tmp58_ = _data_->_tmp57_;
				_data_->_tmp59_ = games_mame_game_info_cleanup_string (_data_->_tmp58_);
				_data_->_tmp60_ = _data_->match_info;
				_data_->_tmp61_ = g_match_info_fetch (_data_->_tmp60_, 10);
				_data_->_tmp62_ = _data_->_tmp61_;
				_data_->_tmp63_ = games_mame_game_info_cleanup_string (_data_->_tmp62_);
				memset (&_data_->_tmp64_, 0, sizeof (GamesMameGameInfo));
				memset (&_data_->_tmp64_, 0, sizeof (GamesMameGameInfo));
				_g_free0 (_data_->_tmp64_.id);
				_data_->_tmp64_.id = _data_->_tmp59_;
				_g_free0 (_data_->_tmp64_.name);
				_data_->_tmp64_.name = _data_->_tmp63_;
				_data_->_tmp65_ = _data_->_tmp64_;
				_g_free0 (_data_->_tmp62_);
				_g_free0 (_data_->_tmp58_);
				_data_->game_info = _data_->_tmp65_;
				_data_->_tmp66_ = games_mame_game_info_supported_games;
				_data_->_tmp67_ = _data_->game_info;
				_data_->_tmp68_ = _data_->_tmp67_.id;
				_data_->_tmp69_ = g_strdup (_data_->_tmp68_);
				_data_->_tmp70_ = _data_->game_info;
				_data_->_tmp71_ = _data_->_tmp70_;
				_data_->_tmp72_ = _games_mame_game_info_dup0 (&_data_->_tmp71_);
				g_hash_table_insert (_data_->_tmp66_, _data_->_tmp69_, _data_->_tmp72_);
				g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _games_mame_game_info_get_supported_games_co_gsource_func, _data_, NULL);
				_data_->_state_ = 2;
				return FALSE;
				_state_2:
				;
				games_mame_game_info_destroy (&_data_->game_info);
				_g_match_info_unref0 (_data_->match_info);
				_g_free0 (_data_->line);
			}
		}
		_data_->line_collection = (_vala_array_free (_data_->line_collection, _data_->line_collection_length1, (GDestroyNotify) g_free), NULL);
	}
	_data_->_tmp73_ = games_mame_game_info_supported_games;
	_data_->_tmp74_ = _g_hash_table_ref0 (_data_->_tmp73_);
	_data_->result = _data_->_tmp74_;
	_g_free0 (_data_->text);
	_g_bytes_unref0 (_data_->bytes);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static inline GRegex* _thread_safe_regex_init (GRegex** re, const gchar * pattern, GRegexMatchFlags match_options) {
	if (g_once_init_enter ((volatile gsize*) re)) {
		GRegex* val = g_regex_new (pattern, match_options, 0, NULL);
		g_once_init_leave ((volatile gsize*) re, (gsize) val);
	}
	return *re;
}


static gpointer _g_regex_ref0 (gpointer self) {
	return self ? g_regex_ref (self) : NULL;
}


static gchar* games_mame_game_info_cleanup_string (const gchar* text) {
	gchar* result = NULL;
	GRegex* _tmp0_;
	GMatchInfo* match_info = NULL;
	GRegex* _tmp2_;
	const gchar* _tmp3_;
	GMatchInfo* _tmp4_ = NULL;
	gboolean _tmp5_;
	gchar* _tmp8_;
	GMatchInfo* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp13_;
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = games_mame_game_info_cleanup_string_regex;
	if (_tmp0_ == NULL) {
		GRegex* _tmp1_;
		_tmp1_ = _g_regex_ref0 (_thread_safe_regex_init (&_tmp_regex_0, "^[\\s\"]*(.*?)[\\s\"]*$", 0));
		_g_regex_unref0 (games_mame_game_info_cleanup_string_regex);
		games_mame_game_info_cleanup_string_regex = _tmp1_;
	}
	_tmp2_ = games_mame_game_info_cleanup_string_regex;
	_tmp3_ = text;
	_tmp5_ = g_regex_match (_tmp2_, _tmp3_, 0, &_tmp4_);
	_g_match_info_unref0 (match_info);
	match_info = _tmp4_;
	if (!_tmp5_) {
		const gchar* _tmp6_;
		gchar* _tmp7_;
		_tmp6_ = text;
		_tmp7_ = g_strdup (_tmp6_);
		result = _tmp7_;
		_g_match_info_unref0 (match_info);
		return result;
	}
	_tmp9_ = match_info;
	_tmp10_ = g_match_info_fetch (_tmp9_, 1);
	_tmp8_ = _tmp10_;
	if (_tmp8_ == NULL) {
		const gchar* _tmp11_;
		gchar* _tmp12_;
		_tmp11_ = text;
		_tmp12_ = g_strdup (_tmp11_);
		_g_free0 (_tmp8_);
		_tmp8_ = _tmp12_;
	}
	_tmp13_ = _tmp8_;
	_tmp8_ = NULL;
	result = _tmp13_;
	_g_free0 (_tmp8_);
	_g_match_info_unref0 (match_info);
	return result;
}


void games_mame_game_info_copy (const GamesMameGameInfo* self, GamesMameGameInfo* dest) {
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	_tmp0_ = (*self).id;
	_tmp1_ = g_strdup (_tmp0_);
	_g_free0 ((*dest).id);
	(*dest).id = _tmp1_;
	_tmp2_ = (*self).name;
	_tmp3_ = g_strdup (_tmp2_);
	_g_free0 ((*dest).name);
	(*dest).name = _tmp3_;
}


void games_mame_game_info_destroy (GamesMameGameInfo* self) {
	_g_free0 ((*self).id);
	_g_free0 ((*self).name);
}


GamesMameGameInfo* games_mame_game_info_dup (const GamesMameGameInfo* self) {
	GamesMameGameInfo* dup;
	dup = g_new0 (GamesMameGameInfo, 1);
	games_mame_game_info_copy (self, dup);
	return dup;
}


void games_mame_game_info_free (GamesMameGameInfo* self) {
	games_mame_game_info_destroy (self);
	g_free (self);
}


GType games_mame_game_info_get_type (void) {
	static volatile gsize games_mame_game_info_type_id__volatile = 0;
	if (g_once_init_enter (&games_mame_game_info_type_id__volatile)) {
		GType games_mame_game_info_type_id;
		games_mame_game_info_type_id = g_boxed_type_register_static ("GamesMameGameInfo", (GBoxedCopyFunc) games_mame_game_info_dup, (GBoxedFreeFunc) games_mame_game_info_free);
		g_once_init_leave (&games_mame_game_info_type_id__volatile, games_mame_game_info_type_id);
	}
	return games_mame_game_info_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);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



