Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #177 from EPashkin/windows_functions
Browse files Browse the repository at this point in the history
Use functions, ending with _utf8 under Windows
  • Loading branch information
GuillaumeGomez committed May 27, 2017
2 parents 5e4e2b4 + ac134ee commit a4e2fba
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 89 deletions.
30 changes: 27 additions & 3 deletions Gir.toml
Expand Up @@ -106,15 +106,39 @@ status = "generate"
ignore = true
[[object.function]]
name = "get_user_name"
#manual pathbuf
#manual pathbuf is_windows_utf8
ignore = true
[[object.function]]
pattern = "[gs]etenv"
#manual unix
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "unsetenv"
#manual unix
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "filename_to_uri"
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "find_program_in_path"
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "get_home_dir"
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "get_real_name"
#manual pathbuf? is_windows_utf8
ignore = true
[[object.function]]
name = "get_tmp_dir"
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "mkstemp"
#manual is_windows_utf8
ignore = true
[[object.function]]
name = "strdup"
Expand Down
40 changes: 0 additions & 40 deletions src/auto/functions.rs
Expand Up @@ -452,26 +452,10 @@ pub fn filename_display_name<P: AsRef<std::path::Path>>(filename: P) -> Option<S
// unsafe { TODO: call ffi::g_filename_from_utf8() }
//}

pub fn filename_to_uri<'a, P: AsRef<std::path::Path>, Q: Into<Option<&'a str>>>(filename: P, hostname: Q) -> Result<String, Error> {
let hostname = hostname.into();
let hostname = hostname.to_glib_none();
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::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_to_utf8<P: AsRef<std::path::Path>>(opsysstring: P, len: /*Unimplemented*/Fundamental: SSize) -> Result<(String, /*Unimplemented*/Fundamental: Size, /*Unimplemented*/Fundamental: Size), Error> {
// unsafe { TODO: call ffi::g_filename_to_utf8() }
//}

pub fn find_program_in_path<P: AsRef<std::path::Path>>(program: P) -> Option<std::path::PathBuf> {
unsafe {
from_glib_full(ffi::g_find_program_in_path(program.as_ref().to_glib_none().0))
}
}

pub fn format_size(size: u64) -> Option<String> {
unsafe {
from_glib_full(ffi::g_format_size(size))
Expand Down Expand Up @@ -524,12 +508,6 @@ pub fn get_environ() -> Vec<String> {
}
}

pub fn get_home_dir() -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_get_home_dir())
}
}

pub fn get_host_name() -> Option<String> {
unsafe {
from_glib_none(ffi::g_get_host_name())
Expand Down Expand Up @@ -561,12 +539,6 @@ pub fn get_num_processors() -> u32 {
}
}

pub fn get_real_name() -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_get_real_name())
}
}

pub fn get_real_time() -> i64 {
unsafe {
ffi::g_get_real_time()
Expand All @@ -585,12 +557,6 @@ pub fn get_system_data_dirs() -> Vec<std::path::PathBuf> {
}
}

pub fn get_tmp_dir() -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_get_tmp_dir())
}
}

pub fn get_user_cache_dir() -> Option<std::path::PathBuf> {
unsafe {
from_glib_none(ffi::g_get_user_cache_dir())
Expand Down Expand Up @@ -902,12 +868,6 @@ pub fn mkdtemp_full<P: AsRef<std::path::Path>>(tmpl: P, mode: i32) -> Option<std
}
}

pub fn mkstemp<P: AsRef<std::path::Path>>(tmpl: P) -> i32 {
unsafe {
ffi::g_mkstemp(tmpl.as_ref().to_glib_none().0)
}
}

pub fn mkstemp_full<P: AsRef<std::path::Path>>(tmpl: P, flags: i32, mode: i32) -> i32 {
unsafe {
ffi::g_mkstemp_full(tmpl.as_ref().to_glib_none().0, flags, mode)
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Expand Up @@ -185,7 +185,8 @@ mod source;
mod time_val;
pub mod translate;
pub mod types;
pub mod utils;
mod utils;
pub use utils::*;
pub mod value;
pub mod variant;
mod variant_type;
133 changes: 88 additions & 45 deletions src/utils.rs
Expand Up @@ -4,8 +4,10 @@

use ffi;
use translate::*;
use std::path::PathBuf;
use std;
use std::path::{Path, PathBuf};
use error::BoolError;
use Error;

/// Same as [`get_prgname()`].
///
Expand Down Expand Up @@ -33,90 +35,131 @@ pub fn set_prgname(name: Option<&str>) {
}
}

#[cfg(not(windows))]
pub fn getenv(variable_name: &str) -> Option<String> {
unsafe {
from_glib_none(ffi::g_getenv(variable_name.to_glib_none().0))
}
}

#[cfg(windows)]
pub fn getenv(variable_name: &str) -> Option<String> {
use libc::c_char;
extern "C" {
fn g_getenv_utf8(variable: *const c_char) -> *const c_char;
}
#[cfg(windows)]
use ffi::g_getenv_utf8 as g_getenv;
#[cfg(not(windows))]
use ffi::g_getenv;

unsafe {
from_glib_none(g_getenv_utf8(variable_name.to_glib_none().0))
from_glib_none(g_getenv(variable_name.to_glib_none().0))
}
}

#[cfg(not(windows))]
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(ffi::g_setenv(variable_name.to_glib_none().0,
BoolError::from_glib(g_setenv(variable_name.to_glib_none().0,
value.to_glib_none().0,
overwrite.to_glib()),
"Failed to set environment variable")
}
}

#[cfg(windows)]
pub fn setenv(variable_name: &str, value: &str, overwrite: bool) -> Result<(), BoolError> {
use libc::c_char;
extern "C" {
fn g_setenv_utf8(variable: *const c_char, value: *const c_char, overwrite: ffi::gboolean) -> ffi::gboolean;
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 {
BoolError::from_glib(g_setenv_utf8(variable_name.to_glib_none().0,
value.to_glib_none().0,
overwrite.to_glib()),
"Failed to set environment variable")
from_glib_none(g_get_user_name())
}
}

#[cfg(not(windows))]
pub fn unsetenv(variable_name: &str) {
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 {
ffi::g_unsetenv(variable_name.to_glib_none().0)
from_glib_none(g_get_real_name())
}
}

#[cfg(windows)]
pub fn unsetenv(variable_name: &str) {
use libc::c_char;
extern "C" {
fn g_unsetenv_utf8(variable: *const c_char);
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 {
g_unsetenv_utf8(variable_name.to_glib_none().0)
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 get_user_name() -> Option<String> {

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_none(ffi::g_get_user_name())
from_glib_full(g_find_program_in_path(program.as_ref().to_glib_none().0))
}
}

#[cfg(not(windows))]
pub fn get_current_dir() -> Option<PathBuf> {
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_full(ffi::g_get_current_dir())
from_glib_none(g_get_home_dir())
}
}

#[cfg(windows)]
pub fn get_current_dir() -> Option<PathBuf> {
use libc::c_char;
extern "C" {
fn g_get_current_dir_utf8() -> *mut c_char;
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 {
from_glib_full(g_get_current_dir_utf8())
g_mkstemp(tmpl.as_ref().to_glib_none().0)
}
}

0 comments on commit a4e2fba

Please sign in to comment.