/* gio-async-read.c generated by valac 0.11.3, the Vala compiler
 * generated from gio-async-read.vala, do not modify */


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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
typedef struct _ReadSomethingAsyncData ReadSomethingAsyncData;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))

struct _ReadSomethingAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	GFile* file;
	GString* _tmp0_;
	GString* text;
	GFileInputStream* _tmp1_;
	GFileInputStream* _tmp2_;
	GFileInputStream* _tmp3_;
	GDataInputStream* _tmp4_;
	GDataInputStream* _tmp5_;
	GDataInputStream* dis;
	gchar* line;
	gsize length;
	gsize* _tmp6_;
	gchar* _tmp7_;
	gsize* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	GError * e;
	GError * _inner_error_;
};


extern GMainLoop* main_loop;
GMainLoop* main_loop = NULL;

static void read_something_async_data_free (gpointer _data);
void read_something_async (GFile* file, GAsyncReadyCallback _callback_, gpointer _user_data_);
void read_something_finish (GAsyncResult* _res_);
static gboolean read_something_async_co (ReadSomethingAsyncData* data);
static void read_something_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
void _vala_main (gchar** args, int args_length1);


static void read_something_async_data_free (gpointer _data) {
	ReadSomethingAsyncData* data;
	data = _data;
	_g_object_unref0 (data->file);
	g_slice_free (ReadSomethingAsyncData, data);
}


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


void read_something_async (GFile* file, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	ReadSomethingAsyncData* _data_;
	_data_ = g_slice_new0 (ReadSomethingAsyncData);
	_data_->_async_result = g_simple_async_result_new (g_object_newv (G_TYPE_OBJECT, 0, NULL), _callback_, _user_data_, read_something_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, read_something_async_data_free);
	_data_->file = _g_object_ref0 (file);
	read_something_async_co (_data_);
}


void read_something_finish (GAsyncResult* _res_) {
	ReadSomethingAsyncData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void read_something_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	ReadSomethingAsyncData* data;
	data = _user_data_;
	data->_source_object_ = source_object;
	data->_res_ = _res_;
	read_something_async_co (data);
}


static gboolean read_something_async_co (ReadSomethingAsyncData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	data->_tmp0_ = NULL;
	data->_tmp0_ = g_string_new ("");
	data->text = data->_tmp0_;
	g_print ("Start...\n");
	data->_tmp1_ = NULL;
	data->_tmp1_ = g_file_read (data->file, NULL, &data->_inner_error_);
	data->_tmp2_ = data->_tmp1_;
	if (data->_inner_error_ != NULL) {
		goto __catch0_g_error;
	}
	data->_tmp3_ = data->_tmp2_;
	data->_tmp4_ = NULL;
	data->_tmp4_ = g_data_input_stream_new ((GInputStream*) data->_tmp3_);
	data->dis = (data->_tmp5_ = data->_tmp4_, _g_object_unref0 (data->_tmp3_), data->_tmp5_);
	data->line = NULL;
	data->length = (gsize) 0;
	while (TRUE) {
		data->_tmp6_ = NULL;
		data->_state_ = 1;
		g_data_input_stream_read_line_async (data->dis, G_PRIORITY_DEFAULT, NULL, read_something_async_ready, data);
		return FALSE;
		_state_1:
		data->_tmp7_ = NULL;
		data->_tmp7_ = g_data_input_stream_read_line_finish (data->dis, data->_res_, &data->_tmp6_, &data->_inner_error_);
		data->_tmp8_ = data->_tmp6_;
		data->length = *data->_tmp8_;
		data->_tmp10_ = (data->_tmp9_ = data->_tmp7_, _g_free0 (data->_tmp8_), data->_tmp9_);
		if (data->_inner_error_ != NULL) {
			_g_free0 (data->line);
			_g_object_unref0 (data->dis);
			goto __catch0_g_error;
		}
		data->_tmp11_ = data->_tmp10_;
		_g_free0 (data->line);
		data->line = data->_tmp11_;
		if (!(data->line != NULL)) {
			break;
		}
		data->_tmp12_ = NULL;
		data->_tmp12_ = g_strdup_printf ("line length: %lu)\n", (gulong) data->length);
		data->_tmp13_ = data->_tmp12_;
		g_string_append (data->text, data->_tmp13_);
		_g_free0 (data->_tmp13_);
		g_string_append (data->text, data->line);
		g_string_append_c (data->text, '\n');
	}
	g_print ("%s", data->text->str);
	_g_free0 (data->line);
	_g_object_unref0 (data->dis);
	goto __finally0;
	__catch0_g_error:
	{
		data->e = data->_inner_error_;
		data->_inner_error_ = NULL;
		g_error ("gio-async-read.vala:18: %s", data->e->message);
		_g_error_free0 (data->e);
	}
	__finally0:
	if (data->_inner_error_ != NULL) {
		_g_string_free0 (data->text);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, data->_inner_error_->message, g_quark_to_string (data->_inner_error_->domain), data->_inner_error_->code);
		g_clear_error (&data->_inner_error_);
		return FALSE;
	}
	g_main_loop_quit (main_loop);
	_g_string_free0 (data->text);
	if (data->_state_ == 0) {
		g_simple_async_result_complete_in_idle (data->_async_result);
	} else {
		g_simple_async_result_complete (data->_async_result);
	}
	g_object_unref (data->_async_result);
	return FALSE;
}


void _vala_main (gchar** args, int args_length1) {
	GFile* _tmp0_ = NULL;
	GFile* file;
	GMainLoop* _tmp3_ = NULL;
	GMainLoop* _tmp4_;
	_tmp0_ = g_file_new_for_uri ("http://www.gnome.org");
	file = _tmp0_;
	if (args_length1 > 1) {
		GFile* _tmp1_ = NULL;
		GFile* _tmp2_;
		_tmp1_ = g_file_new_for_commandline_arg (args[1]);
		_tmp2_ = _tmp1_;
		_g_object_unref0 (file);
		file = _tmp2_;
	}
	_tmp3_ = g_main_loop_new (NULL, FALSE);
	_tmp4_ = _tmp3_;
	_g_main_loop_unref0 (main_loop);
	main_loop = _tmp4_;
	read_something_async (file, NULL, NULL);
	g_main_loop_run (main_loop);
	_g_object_unref0 (file);
}


int main (int argc, char ** argv) {
	g_type_init ();
	_vala_main (argv, argc);
	return 0;
}



