Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
c24c3e02
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
大约 1 年 前同步成功
通知
6
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c24c3e02
编写于
5月 01, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add cJSON_DetachItemViaPointer
上级
cc3a75dd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
71 addition
and
0 deletion
+71
-0
cJSON.c
cJSON.c
+30
-0
cJSON.h
cJSON.h
+1
-0
tests/misc_tests.c
tests/misc_tests.c
+40
-0
未找到文件。
cJSON.c
浏览文件 @
c24c3e02
...
...
@@ -1814,6 +1814,36 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *str
cJSON_AddItemToObject
(
object
,
string
,
create_reference
(
item
,
&
global_hooks
));
}
CJSON_PUBLIC
(
cJSON
*
)
cJSON_DetachItemViaPointer
(
cJSON
*
parent
,
cJSON
*
const
item
)
{
if
((
parent
==
NULL
)
||
(
item
==
NULL
))
{
return
NULL
;
}
if
(
item
->
prev
!=
NULL
)
{
/* not the first element */
item
->
prev
->
next
=
item
->
next
;
}
if
(
item
->
next
!=
NULL
)
{
/* not the last element */
item
->
next
->
prev
=
item
->
prev
;
}
if
(
item
==
parent
->
child
)
{
/* first element */
parent
->
child
=
item
->
next
;
}
/* make sure the detached item doesn't point anywhere anymore */
item
->
prev
=
NULL
;
item
->
next
=
NULL
;
return
item
;
}
static
cJSON
*
DetachItemFromArray
(
cJSON
*
array
,
size_t
which
)
{
cJSON
*
c
=
array
->
child
;
...
...
cJSON.h
浏览文件 @
c24c3e02
...
...
@@ -203,6 +203,7 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC
(
void
)
cJSON_AddItemReferenceToObject
(
cJSON
*
object
,
const
char
*
string
,
cJSON
*
item
);
/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC
(
cJSON
*
)
cJSON_DetachItemViaPointer
(
cJSON
*
parent
,
cJSON
*
const
item
);
CJSON_PUBLIC
(
cJSON
*
)
cJSON_DetachItemFromArray
(
cJSON
*
array
,
int
which
);
CJSON_PUBLIC
(
void
)
cJSON_DeleteItemFromArray
(
cJSON
*
array
,
int
which
);
CJSON_PUBLIC
(
cJSON
*
)
cJSON_DetachItemFromObject
(
cJSON
*
object
,
const
char
*
string
);
...
...
tests/misc_tests.c
浏览文件 @
c24c3e02
...
...
@@ -218,6 +218,45 @@ static void cjson_set_number_value_should_set_numbers(void)
TEST_ASSERT_EQUAL_DOUBLE
(
-
1
+
(
double
)
INT_MIN
,
number
->
valuedouble
);
}
static
void
cjson_detach_item_via_pointer_should_detach_items
(
void
)
{
cJSON
list
[
4
];
cJSON
parent
[
1
];
memset
(
list
,
'\0'
,
sizeof
(
list
));
/* link the list */
list
[
0
].
next
=
&
(
list
[
1
]);
list
[
1
].
next
=
&
(
list
[
2
]);
list
[
2
].
next
=
&
(
list
[
3
]);
list
[
3
].
prev
=
&
(
list
[
2
]);
list
[
2
].
prev
=
&
(
list
[
1
]);
list
[
1
].
prev
=
&
(
list
[
0
]);
parent
->
child
=
&
list
[
0
];
/* detach in the middle (list[1]) */
TEST_ASSERT_TRUE_MESSAGE
(
cJSON_DetachItemViaPointer
(
parent
,
&
(
list
[
1
]))
==
&
(
list
[
1
]),
"Failed to detach in the middle."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
1
].
prev
==
NULL
)
&&
(
list
[
1
].
next
==
NULL
),
"Didn't set pointers of detached item to NULL."
);
TEST_ASSERT_TRUE
((
list
[
0
].
next
==
&
(
list
[
2
]))
&&
(
list
[
2
].
prev
==
&
(
list
[
0
])));
/* detach beginning (list[0]) */
TEST_ASSERT_TRUE_MESSAGE
(
cJSON_DetachItemViaPointer
(
parent
,
&
(
list
[
0
]))
==
&
(
list
[
0
]),
"Failed to detach beginning."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
0
].
prev
==
NULL
)
&&
(
list
[
0
].
next
==
NULL
),
"Didn't set pointers of detached item to NULL."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
2
].
prev
==
NULL
)
&&
(
parent
->
child
==
&
(
list
[
2
])),
"Didn't set the new beginning."
);
/* detach end (list[3])*/
TEST_ASSERT_TRUE_MESSAGE
(
cJSON_DetachItemViaPointer
(
parent
,
&
(
list
[
3
]))
==
&
(
list
[
3
]),
"Failed to detach end."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
3
].
prev
==
NULL
)
&&
(
list
[
3
].
next
==
NULL
),
"Didn't set pointers of detached item to NULL."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
2
].
next
==
NULL
)
&&
(
parent
->
child
==
&
(
list
[
2
])),
"Didn't set the new end"
);
/* detach single item (list[2]) */
TEST_ASSERT_TRUE_MESSAGE
(
cJSON_DetachItemViaPointer
(
parent
,
&
list
[
2
])
==
&
list
[
2
],
"Failed to detach single item."
);
TEST_ASSERT_TRUE_MESSAGE
((
list
[
2
].
prev
==
NULL
)
&&
(
list
[
2
].
next
==
NULL
),
"Didn't set pointers of detached item to NULL."
);
TEST_ASSERT_NULL_MESSAGE
(
parent
->
child
,
"Child of the parent wasn't set to NULL."
);
}
int
main
(
void
)
{
UNITY_BEGIN
();
...
...
@@ -229,6 +268,7 @@ int main(void)
RUN_TEST
(
typecheck_functions_should_check_type
);
RUN_TEST
(
cjson_should_not_parse_to_deeply_nested_jsons
);
RUN_TEST
(
cjson_set_number_value_should_set_numbers
);
RUN_TEST
(
cjson_detach_item_via_pointer_should_detach_items
);
return
UNITY_END
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录