~ubuntu-branches/ubuntu/karmic/gcalctool/karmic-updates

« back to all changes in this revision

Viewing changes to gcalctool/register.c

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2008-12-26 16:20:06 UTC
  • mfrom: (1.1.46 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226162006-brnuirfez3bznt4i
Tags: 5.25.3-0ubuntu1
* New upstream version (LP: #312125).
  - Bitcalculating extension calculate value properly (LP: #200590).
  - Usage of correct operator precedence (LP: #302115).
  - Allow switch to rad (LP: #306702).

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 *  02111-1307, USA.
20
20
 */
21
21
 
 
22
#include <stdio.h>
 
23
 
22
24
#include "register.h"
23
25
#include "calctool.h"
24
 
 
25
 
void
26
 
do_sto_reg(int reg, int value[MP_SIZE])
27
 
{
28
 
    if ((reg >= 0) && (reg <= 10))
29
 
        mp_set_from_mp(value, v->MPmvals[reg]);
30
 
}
31
 
 
32
 
 
33
 
void
34
 
do_rcl_reg(int reg, int value[MP_SIZE])
35
 
{
36
 
    if ((reg >= 0) && (reg <= 10))
37
 
        mp_set_from_mp(v->MPmvals[reg], value);
 
26
#include "get.h"
 
27
#include "mp.h"
 
28
 
 
29
static char constant_names[MAX_CONSTANTS][MAXLINE];  /* Selectable constant names. */
 
30
static int constant_values[MAX_CONSTANTS][MP_SIZE];  /* Selectable constants. */
 
31
 
 
32
static char function_names[MAX_FUNCTIONS][MAXLINE];  /* Function names from .gcalctoolcf. */
 
33
static char function_values[MAX_FUNCTIONS][MAXLINE];   /* Function defs from .gcalctoolcf. */
 
34
 
 
35
static int registers[MAX_REGISTERS][MP_SIZE];     /* Memory register values. */
 
36
 
 
37
static const char *default_constants[][2] =
 
38
{
 
39
    /* Translators: This is the label for the default constant, the number of miles in one kilometer (0.621) */
 
40
    { N_("Kilometer-to-mile conversion factor"), "0.621" },
 
41
    /* Translators: This is the label for the default constant, the square root of 2 (1.41421) */
 
42
    { N_("square root of 2"), "1.4142135623" },
 
43
    /* Translators: This is the label for the default constant, Euler's number (2.71828) */
 
44
    { N_("Euler's Number (e)"), "2.7182818284" },
 
45
    /* Translators: This is the label for the default constant, π (3.14159) */
 
46
    { N_("π"), "3.1415926536" },
 
47
    /* Translators: This is the label for the default constant, the number of inches in a centimeter (0.39370) */
 
48
    { N_("Centimeter-to-inch conversion factor"), "0.3937007" },
 
49
    /* Translators: This is the label for the default constant, the number of degrees in a radian (57.2958) */
 
50
    { N_("degrees in a radian"), "57.295779513" },
 
51
    /* Translators: This is the label for the default constant, 2 to the power of 20 (1048576) */
 
52
    { N_("2 ^ 20"), "1048576.0" },
 
53
    /* Translators: This is the label for the default constant, the number of ounces in one gram (0.0353) */
 
54
    { N_("Gram-to-ounce conversion factor"), "0.0353" },
 
55
    /* Translators: This is the label for the default constant, the number of British Thermal Units in one Kilojoule (0.948) */
 
56
    { N_("Kilojoule-to-British-thermal-unit conversion factor"), "0.948" },
 
57
    /* Translators: This is the label for the default constant, the number of cubic inches in one cubic centimeter (0.0610) */
 
58
    { N_("Cubic-centimeter-to-cubic-inch conversion factor"), "0.0610" }
 
59
};
 
60
 
 
61
void register_init()
 
62
{
 
63
    int i;
 
64
    for (i = 0; i < MAX_REGISTERS; i++) {
 
65
        mp_set_from_integer(0, registers[i]);
 
66
    }
 
67
    
 
68
    for (i = 0; i < MAX_CONSTANTS; i++) {
 
69
        char nkey[MAXLINE], *nline;
 
70
        char vkey[MAXLINE], *vline = NULL;
 
71
        int value[MP_SIZE];
 
72
 
 
73
        SNPRINTF(nkey, MAXLINE, "constant%1dname", i);
 
74
        nline = get_resource(nkey);
 
75
        if (nline) {
 
76
            SNPRINTF(vkey, MAXLINE, "constant%1dvalue", i);
 
77
            vline = get_resource(vkey);
 
78
            if (vline == NULL)
 
79
                g_free(nline);
 
80
        }
 
81
 
 
82
        if (nline && vline) {
 
83
            mp_set_from_string(vline, 10, value);
 
84
            constant_set(i, nline, value);
 
85
            g_free(nline);
 
86
            g_free(vline);
 
87
        }
 
88
        else {
 
89
            mp_set_from_string(default_constants[i][1], 10, value);
 
90
            constant_set(i, default_constants[i][0], value);
 
91
        }
 
92
    }
 
93
    
 
94
    for (i = 0; i < MAX_FUNCTIONS; i++) {
 
95
        char nkey[MAXLINE], *nline;
 
96
        char vkey[MAXLINE], *vline;
 
97
        
 
98
        SNPRINTF(nkey, MAXLINE, "function%1dname", i);
 
99
        nline = get_resource(nkey);
 
100
        if (nline) {
 
101
            SNPRINTF(vkey, MAXLINE, "function%1dvalue", i);
 
102
            vline = get_resource(vkey);
 
103
            if (vline == NULL)
 
104
                g_free(nline);
 
105
        }
 
106
 
 
107
        if (nline && vline) {
 
108
            function_set(i, nline, convert(vline));
 
109
            g_free(nline);
 
110
            g_free(vline);
 
111
        }
 
112
        else {
 
113
            function_set(i, "", "");
 
114
        }
 
115
    }
 
116
}
 
117
 
 
118
 
 
119
void
 
120
register_set(int index, int value[MP_SIZE])
 
121
{
 
122
    if ((index >= 0) && (index <= 10))
 
123
        mp_set_from_mp(value, registers[index]);
 
124
}
 
125
 
 
126
 
 
127
void
 
128
register_get(int index, int value[MP_SIZE])
 
129
{
 
130
    if ((index >= 0) && (index <= 10))
 
131
        mp_set_from_mp(registers[index], value);
 
132
}
 
133
 
 
134
 
 
135
void constant_set(int index, const char *name, int value[MP_SIZE])
 
136
{
 
137
    char key[MAXLINE], text[MAX_LOCALIZED];
 
138
 
 
139
    STRNCPY(constant_names[index], name, MAXLINE - 1);
 
140
    mp_set_from_mp(value, constant_values[index]);
 
141
 
 
142
    SNPRINTF(key, MAXLINE, "constant%1dname", index);
 
143
    set_resource(key, name);
 
144
 
 
145
    /* NOTE: Constants are written out with no thousands separator and with a
 
146
       radix character of ".". */
 
147
    mp_cast_to_string(text, MAX_LOCALIZED, value, 10, MAX_DIGITS);
 
148
    SNPRINTF(key, MAXLINE, "constant%1dvalue", index);
 
149
    set_resource(key, text);
 
150
}
 
151
 
 
152
 
 
153
const char *constant_get_name(int index)
 
154
{
 
155
    return constant_names[index];
 
156
}
 
157
 
 
158
 
 
159
const int *constant_get_value(int index)
 
160
{
 
161
    return constant_values[index];
 
162
}
 
163
 
 
164
 
 
165
void function_set(int index, const char *name, const char *value)
 
166
{
 
167
    char key[MAXLINE];
 
168
 
 
169
    STRNCPY(function_names[index], name, MAXLINE - 1);        
 
170
    STRNCPY(function_values[index], value, MAXLINE - 1);
 
171
    
 
172
    SNPRINTF(key, MAXLINE, "function%1dname", index);
 
173
    set_resource(key, name);
 
174
    SNPRINTF(key, MAXLINE, "function%1dvalue", index);
 
175
    set_resource(key, value);
 
176
}
 
177
 
 
178
 
 
179
const char *function_get_name(int index)
 
180
{
 
181
    return function_names[index];
 
182
}
 
183
 
 
184
 
 
185
const char *function_get_value(int index)
 
186
{
 
187
    return function_values[index];
38
188
}