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;
}
相关推荐
Rsingstarzengjx12 小时前
第三十九章 Linux PWM 驱动实验
linux·运维·服务器
SeanQhl13 小时前
BoostKit 环境准备
linux·openeuler·鲲鹏·boostkit·软件源
笔记本note15 小时前
Linux 内存管理
linux·服务器·内存管理
郝学胜-神的一滴15 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
GeW16 小时前
入行 Linux 与数据库运维:一条路径、三项习惯、一个考证观
linux·数据库
Regulus-Moon17 小时前
Quorum:用一个不等式换掉分布式锁
数据结构·论文阅读·分布式·算法·系统架构
老师我太想进步了202618 小时前
【数据结构-16】链式二叉树在线OJ题
c语言·数据结构·算法
程序员大阳18 小时前
副队长C语言数据结构教程(4)--双向链表
c语言·数据结构·链表·双向链表
南子北游18 小时前
Linux 进程状态全景(上)
linux·运维·服务器
无奈笑天下1 天前
银河麒麟高级服务器操作系统【配置 sudo 操作日志记录】操作方法
linux·运维·服务器