1
/* Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
2
* Copyright (c) 2008-2009 Robert Ancell
4
* Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
4
6
* This program is free software; you can redistribute it and/or modify
5
7
* it under the terms of the GNU General Public License as published by
6
8
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful, but
11
* This program is distributed in the hope that it will be useful, but
10
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
14
* General Public License for more details.
14
16
* You should have received a copy of the GNU General Public License
15
17
* along with this program; if not, write to the Free Software
16
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
23
#include <string.h>
23
26
#include <sys/types.h>
24
27
#include <glib-object.h>
26
29
#include "calctool.h"
28
30
#include "unittest.h"
30
32
#include "display.h"
33
#include "functions.h"
32
36
#include "register.h"
33
37
#include "mp-equation.h"
35
41
/* Calctool variables and options. */
36
42
static CalculatorVariables calc_state;
37
43
CalculatorVariables *v;
47
53
solve(const char *equation)
49
55
MPEquationOptions options;
52
58
char result_str[MAXLINE];
54
60
memset(&options, 0, sizeof(options));
55
62
options.wordlen = 32;
56
63
options.angle_units = MP_DEGREES;
58
error = mp_equation_parse(equation, &options, &result, NULL);
59
if(error == PARSER_ERR_MP) {
60
fprintf(stderr, "Error: %s\n", mp_get_error());
64
fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
65
error = mp_equation_parse(equation, &options, &result);
67
fprintf(stderr, "Error %d\n", error);
68
mp_cast_to_string(&result, 10, 9, 1, result_str, MAXLINE);
71
mp_cast_to_string(&result, v->base, 9, 1, result_str, MAXLINE);
69
72
printf("%s\n", result_str);
75
78
usage(int show_gtk)
80
/* Translators: Description on how to use gcalctool displayed on command-line */
78
/* Description on how to use gcalctool displayed on command-line */
80
" %s — Perform mathematical calculations"), v->progname);
83
" %s - Perform mathematical calculations"), v->progname);
88
/* Translators: Description on gcalctool command-line help options displayed on command-line */
86
/* Description on gcalctool command-line help options displayed on command-line */
87
90
_("Help Options:\n"
88
91
" -v, --version Show release version\n"
89
92
" -h, -?, --help Show help options\n"
91
94
" --help-gtk Show GTK+ options"));
99
/* Translators: Description on gcalctool command-line GTK+ options displayed on command-line */
97
/* Description on gcalctool command-line GTK+ options displayed on command-line */
98
101
_("GTK+ Options:\n"
99
102
" --class=CLASS Program class as used by the window manager\n"
100
103
" --name=NAME Program name as used by the window manager\n"
112
/* Translators: Description on gcalctool application options displayed on command-line */
110
/* Description on gcalctool application options displayed on command-line */
111
114
_("Application Options:\n"
112
" -u, --unittest Perform unit tests\n"
115
" -u, --unittest Perform unittests\n"
113
116
" -s, --solve <equation> Solve the given equation"));
124
127
for (i = 1; i < argc; i++) {
127
if (strcmp(arg, "-v") == 0 ||
128
strcmp(arg, "--version") == 0) {
130
if (strcmp(arg, "-v") == 0 ||
131
strcmp(arg, "--version") == 0 ||
132
strcmp(arg, "-?") == 0) {
132
else if (strcmp(arg, "-h") == 0 ||
133
strcmp(arg, "-?") == 0 ||
136
else if (strcmp(arg, "-h") == 0 ||
134
137
strcmp(arg, "--help") == 0) {
143
146
strcmp(arg, "--solve") == 0) {
147
/* Error printed to stderr when user uses --solve argument without an equation */
148
_("Argument --solve requires an equation to solve"));
149
/* Translators: Error printed to stderr when user uses --solve argument without an equation */
150
fprintf(stderr, _("Argument --solve requires an equation to solve"));
149
151
fprintf(stderr, "\n");
161
/* Error printed to stderr when user provides an unknown command-line argument */
162
_("Unknown argument '%s'"), arg);
162
/* Translators: Error printed to stderr when user provides an unknown command-line argument */
163
fprintf(stderr, _("Unknown argument '%s'"), arg);
163
164
fprintf(stderr, "\n");
174
/* Translators: Digits localized for the given language */
175
const char *digit_values = _("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
176
const char *default_digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
178
gboolean use_default_digits = FALSE;
181
digits = g_strsplit(digit_values, ",", -1);
182
for (i = 0; i < 16; i++) {
183
if (use_default_digits || digits[i] == NULL) {
184
use_default_digits = TRUE;
185
v->digits[i] = strdup(default_digits[i]);
188
v->digits[i] = strdup(digits[i]);
177
acc = MAX_DIGITS + 12; /* MP internal accuracy. */
179
v->error = FALSE; /* No calculator error initially. */
192
180
v->radix = get_radix(); /* Locale specific radix string. */
193
181
v->tsep = get_tsep(); /* Locale specific thousands separator. */
194
182
v->tsep_count = get_tsep_count();
184
if (get_int_resource(R_ACCURACY, &i))
187
v->accuracy = DEFAULT_ACCURACY;
188
if (v->accuracy < 0 || v->accuracy > MAXACC) {
189
/* Translators: A log message displayed when an invalid accuracy
190
is read from the configuration */
191
fprintf(stderr, _("%s: accuracy should be in the range 0-%d\n"),
192
v->progname, MAXACC);
193
v->accuracy = DEFAULT_ACCURACY;
196
if (get_int_resource(R_BASE, &i))
201
if (get_enumerated_resource(R_TRIG, Rtstr, &i))
202
v->ttype = (MPAngleUnit) i;
204
v->ttype = MP_DEGREES;
206
if (get_int_resource(R_WORDLEN, &i))
201
216
memset(&calc_state, 0, sizeof(calc_state));
206
bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
221
bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
207
222
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
208
223
textdomain(GETTEXT_PACKAGE);
210
225
v->progname = g_path_get_basename(argv[0]);
212
/* Seed random number generator. */
227
/* Seed random number generator. */
213
228
srand48((long) time((time_t *) 0));
215
230
resources_init();
219
234
display_init(&v->display);