~ubuntu-branches/ubuntu/lucid/gcalctool/lucid-updates

« back to all changes in this revision

Viewing changes to src/calctool.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-04-07 19:41:56 UTC
  • mfrom: (1.3.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407194156-hgj8g002ffce2de0
Tags: 5.30.0.is.5.28.2-0ubuntu1
Downgrade to 5.28.2 as the new version does not support number bases
as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
2
 
 *  Copyright (c) 2008-2009 Robert Ancell
 
1
 
 
2
/*  $Header$
3
3
 *
 
4
 *  Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
 
5
 *           
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)
7
9
 *  any later version.
8
 
 *
9
 
 *  This program is distributed in the hope that it will be useful, but
 
10
 *           
 
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.
13
 
 *
 
15
 *           
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
17
19
 *  02111-1307, USA.
18
20
 */
19
21
 
20
 
#include <stdlib.h>
21
22
#include <stdio.h>
22
23
#include <string.h>
 
24
#include <stdlib.h>
 
25
#include <assert.h>
23
26
#include <sys/types.h>
24
27
#include <glib-object.h>
25
28
 
26
29
#include "calctool.h"
27
 
#include "currency.h"
28
30
#include "unittest.h"
29
31
#include "get.h"
30
32
#include "display.h"
 
33
#include "functions.h"
31
34
#include "ui.h"
 
35
#include "mp.h"
32
36
#include "register.h"
33
37
#include "mp-equation.h"
34
38
 
 
39
time_t time();
 
40
 
35
41
/* Calctool variables and options. */
36
42
static CalculatorVariables calc_state;
37
43
CalculatorVariables *v;
47
53
solve(const char *equation)
48
54
{
49
55
    MPEquationOptions options;
50
 
    MPErrorCode error;
 
56
    int error;
51
57
    MPNumber result;
52
58
    char result_str[MAXLINE];
53
 
 
 
59
    
54
60
    memset(&options, 0, sizeof(options));
 
61
    options.base = 10;
55
62
    options.wordlen = 32;
56
63
    options.angle_units = MP_DEGREES;
57
 
 
58
 
    error = mp_equation_parse(equation, &options, &result, NULL);
59
 
    if(error == PARSER_ERR_MP) {
60
 
        fprintf(stderr, "Error: %s\n", mp_get_error());
61
 
        exit(1);
62
 
    }
63
 
    else if(error != 0) {
64
 
        fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
 
64
    
 
65
    error = mp_equation_parse(equation, &options, &result);
 
66
    if(error != 0) {
 
67
        fprintf(stderr, "Error %d\n", error);
65
68
        exit(1);
66
69
    }
67
70
    else {
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);
70
73
        exit(0);
71
74
    }
74
77
static void
75
78
usage(int show_gtk)
76
79
{
 
80
    /* Translators: Description on how to use gcalctool displayed on command-line */
77
81
    fprintf(stderr,
78
 
            /* Description on how to use gcalctool displayed on command-line */
79
82
            _("Usage:\n"
80
 
              "  %s — Perform mathematical calculations"), v->progname);
 
83
              "  %s - Perform mathematical calculations"), v->progname);
81
84
 
82
85
    fprintf(stderr,
83
86
            "\n\n");
84
87
 
 
88
    /* Translators: Description on gcalctool command-line help options displayed on command-line */
85
89
    fprintf(stderr,
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"));
92
95
    fprintf(stderr,
93
96
            "\n\n");
94
 
 
 
97
    
95
98
    if (show_gtk) {
 
99
        /* Translators: Description on gcalctool command-line GTK+ options displayed on command-line */
96
100
        fprintf(stderr,
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"
106
109
                "\n\n");
107
110
    }
108
111
 
 
112
    /* Translators: Description on gcalctool application options displayed on command-line */    
109
113
    fprintf(stderr,
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"));
114
117
    fprintf(stderr,
115
118
            "\n\n");
120
123
{
121
124
    int i;
122
125
    char *arg;
123
 
 
 
126
   
124
127
    for (i = 1; i < argc; i++) {
125
128
        arg = argv[i];
126
129
 
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) {
129
133
            version();
130
134
            exit(0);
131
135
        }
132
 
        else if (strcmp(arg, "-h") == 0 ||
133
 
                 strcmp(arg, "-?") == 0 ||
 
136
        else if (strcmp(arg, "-h") == 0 || 
134
137
                 strcmp(arg, "--help") == 0) {
135
138
            usage(FALSE);
136
139
            exit(0);
143
146
            strcmp(arg, "--solve") == 0) {
144
147
            i++;
145
148
            if (i >= argc) {
146
 
                fprintf(stderr,
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");
150
152
                exit(1);
151
153
            }
157
159
            unittest();
158
160
        }
159
161
        else {
160
 
            fprintf(stderr,
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");
164
165
            usage(FALSE);
165
166
            exit(1);
171
172
static void
172
173
init_state(void)
173
174
{
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"};
177
 
    gchar **digits;
178
 
    gboolean use_default_digits = FALSE;
179
 
    int i;
180
 
 
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]);
186
 
        }
187
 
        else
188
 
            v->digits[i] = strdup(digits[i]);
189
 
    }
190
 
    g_strfreev(digits);
191
 
 
 
175
    int acc, i;
 
176
 
 
177
    acc              = MAX_DIGITS + 12;     /* MP internal accuracy. */
 
178
 
 
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();
 
183
    
 
184
    if (get_int_resource(R_ACCURACY, &i))
 
185
        v->accuracy = i;
 
186
    else
 
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;
 
194
    }
 
195
 
 
196
    if (get_int_resource(R_BASE, &i))
 
197
       v->base = i;
 
198
    else
 
199
       v->base = 10;
 
200
 
 
201
    if (get_enumerated_resource(R_TRIG, Rtstr, &i))
 
202
       v->ttype = (MPAngleUnit) i;
 
203
    else
 
204
       v->ttype = MP_DEGREES;
 
205
 
 
206
    if (get_int_resource(R_WORDLEN, &i))
 
207
       v->wordlen = i;
 
208
    else
 
209
       v->wordlen = 64;
195
210
}
196
211
 
197
212
 
200
215
{
201
216
    memset(&calc_state, 0, sizeof(calc_state));
202
217
    v = &calc_state;
203
 
 
 
218
    
204
219
    g_type_init();
205
220
 
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);
209
224
 
210
225
    v->progname = g_path_get_basename(argv[0]);
211
 
 
212
 
    /* Seed random number generator. */
 
226
    
 
227
    /* Seed random number generator. */   
213
228
    srand48((long) time((time_t *) 0));
214
 
 
 
229
   
215
230
    resources_init();
216
 
 
 
231
    
217
232
    init_state();
218
233
    register_init();
219
234
    display_init(&v->display);
223
238
 
224
239
    ui_load();
225
240
    ui_start();
226
 
 
227
 
    currency_free_resources();
228
 
 
 
241
    
229
242
    return(0);
230
243
}