/* gcalccmd.c generated by valac 0.34.4, 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 <glib-object.h>
#include "libcalculator.h"
#include <stdlib.h>
#include <string.h>
#include <langinfo.h>
#include <stdio.h>
#include <locale.h>

#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_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (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)))


extern Serializer* result_serializer;
Serializer* result_serializer = NULL;

#define MAXLINE 1024
void solve (const gchar* equation);
gint _vala_main (gchar** args, int args_length1);


static gchar* string_replace (const gchar* self, const gchar* old, const gchar* replacement) {
	gchar* result = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	{
		GRegex* regex = NULL;
		const gchar* _tmp0_ = NULL;
		gchar* _tmp1_ = NULL;
		gchar* _tmp2_ = NULL;
		GRegex* _tmp3_ = NULL;
		GRegex* _tmp4_ = NULL;
		gchar* _tmp5_ = NULL;
		GRegex* _tmp6_ = NULL;
		const gchar* _tmp7_ = NULL;
		gchar* _tmp8_ = NULL;
		gchar* _tmp9_ = NULL;
		_tmp0_ = old;
		_tmp1_ = g_regex_escape_string (_tmp0_, -1);
		_tmp2_ = _tmp1_;
		_tmp3_ = g_regex_new (_tmp2_, 0, 0, &_inner_error_);
		_tmp4_ = _tmp3_;
		_g_free0 (_tmp2_);
		regex = _tmp4_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			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;
		}
		_tmp6_ = regex;
		_tmp7_ = replacement;
		_tmp8_ = g_regex_replace_literal (_tmp6_, self, (gssize) -1, 0, _tmp7_, 0, &_inner_error_);
		_tmp5_ = _tmp8_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			_g_regex_unref0 (regex);
			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;
		}
		_tmp9_ = _tmp5_;
		_tmp5_ = NULL;
		result = _tmp9_;
		_g_free0 (_tmp5_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		GError* e = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_assert_not_reached ();
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		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;
	}
}


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


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


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	GDateTime* now = NULL;
	GDateTime* _tmp0_ = NULL;
	gboolean requires_new_line = FALSE;
	GDateTime* _tmp1_ = NULL;
	gint _tmp2_ = 0;
	Serializer* _tmp3_ = NULL;
	gchar* buffer = NULL;
	gchar* _tmp4_ = NULL;
	gint buffer_length1 = 0;
	gint _buffer_size_ = 0;
	gboolean _tmp21_ = FALSE;
	_tmp0_ = g_date_time_new_now_utc ();
	now = _tmp0_;
	requires_new_line = FALSE;
	_tmp1_ = now;
	_tmp2_ = g_date_time_get_microsecond (_tmp1_);
	g_random_set_seed ((guint32) _tmp2_);
	setlocale (LC_ALL, "");
	_tmp3_ = serializer_new (DISPLAY_FORMAT_AUTOMATIC, 10, 9);
	_g_object_unref0 (result_serializer);
	result_serializer = _tmp3_;
	_tmp4_ = g_new0 (gchar, 1024);
	buffer = _tmp4_;
	buffer_length1 = 1024;
	_buffer_size_ = buffer_length1;
	while (TRUE) {
		FILE* _tmp5_ = NULL;
		gchar* line = NULL;
		FILE* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		gint _tmp7__length1 = 0;
		const gchar* _tmp8_ = NULL;
		gchar* _tmp9_ = NULL;
		const gchar* _tmp10_ = NULL;
		gboolean _tmp13_ = FALSE;
		gboolean _tmp14_ = FALSE;
		gboolean _tmp15_ = FALSE;
		const gchar* _tmp16_ = NULL;
		const gchar* _tmp20_ = NULL;
		_tmp5_ = stdout;
		fprintf (_tmp5_, "> ");
		_tmp6_ = stdin;
		_tmp7_ = buffer;
		_tmp7__length1 = buffer_length1;
		_tmp8_ = fgets (_tmp7_, _tmp7__length1, _tmp6_);
		_tmp9_ = g_strdup (_tmp8_);
		line = _tmp9_;
		_tmp10_ = line;
		if (_tmp10_ != NULL) {
			const gchar* _tmp11_ = NULL;
			gchar* _tmp12_ = NULL;
			_tmp11_ = line;
			_tmp12_ = string_strip (_tmp11_);
			_g_free0 (line);
			line = _tmp12_;
		} else {
			requires_new_line = TRUE;
		}
		_tmp16_ = line;
		if (_tmp16_ == NULL) {
			_tmp15_ = TRUE;
		} else {
			const gchar* _tmp17_ = NULL;
			_tmp17_ = line;
			_tmp15_ = g_strcmp0 (_tmp17_, "exit") == 0;
		}
		if (_tmp15_) {
			_tmp14_ = TRUE;
		} else {
			const gchar* _tmp18_ = NULL;
			_tmp18_ = line;
			_tmp14_ = g_strcmp0 (_tmp18_, "quit") == 0;
		}
		if (_tmp14_) {
			_tmp13_ = TRUE;
		} else {
			const gchar* _tmp19_ = NULL;
			_tmp19_ = line;
			_tmp13_ = g_strcmp0 (_tmp19_, "") == 0;
		}
		if (_tmp13_) {
			_g_free0 (line);
			break;
		}
		_tmp20_ = line;
		solve (_tmp20_);
		_g_free0 (line);
	}
	_tmp21_ = requires_new_line;
	if (_tmp21_) {
		FILE* _tmp22_ = NULL;
		_tmp22_ = stdout;
		fprintf (_tmp22_, "\n");
	}
	result = EXIT_SUCCESS;
	buffer = (g_free (buffer), NULL);
	_g_date_time_unref0 (now);
	return result;
}


int main (int argc, char ** argv) {
#if !GLIB_CHECK_VERSION (2,35,0)
	g_type_init ();
#endif
	return _vala_main (argv, argc);
}



