use crate::co;
use crate::decl::*;
use crate::kernel::iterators::*;
use crate::prelude::*;
#[must_use]
pub fn dir_list<'a>(
dir_path: &'a str,
filter: Option<&'a str>,
) -> impl Iterator<Item = SysResult<String>> + 'a {
DirListIter::new(dir_path.to_owned(), filter)
}
#[must_use]
pub fn dir_walk<'a>(dir_path: &'a str) -> impl Iterator<Item = SysResult<String>> + 'a {
DirWalkIter::new(dir_path.to_owned())
}
#[cfg(debug_assertions)]
#[must_use]
pub fn exe_path() -> SysResult<String> {
let dbg = HINSTANCE::NULL.GetModuleFileName()?;
Ok(get_path(
get_path(
get_path(&dbg).unwrap(), )
.unwrap(),
)
.unwrap()
.to_owned())
}
#[cfg(not(debug_assertions))]
#[must_use]
pub fn exe_path() -> SysResult<String> {
Ok(get_path(&HINSTANCE::NULL.GetModuleFileName()?)
.unwrap()
.to_owned())
}
#[must_use]
pub fn exists(full_path: &str) -> bool {
GetFileAttributes(full_path).is_ok()
}
#[must_use]
pub fn get_file_name(full_path: &str) -> Option<&str> {
match full_path.rfind('\\') {
None => Some(full_path), Some(idx) => {
if idx == full_path.chars().count() - 1 {
None } else {
Some(&full_path[idx + 1..])
}
},
}
}
#[must_use]
pub fn get_path(full_path: &str) -> Option<&str> {
full_path
.rfind('\\') .map(|idx| &full_path[0..idx])
}
#[must_use]
pub fn has_extension(full_path: &str, extensions: &[impl AsRef<str>]) -> bool {
let full_path_u = full_path.to_uppercase();
extensions
.iter()
.find(|ext| {
let ext_u = ext.as_ref().to_uppercase();
full_path_u.ends_with(&ext_u)
})
.is_some()
}
#[must_use]
pub fn is_directory(full_path: &str) -> bool {
let flags = GetFileAttributes(full_path).unwrap();
flags.has(co::FILE_ATTRIBUTE::DIRECTORY)
}
#[must_use]
pub fn is_hidden(full_path: &str) -> bool {
let flags = GetFileAttributes(full_path).unwrap();
flags.has(co::FILE_ATTRIBUTE::HIDDEN)
}
#[must_use]
pub fn replace_extension(full_path: &str, new_extension: &str) -> String {
if let Some(last) = full_path.chars().last() {
if last == '\\' {
return rtrim_backslash(full_path).to_owned(); }
}
let new_has_dot = new_extension.chars().next() == Some('.');
match full_path.rfind('.') {
None => format!(
"{}{}{}", full_path,
if new_has_dot { "" } else { "." },
new_extension,
),
Some(idx) => {
format!("{}{}{}", &full_path[0..idx], if new_has_dot { "" } else { "." }, new_extension,)
},
}
}
#[must_use]
pub fn replace_file_name(full_path: &str, new_file: &str) -> String {
match get_path(full_path) {
None => new_file.to_owned(),
Some(path) => format!("{}\\{}", path, new_file),
}
}
#[must_use]
pub fn replace_path(full_path: &str, new_path: &str) -> String {
let file_name = get_file_name(full_path);
format!(
"{}{}{}",
rtrim_backslash(new_path),
if file_name.is_some() { "\\" } else { "" },
file_name.unwrap_or("")
)
}
#[must_use]
pub fn rtrim_backslash(full_path: &str) -> &str {
match full_path.chars().last() {
None => full_path, Some(last_ch) => {
if last_ch == '\\' {
let mut chars = full_path.chars();
chars.next_back(); chars.as_str()
} else {
full_path }
},
}
}
#[must_use]
pub fn split_parts(full_path: &str) -> Vec<&str> {
let no_bs = rtrim_backslash(full_path);
no_bs.split('\\').collect()
}