5
Copyright 2011 Jiří Janoušek <janousek.jiri@gmail.com>
7
* Based on code from Wingpanel <https://launchpad.net/wingpanel>,
8
Copyright 2010-2011 Wingpanel developers
10
This program is free software: you can redistribute it and/or modify it
11
under the terms of the GNU General Public License version 3, as published
12
by the Free Software Foundation.
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranties of
16
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17
PURPOSE. See the GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License along
20
with this program. If not, see <http://www.gnu.org/licenses/>.
24
* create real shared library
30
* Class parsing key-value configuration file.
32
* Class parser configuration files which contain one key-value pair per line.
33
* Leading and traling white characters are removed.
34
* Key and value are separated by "=" (value may contain "=" character).
35
* Lines starting with a hash "#" are ignored.
40
* # This is also valid:
41
* comparation = <, =, >
44
public class ConfigFile: HashMap<string, string>{
46
public bool readonly{get; set;}
48
* Creates new map representation of given file.
50
* Creates new map representation of given file. Prints warning on parse errors.
51
* Note: key names are always lowercased.
53
* @param file file to parse
55
public ConfigFile(File file, bool readonly=false){
58
this.readonly = readonly;
60
var dis = new DataInputStream(file.read());
63
while ((line = dis.read_line (null)) != null){
65
string item = line.strip();
66
// Ignore empty lines or comments
67
if(item.length == 0 || item[0] == '#') continue;
68
string[] parts = item.split("=", 2);
69
if(parts.length != 2){
70
// Implicit keys without value are not supported
71
warning("ConfigFile: Parse error at %s:%i",
72
file.get_uri(), lineno);
75
string key = parts[0].strip();
78
warning("ConfigFile: Parse error at %s:%i",
79
file.get_uri(), lineno);
82
this[key] = parts[1].strip();
86
debug("ConfigFile: Unable to read %s", file.get_uri());
90
* Returns boolean value.
92
* @param key option identifier
93
* @return false if value is empty or "false" (case insensitive), true otherwise
95
public bool get_bool(string key){
96
string? val = this[key];
97
return val != null && val != "" && val.down() != "false";
101
* Saves configuration to file.
103
* @throw Error on failure
105
public void save() throws Error{
106
if(readonly) throw new IOError.READ_ONLY("Configuration file is opened read-only");
107
var builder = new StringBuilder();
108
foreach(var e in this.entries){
109
builder.append(e.key);
110
builder.append(" = ");
111
builder.append(e.value);
112
builder.append_c('\n');
114
string str = builder.str;
116
file.get_parent().make_directory_with_parents();
119
file.replace_contents(str, str.length, null, false, FileCreateFlags.NONE, null);
125
* Search user data directory and system data directories for subdirectories.
127
* @param name name of subdirectory
128
* @return Existing subdirectories or empty array in no subdirectory has been found.
130
public static File[] get_data_dirs(string name){
132
File dir = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
133
if(dir.query_file_type(0) == FileType.DIRECTORY) dirs += dir;
134
foreach(string path in Environment.get_system_data_dirs()){
135
dir = File.new_for_path(path).get_child(name);
136
if(dir.query_file_type(0) == FileType.DIRECTORY) dirs += dir;
142
* Search user data directory and system data directories for subdirectory.
144
* @param name name of subdirectory
145
* @return First existing subdirectory or null in no subdirectory has been found.
147
public static File? get_data_dir(string name){
148
File dir = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
149
if(dir.query_file_type(0) == FileType.DIRECTORY) return dir;
150
foreach(string path in Environment.get_system_data_dirs()){
151
dir = File.new_for_path(path).get_child(name);
152
if(dir.query_file_type(0) == FileType.DIRECTORY) return dir;
158
* Search user data directory and system data directories for file.
160
* @param name name of the file
161
* @return First existing file or null in no file has been found.
164
public static File? get_data_file(string name){
165
File f = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
166
if(f.query_file_type(0) == FileType.REGULAR) return f;
167
foreach(string path in Environment.get_system_data_dirs()){
168
f = File.new_for_path(path).get_child(name);
169
if(f.query_file_type(0) == FileType.REGULAR) return f;
174
public static File get_default_config_file(string dir, string file){
175
return File.new_for_path(Environment.get_user_config_dir())
176
.get_child(dir).get_child(file);