Changed mind about translating short currency names, instead use the full name in...
[gcalctool.git] / src / math-variables.c
blobe45096915858b08970e63ab9ebacdb2e5dff7c71
1 /* Copyright (c) 2008-2009 Robert Ancell
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 * 02111-1307, USA.
19 #include <stdio.h>
20 #include <string.h>
22 #include "math-variables.h"
23 #include "mp-serializer.h"
26 struct MathVariablesPrivate
28 gchar *file_name;
29 GHashTable *registers;
30 MpSerializer *serializer;
33 G_DEFINE_TYPE (MathVariables, math_variables, G_TYPE_OBJECT);
36 MathVariables *
37 math_variables_new()
39 return g_object_new (math_variables_get_type(), NULL);
43 static void
44 registers_load(MathVariables *variables)
46 FILE *f;
47 char line[1024];
49 f = fopen(variables->priv->file_name, "r");
50 if (!f)
51 return;
53 g_hash_table_remove_all(variables->priv->registers);
55 while (fgets(line, 1024, f) != NULL)
57 char *name, *value;
58 MPNumber *t;
60 value = strchr(line, '=');
61 if (!value)
62 continue;
63 *value = '\0';
64 value = value + 1;
66 name = g_strstrip(line);
67 value = g_strstrip(value);
69 t = g_malloc(sizeof(MPNumber));
70 if (mp_set_from_string(value, 10, t) == 0)
71 g_hash_table_insert(variables->priv->registers, g_strdup(name), t);
72 else
73 g_free(t);
75 fclose(f);
79 static void
80 registers_save(MathVariables *variables)
82 gchar *dir;
83 FILE *f;
84 GHashTableIter iter;
85 gpointer key, val;
87 dir = g_path_get_dirname(variables->priv->file_name);
88 g_mkdir_with_parents(dir, 0700);
89 g_free(dir);
91 f = fopen(variables->priv->file_name, "w");
92 if (!f)
93 return;
95 g_hash_table_iter_init(&iter, variables->priv->registers);
96 while (g_hash_table_iter_next(&iter, &key, &val))
98 gchar *name = key;
99 MPNumber *value = val;
100 char *number;
102 number = mp_serializer_to_string(variables->priv->serializer, value);
103 fprintf(f, "%s=%s\n", name, number);
104 g_free(number);
106 fclose(f);
110 // FIXME: Sort
111 gchar **
112 math_variables_get_names(MathVariables *variables)
114 GHashTableIter iter;
115 gpointer key;
116 gint i = 0;
117 gchar **names;
119 names = g_malloc0(sizeof(gchar *) * (g_hash_table_size(variables->priv->registers) + 1));
121 g_hash_table_iter_init(&iter, variables->priv->registers);
122 while (g_hash_table_iter_next(&iter, &key, NULL))
124 gchar *name = key;
125 names[i] = g_strdup(name);
126 i++;
128 names[i] = NULL;
130 return names;
134 void
135 math_variables_set(MathVariables *variables, const char *name, const MPNumber *value)
137 MPNumber *t;
139 t = g_malloc(sizeof(MPNumber));
140 mp_set_from_mp(value, t);
141 g_hash_table_insert(variables->priv->registers, g_strdup(name), t);
142 registers_save(variables);
146 MPNumber *
147 math_variables_get(MathVariables *variables, const char *name)
149 return g_hash_table_lookup(variables->priv->registers, name);
153 void
154 math_variables_delete(MathVariables *variables, const char *name)
156 g_hash_table_remove(variables->priv->registers, name);
157 registers_save(variables);
161 static void
162 math_variables_class_init (MathVariablesClass *klass)
164 g_type_class_add_private (klass, sizeof (MathVariablesPrivate));
168 static void
169 math_variables_init(MathVariables *variables)
171 variables->priv = G_TYPE_INSTANCE_GET_PRIVATE (variables, math_variables_get_type(), MathVariablesPrivate);
172 variables->priv->registers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
173 variables->priv->file_name = g_build_filename(g_get_user_data_dir(), "gcalctool", "registers", NULL);
174 variables->priv->serializer = mp_serializer_new(MP_DISPLAY_FORMAT_SCIENTIFIC, 10, 50);
175 mp_serializer_set_radix(variables->priv->serializer, '.');
176 registers_load(variables);