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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use ffi;
use translate::*;
use std;
use std::path::{Path, PathBuf};
use error::BoolError;
use Error;
use std::ptr;
pub fn get_program_name() -> Option<String> {
get_prgname()
}
pub fn get_prgname() -> Option<String> {
unsafe {
from_glib_none(ffi::g_get_prgname())
}
}
pub fn set_program_name(name: Option<&str>) {
set_prgname(name)
}
pub fn set_prgname(name: Option<&str>) {
unsafe {
ffi::g_set_prgname(name.to_glib_none().0)
}
}
pub fn getenv(variable_name: &str) -> Option<String> {
#[cfg(windows)]
use ffi::g_getenv_utf8 as g_getenv;
#[cfg(not(windows))]
use ffi::g_getenv;
unsafe {
from_glib_none(g_getenv(variable_name.to_glib_none().0))
}
}
pub fn setenv(variable_name: &str, value: &str, overwrite: bool) -> Result<(), BoolError> {
#[cfg(windows)]
use ffi::g_setenv_utf8 as g_setenv;
#[cfg(not(windows))]
use ffi::g_setenv;
unsafe {
BoolError::from_glib(g_setenv(variable_name.to_glib_none().0,
value.to_glib_none().0,
overwrite.to_glib()),
"Failed to set environment variable")
}
}
pub fn unsetenv(variable_name: &str) {
#[cfg(windows)]
use ffi::g_unsetenv_utf8 as g_unsetenv;
#[cfg(not(windows))]
use ffi::g_unsetenv;
unsafe {
g_unsetenv(variable_name.to_glib_none().0)
}
}
pub fn get_user_name() -> Option<String> {
#[cfg(all(windows,target_arch="x86"))]
use ffi::g_get_user_name_utf8 as g_get_user_name;
#[cfg(not(all(windows,target_arch="x86")))]
use ffi::g_get_user_name;
unsafe {
from_glib_none(g_get_user_name())
}
}
pub fn get_real_name() -> Option<std::path::PathBuf> {
#[cfg(all(windows,target_arch="x86"))]
use ffi::g_get_real_name_utf8 as g_get_real_name;
#[cfg(not(all(windows,target_arch="x86")))]
use ffi::g_get_real_name;
unsafe {
from_glib_none(g_get_real_name())
}
}
pub fn get_current_dir() -> Option<PathBuf> {
#[cfg(windows)]
use ffi::g_get_current_dir_utf8 as g_get_current_dir;
#[cfg(not(windows))]
use ffi::g_get_current_dir;
unsafe {
from_glib_full(g_get_current_dir())
}
}
pub fn filename_to_uri<'a, P: AsRef<Path>, Q: Into<Option<&'a str>>>(filename: P, hostname: Q) -> Result<String, Error> {
#[cfg(windows)]
use ffi::g_filename_to_uri_utf8 as g_filename_to_uri;
#[cfg(not(windows))]
use ffi::g_filename_to_uri;
let hostname = hostname.into();
let hostname = hostname.to_glib_none();
unsafe {
let mut error = std::ptr::null_mut();
let ret = g_filename_to_uri(filename.as_ref().to_glib_none().0, hostname.0, &mut error);
if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
}
}
pub fn filename_from_uri(uri: &str) -> Result<(std::path::PathBuf, Option<String>), Error> {
#[cfg(windows)]
use ffi::g_filename_from_uri_utf8 as g_filename_from_uri;
#[cfg(not(windows))]
use ffi::g_filename_from_uri;
unsafe {
let mut hostname = ptr::null_mut();
let mut error = ptr::null_mut();
let ret = g_filename_from_uri(uri.to_glib_none().0, &mut hostname, &mut error);
if error.is_null() { Ok((from_glib_full(ret), from_glib_full(hostname))) } else { Err(from_glib_full(error)) }
}
}
pub fn find_program_in_path<P: AsRef<Path>>(program: P) -> Option<PathBuf> {
#[cfg(all(windows,target_arch="x86"))]
use ffi::g_find_program_in_path_utf8 as g_find_program_in_path;
#[cfg(not(all(windows,target_arch="x86")))]
use ffi::g_find_program_in_path;
unsafe {
from_glib_full(g_find_program_in_path(program.as_ref().to_glib_none().0))
}
}
pub fn get_home_dir() -> Option<std::path::PathBuf> {
#[cfg(all(windows,target_arch="x86"))]
use ffi::g_get_home_dir_utf8 as g_get_home_dir;
#[cfg(not(all(windows,target_arch="x86")))]
use ffi::g_get_home_dir;
unsafe {
from_glib_none(g_get_home_dir())
}
}
pub fn get_tmp_dir() -> Option<std::path::PathBuf> {
#[cfg(all(windows,target_arch="x86"))]
use ffi::g_get_tmp_dir_utf8 as g_get_tmp_dir;
#[cfg(not(all(windows,target_arch="x86")))]
use ffi::g_get_tmp_dir;
unsafe {
from_glib_none(g_get_tmp_dir())
}
}
pub fn mkstemp<P: AsRef<std::path::Path>>(tmpl: P) -> i32 {
#[cfg(windows)]
use ffi::g_mkstemp_utf8 as g_mkstemp;
#[cfg(not(windows))]
use ffi::g_mkstemp;
unsafe {
g_mkstemp(tmpl.as_ref().to_glib_none().0)
}
}