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)
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
22 #include "math-variables.h"
23 #include "mp-serializer.h"
26 struct MathVariablesPrivate
29 GHashTable
*registers
;
30 MpSerializer
*serializer
;
33 G_DEFINE_TYPE (MathVariables
, math_variables
, G_TYPE_OBJECT
);
39 return g_object_new (math_variables_get_type(), NULL
);
44 registers_load(MathVariables
*variables
)
49 f
= fopen(variables
->priv
->file_name
, "r");
53 g_hash_table_remove_all(variables
->priv
->registers
);
55 while (fgets(line
, 1024, f
) != NULL
)
60 value
= strchr(line
, '=');
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
);
80 registers_save(MathVariables
*variables
)
87 dir
= g_path_get_dirname(variables
->priv
->file_name
);
88 g_mkdir_with_parents(dir
, 0700);
91 f
= fopen(variables
->priv
->file_name
, "w");
95 g_hash_table_iter_init(&iter
, variables
->priv
->registers
);
96 while (g_hash_table_iter_next(&iter
, &key
, &val
))
99 MPNumber
*value
= val
;
102 number
= mp_serializer_to_string(variables
->priv
->serializer
, value
);
103 fprintf(f
, "%s=%s\n", name
, number
);
112 math_variables_get_names(MathVariables
*variables
)
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
))
125 names
[i
] = g_strdup(name
);
135 math_variables_set(MathVariables
*variables
, const char *name
, const MPNumber
*value
)
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
);
147 math_variables_get(MathVariables
*variables
, const char *name
)
149 return g_hash_table_lookup(variables
->priv
->registers
, name
);
154 math_variables_delete(MathVariables
*variables
, const char *name
)
156 g_hash_table_remove(variables
->priv
->registers
, name
);
157 registers_save(variables
);
162 math_variables_class_init (MathVariablesClass
*klass
)
164 g_type_class_add_private (klass
, sizeof (MathVariablesPrivate
));
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
);