wow-string-list文件说明
初始化与退出:
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;
}