summaryrefslogtreecommitdiff
path: root/src/math-variables.c
diff options
context:
space:
mode:
authorSteve Zesch <[email protected]>2012-11-24 20:44:45 -0500
committerSteve Zesch <[email protected]>2012-11-24 20:44:45 -0500
commit6b24c91d3aa81fdb99500c8c2c12f830fabaefb6 (patch)
tree6a0038ecfaa77e156ee2cc9059220685091217d7 /src/math-variables.c
parenta10375c2851e8569353c0da9921b8d0d9cbea2e6 (diff)
downloadmate-calc-6b24c91d3aa81fdb99500c8c2c12f830fabaefb6.tar.bz2
mate-calc-6b24c91d3aa81fdb99500c8c2c12f830fabaefb6.tar.xz
Update codebase.
Diffstat (limited to 'src/math-variables.c')
-rw-r--r--src/math-variables.c64
1 files changed, 40 insertions, 24 deletions
diff --git a/src/math-variables.c b/src/math-variables.c
index a76261c..7e20cdc 100644
--- a/src/math-variables.c
+++ b/src/math-variables.c
@@ -1,31 +1,25 @@
-/* Copyright (c) 2008-2009 Robert Ancell
+/*
+ * Copyright (C) 2008-2011 Robert Ancell
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
*/
#include <stdio.h>
#include <string.h>
#include "math-variables.h"
+#include "mp-serializer.h"
struct MathVariablesPrivate
{
gchar *file_name;
GHashTable *registers;
+ MpSerializer *serializer;
};
G_DEFINE_TYPE (MathVariables, math_variables, G_TYPE_OBJECT);
@@ -43,24 +37,24 @@ registers_load(MathVariables *variables)
{
FILE *f;
char line[1024];
-
+
f = fopen(variables->priv->file_name, "r");
if (!f)
return;
-
+
g_hash_table_remove_all(variables->priv->registers);
while (fgets(line, 1024, f) != NULL)
{
char *name, *value;
MPNumber *t;
-
+
value = strchr(line, '=');
if (!value)
continue;
*value = '\0';
value = value + 1;
-
+
name = g_strstrip(line);
value = g_strstrip(value);
@@ -89,16 +83,17 @@ registers_save(MathVariables *variables)
f = fopen(variables->priv->file_name, "w");
if (!f)
return;
-
+
g_hash_table_iter_init(&iter, variables->priv->registers);
while (g_hash_table_iter_next(&iter, &key, &val))
{
gchar *name = key;
MPNumber *value = val;
- char number[1024];
+ char *number;
- mp_cast_to_string(value, 10, 10, 50, TRUE, number, 1024);
+ number = mp_serializer_to_string(variables->priv->serializer, value);
fprintf(f, "%s=%s\n", name, number);
+ g_free(number);
}
fclose(f);
}
@@ -112,6 +107,8 @@ math_variables_get_names(MathVariables *variables)
gpointer key;
gint i = 0;
gchar **names;
+
+ g_return_val_if_fail(variables != NULL, NULL);
names = g_malloc0(sizeof(gchar *) * (g_hash_table_size(variables->priv->registers) + 1));
@@ -129,9 +126,14 @@ math_variables_get_names(MathVariables *variables)
void
-math_variables_set_value(MathVariables *variables, const char *name, const MPNumber *value)
+math_variables_set(MathVariables *variables, const char *name, const MPNumber *value)
{
MPNumber *t;
+
+ g_return_if_fail(variables != NULL);
+ g_return_if_fail(name != NULL);
+ g_return_if_fail(value != NULL);
+
t = g_malloc(sizeof(MPNumber));
mp_set_from_mp(value, t);
g_hash_table_insert(variables->priv->registers, g_strdup(name), t);
@@ -140,12 +142,24 @@ math_variables_set_value(MathVariables *variables, const char *name, const MPNum
MPNumber *
-math_variables_get_value(MathVariables *variables, const char *name)
+math_variables_get(MathVariables *variables, const char *name)
{
+ g_return_val_if_fail(variables != NULL, NULL);
+ g_return_val_if_fail(name != NULL, NULL);
return g_hash_table_lookup(variables->priv->registers, name);
}
+void
+math_variables_delete(MathVariables *variables, const char *name)
+{
+ g_return_if_fail(variables != NULL);
+ g_return_if_fail(name != NULL);
+ g_hash_table_remove(variables->priv->registers, name);
+ registers_save(variables);
+}
+
+
static void
math_variables_class_init (MathVariablesClass *klass)
{
@@ -159,5 +173,7 @@ math_variables_init(MathVariables *variables)
variables->priv = G_TYPE_INSTANCE_GET_PRIVATE (variables, math_variables_get_type(), MathVariablesPrivate);
variables->priv->registers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
variables->priv->file_name = g_build_filename(g_get_user_data_dir(), "mate-calc", "registers", NULL);
+ variables->priv->serializer = mp_serializer_new(MP_DISPLAY_FORMAT_SCIENTIFIC, 10, 50);
+ mp_serializer_set_radix(variables->priv->serializer, '.');
registers_load(variables);
}