/* gcalccmd.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from gcalccmd.vala, do not modify */

/*
 * Copyright (C) 2009 Rich Burridge
 * Copyright (C) 2012 Robert Ancell
 *
 * 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 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <glib.h>
#include "calculator.h"
#include <stdlib.h>
#include <string.h>
#include <langinfo.h>
#include <stdio.h>
#include <locale.h>
#include <gio/gio.h>

#define MAXLINE 1024
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#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 _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))

VALA_EXTERN Serializer* result_serializer;
Serializer* result_serializer = NULL;

VALA_EXTERN void solve (const gchar* equation);
static gint _vala_main (gchar** args,
                 gint args_length1);

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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 NULL;
}

void
solve (const gchar* equation)
{
	gchar* tsep_string = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gboolean _tmp2_ = FALSE;
	const gchar* _tmp3_;
	gchar* decimal = NULL;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* error_token = NULL;
	Equation* e = NULL;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	const gchar* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	Equation* _tmp18_;
	Equation* _tmp19_;
	Equation* _tmp20_;
	Equation* _tmp21_;
	Equation* _tmp22_;
	ErrorCode ret = 0;
	guint representation_base = 0U;
	Number* z = NULL;
	Equation* _tmp23_;
	guint _tmp24_ = 0U;
	ErrorCode _tmp25_ = 0;
	gchar* _tmp26_ = NULL;
	Number* _tmp27_;
	Serializer* _tmp28_;
	Number* _tmp29_;
	g_return_if_fail (equation != NULL);
	_tmp0_ = nl_langinfo (THOUSEP);
	_tmp1_ = g_strdup (_tmp0_);
	tsep_string = _tmp1_;
	_tmp3_ = tsep_string;
	if (_tmp3_ == NULL) {
		_tmp2_ = TRUE;
	} else {
		const gchar* _tmp4_;
		_tmp4_ = tsep_string;
		_tmp2_ = g_strcmp0 (_tmp4_, "") == 0;
	}
	if (_tmp2_) {
		gchar* _tmp5_;
		_tmp5_ = g_strdup (" ");
		_g_free0 (tsep_string);
		tsep_string = _tmp5_;
	} else {
		const gchar* _tmp6_;
		_tmp6_ = tsep_string;
		if (g_strcmp0 (_tmp6_, ".") == 0) {
			gchar* _tmp7_;
			_tmp7_ = g_strdup ("");
			_g_free0 (tsep_string);
			tsep_string = _tmp7_;
		}
	}
	_tmp8_ = nl_langinfo (RADIXCHAR);
	_tmp9_ = g_strdup (_tmp8_);
	decimal = _tmp9_;
	_tmp10_ = decimal;
	if (_tmp10_ == NULL) {
		gchar* _tmp11_;
		_tmp11_ = g_strdup ("");
		_g_free0 (decimal);
		decimal = _tmp11_;
	}
	error_token = NULL;
	_tmp12_ = tsep_string;
	_tmp13_ = string_replace (equation, _tmp12_, "");
	_tmp14_ = _tmp13_;
	_tmp15_ = decimal;
	_tmp16_ = string_replace (_tmp14_, _tmp15_, ".");
	_tmp17_ = _tmp16_;
	_tmp18_ = equation_new (_tmp17_);
	_tmp19_ = _tmp18_;
	_g_free0 (_tmp17_);
	_g_free0 (_tmp14_);
	e = _tmp19_;
	_tmp20_ = e;
	_tmp20_->base = 10;
	_tmp21_ = e;
	_tmp21_->wordlen = 32;
	_tmp22_ = e;
	_tmp22_->angle_units = ANGLE_UNIT_DEGREES;
	_tmp23_ = e;
	_tmp27_ = equation_parse (_tmp23_, &_tmp24_, &_tmp25_, &_tmp26_, NULL, NULL);
	representation_base = _tmp24_;
	ret = _tmp25_;
	_g_free0 (error_token);
	error_token = _tmp26_;
	z = _tmp27_;
	_tmp28_ = result_serializer;
	serializer_set_representation_base (_tmp28_, representation_base);
	_tmp29_ = z;
	if (_tmp29_ != NULL) {
		gchar* str = NULL;
		Serializer* _tmp30_;
		Number* _tmp31_;
		gchar* _tmp32_;
		Serializer* _tmp33_;
		const gchar* _tmp34_;
		const gchar* _tmp35_;
		_tmp30_ = result_serializer;
		_tmp31_ = z;
		_tmp32_ = serializer_to_string (_tmp30_, _tmp31_);
		str = _tmp32_;
		_tmp33_ = result_serializer;
		_tmp34_ = serializer_get_error (_tmp33_);
		_tmp35_ = _tmp34_;
		if (_tmp35_ != NULL) {
			FILE* _tmp36_;
			Serializer* _tmp37_;
			const gchar* _tmp38_;
			const gchar* _tmp39_;
			Serializer* _tmp40_;
			_tmp36_ = stderr;
			_tmp37_ = result_serializer;
			_tmp38_ = serializer_get_error (_tmp37_);
			_tmp39_ = _tmp38_;
			fprintf (_tmp36_, "%s\n", _tmp39_);
			_tmp40_ = result_serializer;
			serializer_set_error (_tmp40_, NULL);
		} else {
			FILE* _tmp41_;
			const gchar* _tmp42_;
			_tmp41_ = stdout;
			_tmp42_ = str;
			fprintf (_tmp41_, "%s\n", _tmp42_);
		}
		_g_free0 (str);
	} else {
		if (ret == ERROR_CODE_MP) {
			const gchar* _tmp43_ = NULL;
			const gchar* _tmp44_;
			const gchar* _tmp45_;
			FILE* _tmp49_;
			_tmp44_ = number_get_error ();
			_tmp45_ = _tmp44_;
			if (_tmp45_ != NULL) {
				const gchar* _tmp46_;
				const gchar* _tmp47_;
				_tmp46_ = number_get_error ();
				_tmp47_ = _tmp46_;
				_tmp43_ = _tmp47_;
			} else {
				const gchar* _tmp48_;
				_tmp48_ = error_token;
				_tmp43_ = _tmp48_;
			}
			_tmp49_ = stderr;
			fprintf (_tmp49_, "Error %s\n", _tmp43_);
		} else {
			FILE* _tmp50_;
			_tmp50_ = stderr;
			fprintf (_tmp50_, "Error %d\n", (gint) ret);
		}
	}
	_g_object_unref0 (z);
	_g_object_unref0 (e);
	_g_free0 (error_token);
	_g_free0 (decimal);
	_g_free0 (tsep_string);
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static gint
_vala_main (gchar** args,
            gint args_length1)
{
	GDateTime* now = NULL;
	GDateTime* _tmp0_;
	gboolean requires_new_line = FALSE;
	GDateTime* _tmp1_;
	Serializer* _tmp2_;
	GSettings* settings = NULL;
	GSettings* _tmp3_;
	CurrencyManager* _tmp4_;
	CurrencyManager* _tmp5_;
	GSettings* _tmp6_;
	CurrencyManager* _tmp7_;
	CurrencyManager* _tmp8_;
	gchar* buffer = NULL;
	gchar* _tmp10_;
	gint buffer_length1;
	gint _buffer_size_;
	gint result;
	_tmp0_ = g_date_time_new_now_utc ();
	now = _tmp0_;
	requires_new_line = FALSE;
	_tmp1_ = now;
	g_random_set_seed ((guint32) g_date_time_get_microsecond (_tmp1_));
	setlocale (LC_ALL, "");
	_tmp2_ = serializer_new (DISPLAY_FORMAT_AUTOMATIC, 10, 9);
	_g_object_unref0 (result_serializer);
	result_serializer = _tmp2_;
	_tmp3_ = g_settings_new ("org.gnome.calculator");
	settings = _tmp3_;
	_tmp4_ = currency_manager_get_default (FALSE, TRUE);
	_tmp5_ = _tmp4_;
	_tmp6_ = settings;
	currency_manager_set_refresh_interval (_tmp5_, g_settings_get_int (_tmp6_, "refresh-interval"));
	_g_object_unref0 (_tmp5_);
	_tmp7_ = currency_manager_get_default (FALSE, TRUE);
	_tmp8_ = _tmp7_;
	currency_manager_refresh_sync (_tmp8_);
	_g_object_unref0 (_tmp8_);
	if (args_length1 == 2) {
		const gchar* _tmp9_;
		_tmp9_ = args[1];
		solve (_tmp9_);
		result = EXIT_SUCCESS;
		_g_object_unref0 (settings);
		_g_date_time_unref0 (now);
		return result;
	}
	_tmp10_ = g_new0 (gchar, 1024);
	buffer = _tmp10_;
	buffer_length1 = 1024;
	_buffer_size_ = buffer_length1;
	while (TRUE) {
		FILE* _tmp11_;
		gchar* line = NULL;
		FILE* _tmp12_;
		gchar* _tmp13_;
		gint _tmp13__length1;
		const gchar* _tmp14_;
		gchar* _tmp15_;
		const gchar* _tmp16_;
		gboolean _tmp19_ = FALSE;
		gboolean _tmp20_ = FALSE;
		gboolean _tmp21_ = FALSE;
		const gchar* _tmp22_;
		const gchar* _tmp26_;
		_tmp11_ = stdout;
		fprintf (_tmp11_, "> ");
		_tmp12_ = stdin;
		_tmp13_ = buffer;
		_tmp13__length1 = buffer_length1;
		_tmp14_ = fgets (_tmp13_, (gint) _tmp13__length1, _tmp12_);
		_tmp15_ = g_strdup (_tmp14_);
		line = _tmp15_;
		_tmp16_ = line;
		if (_tmp16_ != NULL) {
			const gchar* _tmp17_;
			gchar* _tmp18_;
			_tmp17_ = line;
			_tmp18_ = string_strip (_tmp17_);
			_g_free0 (line);
			line = _tmp18_;
		} else {
			requires_new_line = TRUE;
		}
		_tmp22_ = line;
		if (_tmp22_ == NULL) {
			_tmp21_ = TRUE;
		} else {
			const gchar* _tmp23_;
			_tmp23_ = line;
			_tmp21_ = g_strcmp0 (_tmp23_, "exit") == 0;
		}
		if (_tmp21_) {
			_tmp20_ = TRUE;
		} else {
			const gchar* _tmp24_;
			_tmp24_ = line;
			_tmp20_ = g_strcmp0 (_tmp24_, "quit") == 0;
		}
		if (_tmp20_) {
			_tmp19_ = TRUE;
		} else {
			const gchar* _tmp25_;
			_tmp25_ = line;
			_tmp19_ = g_strcmp0 (_tmp25_, "") == 0;
		}
		if (_tmp19_) {
			_g_free0 (line);
			break;
		}
		_tmp26_ = line;
		solve (_tmp26_);
		_g_free0 (line);
	}
	if (requires_new_line) {
		FILE* _tmp27_;
		_tmp27_ = stdout;
		fprintf (_tmp27_, "\n");
	}
	result = EXIT_SUCCESS;
	buffer = (g_free (buffer), NULL);
	_g_object_unref0 (settings);
	_g_date_time_unref0 (now);
	return result;
}

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

