-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
253 lines (210 loc) · 8.09 KB
/
Parser.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "Parser.h"
#include "Errors.h"
bool parse_xml_tag( char *xml_str, unsigned int xml_strlen, char **pptag_name, char **pptag_data, char **pprest_of_string )
{
if( xml_strlen < 7 )
return false;
char tempbuf[128]; //I'm assuming the max strlen for a tag name is 128 chars long
for( unsigned int tn_start = 0; tn_start + 1 < xml_strlen; tn_start++ )
{
//check for beginning of opening tag && make sure it's not actually a closing tag
if( xml_str[tn_start] == '<' && xml_str[tn_start+1] != '/' )
{
for( unsigned int tn_end = tn_start; tn_end + 1 < xml_strlen; tn_end++ )
{
//check for '/' to see if the tag has no data (<emptytag/>)
if( xml_str[tn_end] == '/' )
{
(*pptag_name) = &xml_str[tn_start+1];
xml_str[tn_end] = NULL;
*pptag_data = NULL;
//output rest of string
if( tn_end + 2 < xml_strlen )
(*pprest_of_string) = &xml_str[tn_end+2];
else
(*pprest_of_string) = NULL;
return true;
}
//check for end of opening tag
if( xml_str[tn_end] == '>' )
{
(*pptag_name) = &xml_str[tn_start+1];
xml_str[tn_end] = NULL;
for( unsigned int td_end = tn_end; td_end + 1 < xml_strlen; td_end++ )
{
//check for '</' the start of a closing tag
if( xml_str[td_end] == '/' && xml_str[td_end-1] == '<' )
{
for( unsigned int ct_end = td_end; ct_end < xml_strlen; ct_end++ )
{
//check for '>' the end of a closing tag
if( xml_str[ct_end] == '>' )
{
//check if the tag name in the closing tag is the same as the opening tag
strncpy( tempbuf, &xml_str[td_end+1], ct_end - td_end - 1 );
tempbuf[ ct_end-td_end-1 ] = NULL; //strncpy() does not NULL terminate
if( NULL == strcmp( *pptag_name, tempbuf ) )
{
(*pptag_data) = &xml_str[tn_end+1];
xml_str[td_end-1] = NULL;
//output the rest of the string
if( ct_end + 1 < xml_strlen )
(*pprest_of_string) = &xml_str[ct_end+1];
else
(*pprest_of_string) = NULL;
return true;
}
break;
}
}
}
}
break;
}
}
break;
}
}
return false;
}
bool parse_imgur_xml( char *xml_str, unsigned int xml_strlen, imgur_xml_obj *presponse )
{
bool result;
char *proot = NULL;
char *proot_data = NULL;
char *prest_of_string = NULL;
//if it's not XML, there was a big problem
if( strncmp( xml_str, "<?xml", 5 ) )
{
logger.printf( _T("parse_imgur_xml() FATAL ERROR: xml_str is NOT XML!\r\n") );
return false;
}
//skip XML schema
unsigned int after_schema;
for( after_schema = 0; after_schema < xml_strlen; after_schema++ )
{
if( xml_str[after_schema] == '?' )
{
for( after_schema = after_schema+1; after_schema < xml_strlen; after_schema++ )
{
if( xml_str[after_schema] == '?' )
break;
}
break;
}
}
//extract root node
result = parse_xml_tag( &xml_str[after_schema+2], xml_strlen-after_schema-2, &proot, &proot_data, &prest_of_string );
if( proot_data == NULL || result == false )
return false;
if( NULL == strcmp( proot, "upload" ) )
{
strcpy( presponse->root, proot );
char *pchild;
char *pchild_data;
char *ptemp;
char *ptemp_data;
char *plinks;
//extract image node
parse_xml_tag( proot_data, strlen(proot_data), &pchild, &pchild_data, &prest_of_string );
plinks = prest_of_string;
//discard name, title and caption
parse_xml_tag( pchild_data, strlen(pchild_data), &ptemp, &ptemp_data, &prest_of_string );
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
//glean hash
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.image.hash, ptemp_data );
//glean deletehash
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.image.deletehash, ptemp_data );
//glean datetime
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.image.datetime, ptemp_data );
//glean image type
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
for( int i = 0; i + 1 < strlen(ptemp_data); i++ )
{
if( ptemp_data[i] == '/' )
{
ptemp_data = &ptemp_data[i+1];
break;
}
}
if( !strcmp( ptemp_data, "png" ) )
presponse->upload.image.img_type = PNG;
else if( !strcmp( ptemp_data, "bmp" ) )
presponse->upload.image.img_type = BMP;
else if( !strcmp( ptemp_data, "jpeg" ) )
presponse->upload.image.img_type = JPEG;
else if( !strcmp( ptemp_data, "gif" ) )
presponse->upload.image.img_type = GIF;
else if( !strcmp( ptemp_data, "apng" ) )
presponse->upload.image.img_type = APNG;
else if( !strcmp( ptemp_data, "tiff" ) )
presponse->upload.image.img_type = TIFF;
else if( !strcmp( ptemp_data, "pdf" ) )
presponse->upload.image.img_type = PDF;
else if( !strcmp( ptemp_data, "xcf" ) )
presponse->upload.image.img_type = XCF;
else presponse->upload.image.img_type = INVALID;
//glean animated
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
if( !strcmp( ptemp_data, "true" ) )
presponse->upload.image.animated = true;
else
presponse->upload.image.animated = false;
//glean width
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
presponse->upload.image.width = atoi( ptemp_data );
//glean height
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
presponse->upload.image.height = atoi( ptemp_data );
//glean size
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
presponse->upload.image.size = atoi( ptemp_data );
//glean views
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
presponse->upload.image.views = atoi( ptemp_data );
//glean bandwidh
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
presponse->upload.image.bandwidth = atoi( ptemp_data );
//extract links
parse_xml_tag( plinks, strlen(plinks), &pchild, &pchild_data, &prest_of_string );
//glean original link
parse_xml_tag( pchild_data, strlen(pchild_data), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.links.original, ptemp_data );
//glean imgur page link
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.links.imgur_page, ptemp_data );
//glean delete link
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.links.delete_page, ptemp_data );
//glean small square link
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.links.small_square, ptemp_data );
//glean large thumbnail link
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->upload.links.large_square, ptemp_data );
} else if ( NULL == strcmp( proot, "error" ) )
{
strcpy( presponse->root, proot );
char *ptemp;
char *ptemp_data;
//glean message
parse_xml_tag( proot_data, strlen(proot_data), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->error.message, ptemp_data );
//glean request
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->error.request, ptemp_data );
//glean method
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->error.method, ptemp_data );
//glean format
parse_xml_tag( prest_of_string, strlen(prest_of_string), &ptemp, &ptemp_data, &prest_of_string );
strcpy( presponse->error.format, ptemp_data );
} else {
return false;
}
return true;
}