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
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 ('.');
26 private void registers_load ()
31 FileUtils
.get_contents (file_name
, out data
);
38 registers
.remove_all ();
40 var lines
= data
.split ("\n");
41 foreach (var line
in lines
)
43 var i
= line
.index_of_char ('=');
47 var name
= line
.substring (0, i
).strip ();
48 var value
= line
.substring (i
+1).strip ();
50 var t
= mp_set_from_string (value
);
52 registers
.insert (name
, t
);
59 var iter
= HashTableIter
<string, Number?
> (registers
);
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);
72 FileUtils
.set_contents (file_name
, data
);
80 public string[] get_names ()
82 var names
= new
string[registers
.size () + 1];
84 var iter
= HashTableIter
<string, Number?
> (registers
);
88 while (iter
.next (out name
, out value
))
98 public new
void set (string name
, Number value
)
100 registers
.insert (name
, value
);
104 public new Number?
get (string name
)
106 return registers
.lookup (name
);
109 public void delete (string name
)
111 registers
.remove (name
);