1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use auto::KeyFileFlags;
use error::Error;
use glib_sys;
use gstring::GString;
use libc;
use std;
use std::mem;
use std::path;
use std::ptr;
use translate::*;
use KeyFile;
impl KeyFile {
pub fn save_to_file<T: AsRef<std::path::Path>>(&self, filename: T) -> Result<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = glib_sys::g_key_file_save_to_file(
self.to_glib_none().0,
filename.as_ref().to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
pub fn load_from_data_dirs<T: AsRef<std::path::Path>>(
&self,
file: T,
flags: KeyFileFlags,
) -> Result<path::PathBuf, Error> {
unsafe {
let mut error = ptr::null_mut();
let mut full_path: *mut libc::c_char = ptr::null_mut();
let _ = glib_sys::g_key_file_load_from_data_dirs(
self.to_glib_none().0,
file.as_ref().to_glib_none().0,
&mut full_path,
flags.to_glib(),
&mut error,
);
if error.is_null() {
let path: GString = from_glib_full(full_path);
Ok(path::PathBuf::from(&path))
} else {
Err(from_glib_full(error))
}
}
}
pub fn load_from_dirs<T: AsRef<std::path::Path>, U: AsRef<std::path::Path>>(
&self,
file: T,
search_dirs: &[U],
flags: KeyFileFlags,
) -> Result<path::PathBuf, Error> {
unsafe {
let search_dirs: Vec<&std::path::Path> =
search_dirs.iter().map(AsRef::as_ref).collect();
let mut error = ptr::null_mut();
let mut full_path: *mut libc::c_char = ptr::null_mut();
let _ = glib_sys::g_key_file_load_from_dirs(
self.to_glib_none().0,
file.as_ref().to_glib_none().0,
search_dirs.to_glib_none().0,
&mut full_path,
flags.to_glib(),
&mut error,
);
if error.is_null() {
let path: GString = from_glib_full(full_path);
Ok(path::PathBuf::from(&path))
} else {
Err(from_glib_full(error))
}
}
}
pub fn to_data(&self) -> GString {
unsafe {
let ret = glib_sys::g_key_file_to_data(
self.to_glib_none().0,
ptr::null_mut(),
ptr::null_mut(),
);
from_glib_full(ret)
}
}
pub fn get_boolean(&self, group_name: &str, key: &str) -> Result<bool, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_boolean(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib(ret))
} else {
Err(from_glib_full(error))
}
}
}
pub fn has_key(&self, group_name: &str, key: &str) -> Result<bool, Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_has_key(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib(ret))
} else {
Err(from_glib_full(error))
}
}
}
pub fn get_boolean_list(&self, group_name: &str, key: &str) -> Result<Vec<bool>, Error> {
unsafe {
let mut length = mem::uninitialized();
let mut error = ptr::null_mut();
let ret = glib_sys::g_key_file_get_boolean_list(
self.to_glib_none().0,
group_name.to_glib_none().0,
key.to_glib_none().0,
&mut length,
&mut error,
);
if !error.is_null() {
return Err(from_glib_full(error));
}
Ok(FromGlibContainer::from_glib_container_num(
ret,
length as usize,
))
}
}
}