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
14 #include "math-variables.h"
15 #include "mp-serializer.h"
18 struct MathVariablesPrivate
21 GHashTable
*registers
;
22 MpSerializer
*serializer
;
25 G_DEFINE_TYPE (MathVariables
, math_variables
, G_TYPE_OBJECT
);
31 return g_object_new (math_variables_get_type(), NULL
);
36 registers_load(MathVariables
*variables
)
41 f
= fopen(variables
->priv
->file_name
, "r");
45 g_hash_table_remove_all(variables
->priv
->registers
);
47 while (fgets(line
, 1024, f
) != NULL
)
52 value
= strchr(line
, '=');
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
);
72 registers_save(MathVariables
*variables
)
79 dir
= g_path_get_dirname(variables
->priv
->file_name
);
80 g_mkdir_with_parents(dir
, 0700);
83 f
= fopen(variables
->priv
->file_name
, "w");
87 g_hash_table_iter_init(&iter
, variables
->priv
->registers
);
88 while (g_hash_table_iter_next(&iter
, &key
, &val
))
91 MPNumber
*value
= val
;
94 number
= mp_serializer_to_string(variables
->priv
->serializer
, value
);
95 fprintf(f
, "%s=%s\n", name
, number
);
104 math_variables_get_names(MathVariables
*variables
)
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
))
119 names
[i
] = g_strdup(name
);
129 math_variables_set(MathVariables
*variables
, const char *name
, const MPNumber
*value
)
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
);
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
);
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
);
164 math_variables_class_init (MathVariablesClass
*klass
)
166 g_type_class_add_private (klass
, sizeof (MathVariablesPrivate
));
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
);