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

View File

@ -31,6 +31,7 @@ test: $(TARGET)
./$(TARGET) get ruby ./$(TARGET) get ruby
./$(TARGET) get python ./$(TARGET) get python
.PHONY: xy
xy: xy:
@$(CC) test_xy.c -o xy @$(CC) test_xy.c -o xy
@./xy @./xy

View File

@ -3,7 +3,7 @@
* License : MIT * License : MIT
* Authors : Aoran Zeng <ccmywish@qq.com> * Authors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-08-30> * Created on : <2023-08-30>
* Last modified : <2023-09-09> * Last modified : <2023-09-10>
* *
* test_xy: * test_xy:
* *
@ -70,5 +70,7 @@ main (int argc, char const *argv[])
xy_info("信息: 输出信息内容"); xy_info("信息: 输出信息内容");
xy_warn("警告:输出警告内容"); xy_warn("警告:输出警告内容");
xy_error("错误:输出错误内容"); xy_error("错误:输出错误内容");
putb(xy_file_exist("chsrc.png"));
return 0; return 0;
} }

20
xy.h
View File

@ -30,6 +30,8 @@
#ifdef _WIN32 #ifdef _WIN32
#include <io.h> // For access()
static bool xy_on_windows = true; static bool xy_on_windows = true;
static bool xy_on_linux = false; static bool xy_on_linux = false;
static bool xy_on_macos = false; static bool xy_on_macos = false;
@ -445,6 +447,7 @@ xy_str_strip (const char* str)
return new; return new;
} }
/** /**
* cmd后拿到cmd的执行结果 free掉这段内存 * 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 #endif