-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_parser.c
175 lines (155 loc) · 4.18 KB
/
telegram_parser.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* telegram_message_parser.c
*
* Created on: Nov 14, 2019
* Author: alapshin
*/
#include <stdio.h>
#include <stdlib.h>
#include "telegram.h"
#include "telegram_parser.h"
#include "parser/telegram_result.h"
#include "private_include/telegram_parser.h"
#define GOTO_ERR do { ESP_LOGE(__func__, "error on line %d", __LINE__); goto err_exit; } while (0)
#define JSON_MAX_TOKENS 128
static void character_replace(char *str, char in, char out)
{
if (str == NULL)
{
return;
}
for(; *str != '\0'; str++)
{
if(*str != in)
{
continue;
}
*str = out;
}
}
int compare_json_value (const char *str, const char *json, jsmntok_t *t, int i)
{
int len_1 = strlen(str);
int len_2 = t[i].end - t[i].start;
return strncmp(str, json + t[i].start, (len_1 < len_2) ? len_1 : len_2);
}
int json_parse_string (char *str, int str_len, json_parser_t *parser, int index)
{
int result = SUCCESS;
int json_str_len = parser->t[index].end - parser->t[index].start;
if(str_len < json_str_len)
{
result = TOO_LONG_STRING;
GOTO_ERR;
}
strncpy(str, parser->json + parser->t[index].start, json_str_len);
err_exit:
return result;
}
int json_parse_int(json_parser_t *parser, int index, int *val)
{
int result = SUCCESS;
char *endptr;
*val = strtol(parser->json + parser->t[index].start, &endptr, 10);
if (endptr != parser->json + parser->t[index].end)
{
*val = 0;
result = PARSING_INT;
}
return result;
}
int json_parse_core (json_parser_t *parser, int index, void *data, int (*callback)(json_parser_t *parser, int index, void *data))
{
int i, result = index;
int object_parrent = parser->t[index].parent + 1;
if (parser->t[index].type != JSMN_OBJECT)
{
GOTO_ERR;
}
for (i = index; parser->t[i].type != JSMN_UNDEFINED && i < parser->r; i++)
{
ESP_LOGD(__func__, "Now parsing \"%.*s\"",
parser->t[i].end - parser->t[i].start,
&parser->json[parser->t[i].start]);
if (parser->t[i].parent != object_parrent)
{
continue;
}
result = callback(parser, i, data);
if (result != SUCCESS)
{
ESP_LOGE(__func__, "Error while parsing \"%.*s\"",
parser->t[i].end - parser->t[i].start,
&parser->json[parser->t[i].start]);
goto err_exit;
}
}
result = i;
err_exit:
return result;
}
int parse_updates_message (char *json, telegram_parsed_msg_t *message)
{
int i = 0, result = SUCCESS;
jsmn_parser p;
const char *key = NULL;
size_t key_lenght = 0;
json_parser_t parser = {.json = json, .t = NULL, .r = 0};
if (json == NULL)
{
result = GENERAL_ERROR;
GOTO_ERR;
}
parser.t = calloc(sizeof(jsmntok_t), JSON_MAX_TOKENS);
character_replace(json, '\n', ' ');
jsmn_init(&p);
parser.r = jsmn_parse(&p, json, strlen(json), parser.t, JSON_MAX_TOKENS);
if (parser.r < 0)
{
result = PARSING_ERROR;
GOTO_ERR;
}
/* Assume the top-level element is an object */
if (parser.r < 1 || parser.t[i].type != JSMN_OBJECT)
{
result = PARSING_ERROR;
GOTO_ERR;
}
i++;
key = &parser.json[parser.t[i].start];
key_lenght = parser.t[i].end - parser.t[i].start;
if(!IS_JSON_KEY("ok"))
{
result = PARSING_ERROR;
GOTO_ERR;
}
key = &parser.json[parser.t[i].start];
key_lenght = parser.t[i].end - parser.t[i].start;
if(!IS_JSON_KEY("true"))
{
result = PARSING_ERROR;
GOTO_ERR;
}
key = &parser.json[parser.t[i].start];
key_lenght = parser.t[i].end - parser.t[i].start;
if(!IS_JSON_KEY("result"))
{
result = PARSING_ERROR;
GOTO_ERR;
}
if (parser.t[i].type == JSMN_ARRAY) /* array */
{
i++;
}
if (parser.t[i].type == JSMN_UNDEFINED)
{
goto err_exit;
}
memset(message, 0x00, sizeof(*message));
json_parse_core(&parser, i, message, json_parse_telegram_result_callback);
result = SUCCESS;
err_exit:
free(parser.t);
parser.t = NULL;
return result;
}