add free pointer api

This commit is contained in:
孙传宝 2020-03-12 21:37:45 +08:00
parent 9926a9901c
commit 47a9784100
4 changed files with 59 additions and 72 deletions

View File

@ -185,45 +185,11 @@ void test1()
int ret = csonJsonStr2Struct(jStr, &playList, play_list_ref_tbl);
/* test print */
printf("ret=%d\n", ret);
printPlayList(&playList);
csonPrintProperty(&playList, play_list_ref_tbl);
char* jstrOutput;
ret = csonStruct2JsonStr(&jstrOutput, &playList, play_list_ref_tbl);
printf("ret=%d\nJson:%s\n", ret, jstrOutput);
free(jstrOutput);
freePlayList(&playList);
}
static void* printProperty(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_OBJECT) return NULL;
if (tbl->type == CSON_INTEGER || tbl->type == CSON_TRUE || tbl->type == CSON_FALSE) printf("%s:%d\n", tbl->field, *(int*)pData);
if (tbl->type == CSON_REAL) printf("%s:%f\n", tbl->field, *(double*)pData);
if (tbl->type == CSON_STRING) printf("%s:%s\n", tbl->field, *((char**)pData));
return NULL;
}
static void* freePointer(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_STRING) {
printf("free field %s.\n", tbl->field);
free(*(void**)pData);
}
return NULL;
}
static void printPlayList(PlayList* list)
{
/* 调用loopProperty迭代结构体中的属性,完成迭代输出属性值 */
csonLoopProperty(list, play_list_ref_tbl, printProperty);
}
static void freePlayList(PlayList* list)
{
/* 调用loopProperty迭代结构体中的属性,释放字符串和数组申请的内存空间 */
csonLoopProperty(list, play_list_ref_tbl, freePointer);
csonFreePointer(&playList, play_list_ref_tbl);
}

File diff suppressed because one or more lines are too long

View File

@ -233,4 +233,24 @@ int csonJsonStr2Struct(const char* jstr, void* output, const reflect_item_t* tbl
*/
int csonStruct2JsonStr(char** jstr, void* input, const reflect_item_t* tbl);
/**
* @brief Iterative output properties of data
*
* @param pData: struct object
* @param tbl: property table of input.
*
* @return void.
*/
void csonPrintProperty(void* pData, const reflect_item_t* tbl);
/**
* @brief Iterative free pointer of data
*
* @param pData: struct object
* @param tbl: property table of input.
*
* @return void.
*/
void csonFreePointer(void* list, const reflect_item_t* tbl);
#endif

View File

@ -728,3 +728,37 @@ void csonLoopProperty(void* pData, const reflect_item_t* tbl, loop_func_t func)
i++;
}
}
static void* printPropertySub(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_OBJECT) return NULL;
if (tbl->type == CSON_INTEGER || tbl->type == CSON_TRUE || tbl->type == CSON_FALSE) printf("%s:%d\n", tbl->field, *(int*)pData);
if (tbl->type == CSON_REAL) printf("%s:%f\n", tbl->field, *(double*)pData);
if (tbl->type == CSON_STRING) printf("%s:%s\n", tbl->field, *((char**)pData));
return NULL;
}
static void* freePointerSub(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_STRING) {
printf("free field %s.\n", tbl->field);
free(*(void**)pData);
}
return NULL;
}
void csonPrintProperty(void* pData, const reflect_item_t* tbl)
{
/* 调用loopProperty迭代结构体中的属性,完成迭代输出属性值 */
csonLoopProperty(pData, tbl, printPropertySub);
}
void csonFreePointer(void* list, const reflect_item_t* tbl)
{
/* 调用loopProperty迭代结构体中的属性,释放字符串和数组申请的内存空间 */
csonLoopProperty(list, tbl, freePointerSub);
}