Updated Danish translation
[gcalctool.git] / src / register.c
blobd3189ac4e63fb56de6a967b159d27f7e5a4d6a5c
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>
21 #include <glib.h>
23 #include "register.h"
26 static gchar *file_name = NULL;
27 static GHashTable *registers = NULL;
30 static void
31 registers_load()
33 FILE *f;
34 char line[1024];
36 f = fopen(file_name, "r");
37 if (!f)
38 return;
40 g_hash_table_remove_all(registers);
42 while (fgets(line, 1024, f) != NULL)
44 char *name, *value;
45 MPNumber *t;
47 value = strchr(line, '=');
48 if (!value)
49 continue;
50 *value = '\0';
51 value = value + 1;
53 name = g_strstrip(line);
54 value = g_strstrip(value);
56 t = g_malloc(sizeof(MPNumber));
57 if (mp_set_from_string(value, t) == 0)
58 g_hash_table_insert(registers, g_strdup(name), t);
59 else
60 g_free(t);
62 fclose(f);
66 static void
67 registers_save()
69 gchar *dir;
70 FILE *f;
71 GHashTableIter iter;
72 gpointer key, val;
74 dir = g_path_get_dirname(file_name);
75 g_mkdir_with_parents(dir, 0700);
76 g_free(dir);
78 f = fopen(file_name, "w");
79 if (!f)
80 return;
82 g_hash_table_iter_init(&iter, registers);
83 while (g_hash_table_iter_next(&iter, &key, &val))
85 gchar *name = key;
86 MPNumber *value = val;
87 char number[1024];
89 mp_cast_to_string(value, 10, 50, TRUE, number, 1024);
90 fprintf(f, "%s=%s\n", name, number);
92 fclose(f);
96 void
97 register_init()
99 registers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
100 file_name = g_build_filename(g_get_user_data_dir(), "gcalctool", "registers", NULL);
101 registers_load();
105 void
106 register_set_value(const char *name, const MPNumber *value)
108 MPNumber *t;
109 t = g_malloc(sizeof(MPNumber));
110 mp_set_from_mp(value, t);
111 g_hash_table_insert(registers, g_strdup(name), t);
112 registers_save();
116 MPNumber *
117 register_get_value(const char *name)
119 return g_hash_table_lookup(registers, name);