From cda1c2d5d283ecd8cebfa7c9585992781fc8daa6 Mon Sep 17 00:00:00 2001 From: Luca BRUNO Date: Thu, 29 Oct 2020 15:45:27 +0000 Subject: [PATCH] glib/functions: fix get_charset logic This tweaks `get_charset()` signature and logic to properly return both the charset label and the UTF-8 boolean. --- glib/Gir.toml | 4 ++++ glib/src/auto/functions.rs | 12 ------------ glib/src/functions.rs | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/glib/Gir.toml b/glib/Gir.toml index b6e02ea52956..9a70214f9fb4 100644 --- a/glib/Gir.toml +++ b/glib/Gir.toml @@ -259,6 +259,10 @@ status = "generate" # unusable ignore = true [[object.function]] + name = "get_charset" + # boolean return value + ignore = true + [[object.function]] name = "get_environ" [object.function.return] string_type = "os_string" diff --git a/glib/src/auto/functions.rs b/glib/src/auto/functions.rs index e4b400b3372e..64f464b0988d 100644 --- a/glib/src/auto/functions.rs +++ b/glib/src/auto/functions.rs @@ -571,18 +571,6 @@ pub fn get_application_name() -> Option { unsafe { from_glib_none(glib_sys::g_get_application_name()) } } -pub fn get_charset() -> Option { - unsafe { - let mut charset = ptr::null(); - let ret = from_glib(glib_sys::g_get_charset(&mut charset)); - if ret { - Some(from_glib_none(charset)) - } else { - None - } - } -} - pub fn get_codeset() -> GString { unsafe { from_glib_full(glib_sys::g_get_codeset()) } } diff --git a/glib/src/functions.rs b/glib/src/functions.rs index 18ffa82f5a1d..87f7a051e7f8 100644 --- a/glib/src/functions.rs +++ b/glib/src/functions.rs @@ -1,4 +1,3 @@ -#[cfg(not(windows))] use glib_sys; #[cfg(any(feature = "v2_58", feature = "dox"))] #[cfg(not(windows))] @@ -15,12 +14,11 @@ use std::os::unix::io::FromRawFd; // #[cfg(windows)] // #[cfg(any(feature = "v2_58", feature = "dox"))] // use std::os::windows::io::AsRawHandle; -#[cfg(not(windows))] use std::ptr; -#[cfg(not(windows))] use translate::*; #[cfg(not(windows))] use Error; +use GString; #[cfg(not(windows))] use Pid; #[cfg(not(windows))] @@ -212,3 +210,17 @@ pub fn spawn_async_with_pipes< } } } + +/// Obtain the character set for the current locale. +/// +/// This returns whether the locale's encoding is UTF-8, and the current +/// charset if available. +pub fn get_charset() -> (bool, Option) { + let (is_utf8, charset); + let mut out_charset = ptr::null(); + unsafe { + is_utf8 = from_glib(glib_sys::g_get_charset(&mut out_charset)); + charset = from_glib_none(out_charset); + }; + (is_utf8, charset) +}