Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
782770c6
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,发现更多精彩内容 >>
提交
782770c6
编写于
9月 28, 2016
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
reformatting: print_array
上级
ad711e6f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
160 addition
and
74 deletion
+160
-74
cJSON.c
cJSON.c
+160
-74
未找到文件。
cJSON.c
浏览文件 @
782770c6
...
...
@@ -1045,83 +1045,169 @@ static const char *parse_array(cJSON *item,const char *value,const char **ep)
}
/* Render an array to text */
static
char
*
print_array
(
cJSON
*
item
,
int
depth
,
int
fmt
,
printbuffer
*
p
)
static
char
*
print_array
(
cJSON
*
item
,
int
depth
,
int
fmt
,
printbuffer
*
p
)
{
char
**
entries
;
char
*
out
=
0
,
*
ptr
,
*
ret
;
int
len
=
5
;
cJSON
*
child
=
item
->
child
;
int
numentries
=
0
,
i
=
0
,
fail
=
0
;
size_t
tmplen
=
0
;
/* How many entries in the array? */
while
(
child
)
numentries
++
,
child
=
child
->
next
;
/* Explicitly handle numentries==0 */
if
(
!
numentries
)
{
if
(
p
)
out
=
ensure
(
p
,
3
);
else
out
=
(
char
*
)
cJSON_malloc
(
3
);
if
(
out
)
strcpy
(
out
,
"[]"
);
return
out
;
}
char
**
entries
;
char
*
out
=
0
;
char
*
ptr
;
char
*
ret
;
int
len
=
5
;
cJSON
*
child
=
item
->
child
;
int
numentries
=
0
;
int
i
=
0
;
int
fail
=
0
;
size_t
tmplen
=
0
;
if
(
p
)
{
/* Compose the output array. */
i
=
p
->
offset
;
ptr
=
ensure
(
p
,
1
);
if
(
!
ptr
)
return
0
;
*
ptr
=
'['
;
p
->
offset
++
;
child
=
item
->
child
;
while
(
child
&&
!
fail
)
{
print_value
(
child
,
depth
+
1
,
fmt
,
p
);
p
->
offset
=
update
(
p
);
if
(
child
->
next
)
{
len
=
fmt
?
2
:
1
;
ptr
=
ensure
(
p
,
len
+
1
);
if
(
!
ptr
)
return
0
;
*
ptr
++=
','
;
if
(
fmt
)
*
ptr
++=
' '
;
*
ptr
=
0
;
p
->
offset
+=
len
;}
child
=
child
->
next
;
}
ptr
=
ensure
(
p
,
2
);
if
(
!
ptr
)
return
0
;
*
ptr
++=
']'
;
*
ptr
=
0
;
out
=
(
p
->
buffer
)
+
i
;
}
else
{
/* Allocate an array to hold the values for each */
entries
=
(
char
**
)
cJSON_malloc
(
numentries
*
sizeof
(
char
*
));
if
(
!
entries
)
return
0
;
memset
(
entries
,
0
,
numentries
*
sizeof
(
char
*
));
/* Retrieve all the results: */
child
=
item
->
child
;
while
(
child
&&
!
fail
)
{
ret
=
print_value
(
child
,
depth
+
1
,
fmt
,
0
);
entries
[
i
++
]
=
ret
;
if
(
ret
)
len
+=
strlen
(
ret
)
+
2
+
(
fmt
?
1
:
0
);
else
fail
=
1
;
child
=
child
->
next
;
}
/* If we didn't fail, try to malloc the output string */
if
(
!
fail
)
out
=
(
char
*
)
cJSON_malloc
(
len
);
/* If that fails, we fail. */
if
(
!
out
)
fail
=
1
;
/* How many entries in the array? */
while
(
child
)
{
numentries
++
;
child
=
child
->
next
;
}
/* Handle failure. */
if
(
fail
)
{
for
(
i
=
0
;
i
<
numentries
;
i
++
)
if
(
entries
[
i
])
cJSON_free
(
entries
[
i
]);
cJSON_free
(
entries
);
return
0
;
}
/* Compose the output array. */
*
out
=
'['
;
ptr
=
out
+
1
;
*
ptr
=
0
;
for
(
i
=
0
;
i
<
numentries
;
i
++
)
{
tmplen
=
strlen
(
entries
[
i
]);
memcpy
(
ptr
,
entries
[
i
],
tmplen
);
ptr
+=
tmplen
;
if
(
i
!=
numentries
-
1
)
{
*
ptr
++=
','
;
if
(
fmt
)
*
ptr
++=
' '
;
*
ptr
=
0
;}
cJSON_free
(
entries
[
i
]);
}
cJSON_free
(
entries
);
*
ptr
++=
']'
;
*
ptr
++=
0
;
}
return
out
;
/* Explicitly handle numentries == 0 */
if
(
!
numentries
)
{
if
(
p
)
{
out
=
ensure
(
p
,
3
);
}
else
{
out
=
(
char
*
)
cJSON_malloc
(
3
);
}
if
(
out
)
{
strcpy
(
out
,
"[]"
);
}
return
out
;
}
if
(
p
)
{
/* Compose the output array. */
/* opening square bracket */
i
=
p
->
offset
;
ptr
=
ensure
(
p
,
1
);
if
(
!
ptr
)
{
return
0
;
}
*
ptr
=
'['
;
p
->
offset
++
;
child
=
item
->
child
;
while
(
child
&&
!
fail
)
{
print_value
(
child
,
depth
+
1
,
fmt
,
p
);
p
->
offset
=
update
(
p
);
if
(
child
->
next
)
{
len
=
fmt
?
2
:
1
;
ptr
=
ensure
(
p
,
len
+
1
);
if
(
!
ptr
)
{
return
0
;
}
*
ptr
++
=
','
;
if
(
fmt
)
{
*
ptr
++
=
' '
;
}
*
ptr
=
0
;
p
->
offset
+=
len
;
}
child
=
child
->
next
;
}
ptr
=
ensure
(
p
,
2
);
if
(
!
ptr
)
{
return
0
;
}
*
ptr
++
=
']'
;
*
ptr
=
'\0'
;
out
=
(
p
->
buffer
)
+
i
;
}
else
{
/* Allocate an array to hold the pointers to all printed values */
entries
=
(
char
**
)
cJSON_malloc
(
numentries
*
sizeof
(
char
*
));
if
(
!
entries
)
{
return
0
;
}
memset
(
entries
,
0
,
numentries
*
sizeof
(
char
*
));
/* Retrieve all the results: */
child
=
item
->
child
;
while
(
child
&&
!
fail
)
{
ret
=
print_value
(
child
,
depth
+
1
,
fmt
,
0
);
entries
[
i
++
]
=
ret
;
if
(
ret
)
{
len
+=
strlen
(
ret
)
+
2
+
(
fmt
?
1
:
0
);
}
else
{
fail
=
1
;
}
child
=
child
->
next
;
}
/* If we didn't fail, try to malloc the output string */
if
(
!
fail
)
{
out
=
(
char
*
)
cJSON_malloc
(
len
);
}
/* If that fails, we fail. */
if
(
!
out
)
{
fail
=
1
;
}
/* Handle failure. */
if
(
fail
)
{
/* free all the entries in the array */
for
(
i
=
0
;
i
<
numentries
;
i
++
)
{
if
(
entries
[
i
])
{
cJSON_free
(
entries
[
i
]);
}
}
cJSON_free
(
entries
);
return
0
;
}
/* Compose the output array. */
*
out
=
'['
;
ptr
=
out
+
1
;
*
ptr
=
'\0'
;
for
(
i
=
0
;
i
<
numentries
;
i
++
)
{
tmplen
=
strlen
(
entries
[
i
]);
memcpy
(
ptr
,
entries
[
i
],
tmplen
);
ptr
+=
tmplen
;
if
(
i
!=
(
numentries
-
1
))
{
*
ptr
++
=
','
;
if
(
fmt
)
{
*
ptr
++
=
' '
;
}
*
ptr
=
0
;
}
cJSON_free
(
entries
[
i
]);
}
cJSON_free
(
entries
);
*
ptr
++
=
']'
;
*
ptr
++
=
'\0'
;
}
return
out
;
}
/* Build an object from the text. */
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录