Skip to content

Commit 35483b9

Browse files
committed
Refined examples.
1 parent a2c8154 commit 35483b9

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

example/c/lib_buffer_unpack.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ typedef struct receiver {
88
size_t rest;
99
} receiver;
1010

11-
receiver r;
12-
1311
void receiver_init(receiver *r) {
1412
msgpack_packer pk;
1513

@@ -53,6 +51,7 @@ void unpack(receiver* r) {
5351
char* buf;
5452
size_t recv_len;
5553
int recv_count = 0;
54+
int i = 0;
5655

5756
msgpack_unpacked_init(&result);
5857
if (msgpack_unpacker_buffer_capacity(unp) < EACH_RECV_SIZE) {
@@ -65,15 +64,14 @@ void unpack(receiver* r) {
6564
msgpack_unpacker_buffer_consumed(unp, recv_len);
6665

6766

68-
while (recv_len > 0) {
69-
int i = 0;
70-
printf("receive count: %d %zd bytes received.:\n", recv_count++, recv_len);
67+
while (recv_len > 0) {
68+
printf("receive count: %d %zd bytes received.\n", recv_count++, recv_len);
7169
ret = msgpack_unpacker_next(unp, &result);
7270
while (ret == MSGPACK_UNPACK_SUCCESS) {
7371
msgpack_object obj = result.data;
7472

7573
/* Use obj. */
76-
printf("Object no %d:\n", i++);
74+
printf("Object no %d:\n", ++i);
7775
msgpack_object_print(stdout, obj);
7876
printf("\n");
7977
/* If you want to allocate something on the zone, you can use zone. */
@@ -110,10 +108,16 @@ int main(void) {
110108
/* Output */
111109

112110
/*
111+
receive count: 0 4 bytes received.
112+
receive count: 1 4 bytes received.
113+
receive count: 2 4 bytes received.
113114
Object no 1:
114115
[1, true, "example"]
116+
receive count: 3 4 bytes received.
117+
receive count: 4 4 bytes received.
115118
Object no 2:
116119
"second"
120+
receive count: 5 1 bytes received.
117121
Object no 3:
118122
[42, false]
119123
*/

example/c/user_buffer_unpack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void unpack(char const* buf, size_t len) {
3333
msgpack_object obj = result.data;
3434

3535
/* Use obj. */
36-
printf("Object no %d:\n", i++);
36+
printf("Object no %d:\n", ++i);
3737
msgpack_object_print(stdout, obj);
3838
printf("\n");
3939
/* If you want to allocate something on the zone, you can use zone. */
@@ -72,4 +72,5 @@ Object no 2:
7272
"second"
7373
Object no 3:
7474
[42, false]
75+
All msgpack_object in the buffer is consumed.
7576
*/

0 commit comments

Comments
 (0)