-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformat.c
143 lines (120 loc) · 3.31 KB
/
format.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (C) 2023 TDT AG <[email protected]>
*
* This is free software, licensed under the
* GNU Lesser General Public License, Version 2.1.
* See https://www.gnu.org/licenses/lgpl-2.1.txt for more information.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "format.h"
json_object *format_generate_response(bool is_event)
{
json_object *root = json_object_new_object();
json_object_object_add(root, ERRORS_KEY, json_object_new_array());
if (is_event)
json_object_object_add(root, DATA_KEY, json_object_new_array());
return root;
}
json_object *format_parse_error(const char *type, int err)
{
json_object *error = json_object_new_object();
if (type) {
char *message;
asprintf(&message, "%s: %s", type, strerror(err));
json_object_object_add(error, ERROR_MESSAGE_KEY,
json_object_new_string(message));
free(message);
} else
json_object_object_add(error, ERROR_MESSAGE_KEY,
json_object_new_string(strerror(err)));
return error;
}
void format_dump_json(json_object *obj, FILE *out)
{
fprintf(out, "%s\n", json_object_to_json_string_ext(
obj, JSON_C_TO_STRING_PLAIN));
}
void format_add_error(json_object *root, json_object *error)
{
json_object *error_arr;
json_object_object_get_ex(root, ERRORS_KEY, &error_arr);
json_object_array_add(error_arr, error);
}
void format_set_data(json_object *root, json_object *data)
{
json_object *data_obj;
json_object_object_get_ex(root, DATA_KEY, &data_obj);
if (!data_obj)
json_object_object_add(root, DATA_KEY, data);
}
void format_add_data(json_object *root, json_object *data)
{
json_object *data_arr;
json_object_object_get_ex(root, DATA_KEY, &data_arr);
json_object_array_add(data_arr, data);
}
json_object *format_parse_davici_response(struct davici_response *res)
{
GQueue* queue = g_queue_new();
json_object *root = json_object_new_object();
json_object *current;
json_object *next;
const char *name;
char buf[4096];
int ret;
g_queue_push_head(queue, root);
while (true) {
ret = davici_parse(res);
switch (ret) {
case DAVICI_END:
g_queue_free(queue);
return root;
case DAVICI_SECTION_START:
current = g_queue_peek_head(queue);
next = json_object_new_object();
name = davici_get_name(res);
json_object_object_add(current, name, next);
g_queue_push_head(queue, next);
break;
case DAVICI_SECTION_END:
g_queue_pop_head(queue);
break;
case DAVICI_KEY_VALUE:
ret = davici_get_value_str(res, buf, sizeof(buf));
if (ret < 0) {
g_queue_free(queue);
return root;
}
name = davici_get_name(res);
current = g_queue_peek_head(queue);
json_object_object_add(current, name, json_object_new_string(buf));
break;
case DAVICI_LIST_START:
current = g_queue_peek_head(queue);
next = json_object_new_array();
name = davici_get_name(res);
json_object_object_add(current, name, next);
g_queue_push_head(queue, next);
break;
case DAVICI_LIST_ITEM:
ret = davici_get_value_str(res, buf, sizeof(buf));
if (ret < 0) {
g_queue_free(queue);
return root;
}
current = g_queue_peek_head(queue);
json_object_array_add(current, json_object_new_string(buf));
break;
case DAVICI_LIST_END:
g_queue_pop_head(queue);
break;
default:
g_queue_free(queue);
return root;
}
}
}