【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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/42102.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu 22.04 安装向日葵远程控制

1. 前言 由于公司客户的服务器用是图形化桌面&#xff0c;所以我们需要一个远程控制工具来控制服务器&#xff0c;目前市面上两款比较热门的控制软件就是ToDesk和向日葵了&#xff0c;我们今天就来学习一下向日葵的使用 2. 下载软件 前往向日葵官网下载 向日葵远程控制app官…

Linux网络编程(七)——套接字的多种可选项

文章目录 7 套接字的多种可选项 7.1 套接字可选项和I/O缓冲大小 7.1.1 套接字多种可选项 7.1.2 getsockopt & setsockopt 7.1.3 SO_SNDBUF & SO_RCVBUF 7.2 地址再分配 SO_REUSEADDR 7.2.1 发生地址分配错误&#xff08;Binding Error&#xff09; 7.2.2 Time-…

使用 langchain_deepseek 实现自然语言转数据库查询SQL

文章目录 Github官网简介腾讯云DeepSeek APIDeepSeek APIChatDeepSeek安装相关库创建 .env 文件验证 API 接口 生成数据库查询SQL获取测试用数据库验证数据库查询生成数据库查询SQL Github https://github.com/langchain-ai/langchain 官网 https://python.langchain.com/do…

2025年具有AI招聘管理系统选型及攻略分享

2025年&#xff0c;人工智能的深度渗透让招聘管理系统的竞争从“功能堆砌”转向“智能密度”的较量。企业若想在这场人才争夺战中胜出&#xff0c;选对招聘管理系统已不再是“加分项”&#xff0c;而是“生死线”。 然而&#xff0c;市面上的招聘系统五花八门&#xff0c;从老牌…

vue 自定义 tabs 控件,可自动左右滑动使得选中项居中显示

效果图如下&#xff1a; 录屏如下&#xff1a; tabs录屏 控件用法如下&#xff1a; <navi-tabs :data"tabs" changeTab"changeTab"></navi-tabs>import NaviTabs from "/components/navi-tabs";components: { NaviTabs },tabs: [{ …

HarmonyOS:解决UIAbility调用terminateSelf()后设置不保留最近任务列表中的快照

一、概述 在HarmonyOS应用开发中&#xff0c;UIAbilityContext的terminateSelf()方法被用来结束当前的UIAbility实例。 如果希望在调用terminateSelf()后&#xff0c;让应用在最近任务列表中不保留快照&#xff0c;可以通过在module.json5配置文件中配置removeMissionAfterTe…

el-table下的复选框关联勾选

效果展示&#xff1a; <el-table style"height: 500px;" :data"tableData" border empty-text"暂无数据" v-loading"loading":header-cell-style"{ text-align: center }" :cell-style"{ text-align: center }"…

langchain+ollama+deepseek的部署(win)

ANACONDA 安装 官网&#xff1a;Download Anaconda Distribution | Anaconda 配置系统环境 在系统变量中配置 检查是否配置成功 通过 cmd 窗口输入&#xff1a; conda info 如图&#xff1a;表示成功 配置你的虚拟环境 二、安装 ollama allama 安装 官网地址&#xff1a…

深入理解椭圆曲线密码学(ECC)与区块链加密

椭圆曲线密码学&#xff08;ECC&#xff09;在现代加密技术中扮演着至关重要的角色&#xff0c;广泛应用于区块链、数字货币、数字签名等领域。由于其在提供高安全性和高效率上的优势&#xff0c;椭圆曲线密码学成为了数字加密的核心技术之一。本文将详细介绍椭圆曲线的基本原理…

SQL Server 2008安装教程

目录 一.安装SQL Server 二.安装SQL Server Management Studio 三.使用SQL Server Management Studio 一.安装SQL Server 官网下载:SQL Server 下载 | Microsoft 1.选择安装中的全新安装如下图 2.功能选择 3.实例配置 4.后面一直下一步到数据库引擎配置 密码自己设置 系统…

Microi吾码界面设计引擎之基础组件用法大全【内置组件篇·中】

&#x1f380;&#x1f380;&#x1f380; microi-pageengine 界面引擎系列 &#x1f380;&#x1f380;&#x1f380; 一、Microi吾码&#xff1a;一款高效、灵活的低代码开发开源框架【低代码框架】 二、Vue3项目快速集成界面引擎 三、Vue3 界面设计插件 microi-pageengine …

如何在 Windows 上安装并使用 Postman?

Postman 是一个功能强大的API测试工具&#xff0c;它可以帮助程序员更轻松地测试和调试 API。在本文中&#xff0c;我们将讨论如何在 Windows 上安装和使用 Postman。 Windows 如何安装和使用 Postman 教程&#xff1f;

便携版:随时随地,高效处理 PDF 文件

PDF-XChange Editor Plus 便携版是一款功能强大且极其实用的 PDF 阅读与编辑工具。它不仅支持快速浏览 PDF 文件&#xff0c;还提供了丰富的编辑功能&#xff0c;让用户可以轻松处理 PDF 文档。经过大神优化处理&#xff0c;这款软件已经变得十分轻便&#xff0c;非常适合需要随…

MCP Server 实现一个 天气查询

​ Step1. 环境配置 安装 uv curl -LsSf https://astral.sh/uv/install.sh | shQuestion: 什么是 uv 呢和 conda 比有什么区别&#xff1f; Answer: 一个用 Rust 编写的超快速 (100x) Python 包管理器和环境管理工具&#xff0c;由 Astral 开发。定位为 pip 和 venv 的替代品…

MySQL执行计划

MySQL 的 执行计划&#xff08;Execution Plan&#xff09; 是优化器根据 SQL 语句生成的查询执行路径的详细说明。通过分析执行计划&#xff0c;可以了解 MySQL 如何处理 SQL 查询&#xff08;如索引使用情况、表连接顺序等&#xff09;&#xff0c;进而优化查询性能。 1. 获…

数据大屏点亮工业互联网的智慧之眼

在当今数字化飞速发展的时代&#xff0c;数据已成为企业决策的核心依据&#xff0c;而数据大屏作为数据可视化的重要工具&#xff0c;正逐渐成为工业互联网领域不可或缺的一部分。通过直观、动态的可视化展示&#xff0c;数据大屏能够将复杂的数据转化为易于理解的图表和图形&a…

GPT-SoVITS本地部署:低成本实现语音克隆远程生成音频全流程实战

文章目录 前言1.GPT-SoVITS V2下载2.本地运行GPT-SoVITS V23.简单使用演示4.安装内网穿透工具4.1 创建远程连接公网地址 5. 固定远程访问公网地址 前言 今天要给大家安利一个绝对能让你大呼过瘾的声音黑科技——GPT-SoVITS&#xff01;这款由花儿不哭大佬精心打造的语音克隆神…

【AI大模型】DeepSeek + 通义万相高效制作AI视频实战详解

目录 一、前言 二、AI视频概述 2.1 什么是AI视频 2.2 AI视频核心特点 2.3 AI视频应用场景 三、通义万相介绍 3.1 通义万相概述 3.1.1 什么是通义万相 3.2 通义万相核心特点 3.3 通义万相技术特点 3.4 通义万相应用场景 四、DeepSeek 通义万相制作AI视频流程 4.1 D…

【Unity】合批处理和GPU实例化的底层优化原理(完)

【Unity】批处理和实例化的底层优化原理 URP1.基础概念SetPassCallsDrawCallsBatches 2.重要性排序既然如此为什么仍然要合批&#xff1f; 3.unity主流的合批优化方案和优先级Early-Z透明物体情况 4.合批&#xff08;小场景但是很复杂很多小物件刚需&#xff09;合并纹理图集更…

当人类关系重构:从“相互需要”到“鹅卵石化”——生成式人工智能(GAI)认证的角色与影响

在数字化浪潮的席卷之下,人类社会正经历着前所未有的变革。人与人之间的连接方式、互动模式以及价值认同,都在悄然发生着变化。这一过程中,一个显著的现象是,人与人之间的关系逐渐从传统的“相互需要”模式,转变为一种更为复杂、多元且稳定的“鹅卵石化”结构。在此背景下…