glib/
key_file.rs

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