Rename to use xy_run

This commit is contained in:
Aoran Zeng
2024-06-11 16:54:28 +08:00
parent f53c05b2c2
commit 452fc72762

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 : <2024-06-08> * Last modified : <2024-06-11>
* *
* xy: * xy:
* *
@@ -17,7 +17,7 @@
#ifndef XY_H #ifndef XY_H
#define XY_H #define XY_H
#define XY_Version "v0.1.2-2024/06/08" #define XY_Version "v0.1.2-2024/06/11"
#define XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/xy.h" #define XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/xy.h"
#include <assert.h> #include <assert.h>
@@ -545,19 +545,22 @@ _xy_log_remarkably (int level, const char *prompt1, const char *prompt2, const c
* System * System
******************************************************/ ******************************************************/
/** /**
* 执行cmd返回其最后某行输出结果 * 执行cmd返回某行输出结果并对已经遍历过的行执行iter_func
* *
* @param cmd 要执行的命令 * @param cmd 要执行的命令
* @param n 命令打印的结果0 表示最后一行n (n>0) 表示第n行 * @param n 指定命令执行输出的结果行中的某一0 表示最后一行n (n>0) 表示第n行
* @param func 对读取的行执行函数 * 该函数会返回这一行的内容
* @param iter_func 对遍历时经过的行的内容,进行函数调用
* *
* @note 返回的字符串最后面可能有换行符号 * @note 返回的字符串最后面一般有换行符号
*
* 由于目标行会被返回出来,所以 iter_func() 并不执行目标行,只会执行遍历过的行
*/ */
static char * static char *
xy_getcmd (const char *cmd, unsigned long n, void (*func) (const char *)) xy_run (const char *cmd, unsigned long n, void (*iter_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)
@@ -577,9 +580,9 @@ xy_getcmd (const char *cmd, unsigned long n, void (*func) (const char *))
count += 1; count += 1;
if (n == count) if (n == count)
break; break;
if (func) if (iter_func)
{ {
func (buf); iter_func (buf);
} }
} }