Updated kn translations
[gcalctool.git] / src / math-variables.vala
blobf326f1f999b44eba74dc44c3cecb0f67bda5f9cb
1 /*
2 * Copyright (C) 2008-2012 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
8 * license.
9 */
11 public class MathVariables : Object
13 private string file_name;
14 private HashTable<string, Number?> registers;
15 private Serializer serializer;
17 public MathVariables ()
19 registers = new HashTable <string, Number?> (str_hash, str_equal);
20 file_name = Path.build_filename (Environment.get_user_data_dir (), "gcalctool", "registers");
21 serializer = new Serializer (DisplayFormat.SCIENTIFIC, 10, 50);
22 serializer.set_radix ('.');
23 registers_load ();
26 private void registers_load ()
28 string data;
29 try
31 FileUtils.get_contents (file_name, out data);
33 catch (FileError e)
35 return;
38 registers.remove_all ();
40 var lines = data.split ("\n");
41 foreach (var line in lines)
43 var i = line.index_of_char ('=');
44 if (i < 0)
45 continue;
47 var name = line.substring (0, i).strip ();
48 var value = line.substring (i+1).strip ();
50 var t = mp_set_from_string (value);
51 if (t != null)
52 registers.insert (name, t);
56 private void save ()
58 var data = "";
59 var iter = HashTableIter<string, Number?> (registers);
60 string name;
61 Number? value;
62 while (iter.next (out name, out value))
64 var number = serializer.to_string (value);
65 data += "%s=%s\n".printf (name, number);
68 var dir = Path.get_dirname (file_name);
69 DirUtils.create_with_parents (dir, 0700);
70 try
72 FileUtils.set_contents (file_name, data);
74 catch (FileError e)
79 // FIXME: Sort
80 public string[] get_names ()
82 var names = new string[registers.size () + 1];
84 var iter = HashTableIter<string, Number?> (registers);
85 var i = 0;
86 string name;
87 Number? value;
88 while (iter.next (out name, out value))
90 names[i] = name;
91 i++;
93 names[i] = null;
95 return names;
98 public new void set (string name, Number value)
100 registers.insert (name, value);
101 save ();
104 public new Number? get (string name)
106 return registers.lookup (name);
109 public void delete (string name)
111 registers.remove (name);
112 save ();