chsrc/include/xy.h

735 lines
18 KiB
C
Raw Normal View History

2023-09-09 16:57:06 +08:00
/** ------------------------------------------------------------
2024-06-11 20:18:02 +08:00
* SPDX-License-Identifier: MIT
* -------------------------------------------------------------
2023-09-09 16:57:06 +08:00
* File : xy.h
* Authors : Aoran Zeng <ccmywish@qq.com>
2024-06-11 20:18:02 +08:00
* | Heng Guo <2085471348@qq.com>
2024-06-21 01:47:28 +08:00
* Contributors : Null Nil <null@nil.com>
* |
2023-09-09 16:57:06 +08:00
* Created on : <2023-08-28>
2024-07-09 14:06:27 +08:00
* Last modified : <2024-07-09>
2023-09-09 16:57:06 +08:00
*
2024-06-11 20:18:02 +08:00
* xy:
* Corss-Platform C utilities for CLI applications in Ruby flavor
2023-09-09 16:57:06 +08:00
* ------------------------------------------------------------*/
2023-08-28 22:21:14 +08:00
2023-08-30 15:34:56 +08:00
#ifndef XY_H
#define XY_H
2024-07-09 14:06:27 +08:00
#define _XY_Version "v0.1.3.0-2024/07/09"
2024-06-11 20:18:02 +08:00
#define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/include/xy.h"
2024-04-18 22:18:28 +08:00
2024-04-18 22:12:22 +08:00
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
2024-04-18 22:12:22 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2023-08-30 15:34:56 +08:00
// #define NDEBUG
2023-08-28 22:21:14 +08:00
2023-08-29 21:58:51 +08:00
#ifdef _WIN32
2024-06-08 07:24:57 +08:00
#define xy_on_windows true
#define xy_on_linux false
#define xy_on_macos false
#define xy_on_bsd false
#define xy_os_devnull "nul"
#include <windows.h>
#define xy_useutf8() SetConsoleOutputCP (65001)
2023-08-30 17:09:59 +08:00
2023-09-10 14:50:41 +08:00
#elif defined(__linux__) || defined(__linux)
2024-06-08 07:24:57 +08:00
#define xy_on_windows false
#define xy_on_linux true
#define xy_on_macos false
#define xy_on_bsd false
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
2023-09-13 22:41:16 +08:00
#elif defined(__APPLE__)
2024-06-08 07:24:57 +08:00
#define xy_on_windows false
#define xy_on_linux false
#define xy_on_macos true
#define xy_on_bsd false
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
2024-06-08 07:24:57 +08:00
#define xy_on_windows false
#define xy_on_linux false
#define xy_on_macos false
#define xy_on_bsd true
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
2023-08-29 21:58:51 +08:00
#endif
2024-06-08 07:24:57 +08:00
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 (const char *s) { printf ("%s", s); }
void println (const char *s) { printf ("%s\n", s);}
void say (const char *s) { printf ("%s\n", s);}
2023-09-22 15:45:12 +08:00
2024-04-18 22:12:22 +08:00
#define assert_str(a, b) assert (xy_streql ((a), (b)))
2023-08-30 09:50:38 +08:00
2024-06-21 10:50:28 +08:00
#define xy_unsupport assert(!"Unsuppoted")
#define xy_unimplement assert(!"Unimplemented temporarily")
#define xy_unreach assert(!"This code shouldn't be reached")
#define xy_arylen(x) (sizeof (x) / sizeof (x[0]))
2024-04-18 22:12:22 +08:00
static inline void *
2023-08-30 09:50:38 +08:00
xy_malloc0 (size_t size)
{
2024-04-18 22:12:22 +08:00
void *ptr = malloc (size);
memset (ptr, 0, size);
2023-08-30 09:50:38 +08:00
return ptr;
}
2024-06-08 07:13:38 +08:00
/******************************************************
* String
******************************************************/
2023-08-30 20:06:28 +08:00
/**
2023-09-27 16:01:40 +08:00
* str中所有的pat字符串替换成replace
2023-08-30 20:06:28 +08:00
*/
2024-04-18 22:12:22 +08:00
static char *
xy_str_gsub (const char *str, const char *pat, const char *replace)
{
2024-04-18 22:12:22 +08:00
size_t replace_len = strlen (replace);
size_t pat_len = strlen (pat);
2023-09-27 16:01:40 +08:00
int unit = replace_len - pat_len;
2024-04-18 22:12:22 +08:00
if (unit <= 0)
unit = 0;
2023-09-27 16:01:40 +08:00
2024-04-18 22:12:22 +08:00
size_t len = strlen (str);
2023-09-27 16:01:40 +08:00
2024-04-18 22:12:22 +08:00
const char *cur = str;
2023-09-27 16:01:40 +08:00
int count = 0;
2024-04-18 22:12:22 +08:00
while (cur < str + len)
{
char *fnd = strstr (cur, pat);
if (fnd)
{
count++;
cur = fnd + pat_len;
}
else
break;
}
2023-09-27 16:41:28 +08:00
// puti(count); DEBUG 匹配次数
2023-09-27 16:01:40 +08:00
2024-04-18 22:12:22 +08:00
char *ret = malloc (unit * count + len + 1);
char *retcur = ret;
2023-09-27 16:01:40 +08:00
cur = str;
2024-04-18 22:12:22 +08:00
while (cur < str + len)
{
char *fnd = strstr (cur, pat);
if (fnd)
{
ptrdiff_t diff = fnd - cur;
strncpy (retcur, cur, diff);
cur = fnd + pat_len;
retcur += diff;
strcpy (retcur, replace);
retcur += replace_len;
}
else
break;
}
strcpy (retcur, cur);
2023-09-27 16:01:40 +08:00
return ret;
}
2024-04-18 22:12:22 +08:00
static char *
xy_2strjoin (const char *str1, const char *str2)
{
2024-04-18 22:12:22 +08:00
size_t len = strlen (str1);
size_t size = len + strlen (str2) + 1;
char *ret = malloc (size);
strcpy (ret, str1);
strcpy (ret + len, str2);
return ret;
}
2024-04-18 22:12:22 +08:00
static char *
xy_strjoin (unsigned int count, ...)
{
size_t al_fixed = 128;
2024-04-18 22:12:22 +08:00
char *ret = calloc (1, al_fixed);
// 已分配次数
int al_times = 1;
// 当前已分配量
size_t al_cur = al_fixed;
2024-04-18 22:12:22 +08:00
const char *str = NULL;
// 需要分配的量
size_t al_need = 0;
// 用于 strcpy() 到 ret 的哪个位置
2024-04-18 22:12:22 +08:00
char *cur = ret + 0;
va_list args;
2024-04-18 22:12:22 +08:00
va_start (args, count);
for (int i = 0; i < count; i++)
{
// 是否需要重新分配
bool need_realloc = false;
str = va_arg (args, const char *);
al_need += strlen (str);
while (al_need > al_cur)
{
al_times += 1;
al_cur = al_times * al_fixed;
need_realloc = true;
}
// printf("al_times %d, al_need %zd, al_cur %zd\n", al_times, al_need,
// al_cur);
if (need_realloc)
{
ptrdiff_t diff = cur - ret;
ret = realloc (ret, al_cur);
cur = ret + diff;
}
if (NULL == ret)
{
2024-06-08 08:13:54 +08:00
fprintf (stderr, "xy.h: No availble memory!");
2024-04-18 22:12:22 +08:00
return NULL;
}
strcpy (cur, str);
// puts(ret);
cur += strlen (str);
}
2024-04-18 22:12:22 +08:00
va_end (args);
*cur = '\0';
return ret;
}
2024-04-18 22:12:22 +08:00
static char *
xy_strdup (const char *str)
2023-09-04 15:10:18 +08:00
{
2024-04-18 22:12:22 +08:00
size_t len = strlen (str);
char *new = xy_malloc0 (len + 1);
strcpy (new, str);
return new;
2023-09-04 15:10:18 +08:00
}
2024-06-11 17:00:22 +08:00
#define _XY_Str_Bold 1
#define _XY_Str_Faint 2
#define _XY_Str_Italic 3
#define _XY_Str_Underline 4
#define _XY_Str_Blink 5
#define _XY_Str_Cross 9
#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_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_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_Green 32
#define _XY_Str_Yellow 33
#define _XY_Str_Blue 34
#define _XY_Str_Magenta 35
#define _XY_Str_Cyan 36
#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_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_magenta(str) _xy_str_to_terminal_style (_XY_Str_Magenta, str)
2024-06-08 08:13:54 +08:00
#define xy_str_to_purple xy_str_to_magenta
2024-06-11 17:00:22 +08:00
#define xy_str_to_cyan(str) _xy_str_to_terminal_style (_XY_Str_Cyan, str)
2024-04-18 22:12:22 +08:00
static char *
_xy_str_to_terminal_style (int style, const char *str)
2023-09-05 15:15:55 +08:00
{
2024-04-18 22:12:22 +08:00
char *color_fmt_str = NULL;
switch (style)
{
2024-06-11 17:00:22 +08:00
case _XY_Str_Red:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[31m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Green:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[32m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Yellow:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[33m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Blue:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[34m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Magenta:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[35m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Cyan:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[36m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Bold:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[1m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Faint:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[2m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Italic:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[3m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Underline:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[4m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Blink:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[5m%s\e[0m"; break;
2024-06-11 17:00:22 +08:00
case _XY_Str_Cross:
2024-04-18 22:41:15 +08:00
color_fmt_str = "\e[9m%s\e[0m"; break;
2024-04-18 22:12:22 +08:00
}
2023-09-05 15:15:55 +08:00
// -2 把中间%s减掉
2024-04-18 22:12:22 +08:00
size_t len = strlen (color_fmt_str) - 2;
char *buf = malloc (strlen (str) + len + 1);
2023-09-05 15:15:55 +08:00
sprintf (buf, color_fmt_str, str);
return buf;
}
2023-09-10 14:50:41 +08:00
static bool
2024-04-18 22:12:22 +08:00
xy_streql (const char *str1, const char *str2)
{
2024-06-08 15:32:31 +08:00
if (NULL==str1 || NULL==str2)
{
return false;
}
2024-04-18 22:12:22 +08:00
return strcmp (str1, str2) == 0 ? true : false;
2023-08-31 16:54:06 +08:00
}
2024-04-18 22:12:22 +08:00
static char *
xy_str_to_quietcmd (const char *cmd)
2023-09-02 21:36:54 +08:00
{
2024-04-18 22:12:22 +08:00
char *ret = NULL;
2023-09-02 21:36:54 +08:00
#ifdef _WIN32
ret = xy_2strjoin (cmd, " >nul 2>nul ");
2023-09-02 21:36:54 +08:00
#else
ret = xy_2strjoin (cmd, " 1>/dev/null 2>&1 ");
2023-09-02 21:36:54 +08:00
#endif
2023-09-04 15:10:18 +08:00
return ret;
2023-09-02 21:36:54 +08:00
}
2023-09-10 14:50:41 +08:00
static bool
2024-04-18 22:12:22 +08:00
xy_str_end_with (const char *str, const char *suffix)
2023-09-04 21:15:16 +08:00
{
2024-04-18 22:12:22 +08:00
size_t len1 = strlen (str);
size_t len2 = strlen (suffix);
if (0 == len2)
return true; // 空字符串直接返回
if (len1 < len2)
return false;
const char *cur1 = str + len1 - 1;
const char *cur2 = suffix + len2 - 1;
for (int i = 0; i < len2; i++)
{
if (*cur1 != *cur2)
return false;
cur1--;
cur2--;
}
2023-09-04 21:15:16 +08:00
return true;
}
2023-09-10 14:50:41 +08:00
static bool
2024-04-18 22:12:22 +08:00
xy_str_start_with (const char *str, const char *prefix)
{
2024-06-14 01:54:58 +08:00
if (NULL==str || NULL==prefix)
{
return false;
}
2024-04-18 22:12:22 +08:00
size_t len1 = strlen (str);
size_t len2 = strlen (prefix);
if (0 == len2)
return true; // 空字符串直接返回
if (len1 < len2)
return false;
const char *cur1 = str;
const char *cur2 = prefix;
for (int i = 0; i < len2; i++)
{
if (*cur1 != *cur2)
return false;
cur1++;
cur2++;
}
return true;
}
2024-04-18 22:12:22 +08:00
static char *
xy_str_delete_prefix (const char *str, const char *prefix)
{
2024-04-18 22:12:22 +08:00
char *new = xy_strdup (str);
bool yes = xy_str_start_with (str, prefix);
if (!yes)
return new;
2024-04-18 22:12:22 +08:00
size_t len = strlen (prefix);
char *cur = new + len;
return cur;
}
2024-04-18 22:12:22 +08:00
static char *
xy_str_delete_suffix (const char *str, const char *suffix)
2023-09-04 21:21:36 +08:00
{
2024-04-18 22:12:22 +08:00
char *new = xy_strdup (str);
bool yes = xy_str_end_with (str, suffix);
if (!yes)
return new;
size_t len1 = strlen (str);
size_t len2 = strlen (suffix);
char *cur = new + len1 - len2;
2023-09-04 21:21:36 +08:00
*cur = '\0';
return new;
}
2024-04-18 22:12:22 +08:00
static char *
xy_str_strip (const char *str)
2023-09-05 11:09:35 +08:00
{
2024-04-18 22:12:22 +08:00
char *new = xy_strdup (str);
2023-09-05 11:09:35 +08:00
2024-04-18 22:12:22 +08:00
while (strchr ("\n\r\v\t\f ", new[0]))
{
new += 1;
}
2023-09-27 16:12:22 +08:00
2024-04-18 22:12:22 +08:00
size_t len = strlen (new);
2023-09-27 16:12:22 +08:00
2024-04-18 22:12:22 +08:00
char *last = new + len - 1;
2023-09-27 16:12:22 +08:00
2024-04-18 22:12:22 +08:00
while (strchr ("\n\r\v\t\f ", *last))
{
*last = '\0';
last -= 1;
}
2023-09-05 11:09:35 +08:00
return new;
}
2024-06-08 08:13:54 +08:00
2024-06-08 07:13:38 +08:00
/******************************************************
* Logging
******************************************************/
2024-06-11 17:00:22 +08:00
#define _XY_Log_Plain 000000001
#define _XY_Log_Success 000000001 << 1
#define _XY_Log_Info 000000001 << 2
#define _XY_Log_Warn 000000001 << 3
#define _XY_Log_Error 000000001 << 4
2024-06-08 07:13:38 +08:00
2024-06-11 17:00:22 +08:00
#define xy_log(prompt, str) _xy_log (_XY_Log_Plain, prompt, str)
#define xy_succ(prompt,str) _xy_log (_XY_Log_Success, prompt, str)
#define xy_info(prompt,str) _xy_log (_XY_Log_Info, prompt, str)
#define xy_warn(prompt,str) _xy_log (_XY_Log_Warn, prompt, str)
#define xy_error(prompt,str) _xy_log (_XY_Log_Error, prompt, str)
2024-06-08 08:13:54 +08:00
static void
_xy_log (int level, const char *prompt, const char *content)
2024-06-08 07:13:38 +08:00
{
2024-06-08 08:13:54 +08:00
char *str = NULL;
bool to_stderr = false;
/**
* 'app: content'
*/
2024-06-11 17:00:22 +08:00
if (level & _XY_Log_Plain)
2024-06-08 09:23:42 +08:00
{
str = xy_strjoin (3, prompt, ": ", content);
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Success)
2024-06-08 08:13:54 +08:00
{
str = xy_strjoin (3, prompt, ": ", xy_str_to_green (content));
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Info)
2024-06-08 08:13:54 +08:00
{
str = xy_strjoin (3, prompt, ": ", xy_str_to_blue (content));
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Warn)
2024-06-08 08:13:54 +08:00
{
str = xy_strjoin (3, prompt, ": ", xy_str_to_yellow (content));
to_stderr = true;
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Error)
2024-06-08 08:13:54 +08:00
{
str = xy_strjoin (3, prompt, ": ", xy_str_to_red (content));
to_stderr = true;
}
else
{
// xy_assert ("CAN'T REACH!");
}
if (to_stderr)
{
fprintf (stderr, "%s\n", str);
}
else
{
puts (str);
}
free (str);
2024-06-08 07:13:38 +08:00
}
2024-06-08 08:13:54 +08:00
2024-06-08 07:13:38 +08:00
/**
2024-06-08 08:13:54 +08:00
* remarkably pip 便使xy.h的程序应该基于此再定义自己的 app_info_remarkbaly()
2024-06-08 07:13:38 +08:00
*/
2024-06-11 17:00:22 +08:00
#define xy_log_remarkably(prompt1,prompt2,content) _xy_log_remarkably(_XY_Log_Plain, prompt1,prompt2,content)
#define xy_succ_remarkably(prompt1,prompt2,content) _xy_log_remarkably(_XY_Log_Success,prompt1,prompt2,content)
#define xy_info_remarkably(prompt1,prompt2,content) _xy_log_remarkably(_XY_Log_Info, prompt1,prompt2,content)
#define xy_warn_remarkably(prompt1,prompt2,content) _xy_log_remarkably(_XY_Log_Warn, prompt1,prompt2,content)
#define xy_error_remarkably(prompt1,prompt2,content) _xy_log_remarkably(_XY_Log_Error, prompt1,prompt2,content)
2024-06-08 08:13:54 +08:00
static void
_xy_log_remarkably (int level, const char *prompt1, const char *prompt2, const char *content)
2024-06-08 07:13:38 +08:00
{
2024-06-08 08:13:54 +08:00
char *str = NULL;
bool to_stderr = false;
2024-06-11 17:00:22 +08:00
if (level & _XY_Log_Plain)
2024-06-08 09:23:42 +08:00
{
str = xy_strjoin (6, "[", prompt1, " ", prompt2, "] ", content);
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Success)
2024-06-08 08:13:54 +08:00
{
/* [app 成功] [app success] */
2024-06-08 14:53:54 +08:00
str = xy_strjoin (6,
"[", xy_str_to_green (prompt1), " ", xy_str_to_bold (xy_str_to_green (prompt2)), "] ", xy_str_to_green (content));
2024-06-08 08:13:54 +08:00
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Info)
2024-06-08 08:13:54 +08:00
{
/* [app 信息] [app info]
[app ] [app notice]
*/
2024-06-08 14:53:54 +08:00
str = xy_strjoin (6,
"[", xy_str_to_blue (prompt1), " ", xy_str_to_bold (xy_str_to_blue (prompt2)), "] ", xy_str_to_blue (content));
2024-06-08 08:13:54 +08:00
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Warn)
2024-06-08 08:13:54 +08:00
{
/* [app 警告] [app warn] */
2024-06-08 14:53:54 +08:00
str = xy_strjoin (6,
"[", xy_str_to_yellow (prompt1), " ", xy_str_to_bold (xy_str_to_yellow (prompt2)), "] ", xy_str_to_yellow (content));
2024-06-08 08:13:54 +08:00
to_stderr = true;
}
2024-06-11 17:00:22 +08:00
else if (level & _XY_Log_Error)
2024-06-08 08:13:54 +08:00
{
/* [app 错误] [app error] */
2024-06-08 14:53:54 +08:00
str = xy_strjoin (6,
"[", xy_str_to_red (prompt1), " ", xy_str_to_bold (xy_str_to_red (prompt2)), "] ", xy_str_to_red (content));
2024-06-08 08:13:54 +08:00
to_stderr = true;
}
else
{
// xy_assert ("CAN'T REACH!");
}
if (to_stderr)
{
fprintf (stderr, "%s\n", str);
}
else
{
puts (str);
}
free (str);
2024-06-08 07:13:38 +08:00
}
2024-06-08 08:13:54 +08:00
2024-06-08 07:13:38 +08:00
/******************************************************
* System
******************************************************/
2023-09-10 13:15:34 +08:00
/**
2024-06-11 16:54:28 +08:00
* cmditer_func
2023-09-27 19:31:23 +08:00
*
2024-06-11 16:54:28 +08:00
* @param cmd
* @param n 0 n (n>0) n行
*
* @param iter_func
2023-09-27 11:32:13 +08:00
*
2024-06-11 16:54:28 +08:00
* @note
*
* iter_func()
*/
2024-04-18 22:12:22 +08:00
static char *
2024-06-11 16:54:28 +08:00
xy_run (const char *cmd, unsigned long n, void (*iter_func) (const char *))
{
2023-09-27 11:32:13 +08:00
const int size = 512;
2024-06-11 16:54:28 +08:00
char *buf = (char *) malloc (size);
2024-04-18 22:12:22 +08:00
FILE *stream = popen (cmd, "r");
if (stream == NULL)
{
fprintf (stderr, "xy: 命令执行失败\n");
return NULL;
}
2024-04-18 22:12:22 +08:00
char *ret = NULL;
2023-09-27 19:31:23 +08:00
unsigned long count = 0;
2023-09-27 11:32:13 +08:00
2024-04-18 22:12:22 +08:00
while (true)
{
if (NULL == fgets (buf, size, stream))
break;
ret = buf;
count += 1;
if (n == count)
break;
2024-06-11 16:54:28 +08:00
if (iter_func)
2024-04-18 22:12:22 +08:00
{
2024-06-11 16:54:28 +08:00
iter_func (buf);
2024-04-18 22:12:22 +08:00
}
}
2023-09-27 11:32:13 +08:00
pclose (stream);
2023-09-05 20:48:38 +08:00
return ret;
}
2024-04-18 22:12:22 +08:00
#define xy_os_home _xy_os_home ()
static char *
2023-09-10 13:31:48 +08:00
_xy_os_home ()
{
2024-04-18 22:12:22 +08:00
char *home = NULL;
2023-09-10 13:31:48 +08:00
if (xy_on_windows)
2024-04-18 22:12:22 +08:00
home = getenv ("USERPROFILE");
2023-09-10 13:31:48 +08:00
else
2024-04-18 22:12:22 +08:00
home = getenv ("HOME");
2023-09-10 13:31:48 +08:00
return home;
}
2024-04-18 22:12:22 +08:00
#define xy_win_powershell_profile _xy_win_powershell_profile ()
#define xy_win_powershellv5_profile _xy_win_powershellv5_profile ()
static char *
2023-09-10 13:31:48 +08:00
_xy_win_powershell_profile ()
{
2024-04-18 22:12:22 +08:00
return xy_2strjoin (
xy_os_home, "\\Documents\\PowerShell\\Microsoft.PowerShell_profile.ps1");
2023-09-10 13:31:48 +08:00
}
2024-04-18 22:12:22 +08:00
char *
2023-09-27 16:52:33 +08:00
_xy_win_powershellv5_profile ()
2023-09-10 13:31:48 +08:00
{
2024-04-18 22:12:22 +08:00
return xy_2strjoin (
xy_os_home,
"\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
2023-09-10 13:31:48 +08:00
}
2023-09-10 13:15:34 +08:00
/**
* @note Windows上`path` access()
*/
2023-09-10 14:50:41 +08:00
static bool
2024-04-18 22:12:22 +08:00
xy_file_exist (const char *path)
2023-09-10 13:15:34 +08:00
{
2024-04-18 22:12:22 +08:00
const char *newpath = path;
2023-09-10 13:15:34 +08:00
if (xy_on_windows)
2024-04-18 22:12:22 +08:00
{
if (xy_str_start_with (path, "~"))
{
newpath = xy_2strjoin (xy_os_home, path + 1);
}
2023-09-10 13:15:34 +08:00
}
2024-04-18 22:12:22 +08:00
return access (newpath, 0) ? false : true;
2023-09-10 13:15:34 +08:00
}
2024-07-09 14:29:11 +08:00
/**
* @note xy_file_exist() xy_dir_exist() 使 '~'
* xy_uniform_path()
*/
2024-07-09 14:06:27 +08:00
static bool
xy_dir_exist (const char *path)
{
const char *dir = path;
if (xy_on_windows)
{
if (xy_str_start_with (path, "~"))
{
dir = xy_2strjoin (xy_os_home, path + 1);
}
}
if (xy_on_windows)
{
// 也可以用 opendir() #include <dirent.h>
DWORD attr = GetFileAttributesA (dir);
if (attr == INVALID_FILE_ATTRIBUTES)
{
// Q: 我们应该报错吗?
return false;
}
else if (attr & FILE_ATTRIBUTE_DIRECTORY)
{
return true;
}
else
{
return false;
}
}
else
{
int status = system (xy_2strjoin ("test -d ", dir));
if (0==status)
return true;
else
return false;
}
}
/**
* 使Windows下也可调用
*/
2024-04-18 22:12:22 +08:00
static char *
xy_uniform_path (const char *path)
2023-09-27 16:52:33 +08:00
{
2024-04-18 22:12:22 +08:00
char *new = xy_str_strip (path); // 防止开发者多写了空白符
// 这个函数仅在Windows上才进行替换
2024-04-18 22:12:22 +08:00
if (xy_on_windows)
{
if (xy_str_start_with (new, "~/"))
{
// 或 %USERPROFILE%
new = xy_strjoin (3, xy_os_home, "\\",
xy_str_delete_prefix (new, "~/"));
}
new = xy_str_gsub (new, "/", "\\");
}
return new;
2023-09-27 16:52:33 +08:00
}
2024-04-18 22:12:22 +08:00
static char *
xy_parent_dir (const char *path)
2023-09-29 21:16:16 +08:00
{
2024-04-18 22:12:22 +08:00
char *dir = xy_uniform_path (path);
char *last = NULL;
if (xy_on_windows)
{
last = strrchr (dir, '\\');
*last = '\0';
}
else
{
last = strrchr (dir, '/');
*last = '\0';
}
2023-09-29 21:16:16 +08:00
return dir;
}
2023-08-30 15:34:56 +08:00
#endif