Extract xy_unix_path_to_win()

This commit is contained in:
Aoran Zeng 2023-09-27 16:52:33 +08:00
parent ce309b6c8d
commit b92c68ebed
2 changed files with 16 additions and 6 deletions

View File

@ -290,10 +290,7 @@ chsrc_check_file (const char* path)
{ {
char* cmd = NULL; char* cmd = NULL;
if(xy_on_windows) { if(xy_on_windows) {
if (xy_str_start_with(path, "~/")){ path = xy_unix_path_to_win (path);
path = xy_2strjoin("%USERPROFILE%\\", xy_str_delete_prefix(path, "~/"));
path = xy_str_gsub(path, "/", "\\");
}
cmd = xy_2strjoin ("type ", path); cmd = xy_2strjoin ("type ", path);
} else { } else {
cmd = xy_2strjoin ("cat ", path); cmd = xy_2strjoin ("cat ", path);

13
xy.h
View File

@ -529,4 +529,17 @@ xy_file_exist(char* path)
return access(newpath, 0) ? false : true; return access(newpath, 0) ? false : true;
} }
static const char*
xy_unix_path_to_win (const char* path)
{
path = xy_str_strip(path); // 防止开发者多写了空格
if (xy_str_start_with(path, "~/")){
// 或 %USERPROFILE%
path = xy_strjoin(3, xy_os_home, "\\", xy_str_delete_prefix(path, "~/"));
path = xy_str_gsub(path, "/", "\\");
}
return path;
}
#endif #endif