mirror of
https://github.com/RubyMetric/chsrc.git
synced 2024-11-22 14:11:15 +08:00
Add xy_file_exist()
This commit is contained in:
parent
4938df5d2d
commit
9b2a662f26
1
Makefile
1
Makefile
|
@ -31,6 +31,7 @@ test: $(TARGET)
|
|||
./$(TARGET) get ruby
|
||||
./$(TARGET) get python
|
||||
|
||||
.PHONY: xy
|
||||
xy:
|
||||
@$(CC) test_xy.c -o xy
|
||||
@./xy
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* License : MIT
|
||||
* Authors : Aoran Zeng <ccmywish@qq.com>
|
||||
* Created on : <2023-08-30>
|
||||
* Last modified : <2023-09-09>
|
||||
* Last modified : <2023-09-10>
|
||||
*
|
||||
* test_xy:
|
||||
*
|
||||
|
@ -70,5 +70,7 @@ main (int argc, char const *argv[])
|
|||
xy_info("信息: 输出信息内容");
|
||||
xy_warn("警告:输出警告内容");
|
||||
xy_error("错误:输出错误内容");
|
||||
|
||||
putb(xy_file_exist("chsrc.png"));
|
||||
return 0;
|
||||
}
|
||||
|
|
22
xy.h
22
xy.h
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user