当前位置: 首页 > article >正文

【9】Strongswan collections —— enumerator

//以目录枚举为例子,说明enumerator,从源码剥离可运行

#include <stdio.h>
#include <stdbool.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <sys/time.h>
#include <stdarg.h>/*** This macro allows counting the number of arguments passed to a macro.* Combined with the VA_ARGS_DISPATCH() macro this can be used to implement* macro overloading based on the number of arguments.* 0 to 10 arguments are currently supported.*/
#define VA_ARGS_NUM(...) _VA_ARGS_NUM(0,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)
#define _VA_ARGS_NUM(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NUM,...) NUM/*** This macro can be used to dispatch a macro call based on the number of given* arguments, for instance:** @code* #define MY_MACRO(...) VA_ARGS_DISPATCH(MY_MACRO, __VA_ARGS__)(__VA_ARGS__)* #define MY_MACRO1(arg) one_arg(arg)* #define MY_MACRO2(arg1,arg2) two_args(arg1,arg2)* @endcode** MY_MACRO() can now be called with either one or two arguments, which will* resolve to one_arg(arg) or two_args(arg1,arg2), respectively.*/
#define VA_ARGS_DISPATCH(func, ...) _VA_ARGS_DISPATCH(func, VA_ARGS_NUM(__VA_ARGS__))
#define _VA_ARGS_DISPATCH(func, num) __VA_ARGS_DISPATCH(func, num)
#define __VA_ARGS_DISPATCH(func, num) func ## num/*** Assign variadic arguments to the given variables.** @note The order and types of the variables are significant and must match the* variadic arguments passed to the function that calls this macro exactly.** @param last		the last argument before ... in the function that calls this* @param ...		variable names*/
#define VA_ARGS_GET(last, ...) ({ \va_list _va_args_get_ap; \va_start(_va_args_get_ap, last); \_VA_ARGS_GET_ASGN(__VA_ARGS__) \va_end(_va_args_get_ap); \
})/*** Assign variadic arguments from a va_list to the given variables.** @note The order and types of the variables are significant and must match the* variadic arguments passed to the function that calls this macro exactly.** @param list		the va_list variable in the function that calls this* @param ...		variable names*/
#define VA_ARGS_VGET(list, ...) ({ \va_list _va_args_get_ap; \va_copy(_va_args_get_ap, list); \_VA_ARGS_GET_ASGN(__VA_ARGS__) \va_end(_va_args_get_ap); \
})#define _VA_ARGS_GET_ASGN(...) VA_ARGS_DISPATCH(_VA_ARGS_GET_ASGN, __VA_ARGS__)(__VA_ARGS__)
#define _VA_ARGS_GET_ASGN1(v1) __VA_ARGS_GET_ASGN(v1)
#define _VA_ARGS_GET_ASGN2(v1,v2) __VA_ARGS_GET_ASGN(v1) __VA_ARGS_GET_ASGN(v2)
#define _VA_ARGS_GET_ASGN3(v1,v2,v3) __VA_ARGS_GET_ASGN(v1) __VA_ARGS_GET_ASGN(v2) \__VA_ARGS_GET_ASGN(v3)
#define _VA_ARGS_GET_ASGN4(v1,v2,v3,v4) __VA_ARGS_GET_ASGN(v1) __VA_ARGS_GET_ASGN(v2) \__VA_ARGS_GET_ASGN(v3) __VA_ARGS_GET_ASGN(v4)
#define _VA_ARGS_GET_ASGN5(v1,v2,v3,v4,v5) __VA_ARGS_GET_ASGN(v1) __VA_ARGS_GET_ASGN(v2) \__VA_ARGS_GET_ASGN(v3) __VA_ARGS_GET_ASGN(v4) __VA_ARGS_GET_ASGN(v5)
#define __VA_ARGS_GET_ASGN(v) v = va_arg(_va_args_get_ap, typeof(v));#ifndef FALSE
# define FALSE false
#endif /* FALSE */
#ifndef TRUE
# define TRUE  true
#endif /* TRUE */typedef struct enumerator_t enumerator_t;struct enumerator_t {/*** 枚举集合。** enumerate() 方法接受可变数量的指针参数(枚举到的值被写入这些参数)**通常只需分配调用枚举器的 venumerate() 方法的通用 enumerator_enumerate_default() 函数就足够了。 ...** @param ...	枚举项的变量列表,取决于实现* @return		TRUE if pointers returned*/bool (*enumerate)(enumerator_t *this, ...);/*** 枚举集合。** venumerate() 方法采用一个变量参数列表,其中包含将枚举值写入的指针。** @param args	枚举项的变量列表,取决于实现* @return		TRUE if pointers returned*/bool (*venumerate)(enumerator_t *this, va_list args);/*** Destroy an enumerator_t instance.*/void (*destroy)(enumerator_t *this);
};/*** Enumerator implementation for directory enumerator*/
typedef struct {/** implements enumerator_t */enumerator_t public;/** directory handle */DIR *dir;/** absolute path of current file */char full[PATH_MAX];/** where directory part of full ends and relative file gets written *///完整路径中的目录部分结束以及相对文件将被写入的位置char *full_end;
} dir_enum_t;
/*** Helper function that compares two strings for equality*/
static inline bool streq(const char *x, const char *y)
{return (x == y) || (x && y && strcmp(x, y) == 0);
}bool enumerate_dir_enum(dir_enum_t *this, va_list args){//读取目录中的条目,依靠此函数特性实现枚举struct dirent *entry = readdir(this->dir);struct stat *st = NULL;size_t remaining;char **relative = NULL, **absolute = NULL;;int len;//对应enumerate输入的变参... &relative, &file, NULL 下面写入这些值相当于写入那些值VA_ARGS_VGET(args, relative, absolute, st);if (!entry){return FALSE;}if (streq(entry->d_name, ".") || streq(entry->d_name, "..")){return this->public.enumerate(&this->public, relative, absolute, st);}if (relative){//条目名称*relative = entry->d_name;}if (absolute || st){//full是路径,relativeremaining = sizeof(this->full) - (this->full_end - this->full);len = snprintf(this->full_end, remaining, "%s", entry->d_name);if (len < 0 || len >= remaining){return FALSE;}if (absolute){*absolute = this->full;}if (st && stat(this->full, st)){/* try lstat() e.g. if a symlink is not valid anymore */if ((errno != ENOENT && errno != ENOTDIR) || lstat(this->full, st)){return FALSE;}}}return TRUE;
}# define DIRECTORY_SEPARATOR "/"
static inline bool path_is_separator(char c)
{return c == DIRECTORY_SEPARATOR[0];
}/*** Object allocation/initialization macro, using designated initializer.*/
#define INIT(this, ...) ({ (this) = malloc(sizeof(*(this))); \*(this) = (typeof(*(this))){ __VA_ARGS__ }; (this); })bool enumerator_enumerate_default(enumerator_t *enumerator, ...)
{va_list args;bool result;if (!enumerator->venumerate){return FALSE;}//va_start用于获取函数参数列表...中可变参数的首指针//输出参数args保存函数参数列表中可变参数的首指针(即,可变参数列表)//输入参数enumerator为函数参数列表中最后一个固定参数(...之前)va_start(args, enumerator);result = enumerator->venumerate(enumerator, args);va_end(args);return result;
}void destroy_dir_enum(dir_enum_t *this){closedir(this->dir);free(this);
}enumerator_t* enumerator_create_directory(const char *path)
{dir_enum_t *this;int len;//实例化INIT(this,.public = {.enumerate = enumerator_enumerate_default,.venumerate = enumerate_dir_enum,.destroy = destroy_dir_enum,},);if (*path == '\0'){path = "./";}len = snprintf(this->full, sizeof(this->full)-1, "%s", path);if (len < 0 || len >= sizeof(this->full)-1){free(this);return NULL;}/* append a '/' if not already done */if (!path_is_separator(this->full[len-1])){this->full[len++] = DIRECTORY_SEPARATOR[0];this->full[len] = '\0';}this->full_end = &this->full[len];//打开目录this->dir = opendir(path);if (!this->dir){free(this);return NULL;}return &this->public;
}int main()
{char *file;char *relative;char *st;char *path = "/root/open/strongswan-6.0.0";enumerator_t *enumerator;enumerator = enumerator_create_directory(path);if (enumerator){while (enumerator->enumerate(enumerator, &relative, &file, NULL)){printf("file: %s | relative: %s | size:%s\n", file, relative, NULL);}enumerator->destroy(enumerator);}return 0;
}

相关文章:

【9】Strongswan collections —— enumerator

//以目录枚举为例子&#xff0c;说明enumerator&#xff0c;从源码剥离可运行 #include <stdio.h> #include <stdbool.h> #include <dirent.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h&…...

大数据学习(88)-zookeeper实现的高可用(HA)

&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一…...

Spring Data审计利器:@LastModifiedDate详解(依赖关系补充篇)!!!

&#x1f552; Spring Data审计利器&#xff1a;LastModifiedDate详解&#x1f525;&#xff08;依赖关系补充篇&#xff09; &#x1f50c; 核心依赖解析 使用LastModifiedDate必须知道的依赖关系 #mermaid-svg-qm1OUa9Era9ktbeK {font-family:"trebuchet ms",verd…...

Tweak Power:全方位电脑系统优化的高效工具

Tweak Power&#xff08;系统&#xff09; Tweak Power是一款功能强大的系统优化工具&#xff0c;专为提升Windows电脑的性能和稳定性而设计。它提供了全面的清理、优化和调整选项&#xff0c;帮助用户轻松管理系统资源、提高运行速度、延长设备寿命。 快速扫描并清理系统垃圾…...

CLion下载安装(Windows11)

目录 CLion工具下载安装其他 CLion CLion-2024.1.4.exe 工具 系统&#xff1a;Windows 11 下载 1.通过百度网盘分享的文件&#xff1a;CLion-2024.1.4.exe 链接&#xff1a;https://pan.baidu.com/s/1-zH0rZPCZtQ60IqdHA7Cew?pwdux5a 提取码&#xff1a;ux5a 安装 打开…...

如何用 Postman 进行高效的 Mock 测试?

Postman 是一个强大的 API 开发和测试工具&#xff0c;它可以让你轻松地创建和发送各种 HTTP 请求&#xff0c;查看响应结果&#xff0c;并进行调试和优化。但是有时候&#xff0c;你可能还没有开发好后端服务&#xff0c;或者想要模拟不同的响应场景&#xff0c;这时候就可以使…...

DeepSeek API集成开发指南——Flask示例实践

DeepSeek API集成开发指南——Flask示例实践 序言&#xff1a;智能化开发新范式 DeepSeek API提供了覆盖自然语言处理、代码生成等多领域的先进AI能力。本文将以一个功能完备的Flask示例系统为载体&#xff0c;详解API的集成方法与最佳实践。通过本案例&#xff0c;开发者可快…...

【天梯赛】L2-004 这是二叉搜索树吗(经典问题C++)

解题反思 //镜像树满足&#xff1a;左子树>根节点>右子树 //特殊&#xff1a;独腿二叉树&#xff0c;如pre {2&#xff0c;3&#xff0c;4}&#xff0c;递归函数用if(root tail) return&#xff1b;无法识别这种二叉树 // 用ismirror来将一般二叉树和镜像二叉搜索树的…...

Postman 全局 Header 如何设置?全局设置了解一下

在使用 Postman 设置全局请求头信息的关键步骤包括&#xff1a;在集合设置页面中添加所需的头部信息&#xff0c;并确保选择适当的类型和值&#xff1b;如果需要&#xff0c;可通过 JavaScript 脚本添加其他请求头&#xff1b;最后&#xff0c;验证设置是否成功生效。 Postman…...

科技赋能建筑业变革:中建海龙创新引领高质量发展新路径

在建筑工业化浪潮中&#xff0c;中建海龙科技有限公司&#xff08;以下简称“中建海龙”&#xff09;凭借深厚的技术积累与持续创新&#xff0c;成为推动行业转型升级的标杆企业。作为中国建筑国际集团旗下核心科技力量&#xff0c;中建海龙深耕模块化集成建筑&#xff08;MiC&…...

QT计算器开发

1.项目架构 1.图形化界面 ​ 2.widget.h​ #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QString> #include <QStack>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTp…...

R语言对偏态换数据进行转换(对数、平方根、立方根)

我们进行研究的时候经常会遇见偏态数据&#xff0c;数据转换是统计分析和数据预处理中的一项基本技术。使用 R 时&#xff0c;了解如何正确转换数据有助于满足统计假设、标准化分布并提高分析的准确性。在 R 中实现和可视化最常见的数据转换&#xff1a;对数、平方根和立方根转…...

《Python实战进阶》No37: 强化学习入门:Q-Learning 与 DQN-加餐版1 Q-Learning算法可视化

在《Python实战进阶》No37: 强化学习入门&#xff1a;Q-Learning 与 DQN 这篇文章中&#xff0c;我们介绍了Q-Learning算法走出迷宫的代码实践&#xff0c;本文加餐&#xff0c;把Q-Learning算法通过代码可视化呈现。我尝试了使用Matplotlib实现&#xff0c;但局限于Matplotli…...

【漏洞修复】Android 10 系统源码中的 glibc、curl、openssl、cups、zlib 更新到最新版本

要将 Android 10 系统源码中的 glibc、curl、openssl、cups、zlib 更新到最新版本&#xff0c;需结合交叉编译、源码替换和系统兼容性适配。以下是具体步骤和相关库的版本信息及维护状态分析&#xff1a; 一、Android 10 默认版本及维护状态 库Android 10 默认版本维护状态最新…...

【云服务器】在 Linux(Ubuntu / CentOS 7)上快速搭建我的世界 Minecraft 服务器,并实现远程联机,详细教程

【云服务器】在 Linux&#xff08;Ubuntu / CentOS 7&#xff09;上快速搭建我的世界 Minecraft 服务器&#xff0c;并实现远程联机&#xff0c;详细教程 一、 服务器介绍二、下载 Minecraft 服务端二、安装 JRE 21三、安装 MCS manager 面板四、搭建服务器五、本地测试连接六、…...

docker torcherve打包mar包并部署模型

使用Docker打包深度网络模型mar包到服务端 参考链接&#xff1a;Docker torchserve 部署模型流程——以WSL部署YOLO-FaceV2为例_class myhandler(basehandler): def initialize(self,-CSDN博客 1、docker拉取环境镜像命令 docker images出现此提示为没有权限取执行命令&…...

【安当产品应用案例100集】042-基于安当KADP实现机密文件安全流转

一、客户需求 某集团公司客户&#xff0c;在系统业务流中&#xff0c;存在大量的内部文件流转的需求。内部业务文件有不同的安全密级&#xff0c;最初在文件流转时&#xff0c;公司内部规定点对点的文件传输&#xff0c;要使用加密工具加密后再发给需要的一方。这种方式虽然能…...

[Qt5] QMetaObject::invokeMethod使用

&#x1f4e2;博客主页&#xff1a;https://loewen.blog.csdn.net&#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;本文由 丶布布原创&#xff0c;首发于 CSDN&#xff0c;转载注明出处&#x1f649;&#x1f4e2;现…...

软件设计原则之迪米特法则

迪米特法则&#xff08;Law of Demeter&#xff0c;LoD&#xff09; 核心思想&#xff1a; 一个对象应当尽可能少地了解其他对象&#xff0c;只与直接朋友交互&#xff08;如自身的成员变量、方法参数、方法内部创建的对象&#xff09;&#xff0c;避免通过复杂的调用链访问间…...

JSON5 格式标准 Data Exchange Format 官方文档 中英双语

Standard JSON5 标准 JSON5 1.0.0 / March 2018 1.0.0 / 三月 2018 The JSON5 Data Interchange FormatThe JSON5 数据交换格式 Abstract 摘要 The JSON5 Data Interchange Format is a proposed extension to JSON that aims to make it easier for humans to write an…...

附录C SLAC匹配过程命令定义与实际抓包

附录C SLAC匹配过程命令定义与实际抓包 ISO15118-3 附录A中规定了SLAC匹配过程中的请求命令及应答&#xff0c; 本文将会对比协议中的定义和实际抓包内容&#xff0c;以便读者获得直观的认识。 1 CM_SET_KEY.REQ 定义内容&#xff1a; 实际数据&#xff1a; 注意报文中的 08…...

【QT】新建QT工程(详细步骤)

新建QT工程 1.方法(1)点击new project按钮&#xff0c;弹出对话框&#xff0c;新建即可&#xff0c;步骤如下&#xff1a;(2) 点击文件菜单&#xff0c;选择新建文件或者工程&#xff0c;后续步骤如上 2.QT工程文件介绍(1).pro文件 --》QT工程配置文件(2)main.cpp --》QT工程主…...

安装Webpack并创建vue项目

1、新建一个工程目录 在E盘中进行新建项目 2、从命令行进入该目录,并执行NPM 的初始化命令 3、会看到目录中生成了一个“package.json”文件,它相当于NPM项目的说明书&#xff0c;里面记录了项目名称、版本、仓库地址等信息。 4、执行安装 Webpack 的命令 npm install webpac…...

如何快速解决django存储session变量时出现的django.db.utils.DatabaseError错误

我们在学习django进行web编程的时候&#xff0c;有时需要将一些全局变量信息存储在session中&#xff0c;但使用过程中&#xff0c;却发现会引起数据库的报错。通过查看django源码信息&#xff0c;发现其对session信息进行了ORM映射&#xff0c;如果数据库中不存在对应的表信息…...

04 单目标定实战示例

看文本文,您将获得以下技能: 1:使用opencv进行相机单目标定实战 2:标定结果参数含义和数值分析 3:Python绘制各标定板姿态,查看图像采集多样性 4:如果相机画幅旋转90,标定输入参数该如何设置? 5:图像尺寸缩放,标定结果输出有何影响? 6:单目标定结果应用类别…...

极速全场景 MPP数据库starrocks介绍

目录 一、引子 二、起源 &#xff08;一&#xff09;前身 &#xff08;二&#xff09;定位 三、特点 &#xff08;一&#xff09;高性能架构 &#xff08;二&#xff09;实时分析 &#xff08;三&#xff09;高并发与扩展性 &#xff08;四&#xff09;兼容性与生态 …...

RS232转Profinet网关技术,检漏仪新篇章!

RS232转Profinet网关技术&#xff0c;检漏仪新篇章&#xff01; 在现代医疗监控系统中&#xff0c;RS232转PROFINET网关扮演着至关重要的角色。这种转换设备能够将传统的RS232串行通讯接口无缝转换为PROFINET以太网通信接口&#xff0c;确保老旧设备与现代自动化系统之间的顺畅…...

Linux操作系统7- 线程同步与互斥7(RingQueue环形队列生产者消费者模型改进)

上篇文章&#xff1a;Linux操作系统7- 线程同步与互斥6&#xff08;POSIX信号量与环形队列生产者消费者模型&#xff09;-CSDN博客 本篇代码仓库&#xff1a;myLerningCode/l36 橘子真甜/Linux操作系统与网络编程学习 - 码云 - 开源中国 (gitee.com) 目录 一. 单生产单消费单保…...

景区导游--LCA+树上前缀和

关键就是删点少删边&#xff0c;只删有关的边 LCA找最近祖先&#xff0c;树上前缀和记&#xff0c;画图找公式&#xff0c;特殊情况为删第一和最后 sum和前缀和开ll #include<bits/stdc.h> using namespace std; #define N 100011 typedef long long ll; typedef pair…...

将 Markdown 表格结构转换为Excel 文件

在数据管理和文档编写过程中&#xff0c;我们经常使用 Markdown 来记录表格数据。然而&#xff0c;Markdown 格式的表格在实际应用中不如 Excel 方便&#xff0c;特别是需要进一步处理数据时。因此&#xff0c;我们开发了一个使用 wxPython 的 GUI 工具&#xff0c;将 Markdown…...