2023-03-10 21:47:41 -05:00
|
|
|
use crate::{
|
|
|
|
expand::ExpandChars::HomeDirectory,
|
|
|
|
wchar::{wstr, WExt, WString, L},
|
|
|
|
};
|
2023-03-05 21:38:41 -05:00
|
|
|
|
|
|
|
/// If the given path looks like it's relative to the working directory, then prepend that working
|
|
|
|
/// directory. This operates on unescaped paths only (so a ~ means a literal ~).
|
|
|
|
pub fn path_apply_working_directory(path: &wstr, working_directory: &wstr) -> WString {
|
|
|
|
if path.is_empty() || working_directory.is_empty() {
|
|
|
|
return path.to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to make sure that if we want to prepend the wd, that the string has no leading
|
|
|
|
// "/".
|
2023-03-10 21:47:41 -05:00
|
|
|
let prepend_wd =
|
|
|
|
path.as_char_slice()[0] != '/' && path.as_char_slice()[0] != HomeDirectory.into();
|
2023-03-05 21:38:41 -05:00
|
|
|
|
|
|
|
if !prepend_wd {
|
|
|
|
// No need to prepend the wd, so just return the path we were given.
|
|
|
|
return path.to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove up to one "./".
|
|
|
|
let mut path_component = path.to_owned();
|
|
|
|
if path_component.starts_with("./") {
|
|
|
|
path_component.replace_range(0..2, L!(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing leading /s.
|
|
|
|
while path_component.starts_with("/") {
|
|
|
|
path_component.replace_range(0..1, L!(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct and return a new path.
|
|
|
|
let mut new_path = working_directory.to_owned();
|
|
|
|
append_path_component(&mut new_path, &path_component);
|
|
|
|
new_path
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn append_path_component(path: &mut WString, component: &wstr) {
|
|
|
|
if path.is_empty() || component.is_empty() {
|
|
|
|
path.push_utfstr(component);
|
|
|
|
} else {
|
|
|
|
let path_len = path.len();
|
|
|
|
let path_slash = path.as_char_slice()[path_len - 1] == '/';
|
|
|
|
let comp_slash = component.as_char_slice()[0] == '/';
|
|
|
|
if !path_slash && !comp_slash {
|
|
|
|
// Need a slash
|
|
|
|
path.push('/');
|
|
|
|
} else if path_slash && comp_slash {
|
|
|
|
// Too many slashes.
|
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
path.push_utfstr(component);
|
|
|
|
}
|
|
|
|
}
|