Add xy_file_exist()

This commit is contained in:
Aoran Zeng
2023-09-10 13:15:34 +08:00
parent 4938df5d2d
commit 9b2a662f26
3 changed files with 25 additions and 2 deletions

22
xy.h
View File

@@ -30,6 +30,8 @@
#ifdef _WIN32
#include <io.h> // For access()
static bool xy_on_windows = true;
static bool xy_on_linux = false;
static bool xy_on_macos = false;
@@ -445,7 +447,8 @@ xy_str_strip (const char* str)
return new;
}
/* *
/**
* 执行cmd后拿到cmd的执行结果 注意从外部free掉这段内存
* 注意:执行结果后面有回车换行
*/
@@ -491,5 +494,22 @@ xy_getcmd(const char * cmd, bool (*func)(const char*))
}
/**
* @note Windows上`path` 不要夹带变量名,因为最终 access() 不会帮你转换
*/
bool
xy_file_exist(char* path)
{
char* newpath = path;
if (xy_on_windows)
{
char* home = getenv("USERPROFILE");
if (xy_str_start_with(path, "~")) {
newpath = xy_2strjoin(home, path+1);
}
}
return access(newpath, 0) ? false : true;
}
#endif