2023-03-06 03:52:11 +00:00
|
|
|
pub mod errors;
|
2023-01-16 22:37:05 +00:00
|
|
|
pub mod format;
|
2023-01-15 21:18:52 +00:00
|
|
|
pub mod gettext;
|
2023-03-06 02:38:41 +00:00
|
|
|
mod normalize_path;
|
2023-03-06 03:52:11 +00:00
|
|
|
pub mod wcstod;
|
|
|
|
pub mod wcstoi;
|
2023-02-27 03:13:40 +00:00
|
|
|
mod wrealpath;
|
2023-01-15 00:34:49 +00:00
|
|
|
|
2023-02-18 01:21:44 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2023-03-13 02:28:17 +00:00
|
|
|
use crate::wchar::{wstr, WString};
|
2023-01-16 22:37:05 +00:00
|
|
|
pub(crate) use format::printf::sprintf;
|
|
|
|
pub(crate) use gettext::{wgettext, wgettext_fmt};
|
2023-03-06 02:38:41 +00:00
|
|
|
pub use normalize_path::*;
|
2023-01-15 00:34:49 +00:00
|
|
|
pub use wcstoi::*;
|
2023-02-27 03:13:40 +00:00
|
|
|
pub use wrealpath::*;
|
2023-02-18 01:21:44 +00:00
|
|
|
|
|
|
|
/// Port of the wide-string wperror from `src/wutil.cpp` but for rust `&str`.
|
|
|
|
pub fn perror(s: &str) {
|
|
|
|
let e = errno::errno().0;
|
|
|
|
let mut stderr = std::io::stderr().lock();
|
|
|
|
if !s.is_empty() {
|
|
|
|
let _ = write!(stderr, "{s}: ");
|
|
|
|
}
|
|
|
|
let slice = unsafe {
|
|
|
|
let msg = libc::strerror(e) as *const u8;
|
|
|
|
let len = libc::strlen(msg as *const _);
|
|
|
|
std::slice::from_raw_parts(msg, len)
|
|
|
|
};
|
|
|
|
let _ = stderr.write_all(slice);
|
|
|
|
let _ = stderr.write_all(b"\n");
|
|
|
|
}
|
2023-03-13 02:28:17 +00:00
|
|
|
|
|
|
|
/// Joins strings with a separator.
|
|
|
|
pub fn join_strings(strs: &[&wstr], sep: char) -> WString {
|
|
|
|
if strs.is_empty() {
|
|
|
|
return WString::new();
|
|
|
|
}
|
|
|
|
let capacity = strs.iter().fold(0, |acc, s| acc + s.len()) + strs.len() - 1;
|
|
|
|
let mut result = WString::with_capacity(capacity);
|
|
|
|
for (i, s) in strs.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
result.push(sep);
|
|
|
|
}
|
|
|
|
result.push_utfstr(s);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_join_strings() {
|
|
|
|
use crate::wchar::L;
|
|
|
|
assert_eq!(join_strings(&[], '/'), "");
|
|
|
|
assert_eq!(join_strings(&[L!("foo")], '/'), "foo");
|
|
|
|
assert_eq!(
|
|
|
|
join_strings(&[L!("foo"), L!("bar"), L!("baz")], '/'),
|
|
|
|
"foo/bar/baz"
|
|
|
|
);
|
|
|
|
}
|