/* contacts-io-parse-main.c generated by valac 0.56.3, the Vala compiler
 * generated from contacts-io-parse-main.vala, do not modify */

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define CONTACTS_IO_TYPE_PARSER (contacts_io_parser_get_type ())
#define CONTACTS_IO_PARSER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_IO_TYPE_PARSER, ContactsIoParser))
#define CONTACTS_IO_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_IO_TYPE_PARSER, ContactsIoParserClass))
#define CONTACTS_IO_IS_PARSER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_IO_TYPE_PARSER))
#define CONTACTS_IO_IS_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_IO_TYPE_PARSER))
#define CONTACTS_IO_PARSER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_IO_TYPE_PARSER, ContactsIoParserClass))

typedef struct _ContactsIoParser ContactsIoParser;
typedef struct _ContactsIoParserClass ContactsIoParserClass;

#define CONTACTS_IO_TYPE_VCARD_PARSER (contacts_io_vcard_parser_get_type ())
#define CONTACTS_IO_VCARD_PARSER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_IO_TYPE_VCARD_PARSER, ContactsIoVCardParser))
#define CONTACTS_IO_VCARD_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_IO_TYPE_VCARD_PARSER, ContactsIoVCardParserClass))
#define CONTACTS_IO_IS_VCARD_PARSER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_IO_TYPE_VCARD_PARSER))
#define CONTACTS_IO_IS_VCARD_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_IO_TYPE_VCARD_PARSER))
#define CONTACTS_IO_VCARD_PARSER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_IO_TYPE_VCARD_PARSER, ContactsIoVCardParserClass))

typedef struct _ContactsIoVCardParser ContactsIoVCardParser;
typedef struct _ContactsIoVCardParserClass ContactsIoVCardParserClass;
#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)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))

static gint _vala_main (gchar** args,
                 gint args_length1);
VALA_EXTERN GType contacts_io_parser_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsIoParser, g_object_unref)
VALA_EXTERN ContactsIoVCardParser* contacts_io_vcard_parser_new (void);
VALA_EXTERN ContactsIoVCardParser* contacts_io_vcard_parser_construct (GType object_type);
VALA_EXTERN GType contacts_io_vcard_parser_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContactsIoVCardParser, g_object_unref)
VALA_EXTERN GHashTable** contacts_io_parser_parse (ContactsIoParser* self,
                                       GInputStream* input,
                                       gint* result_length1,
                                       GError** error);
VALA_EXTERN GVariant* contacts_io_serialize_to_gvariant (GHashTable** details_list,
                                             gint details_list_length1);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static gint
_vala_main (gchar** args,
            gint args_length1)
{
	const gchar* import_type = NULL;
	const gchar* _tmp0_;
	const gchar* path = NULL;
	const gchar* _tmp1_;
	ContactsIoParser* parser = NULL;
	const gchar* _tmp2_;
	GQuark _tmp4_ = 0U;
	static GQuark _tmp3_label0 = 0;
	GHashTable** details_list = NULL;
	gint details_list_length1 = 0;
	gint _details_list_size_ = 0;
	GVariant* serialized = NULL;
	GVariant* _tmp16_;
	FILE* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	guint8* _tmp20_;
	gint _tmp20__length1;
	gint _tmp21_ = 0;
	guint8* _tmp22_;
	gint _tmp22__length1;
	GError* _inner_error0_ = NULL;
	gint result;
	if (args_length1 != 3) {
		g_error ("contacts-io-parse-main.vala:22: Expected exactly 2 arguments, but got " \
"%d", args_length1 - 1);
	}
	_tmp0_ = args[1];
	import_type = _tmp0_;
	if (g_strcmp0 (import_type, "") == 0) {
		g_error ("contacts-io-parse-main.vala:26: Invalid import type: got empty import " \
"type");
	}
	_tmp1_ = args[2];
	path = _tmp1_;
	if (g_strcmp0 (path, "") == 0) {
		g_error ("contacts-io-parse-main.vala:30: Invalid path: path is empty");
	}
	_tmp2_ = import_type;
	_tmp4_ = (NULL == _tmp2_) ? 0 : g_quark_from_string (_tmp2_);
	if (_tmp4_ == ((0 != _tmp3_label0) ? _tmp3_label0 : (_tmp3_label0 = g_quark_from_static_string ("vcard")))) {
		switch (0) {
			default:
			{
				ContactsIoVCardParser* _tmp5_;
				_tmp5_ = contacts_io_vcard_parser_new ();
				_g_object_unref0 (parser);
				parser = (ContactsIoParser*) _tmp5_;
				break;
			}
		}
	} else {
		switch (0) {
			default:
			{
				g_error ("contacts-io-parse-main.vala:38: Unknown import type '%s'", import_type);
			}
		}
	}
	{
		GFile* file = NULL;
		GFile* _tmp6_;
		GFileInputStream* file_stream = NULL;
		GFile* _tmp7_;
		GFileInputStream* _tmp8_;
		GHashTable** _tmp9_ = NULL;
		GFileInputStream* _tmp10_;
		gint _tmp11_ = 0;
		GHashTable** _tmp12_;
		gint _tmp9__length1;
		gint __tmp9__size_;
		GHashTable** _tmp13_;
		gint _tmp13__length1;
		_tmp6_ = g_file_new_for_path (path);
		file = _tmp6_;
		_tmp7_ = file;
		_tmp8_ = g_file_read (_tmp7_, NULL, &_inner_error0_);
		file_stream = _tmp8_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp10_ = file_stream;
		_tmp12_ = contacts_io_parser_parse (parser, (GInputStream*) _tmp10_, &_tmp11_, &_inner_error0_);
		_tmp9_ = _tmp12_;
		_tmp9__length1 = _tmp11_;
		__tmp9__size_ = _tmp9__length1;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file_stream);
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp13_ = _tmp9_;
		_tmp13__length1 = _tmp9__length1;
		_tmp9_ = NULL;
		_tmp9__length1 = 0;
		details_list = (_vala_array_free (details_list, details_list_length1, (GDestroyNotify) g_hash_table_unref), NULL);
		details_list = _tmp13_;
		details_list_length1 = _tmp13__length1;
		_details_list_size_ = details_list_length1;
		_tmp9_ = (_vala_array_free (_tmp9_, _tmp9__length1, (GDestroyNotify) g_hash_table_unref), NULL);
		_g_object_unref0 (file_stream);
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		const gchar* _tmp14_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp14_ = err->message;
		g_error ("contacts-io-parse-main.vala:47: Error while importing file '%s': %s", path, _tmp14_);
		_g_error_free0 (err);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gint _tmp15_ = -1;
		details_list = (_vala_array_free (details_list, details_list_length1, (GDestroyNotify) g_hash_table_unref), NULL);
		_g_object_unref0 (parser);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return _tmp15_;
	}
	_tmp16_ = contacts_io_serialize_to_gvariant (details_list, (gint) details_list_length1);
	serialized = _tmp16_;
	_tmp17_ = stdout;
	_tmp18_ = g_variant_print (serialized, FALSE);
	_tmp19_ = _tmp18_;
	_tmp20_ = string_get_data (_tmp19_, &_tmp21_);
	_tmp20__length1 = _tmp21_;
	_tmp22_ = _tmp20_;
	_tmp22__length1 = _tmp20__length1;
	fwrite (_tmp22_, (gsize) 1, (gint) _tmp22__length1, _tmp17_);
	_g_free0 (_tmp19_);
	result = 0;
	_g_variant_unref0 (serialized);
	details_list = (_vala_array_free (details_list, details_list_length1, (GDestroyNotify) g_hash_table_unref), NULL);
	_g_object_unref0 (parser);
	return result;
}

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

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize 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,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

