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
26 static gchar
*file_name
= NULL
;
27 static GHashTable
*registers
= NULL
;
36 f
= fopen(file_name
, "r");
40 g_hash_table_remove_all(registers
);
42 while (fgets(line
, 1024, f
) != NULL
)
47 value
= strchr(line
, '=');
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
);
74 dir
= g_path_get_dirname(file_name
);
75 g_mkdir_with_parents(dir
, 0700);
78 f
= fopen(file_name
, "w");
82 g_hash_table_iter_init(&iter
, registers
);
83 while (g_hash_table_iter_next(&iter
, &key
, &val
))
86 MPNumber
*value
= val
;
89 mp_cast_to_string(value
, 10, 50, TRUE
, number
, 1024);
90 fprintf(f
, "%s=%s\n", name
, number
);
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
);
106 register_set_value(const char *name
, const MPNumber
*value
)
109 t
= g_malloc(sizeof(MPNumber
));
110 mp_set_from_mp(value
, t
);
111 g_hash_table_insert(registers
, g_strdup(name
), t
);
117 register_get_value(const char *name
)
119 return g_hash_table_lookup(registers
, name
);