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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/tech/webapis.h"
#include "gate/results.h"
#include "gate/encode/xml.h"
#define GATE_WEBAPIS_NEWSFEED_REQUEST_NAME GATE_STRUCT_WEBAPIS_NAME("newsfeed_request")
#define GATE_WEBAPIS_NEWSFEED_RESPONSE_NAME GATE_STRUCT_WEBAPIS_NAME("newsfeed_response")
#define GATE_WEBAPIS_NEWSENTRY_NAME GATE_STRUCT_WEBAPIS_NAME("newsfeed_entry")
/* newsfeed request initialization */
static gate_struct_item_t const newsfeed_request_members[] =
{
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_request_t, GATE_TYPE_STRING, request_url, "Request URL", GATE_STRUCT_FLAG_DEFAULT)
};
static gate_struct_descriptor_t const newsfeed_request_descriptor =
GATE_STRUCT_DESCRIPTOR(gate_tech_webapi_newsfeed_request_t, GATE_WEBAPIS_NEWSFEED_REQUEST_NAME, newsfeed_request_members);
GATE_TECH_API void gate_tech_webapi_newsfeed_request_init(gate_tech_webapi_newsfeed_request_t* request)
{
gate_mem_clear(request, sizeof(gate_tech_webapi_newsfeed_request_t));
request->struct_base.struct_descriptor = &newsfeed_request_descriptor;
}
/* newsentry initialization */
static gate_struct_item_t const newsentry_members[] =
{
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, uid, "Unique-ID", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, title, "Title", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, link, "Link", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_TIME, update_time, "Updated", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, author, "Author", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, category, "Category", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, summary, "Summary", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsentry_t, GATE_TYPE_STRING, content, "Content", GATE_STRUCT_FLAG_DEFAULT)
};
static gate_struct_descriptor_t const newsentry_descriptor =
GATE_STRUCT_DESCRIPTOR(gate_tech_webapi_newsentry_t, GATE_WEBAPIS_NEWSENTRY_NAME, newsentry_members);
void gate_tech_webapi_newsentry_init(gate_tech_webapi_newsentry_t* entry)
{
gate_mem_clear(entry, sizeof(gate_tech_webapi_newsentry_t));
entry->struct_base.struct_descriptor = &newsentry_descriptor;
}
static gate_result_t gate_tech_webapi_newsentry_copyctor(void* target, void const* source)
{
gate_result_t result;
gate_tech_webapi_newsentry_t* ptr_dst = (gate_tech_webapi_newsentry_t*)target;
gate_tech_webapi_newsentry_t const* ptr_src = (gate_tech_webapi_newsentry_t const*)source;
if (ptr_src)
{
gate_mem_clear(ptr_dst, sizeof(gate_tech_webapi_newsentry_t));
result = gate_struct_copy(&ptr_dst->struct_base, &ptr_src->struct_base);
}
else
{
gate_tech_webapi_newsentry_init(ptr_dst);
result = GATE_RESULT_OK;
}
return result;
}
static gate_struct_item_t const newsfeed_response_members[] =
{
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, request_url, "Request URL", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, uid, "Unique-ID", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, title, "Title", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, link, "Link", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_TIME, update_time, "Updated", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, description,"Description", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, language, "Language", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, publisher, "Publisher", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, contact, "Contact", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_STRING, image, "Image", GATE_STRUCT_FLAG_DEFAULT),
GATE_STRUCT_ITEM_EX(gate_tech_webapi_newsfeed_response_t, GATE_TYPE_ARRAYLIST_STRUCT, entries, "Entries", GATE_STRUCT_FLAG_DEFAULT)
};
static gate_struct_descriptor_t const newsfeed_response_descriptor =
GATE_STRUCT_DESCRIPTOR(gate_tech_webapi_newsfeed_response_t, GATE_WEBAPIS_NEWSFEED_RESPONSE_NAME, newsfeed_response_members);
GATE_TECH_API void gate_tech_webapi_newsfeed_response_init(gate_tech_webapi_newsfeed_response_t* response)
{
gate_mem_clear(response, sizeof(gate_tech_webapi_newsfeed_response_t));
response->struct_base.struct_descriptor = &newsfeed_response_descriptor;
response->entries = gate_arraylist_create(sizeof(gate_tech_webapi_newsentry_t), NULL, 0,
&gate_tech_webapi_newsentry_copyctor, &gate_struct_destructor);
}
static gate_bool_t xml_node_is_tag(gate_xml_node_t const* node, gate_string_t const* tagname)
{
return gate_string_equals(&node->tag, tagname);
}
static gate_xml_node_t const* get_child_by_tag(gate_xml_node_t const* node, gate_string_t const* tagname)
{
gate_size_t children = gate_xml_node_children_count(node);
gate_size_t ndx;
gate_xml_node_t const* ptr_child = NULL;
for (ndx = 0; ndx != children; ++ndx)
{
ptr_child = gate_xml_node_child_ptr(node, ndx);
if (!ptr_child)
{
continue;
}
if (xml_node_is_tag(ptr_child, tagname))
{
return ptr_child;
}
}
return NULL;
}
static gate_bool_t parse_time(gate_string_t const* str, gate_datetime_t* dt)
{
/* "Thu, 11 Apr 2024 21:47:03" */
gate_string_t str_day = GATE_STRING_INIT_STATIC("01");
gate_string_t str_mon = GATE_STRING_INIT_STATIC("Jan");
gate_string_t str_year = GATE_STRING_INIT_STATIC("1970");
gate_string_t str_hour = GATE_STRING_INIT_STATIC("00");
gate_string_t str_min = GATE_STRING_INIT_STATIC("00");
gate_string_t str_sec = GATE_STRING_INIT_STATIC("00");
gate_size_t len = gate_string_length(str);
char const* ptr = gate_string_ptr(str, 0);
gate_uint64_t num;
if (len < 16)
{
return false;
}
if ((ptr[3] != ',') || (ptr[4] != ' ') || (ptr[7] != ' ') || (ptr[11] != ' '))
{
return false;
}
gate_mem_clear(dt, sizeof(gate_datetime_t));
gate_string_create_static_len(&str_day, &ptr[5], 2);
gate_string_create_static_len(&str_mon, &ptr[8], 3);
gate_string_create_static_len(&str_year, &ptr[12], 4);
if (len >= 22)
{
gate_string_create_static_len(&str_hour, &ptr[17], 2);
gate_string_create_static_len(&str_min, &ptr[20], 2);
}
if (len >= 25)
{
gate_string_create_static_len(&str_sec, &ptr[23], 2);
}
if (gate_string_parse_uint(&str_day, &num) > 0)
{
dt->date.day = (gate_uint8_t)num;
}
gate_date_parse_month(str_mon.str, str_mon.length, &dt->date.month);
if (gate_string_parse_uint(&str_year, &num) > 0)
{
dt->date.year = (gate_uint16_t)num;
}
if (gate_string_parse_uint(&str_hour, &num) > 0)
{
dt->time.hour = (gate_uint8_t)num;
}
if (gate_string_parse_uint(&str_min, &num) > 0)
{
dt->time.minute = (gate_uint8_t)num;
}
if (gate_string_parse_uint(&str_sec, &num) > 0)
{
dt->time.second = (gate_uint8_t)num;
}
return true;
}
static gate_bool_t extract_content(gate_xml_node_t const* node, gate_string_t* target)
{
gate_size_t children, ndx;
gate_xml_node_t const* child;
gate_strbuilder_t builder = GATE_INIT_EMPTY;
if (node)
{
if (node->node_type == GATE_XML_NODE_TYPE_ELEMENT)
{
children = gate_array_length(&node->child_nodes);
if (children == 1)
{
child = (gate_xml_node_t const*)gate_array_get(&node->child_nodes, 0);
if (child && (child->node_type == GATE_XML_NODE_TYPE_CDATA))
{
gate_string_clone(target, &child->content);
}
}
else if (children > 1)
{
gate_strbuilder_create(&builder, 0);
for (ndx = 0; ndx != children; ++ndx)
{
child = (gate_xml_node_t const*)gate_array_get(&node->child_nodes, ndx);
if (child && (child->node_type == GATE_XML_NODE_TYPE_CDATA))
{
gate_strbuilder_append_string(&builder, &child->content);
}
}
gate_strbuilder_to_string(&builder, target);
gate_strbuilder_release(&builder);
}
else
{
gate_xml_decode(&node->content, target);
}
return true;
}
}
return false;
}
static gate_bool_t extract_attribute(gate_xml_node_t const* node, gate_string_t const* attr, gate_string_t* target)
{
gate_string_t const* ptr_attrvalue;<--- The scope of the variable 'ptr_attrvalue' can be reduced. [+]The scope of the variable 'ptr_attrvalue' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (node)
{
ptr_attrvalue = gate_xml_node_attribute_ptr_by_name(node, attr);
if (ptr_attrvalue)
{
gate_string_clone(target, ptr_attrvalue);
return true;
}
}
return false;
}
static gate_bool_t extract_node_attribute(gate_xml_node_t const* node, gate_string_t const* tagname, gate_string_t const* attr, gate_string_t* target)
{
gate_string_t const* ptr_attrvalue;<--- The scope of the variable 'ptr_attrvalue' can be reduced. [+]The scope of the variable 'ptr_attrvalue' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_xml_node_t const* child = get_child_by_tag(node, tagname);
if (child)
{
ptr_attrvalue = gate_xml_node_attribute_ptr_by_name(node, attr);
if (ptr_attrvalue)
{
gate_string_clone(target, ptr_attrvalue);
return true;
}
}
return false;
}
static gate_bool_t extract_time(gate_xml_node_t const* node, gate_time_t* timestamp)
{
gate_datetime_t dt;
if (node)
{
if (gate_date_parse_string(node->content.str, node->content.length, &dt, NULL) > 0)
{
gate_date_to_time(&dt, timestamp);
return true;
}
}
return false;
}
static gate_result_t parse_atom(gate_string_t const* url, gate_xml_doc_t const* doc, gate_xml_node_t const* root, gate_tech_webapi_newsfeed_response_t* response)
{
static gate_string_t const tag_title = GATE_STRING_INIT_STATIC("title");
static gate_string_t const tag_subtitle = GATE_STRING_INIT_STATIC("subtitle");
static gate_string_t const tag_link = GATE_STRING_INIT_STATIC("link");
static gate_string_t const tag_id = GATE_STRING_INIT_STATIC("id");
static gate_string_t const tag_updated = GATE_STRING_INIT_STATIC("updated");
static gate_string_t const attr_href = GATE_STRING_INIT_STATIC("href");
static gate_string_t const tag_entry = GATE_STRING_INIT_STATIC("entry");
/*static gate_string_t const tag_published = GATE_STRING_INIT_STATIC("published");*/
static gate_string_t const tag_summary = GATE_STRING_INIT_STATIC("summary");
static gate_string_t const tag_content = GATE_STRING_INIT_STATIC("content");
static gate_string_t const tag_author = GATE_STRING_INIT_STATIC("author");
static gate_string_t const tag_name = GATE_STRING_INIT_STATIC("name");
static gate_string_t const tag_email = GATE_STRING_INIT_STATIC("email");
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t root_children, ndx;
gate_xml_node_t const* entry;
gate_xml_node_t const* subentry;<--- The scope of the variable 'subentry' can be reduced. [+]The scope of the variable 'subentry' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_tech_webapi_newsentry_t news_entry;
GATE_UNUSED_ARG(doc);
do
{
gate_string_clone(&response->request_url, url);
extract_content(get_child_by_tag(root, &tag_title), &response->title);
extract_content(get_child_by_tag(root, &tag_subtitle), &response->description);
extract_attribute(get_child_by_tag(root, &tag_link), &attr_href, &response->link);
extract_content(get_child_by_tag(root, &tag_id), &response->uid);
extract_time(get_child_by_tag(root, &tag_updated), &response->update_time);
subentry = get_child_by_tag(root, &tag_author);
if (subentry)
{
extract_content(get_child_by_tag(subentry, &tag_email), &response->contact);
extract_content(get_child_by_tag(subentry, &tag_name), &response->publisher);
}
root_children = gate_xml_node_children_count(root);
for (ndx = 0; ndx != root_children; ++ndx)
{
entry = gate_xml_node_child_ptr(root, ndx);
if (!entry)
{
continue;
}
if (!xml_node_is_tag(entry, &tag_entry))
{
/* skip non-channels */
continue;
}
gate_tech_webapi_newsentry_init(&news_entry);
extract_content(get_child_by_tag(entry, &tag_title), &news_entry.title);
extract_attribute(get_child_by_tag(entry, &tag_link), &attr_href, &news_entry.link);
extract_content(get_child_by_tag(entry, &tag_id), &news_entry.uid);
extract_content(get_child_by_tag(entry, &tag_summary), &news_entry.summary);
extract_content(get_child_by_tag(entry, &tag_content), &news_entry.content);
extract_time(get_child_by_tag(entry, &tag_updated), &news_entry.update_time);
subentry = get_child_by_tag(entry, &tag_author);
if (subentry)
{
if (!extract_content(get_child_by_tag(subentry, &tag_name), &news_entry.author))
{
gate_string_clone(&news_entry.author, &response->publisher);
}
}
else
{
gate_string_clone(&news_entry.author, &response->publisher);
}
gate_arraylist_add(response->entries, &news_entry);
gate_struct_release(&news_entry.struct_base);
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t parse_rss(gate_string_t const* url, gate_xml_doc_t const* doc, gate_xml_node_t const* root, gate_tech_webapi_newsfeed_response_t* response)
{
static gate_string_t const tag_channel = GATE_STRING_INIT_STATIC("channel");
/*static gate_string_t const tag_category = GATE_STRING_INIT_STATIC("category");*/
static gate_string_t const tag_title = GATE_STRING_INIT_STATIC("title");
static gate_string_t const tag_link = GATE_STRING_INIT_STATIC("link");
static gate_string_t const tag_descr = GATE_STRING_INIT_STATIC("description");
static gate_string_t const tag_date = GATE_STRING_INIT_STATIC("pubDate");
static gate_string_t const tag_item = GATE_STRING_INIT_STATIC("item");
/*static gate_string_t const tag_author = GATE_STRING_INIT_STATIC("author");*/
/*static gate_string_t const tag_guid = GATE_STRING_INIT_STATIC("guid");*/
/*static gate_string_t const tag_content = GATE_STRING_INIT_STATIC("content");*/
static gate_string_t const tag_language = GATE_STRING_INIT_STATIC("language");
static gate_string_t const tag_image = GATE_STRING_INIT_STATIC("image");
static gate_string_t const tag_webmaster = GATE_STRING_INIT_STATIC("webMaster");
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t root_children;
gate_size_t ndx_channel;
gate_xml_node_t const* ptr_channel;
gate_xml_node_t const* ptr_channel_title;
gate_xml_node_t const* ptr_channel_link;
gate_xml_node_t const* ptr_channel_descr;
gate_xml_node_t const* ptr_channel_date;
gate_xml_node_t const* ptr_channel_lang;
gate_xml_node_t const* ptr_channel_icon;
gate_xml_node_t const* ptr_channel_webmaster;
gate_xml_node_t const* ptr_item;
gate_size_t item_counter;
gate_size_t item_index;
gate_xml_node_t const* ptr_item_title;
gate_xml_node_t const* ptr_item_link;
gate_xml_node_t const* ptr_item_description;
gate_xml_node_t const* ptr_item_pubdate;
gate_datetime_t dt;
gate_tech_webapi_newsentry_t news_entry;
do
{
gate_string_clone(&response->request_url, url);
gate_string_clone(&response->uid, url);
root_children = gate_xml_node_children_count(root);
for (ndx_channel = 0; ndx_channel != root_children; ++ndx_channel)
{
ptr_channel = gate_xml_node_child_ptr(root, ndx_channel);
if (!ptr_channel)
{
continue;
}
if (!xml_node_is_tag(ptr_channel, &tag_channel))
{
/* skip non-channels */
continue;
}
ptr_channel_title = get_child_by_tag(ptr_channel, &tag_title);
ptr_channel_link = get_child_by_tag(ptr_channel, &tag_link);
ptr_channel_descr = get_child_by_tag(ptr_channel, &tag_descr);
ptr_channel_date = get_child_by_tag(ptr_channel, &tag_date);
ptr_channel_lang = get_child_by_tag(ptr_channel, &tag_language);
ptr_channel_icon = get_child_by_tag(ptr_channel, &tag_image);
ptr_channel_webmaster = get_child_by_tag(ptr_channel, &tag_webmaster);
extract_content(ptr_channel_title, &response->title);
extract_content(ptr_channel_link, &response->link);
extract_content(ptr_channel_descr, &response->description);
extract_content(ptr_channel_lang, &response->language);
extract_content(ptr_channel_icon, &response->image);
extract_content(ptr_channel_webmaster, &response->contact);
if (ptr_channel_date)
{
if (parse_time(&ptr_channel_date->content, &dt))
{
gate_date_to_time(&dt, &response->update_time);
}
}
item_counter = gate_xml_node_children_count(ptr_channel);
for (item_index = 0; item_index != item_counter; ++item_index)
{
ptr_item = gate_xml_node_child_ptr(ptr_channel, item_index);
if (!ptr_item)
{
continue;
}
if (!xml_node_is_tag(ptr_item, &tag_item))
{
/* skip non-items*/
continue;
}
gate_tech_webapi_newsentry_init(&news_entry);
ptr_item_title = get_child_by_tag(ptr_item, &tag_title);
ptr_item_link = get_child_by_tag(ptr_item, &tag_link);
ptr_item_description = get_child_by_tag(ptr_item, &tag_descr);
ptr_item_pubdate = get_child_by_tag(ptr_item, &tag_date);
extract_content(ptr_item_title, &news_entry.title);
extract_content(ptr_item_link, &news_entry.link);
extract_content(ptr_item_description, &news_entry.summary);
if (ptr_item_pubdate)
{
if (parse_time(&ptr_item_pubdate->content, &dt))
{
gate_date_to_time(&dt, &news_entry.update_time);
}
}
gate_arraylist_add(response->entries, &news_entry);
gate_struct_release(&news_entry.struct_base);
}
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_tech_webapi_newsfeed(gate_tech_webapi_context_t* context,
gate_tech_webapi_newsfeed_request_t* request,
gate_tech_webapi_newsfeed_response_t* response)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_uri_t url = GATE_INIT_EMPTY;
gate_uint32_t http_status = 0;
gate_stringstream_t* content = NULL;
gate_xml_doc_t doc = GATE_INIT_EMPTY;
gate_xml_node_t const* ptr_node;
gate_string_t tag_name = GATE_STRING_INIT_EMPTY;
static gate_string_t const atom_root = GATE_STRING_INIT_STATIC("feed");
static gate_string_t const rss_root = GATE_STRING_INIT_STATIC("rss");
do
{
ret = gate_uri_parse(&url, &request->request_url);
GATE_BREAK_IF_FAILED(ret);
content = gate_stringstream_create(0);
if (!content)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_tech_webapi_get(context, &url, NULL, &http_status, (gate_stream_t*)content, NULL);
GATE_BREAK_IF_FAILED(ret);
ret = gate_xml_doc_load(&doc, (gate_stream_t*)content);
GATE_BREAK_IF_FAILED(ret);
ret = gate_xml_doc_root_element(&doc, &ptr_node);
GATE_BREAK_IF_FAILED(ret);
ret = gate_xml_parse_decompose_name(&ptr_node->tag, NULL, &tag_name);
GATE_BREAK_IF_FAILED(ret);
if (gate_string_equals(&tag_name, &atom_root))
{
/* parse atom feed */
ret = parse_atom(&request->request_url, &doc, ptr_node, response);
}
else if (gate_string_equals(&tag_name, &rss_root))
{
/* parse RSS */
ret = parse_rss(&request->request_url, &doc, ptr_node, response);
}
else
{
ret = GATE_RESULT_INVALIDCONTENT;
}
} while (0);
gate_string_release(&tag_name);
gate_xml_doc_destroy(&doc);
if (content != NULL)
{
gate_object_release(content);
}
gate_uri_destroy(&url);
return ret;
}
|