Change 'undo' and 'clear' buttons to use symbolic icons
[gcalctool.git] / src / math-variables.c
blobc8fcd24f77d894d307ac5b68d216c9cb1496c145
1 /*
2 * Copyright (C) 2008-2011 Robert Ancell
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 2 of the License, or (at your option) any later
7 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
8 * license.
9 */
11 #include <stdio.h>
12 #include <string.h>
14 #include "math-variables.h"
15 #include "mp-serializer.h"
18 struct MathVariablesPrivate
20 gchar *file_name;
21 GHashTable *registers;
22 MpSerializer *serializer;
25 G_DEFINE_TYPE (MathVariables, math_variables, G_TYPE_OBJECT);
28 MathVariables *
29 math_variables_new()
31 return g_object_new (math_variables_get_type(), NULL);
35 static void
36 registers_load(MathVariables *variables)
38 FILE *f;
39 char line[1024];
41 f = fopen(variables->priv->file_name, "r");
42 if (!f)
43 return;
45 g_hash_table_remove_all(variables->priv->registers);
47 while (fgets(line, 1024, f) != NULL)
49 char *name, *value;
50 MPNumber *t;
52 value = strchr(line, '=');
53 if (!value)
54 continue;
55 *value = '\0';
56 value = value + 1;
58 name = g_strstrip(line);
59 value = g_strstrip(value);
61 t = g_malloc(sizeof(MPNumber));
62 if (mp_set_from_string(value, 10, t) == 0)
63 g_hash_table_insert(variables->priv->registers, g_strdup(name), t);
64 else
65 g_free(t);
67 fclose(f);
71 static void
72 registers_save(MathVariables *variables)
74 gchar *dir;
75 FILE *f;
76 GHashTableIter iter;
77 gpointer key, val;
79 dir = g_path_get_dirname(variables->priv->file_name);
80 g_mkdir_with_parents(dir, 0700);
81 g_free(dir);
83 f = fopen(variables->priv->file_name, "w");
84 if (!f)
85 return;
87 g_hash_table_iter_init(&iter, variables->priv->registers);
88 while (g_hash_table_iter_next(&iter, &key, &val))
90 gchar *name = key;
91 MPNumber *value = val;
92 char *number;
94 number = mp_serializer_to_string(variables->priv->serializer, value);
95 fprintf(f, "%s=%s\n", name, number);
96 g_free(number);
98 fclose(f);
102 // FIXME: Sort
103 gchar **
104 math_variables_get_names(MathVariables *variables)
106 GHashTableIter iter;
107 gpointer key;
108 gint i = 0;
109 gchar **names;
111 g_return_val_if_fail(variables != NULL, NULL);
113 names = g_malloc0(sizeof(gchar *) * (g_hash_table_size(variables->priv->registers) + 1));
115 g_hash_table_iter_init(&iter, variables->priv->registers);
116 while (g_hash_table_iter_next(&iter, &key, NULL))
118 gchar *name = key;
119 names[i] = g_strdup(name);
120 i++;
122 names[i] = NULL;
124 return names;
128 void
129 math_variables_set(MathVariables *variables, const char *name, const MPNumber *value)
131 MPNumber *t;
133 g_return_if_fail(variables != NULL);
134 g_return_if_fail(name != NULL);
135 g_return_if_fail(value != NULL);
137 t = g_malloc(sizeof(MPNumber));
138 mp_set_from_mp(value, t);
139 g_hash_table_insert(variables->priv->registers, g_strdup(name), t);
140 registers_save(variables);
144 MPNumber *
145 math_variables_get(MathVariables *variables, const char *name)
147 g_return_val_if_fail(variables != NULL, NULL);
148 g_return_val_if_fail(name != NULL, NULL);
149 return g_hash_table_lookup(variables->priv->registers, name);
153 void
154 math_variables_delete(MathVariables *variables, const char *name)
156 g_return_if_fail(variables != NULL);
157 g_return_if_fail(name != NULL);
158 g_hash_table_remove(variables->priv->registers, name);
159 registers_save(variables);
163 static void
164 math_variables_class_init (MathVariablesClass *klass)
166 g_type_class_add_private (klass, sizeof (MathVariablesPrivate));
170 static void
171 math_variables_init(MathVariables *variables)
173 variables->priv = G_TYPE_INSTANCE_GET_PRIVATE (variables, math_variables_get_type(), MathVariablesPrivate);
174 variables->priv->registers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
175 variables->priv->file_name = g_build_filename(g_get_user_data_dir(), "gcalctool", "registers", NULL);
176 variables->priv->serializer = mp_serializer_new(MP_DISPLAY_FORMAT_SCIENTIFIC, 10, 50);
177 mp_serializer_set_radix(variables->priv->serializer, '.');
178 registers_load(variables);