/* grep.c generated by valac 0.36.6.6-c7bee, the Vala compiler
 * generated from grep.vala, do not modify */

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

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

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))



gsize* games_grep_get_offsets (const gchar* filename, const gchar* text, int* result_length1);
static void _vala_array_add23 (gsize* * array, int* length, int* size, gsize value);
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 gboolean uint64_try_parse (const gchar* str, guint64* _result_) {
	guint64 _vala_result = 0ULL;
	gboolean result = FALSE;
	gchar* endptr = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_ = NULL;
	guint64 _tmp2_;
	gchar* _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	gint _tmp6_;
	gint _tmp7_;
	g_return_val_if_fail (str != NULL, FALSE);
	_tmp0_ = str;
	_tmp2_ = g_ascii_strtoull (_tmp0_, &_tmp1_, (guint) 0);
	endptr = _tmp1_;
	_vala_result = _tmp2_;
	_tmp3_ = endptr;
	_tmp4_ = str;
	_tmp5_ = str;
	_tmp6_ = strlen (_tmp5_);
	_tmp7_ = _tmp6_;
	if (_tmp3_ == (((gchar*) _tmp4_) + _tmp7_)) {
		result = TRUE;
		if (_result_) {
			*_result_ = _vala_result;
		}
		return result;
	} else {
		result = FALSE;
		if (_result_) {
			*_result_ = _vala_result;
		}
		return result;
	}
	if (_result_) {
		*_result_ = _vala_result;
	}
}


static void _vala_array_add23 (gsize* * array, int* length, int* size, gsize value) {
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gsize, *array, *size);
	}
	(*array)[(*length)++] = value;
}


gsize* games_grep_get_offsets (const gchar* filename, const gchar* text, int* result_length1) {
	gsize* result = NULL;
	gchar* working_dir = NULL;
	gchar* _tmp0_;
	gchar** args = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	gchar** _tmp10_;
	gint args_length1;
	gint _args_size_;
	gchar** env = NULL;
	gchar** _tmp11_;
	gchar** _tmp12_;
	gint env_length1;
	gint _env_size_;
	gboolean success = FALSE;
	gchar* output = NULL;
	gboolean _tmp21_;
	gsize* offsets = NULL;
	gsize* _tmp24_;
	gint offsets_length1;
	gint _offsets_size_;
	const gchar* _tmp25_;
	gchar** _tmp26_;
	gchar** _tmp27_;
	gsize* _tmp42_;
	gint _tmp42__length1;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (filename != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = g_get_current_dir ();
	working_dir = _tmp0_;
	_tmp1_ = g_strdup ("grep");
	_tmp2_ = g_strdup ("--byte-offset");
	_tmp3_ = g_strdup ("--only-matching");
	_tmp4_ = g_strdup ("--text");
	_tmp5_ = text;
	_tmp6_ = g_strdup (_tmp5_);
	_tmp7_ = g_strdup ("--");
	_tmp8_ = filename;
	_tmp9_ = g_strdup (_tmp8_);
	_tmp10_ = g_new0 (gchar*, 7 + 1);
	_tmp10_[0] = _tmp1_;
	_tmp10_[1] = _tmp2_;
	_tmp10_[2] = _tmp3_;
	_tmp10_[3] = _tmp4_;
	_tmp10_[4] = _tmp6_;
	_tmp10_[5] = _tmp7_;
	_tmp10_[6] = _tmp9_;
	args = _tmp10_;
	args_length1 = 7;
	_args_size_ = args_length1;
	_tmp12_ = _tmp11_ = g_get_environ ();
	env = _tmp12_;
	env_length1 = _vala_array_length (_tmp11_);
	_env_size_ = env_length1;
	success = FALSE;
	{
		gboolean _tmp13_ = FALSE;
		const gchar* _tmp14_;
		gchar** _tmp15_;
		gint _tmp15__length1;
		gchar** _tmp16_;
		gint _tmp16__length1;
		gchar* _tmp17_ = NULL;
		gboolean _tmp18_;
		_tmp14_ = working_dir;
		_tmp15_ = args;
		_tmp15__length1 = args_length1;
		_tmp16_ = env;
		_tmp16__length1 = env_length1;
		_tmp18_ = g_spawn_sync (_tmp14_, _tmp15_, _tmp16_, G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp17_, NULL, NULL, &_inner_error_);
		_g_free0 (output);
		output = _tmp17_;
		_tmp13_ = _tmp18_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			if (_inner_error_->domain == G_SPAWN_ERROR) {
				goto __catch41_g_spawn_error;
			}
			_g_free0 (output);
			env = (_vala_array_free (env, env_length1, (GDestroyNotify) g_free), NULL);
			args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (working_dir);
			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;
		}
		success = _tmp13_;
	}
	goto __finally41;
	__catch41_g_spawn_error:
	{
		GError* e = NULL;
		gsize* _tmp19_;
		gsize* _tmp20_;
		gint _tmp20__length1;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp19_ = g_new0 (gsize, 0);
		_tmp20_ = _tmp19_;
		_tmp20__length1 = 0;
		if (result_length1) {
			*result_length1 = _tmp20__length1;
		}
		result = _tmp20_;
		_g_error_free0 (e);
		_g_free0 (output);
		env = (_vala_array_free (env, env_length1, (GDestroyNotify) g_free), NULL);
		args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (working_dir);
		return result;
	}
	__finally41:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		_g_free0 (output);
		env = (_vala_array_free (env, env_length1, (GDestroyNotify) g_free), NULL);
		args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (working_dir);
		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;
	}
	_tmp21_ = success;
	if (!_tmp21_) {
		gsize* _tmp22_;
		gsize* _tmp23_;
		gint _tmp23__length1;
		_tmp22_ = g_new0 (gsize, 0);
		_tmp23_ = _tmp22_;
		_tmp23__length1 = 0;
		if (result_length1) {
			*result_length1 = _tmp23__length1;
		}
		result = _tmp23_;
		_g_free0 (output);
		env = (_vala_array_free (env, env_length1, (GDestroyNotify) g_free), NULL);
		args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (working_dir);
		return result;
	}
	_tmp24_ = g_new0 (gsize, 0);
	offsets = _tmp24_;
	offsets_length1 = 0;
	_offsets_size_ = offsets_length1;
	_tmp25_ = output;
	_tmp27_ = _tmp26_ = g_strsplit (_tmp25_, "\n", 0);
	{
		gchar** line_collection = NULL;
		gint line_collection_length1 = 0;
		gint _line_collection_size_ = 0;
		gint line_it = 0;
		line_collection = _tmp27_;
		line_collection_length1 = _vala_array_length (_tmp26_);
		for (line_it = 0; line_it < _vala_array_length (_tmp26_); line_it = line_it + 1) {
			gchar* _tmp28_;
			gchar* line = NULL;
			_tmp28_ = g_strdup (line_collection[line_it]);
			line = _tmp28_;
			{
				gchar** splitted_line = NULL;
				const gchar* _tmp29_;
				gchar** _tmp30_;
				gchar** _tmp31_;
				gint splitted_line_length1;
				gint _splitted_line_size_;
				gchar** _tmp32_;
				gint _tmp32__length1;
				gchar** _tmp33_;
				gint _tmp33__length1;
				const gchar* _tmp34_;
				const gchar* _tmp35_;
				guint64 offset = 0ULL;
				gchar** _tmp36_;
				gint _tmp36__length1;
				const gchar* _tmp37_;
				guint64 _tmp38_ = 0ULL;
				gboolean _tmp39_;
				gsize* _tmp40_;
				gint _tmp40__length1;
				guint64 _tmp41_;
				_tmp29_ = line;
				_tmp31_ = _tmp30_ = g_strsplit (_tmp29_, ":", 0);
				splitted_line = _tmp31_;
				splitted_line_length1 = _vala_array_length (_tmp30_);
				_splitted_line_size_ = splitted_line_length1;
				_tmp32_ = splitted_line;
				_tmp32__length1 = splitted_line_length1;
				if (_tmp32__length1 != 2) {
					splitted_line = (_vala_array_free (splitted_line, splitted_line_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (line);
					continue;
				}
				_tmp33_ = splitted_line;
				_tmp33__length1 = splitted_line_length1;
				_tmp34_ = _tmp33_[1];
				_tmp35_ = text;
				if (g_strcmp0 (_tmp34_, _tmp35_) != 0) {
					splitted_line = (_vala_array_free (splitted_line, splitted_line_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (line);
					continue;
				}
				_tmp36_ = splitted_line;
				_tmp36__length1 = splitted_line_length1;
				_tmp37_ = _tmp36_[0];
				_tmp39_ = uint64_try_parse (_tmp37_, &_tmp38_);
				offset = _tmp38_;
				if (!_tmp39_) {
					splitted_line = (_vala_array_free (splitted_line, splitted_line_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (line);
					continue;
				}
				_tmp40_ = offsets;
				_tmp40__length1 = offsets_length1;
				_tmp41_ = offset;
				_vala_array_add23 (&offsets, &offsets_length1, &_offsets_size_, (gsize) _tmp41_);
				splitted_line = (_vala_array_free (splitted_line, splitted_line_length1, (GDestroyNotify) g_free), NULL);
				_g_free0 (line);
			}
		}
		line_collection = (_vala_array_free (line_collection, line_collection_length1, (GDestroyNotify) g_free), NULL);
	}
	_tmp42_ = offsets;
	_tmp42__length1 = offsets_length1;
	if (result_length1) {
		*result_length1 = _tmp42__length1;
	}
	result = _tmp42_;
	_g_free0 (output);
	env = (_vala_array_free (env, env_length1, (GDestroyNotify) g_free), NULL);
	args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (working_dir);
	return result;
}


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



