wow-string-list文件说明

wow-string-list文件说明

  • 项目地址:https://gitee.com/wow-iot/wow-iot7
  • 本文件主要功能用于字符串链表相关操作,主要涉及创建、销毁、插入、获取、分隔、格式转换;

初始化与退出:

复制代码
StringList_T* wow_stringlist_new(void)
{
	return (StringList_T*)wow_slist_create();	
}
void wow_stringlist_free(StringList_T** pptStrList)
{
	wow_slist_destroy(pptStrList,slist_free_func);
}

字符串链表添加元素

复制代码
int wow_stringlist_append(StringList_T* ptStrList, const char *pcElem)
{
	CHECK_RET_VAL_P(ptStrList,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
	CHECK_RET_VAL_P(pcElem && strlen(pcElem) > 0,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");

	int len = strlen(pcElem);

	char* data = MALLOC(len + 1);
	CHECK_RET_VAL_P(data,-SYSTEM_MALLOC_FAILED,"malloc data size!\n");
	memset(data,0,len + 1);
	memcpy(data,pcElem,len);

	return wow_slist_insert_tail(ptStrList,data);
}

获取字符串链表元素个数与内容

复制代码
int wow_stringlist_size(StringList_T* ptStrList)
{
	return wow_slist_size(ptStrList);
}
char* wow_stringlist_data(StringList_T* ptStrList,int nIdx)
{
	return (char*)wow_slist_peek_by_index(ptStrList,nIdx);
}

字符串链表元素格式转换

复制代码
int wow_stringlist_toInt(StringList_T* ptStrList,int nIdx)
{
	char* data = (char*)wow_slist_peek_by_index(ptStrList,nIdx);
	CHECK_RET_ABORT(data);

	return strtol(data,NULL,10);
}

float wow_stringlist_toFloat(StringList_T* ptStrList,int nIdx)
{
	char* data = (char*)wow_slist_peek_by_index(ptStrList,nIdx);
	CHECK_RET_ABORT(data);

	return (float)strtod(data,NULL);
}

分割字符串

复制代码
StringList_T* wow_stringlist_split(char *pcStr, const char *pcDelim)
{
	StringList_T* ptStrList = wow_stringlist_new();
	CHECK_RET_VAL_P(ptStrList,NULL, "string_list new failed!\n");

	char *copy = strdup(pcStr);
	CHECK_RET_GOTO_P_A(copy,out, "strdup failed!\n");

	int ret = -1;
	char *token = strtok(copy, pcDelim);
	while (token){
		ret = wow_stringlist_append(ptStrList, token);
		CHECK_RET_GOTO(ret == 0,out);

		token  = strtok(NULL, pcDelim);
	}

	free(copy);
	return ptStrList;

out:
	wow_stringlist_free(&ptStrList);
	free(copy);
	return NULL;
}
相关推荐
DeeplyMind9 分钟前
第二章:模块的编译与运行-7 Loading and Unloading Modules
linux·驱动开发
---学无止境---43 分钟前
Linux中驱动程序通过fasync异步通知应用程序的实现
linux
cccyi71 小时前
Linux 进程间通信机制详解
linux·进程通信
北京迅为1 小时前
【北京迅为】iTOP-4412精英版使用手册-第三十五章 WEB控制LED
linux·嵌入式硬件·嵌入式·4412
让我们一起加油好吗1 小时前
【C++】封装红黑树模拟实现 set 和 map
linux·c++·set·map·红黑树
暴富奥利奥1 小时前
完成docker方式的ros环境配置
linux·学习·docker·容器
冷徹 .1 小时前
2024ICPC区域赛香港站
数据结构·c++·算法
秃头菜狗1 小时前
十四、运行经典案例 wordcount
大数据·linux·hadoop
望获linux2 小时前
【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
大数据·linux·服务器·开发语言·网络·操作系统
hweiyu002 小时前
Linux 命令:mount
linux·运维·服务器