This commit is contained in:
Aoran Zeng 2024-04-18 22:12:22 +08:00
parent d7bc099788
commit 0ef468f524

587
xy.h
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-28> * Created on : <2023-08-28>
* Last modified : <2023-09-29> * Last modified : <2024-04-18>
* *
* xy: * xy:
* *
@ -17,76 +17,96 @@
#ifndef XY_H #ifndef XY_H
#define XY_H #define XY_H
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <unistd.h> // For access() #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// #define NDEBUG // #define NDEBUG
#ifdef _WIN32 #ifdef _WIN32
#define xy_on_windows true #define xy_on_windows true
#define xy_on_linux false #define xy_on_linux false
#define xy_on_macos false #define xy_on_macos false
#define xy_on_bsd false #define xy_on_bsd false
#define xy_os_devnull "nul" #define xy_os_devnull "nul"
#include <windows.h> #include <windows.h>
#define xy_useutf8() SetConsoleOutputCP(65001) #define xy_useutf8() SetConsoleOutputCP (65001)
#elif defined(__linux__) || defined(__linux) #elif defined(__linux__) || defined(__linux)
#define xy_on_windows false #define xy_on_windows false
#define xy_on_linux true #define xy_on_linux true
#define xy_on_macos false #define xy_on_macos false
#define xy_on_bsd false #define xy_on_bsd false
#define xy_os_devnull "/dev/null" #define xy_os_devnull "/dev/null"
#define xy_useutf8() #define xy_useutf8()
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define xy_on_windows false #define xy_on_windows false
#define xy_on_linux false #define xy_on_linux false
#define xy_on_macos true #define xy_on_macos true
#define xy_on_bsd false #define xy_on_bsd false
#define xy_os_devnull "/dev/null" #define xy_os_devnull "/dev/null"
#define xy_useutf8() #define xy_useutf8()
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
#define xy_on_windows false #define xy_on_windows false
#define xy_on_linux false #define xy_on_linux false
#define xy_on_macos false #define xy_on_macos false
#define xy_on_bsd true #define xy_on_bsd true
#define xy_os_devnull "/dev/null" #define xy_os_devnull "/dev/null"
#define xy_useutf8() #define xy_useutf8()
#endif #endif
void
putf (double n)
{
printf ("%f\n", n);
}
void
puti (long long n)
{
printf ("%lld\n", n);
}
void
putb (bool n)
{
if (n)
puts ("true");
else
puts ("false");
}
void
print (char *s)
{
printf ("%s", s);
}
void
println (char *s)
{
printf ("%s\n", s);
}
void putf(double n) {printf("%f\n", n);} #define xy_arylen(x) (sizeof (x) / sizeof (x[0]))
void puti(long long n) {printf("%lld\n", n);}
void putb(bool n) {if(n) puts("true"); else puts("false");}
void print(char* s) {printf("%s", s);}
void println(char* s) {printf("%s\n", s);}
#define xy_arylen(x) (sizeof(x) / sizeof(x[0])) #define assert_str(a, b) assert (xy_streql ((a), (b)))
#define assert_str(a,b) assert(xy_streql((a), (b))) static inline void *
static inline void*
xy_malloc0 (size_t size) xy_malloc0 (size_t size)
{ {
void* ptr = malloc(size); void *ptr = malloc (size);
memset(ptr, 0, size); memset (ptr, 0, size);
return ptr; return ptr;
} }
#define XY_Log_Info 00001 #define XY_Log_Info 00001
#define XY_Log_Success 00001<<1 #define XY_Log_Success 00001 << 1
#define XY_Log_Warn 00001<<2 #define XY_Log_Warn 00001 << 2
#define XY_Log_Error 00001<<3 #define XY_Log_Error 00001 << 3
#define xy_success(str) _xy_log (XY_Log_Success, str) #define xy_success(str) _xy_log (XY_Log_Success, str)
#define xy_info(str) _xy_log (XY_Log_Info, str) #define xy_info(str) _xy_log (XY_Log_Info, str)
@ -94,165 +114,182 @@ xy_malloc0 (size_t size)
#define xy_error(str) _xy_log (XY_Log_Error, str) #define xy_error(str) _xy_log (XY_Log_Error, str)
static void static void
_xy_log (int level, const char* str) _xy_log (int level, const char *str)
{ {
char* color_fmt_str = NULL; char *color_fmt_str = NULL;
bool to_stderr = false; bool to_stderr = false;
if (level & XY_Log_Info) { if (level & XY_Log_Info)
{
color_fmt_str = "\033[34m%s\033[0m"; // 蓝色 color_fmt_str = "\033[34m%s\033[0m"; // 蓝色
} }
else if (level & XY_Log_Success) { else if (level & XY_Log_Success)
{
color_fmt_str = "\033[32m%s\033[0m"; // 绿色 color_fmt_str = "\033[32m%s\033[0m"; // 绿色
} }
else if (level & XY_Log_Warn) { else if (level & XY_Log_Warn)
{
color_fmt_str = "\033[33m%s\033[0m\n"; // 黄色 color_fmt_str = "\033[33m%s\033[0m\n"; // 黄色
to_stderr = true; to_stderr = true;
} }
else if (level & XY_Log_Error) { else if (level & XY_Log_Error)
{
color_fmt_str = "\033[31m%s\033[0m\n"; // 红色 color_fmt_str = "\033[31m%s\033[0m\n"; // 红色
to_stderr = true; to_stderr = true;
} }
else { else
//xy_assert ("CAN'T REACH!"); {
// xy_assert ("CAN'T REACH!");
} }
// -2 把中间%s减掉 // -2 把中间%s减掉
size_t len = strlen(color_fmt_str) -2; size_t len = strlen (color_fmt_str) - 2;
char* buf = malloc(strlen(str) + len + 1); char *buf = malloc (strlen (str) + len + 1);
sprintf (buf, color_fmt_str, str); sprintf (buf, color_fmt_str, str);
if (to_stderr) { if (to_stderr)
fprintf(stderr, "%s",buf); {
} else { fprintf (stderr, "%s", buf);
puts(buf);
} }
free(buf); else
{
puts (buf);
}
free (buf);
} }
/** /**
* str中所有的pat字符串替换成replace * str中所有的pat字符串替换成replace
*/ */
static char* static char *
xy_str_gsub (const char* str, const char* pat, const char* replace) xy_str_gsub (const char *str, const char *pat, const char *replace)
{ {
size_t replace_len = strlen(replace); size_t replace_len = strlen (replace);
size_t pat_len = strlen(pat); size_t pat_len = strlen (pat);
int unit = replace_len - pat_len; int unit = replace_len - pat_len;
if (unit<=0) unit = 0; if (unit <= 0)
unit = 0;
size_t len = strlen(str); size_t len = strlen (str);
const char* cur = str; const char *cur = str;
int count = 0; int count = 0;
while (cur<str+len) { while (cur < str + len)
char* fnd = strstr(cur, pat); {
if (fnd) { char *fnd = strstr (cur, pat);
if (fnd)
{
count++; count++;
cur = fnd + pat_len; cur = fnd + pat_len;
} else break; }
else
break;
} }
// puti(count); DEBUG 匹配次数 // puti(count); DEBUG 匹配次数
char* ret = malloc (unit * count + len + 1); char *ret = malloc (unit * count + len + 1);
char* retcur = ret; char *retcur = ret;
cur = str; cur = str;
while(cur<str+len) { while (cur < str + len)
char* fnd = strstr(cur, pat); {
if (fnd) { char *fnd = strstr (cur, pat);
if (fnd)
{
ptrdiff_t diff = fnd - cur; ptrdiff_t diff = fnd - cur;
strncpy(retcur, cur, diff); strncpy (retcur, cur, diff);
cur = fnd + pat_len; cur = fnd + pat_len;
retcur += diff; retcur += diff;
strcpy(retcur, replace); strcpy (retcur, replace);
retcur += replace_len; retcur += replace_len;
} else break;
} }
strcpy(retcur, cur); else
break;
}
strcpy (retcur, cur);
return ret; return ret;
} }
static char *
static char* xy_2strjoin (const char *str1, const char *str2)
xy_2strjoin (const char* str1, const char* str2)
{ {
size_t len = strlen(str1); size_t len = strlen (str1);
size_t size = len + strlen(str2) + 1; size_t size = len + strlen (str2) + 1;
char* ret = malloc(size); char *ret = malloc (size);
strcpy(ret, str1); strcpy (ret, str1);
strcpy(ret+len, str2); strcpy (ret + len, str2);
return ret; return ret;
} }
static char *
static char*
xy_strjoin (unsigned int count, ...) xy_strjoin (unsigned int count, ...)
{ {
size_t al_fixed = 128; size_t al_fixed = 128;
char* ret = calloc(1, al_fixed); char *ret = calloc (1, al_fixed);
// 已分配次数 // 已分配次数
int al_times = 1; int al_times = 1;
// 当前已分配量 // 当前已分配量
size_t al_cur = al_fixed; size_t al_cur = al_fixed;
const char* str = NULL; const char *str = NULL;
// 需要分配的量 // 需要分配的量
size_t al_need = 0; size_t al_need = 0;
// 用于 strcpy() 到 ret 的哪个位置 // 用于 strcpy() 到 ret 的哪个位置
char* cur = ret + 0; char *cur = ret + 0;
va_list args; va_list args;
va_start(args, count); va_start (args, count);
for(int i=0; i<count; i++) for (int i = 0; i < count; i++)
{ {
// 是否需要重新分配 // 是否需要重新分配
bool need_realloc = false; bool need_realloc = false;
str = va_arg(args, const char*); str = va_arg (args, const char *);
al_need += strlen(str); al_need += strlen (str);
while (al_need > al_cur) { while (al_need > al_cur)
al_times += 1; al_cur = al_times * al_fixed; {
al_times += 1;
al_cur = al_times * al_fixed;
need_realloc = true; need_realloc = true;
} }
// printf("al_times %d, al_need %zd, al_cur %zd\n", al_times, al_need, al_cur); // printf("al_times %d, al_need %zd, al_cur %zd\n", al_times, al_need,
if (need_realloc) { // al_cur);
if (need_realloc)
{
ptrdiff_t diff = cur - ret; ptrdiff_t diff = cur - ret;
ret = realloc(ret, al_cur); ret = realloc (ret, al_cur);
cur = ret + diff; cur = ret + diff;
} }
if (NULL==ret) { if (NULL == ret)
xy_error ("xy: No availble memory!"); return NULL; {
xy_error ("xy: No availble memory!");
return NULL;
} }
strcpy(cur, str); strcpy (cur, str);
// puts(ret); // puts(ret);
cur += strlen(str); cur += strlen (str);
} }
va_end(args); va_end (args);
*cur = '\0'; *cur = '\0';
return ret; return ret;
} }
static char *
static char* xy_strdup (const char *str)
xy_strdup(const char* str)
{ {
size_t len = strlen(str); size_t len = strlen (str);
char* new = xy_malloc0(len+1); char *new = xy_malloc0 (len + 1);
strcpy(new, str); strcpy (new, str);
return new; return new;
} }
#define XY_Str_Bold 1 #define XY_Str_Bold 1
#define XY_Str_Faint 2 #define XY_Str_Faint 2
#define XY_Str_Italic 3 #define XY_Str_Italic 3
@ -260,12 +297,13 @@ xy_strdup(const char* str)
#define XY_Str_Blink 5 #define XY_Str_Blink 5
#define XY_Str_Cross 9 #define XY_Str_Cross 9
#define xy_str_to_bold(str) _xy_str_to_terminal_style(XY_Str_Bold, str) #define xy_str_to_bold(str) _xy_str_to_terminal_style (XY_Str_Bold, str)
#define xy_str_to_faint(str) _xy_str_to_terminal_style(XY_Str_Faint, str) #define xy_str_to_faint(str) _xy_str_to_terminal_style (XY_Str_Faint, str)
#define xy_str_to_italic(str) _xy_str_to_terminal_style(XY_Str_Italic, str) #define xy_str_to_italic(str) _xy_str_to_terminal_style (XY_Str_Italic, str)
#define xy_str_to_underline(str) _xy_str_to_terminal_style(XY_Str_Underline,str) #define xy_str_to_underline(str) \
#define xy_str_to_blink(str) _xy_str_to_terminal_style(XY_Str_Blink, str) _xy_str_to_terminal_style (XY_Str_Underline, str)
#define xy_str_to_cross(str) _xy_str_to_terminal_style(XY_Str_Cross, str) #define xy_str_to_blink(str) _xy_str_to_terminal_style (XY_Str_Blink, str)
#define xy_str_to_cross(str) _xy_str_to_terminal_style (XY_Str_Cross, str)
#define XY_Str_Red 31 #define XY_Str_Red 31
#define XY_Str_Green 32 #define XY_Str_Green 32
@ -274,87 +312,64 @@ xy_strdup(const char* str)
#define XY_Str_Magenta 35 #define XY_Str_Magenta 35
#define XY_Str_Cyan 36 #define XY_Str_Cyan 36
#define xy_str_to_red(str) _xy_str_to_terminal_style(XY_Str_Red, str) #define xy_str_to_red(str) _xy_str_to_terminal_style (XY_Str_Red, str)
#define xy_str_to_green(str) _xy_str_to_terminal_style(XY_Str_Green, str) #define xy_str_to_green(str) _xy_str_to_terminal_style (XY_Str_Green, str)
#define xy_str_to_yellow(str) _xy_str_to_terminal_style(XY_Str_Yellow, str) #define xy_str_to_yellow(str) _xy_str_to_terminal_style (XY_Str_Yellow, str)
#define xy_str_to_blue(str) _xy_str_to_terminal_style(XY_Str_Blue, str) #define xy_str_to_blue(str) _xy_str_to_terminal_style (XY_Str_Blue, str)
#define xy_str_to_magenta(str) _xy_str_to_terminal_style(XY_Str_Magenta,str) #define xy_str_to_magenta(str) _xy_str_to_terminal_style (XY_Str_Magenta, str)
#define xy_str_to_purple xy_str_to_magenta #define xy_str_to_purple xy_str_to_magenta
#define xy_str_to_cyan(str) _xy_str_to_terminal_style(XY_Str_Cyan, str) #define xy_str_to_cyan(str) _xy_str_to_terminal_style (XY_Str_Cyan, str)
static char* static char *
_xy_str_to_terminal_style(int style, const char* str) _xy_str_to_terminal_style (int style, const char *str)
{ {
char* color_fmt_str = NULL; char *color_fmt_str = NULL;
if (XY_Str_Red==style)
{
color_fmt_str = "\e[31m%s\e[0m"; // 红色
}
else if (XY_Str_Green==style)
{
color_fmt_str = "\e[32m%s\e[0m"; // 绿色
}
else if (XY_Str_Yellow==style)
{
color_fmt_str = "\e[33m%s\e[0m"; // 黄色
}
else if (XY_Str_Blue==style)
{
color_fmt_str = "\e[34m%s\e[0m"; // 蓝色
}
else if (XY_Str_Magenta==style)
{
color_fmt_str = "\e[35m%s\e[0m"; // 蓝色
}
else if (XY_Str_Cyan==style)
{
color_fmt_str = "\e[36m%s\e[0m"; // 蓝色
}
switch (style)
else if (XY_Str_Bold==style)
{ {
case XY_Str_Red:
color_fmt_str = "\e[31m%s\e[0m";
case XY_Str_Green:
color_fmt_str = "\e[32m%s\e[0m";
case XY_Str_Yellow:
color_fmt_str = "\e[33m%s\e[0m";
case XY_Str_Blue:
color_fmt_str = "\e[34m%s\e[0m";
case XY_Str_Magenta:
color_fmt_str = "\e[35m%s\e[0m";
case XY_Str_Cyan:
color_fmt_str = "\e[36m%s\e[0m";
case XY_Str_Bold:
color_fmt_str = "\e[1m%s\e[0m"; color_fmt_str = "\e[1m%s\e[0m";
} case XY_Str_Faint:
else if (XY_Str_Faint==style)
{
color_fmt_str = "\e[2m%s\e[0m"; color_fmt_str = "\e[2m%s\e[0m";
} case XY_Str_Italic:
else if (XY_Str_Italic==style)
{
color_fmt_str = "\e[3m%s\e[0m"; color_fmt_str = "\e[3m%s\e[0m";
} case XY_Str_Underline:
else if (XY_Str_Underline==style)
{
color_fmt_str = "\e[4m%s\e[0m"; color_fmt_str = "\e[4m%s\e[0m";
} case XY_Str_Blink:
else if (XY_Str_Blink==style)
{
color_fmt_str = "\e[5m%s\e[0m"; color_fmt_str = "\e[5m%s\e[0m";
} case XY_Str_Cross:
else if (XY_Str_Cross==style)
{
color_fmt_str = "\e[9m%s\e[0m"; color_fmt_str = "\e[9m%s\e[0m";
} }
// -2 把中间%s减掉 // -2 把中间%s减掉
size_t len = strlen(color_fmt_str) -2; size_t len = strlen (color_fmt_str) - 2;
char* buf = malloc(strlen(str) + len + 1); char *buf = malloc (strlen (str) + len + 1);
sprintf (buf, color_fmt_str, str); sprintf (buf, color_fmt_str, str);
return buf; return buf;
} }
static bool static bool
xy_streql(const char* str1, const char* str2) { xy_streql (const char *str1, const char *str2)
return strcmp(str1, str2) == 0 ? true : false; {
return strcmp (str1, str2) == 0 ? true : false;
} }
static char *
static char* xy_str_to_quietcmd (const char *cmd)
xy_str_to_quietcmd (const char* cmd)
{ {
char* ret = NULL; char *ret = NULL;
#ifdef _WIN32 #ifdef _WIN32
ret = xy_2strjoin (cmd, " >nul 2>nul "); ret = xy_2strjoin (cmd, " >nul 2>nul ");
#else #else
@ -363,90 +378,97 @@ xy_str_to_quietcmd (const char* cmd)
return ret; return ret;
} }
static bool static bool
xy_str_end_with (const char* str, const char* suffix) xy_str_end_with (const char *str, const char *suffix)
{ {
size_t len1 = strlen(str); size_t len1 = strlen (str);
size_t len2 = strlen(suffix); size_t len2 = strlen (suffix);
if (0==len2) return true; // 空字符串直接返回 if (0 == len2)
if (len1 < len2) return false; return true; // 空字符串直接返回
if (len1 < len2)
return false;
const char* cur1 = str + len1 - 1; const char *cur1 = str + len1 - 1;
const char* cur2 = suffix + len2 - 1; const char *cur2 = suffix + len2 - 1;
for (int i=0; i<len2; i++) for (int i = 0; i < len2; i++)
{ {
if (*cur1 != *cur2) return false; if (*cur1 != *cur2)
cur1--; cur2--; return false;
cur1--;
cur2--;
} }
return true; return true;
} }
static bool static bool
xy_str_start_with (const char* str, const char* prefix) xy_str_start_with (const char *str, const char *prefix)
{ {
size_t len1 = strlen(str); size_t len1 = strlen (str);
size_t len2 = strlen(prefix); size_t len2 = strlen (prefix);
if (0==len2) return true; // 空字符串直接返回 if (0 == len2)
if (len1 < len2) return false; return true; // 空字符串直接返回
if (len1 < len2)
return false;
const char* cur1 = str; const char *cur1 = str;
const char* cur2 = prefix; const char *cur2 = prefix;
for (int i=0; i<len2; i++) for (int i = 0; i < len2; i++)
{ {
if (*cur1 != *cur2) return false; if (*cur1 != *cur2)
cur1++; cur2++; return false;
cur1++;
cur2++;
} }
return true; return true;
} }
static char* static char *
xy_str_delete_prefix (const char* str, const char* prefix) xy_str_delete_prefix (const char *str, const char *prefix)
{ {
char* new = xy_strdup(str); char *new = xy_strdup (str);
bool yes = xy_str_start_with(str, prefix); bool yes = xy_str_start_with (str, prefix);
if (!yes) return new; if (!yes)
return new;
size_t len = strlen(prefix); size_t len = strlen (prefix);
char* cur = new + len; char *cur = new + len;
return cur; return cur;
} }
static char *
static char* xy_str_delete_suffix (const char *str, const char *suffix)
xy_str_delete_suffix (const char* str, const char* suffix)
{ {
char* new = xy_strdup(str); char *new = xy_strdup (str);
bool yes = xy_str_end_with(str, suffix); bool yes = xy_str_end_with (str, suffix);
if (!yes) return new; if (!yes)
return new;
size_t len1 = strlen(str); size_t len1 = strlen (str);
size_t len2 = strlen(suffix); size_t len2 = strlen (suffix);
char* cur = new + len1 - len2; char *cur = new + len1 - len2;
*cur = '\0'; *cur = '\0';
return new; return new;
} }
static char *
static char* xy_str_strip (const char *str)
xy_str_strip (const char* str)
{ {
char* new = xy_strdup(str); char *new = xy_strdup (str);
while (strchr("\n\r\v\t\f ", new[0])) while (strchr ("\n\r\v\t\f ", new[0]))
{ {
new += 1; new += 1;
} }
size_t len = strlen(new); size_t len = strlen (new);
char* last = new + len - 1; char *last = new + len - 1;
while (strchr("\n\r\v\t\f ", *last)) while (strchr ("\n\r\v\t\f ", *last))
{ {
*last = '\0'; *last = '\0';
last -= 1; last -= 1;
@ -454,7 +476,6 @@ xy_str_strip (const char* str)
return new; return new;
} }
/** /**
* cmd * cmd
* *
@ -464,110 +485,122 @@ xy_str_strip (const char* str)
* *
* @note * @note
*/ */
static char* static char *
xy_getcmd (const char* cmd, unsigned long n, void (*func)(const char*)) xy_getcmd (const char *cmd, unsigned long n, void (*func) (const char *))
{ {
const int size = 512; const int size = 512;
char* buf = (char*) malloc(size); char *buf = (char *)malloc (size);
FILE* stream = popen(cmd, "r"); FILE *stream = popen (cmd, "r");
if (stream == NULL) { if (stream == NULL)
fprintf(stderr, "xy: 命令执行失败\n"); {
fprintf (stderr, "xy: 命令执行失败\n");
return NULL; return NULL;
} }
char* ret = NULL; char *ret = NULL;
unsigned long count = 0; unsigned long count = 0;
while (true) { while (true)
if (NULL==fgets(buf, size, stream)) break; {
if (NULL == fgets (buf, size, stream))
break;
ret = buf; ret = buf;
count += 1; count += 1;
if (n==count) break; if (n == count)
if (func) { func(buf); } break;
if (func)
{
func (buf);
}
} }
pclose (stream); pclose (stream);
return ret; return ret;
} }
#define xy_os_home _xy_os_home ()
#define xy_os_home _xy_os_home() static char *
static char*
_xy_os_home () _xy_os_home ()
{ {
char* home = NULL; char *home = NULL;
if (xy_on_windows) if (xy_on_windows)
home = getenv("USERPROFILE"); home = getenv ("USERPROFILE");
else else
home = getenv("HOME"); home = getenv ("HOME");
return home; return home;
} }
#define xy_win_powershell_profile _xy_win_powershell_profile ()
#define xy_win_powershell_profile _xy_win_powershell_profile() #define xy_win_powershellv5_profile _xy_win_powershellv5_profile ()
#define xy_win_powershellv5_profile _xy_win_powershellv5_profile() static char *
static char*
_xy_win_powershell_profile () _xy_win_powershell_profile ()
{ {
return xy_2strjoin(xy_os_home, "\\Documents\\PowerShell\\Microsoft.PowerShell_profile.ps1"); return xy_2strjoin (
xy_os_home, "\\Documents\\PowerShell\\Microsoft.PowerShell_profile.ps1");
} }
char* char *
_xy_win_powershellv5_profile () _xy_win_powershellv5_profile ()
{ {
return xy_2strjoin(xy_os_home, "\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1"); return xy_2strjoin (
xy_os_home,
"\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
} }
/** /**
* @note Windows上`path` access() * @note Windows上`path` access()
*/ */
static bool static bool
xy_file_exist (const char* path) xy_file_exist (const char *path)
{ {
const char* newpath = path; const char *newpath = path;
if (xy_on_windows) if (xy_on_windows)
{ {
if (xy_str_start_with(path, "~")) { if (xy_str_start_with (path, "~"))
newpath = xy_2strjoin(xy_os_home, path+1); {
newpath = xy_2strjoin (xy_os_home, path + 1);
} }
} }
return access(newpath, 0) ? false : true; return access (newpath, 0) ? false : true;
} }
/** /**
* 使Windows下也可调用 * 使Windows下也可调用
*/ */
static char* static char *
xy_uniform_path (const char* path) xy_uniform_path (const char *path)
{ {
char* new = xy_str_strip(path); // 防止开发者多写了空白符 char *new = xy_str_strip (path); // 防止开发者多写了空白符
// 这个函数仅在Windows上才进行替换 // 这个函数仅在Windows上才进行替换
if (xy_on_windows) { if (xy_on_windows)
if (xy_str_start_with(new, "~/")){ {
if (xy_str_start_with (new, "~/"))
{
// 或 %USERPROFILE% // 或 %USERPROFILE%
new = xy_strjoin(3, xy_os_home, "\\", xy_str_delete_prefix(new, "~/")); new = xy_strjoin (3, xy_os_home, "\\",
xy_str_delete_prefix (new, "~/"));
} }
new = xy_str_gsub(new, "/", "\\"); new = xy_str_gsub (new, "/", "\\");
} }
return new; return new;
} }
static char *
static char* xy_parent_dir (const char *path)
xy_parent_dir (const char* path)
{ {
char* dir = xy_uniform_path (path); char *dir = xy_uniform_path (path);
char* last = NULL; char *last = NULL;
if (xy_on_windows) { if (xy_on_windows)
last = strrchr(dir, '\\'); {
last = strrchr (dir, '\\');
*last = '\0'; *last = '\0';
} else { }
last = strrchr(dir, '/'); else
{
last = strrchr (dir, '/');
*last = '\0'; *last = '\0';
} }
return dir; return dir;