-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_handler.c
61 lines (53 loc) · 1.37 KB
/
xml_handler.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
#include <string.h>
#include <stdbool.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "util.h"
int xml_fullcount(struct XML xml)
{
bool failed = false;
xmlDocPtr doc;
xmlNode *root_element = NULL;
doc = xmlReadMemory(xml.data, xml.size, "noname.xml", NULL, 0);
free(xml.data);
if(doc == NULL)
{
fprintf(stderr, "Failed to parse XML.\n");
failed = true;
}
root_element = xmlDocGetRootElement(doc);
// A HTML 401 is returned if authentication is invalid
if(!strncmp((char *) root_element->name, "HTML", sizeof("HTML")))
if(!strncmp(
(char *) xmlNodeGetContent(
// TITLE tag
xmlFirstElementChild(xmlFirstElementChild(root_element))),
"Unauthorized",
sizeof("Unauthorized")))
{
fprintf(stderr, "Authentication failed\n");
failed = true;
}
int fullcount;
xmlNode *cur_node = NULL;
for(cur_node = xmlFirstElementChild(root_element);
cur_node;
cur_node = cur_node->next)
{
if(cur_node->type == XML_ELEMENT_NODE)
{
if(!strncmp((char *) cur_node->name,
"fullcount", sizeof("fullcount")))
{
xmlChar *xml_count = xmlNodeGetContent(cur_node);
sscanf((char *) xml_count, "%d", &fullcount);
xmlFree(xml_count);
}
}
}
xmlFreeDoc(doc);
xmlCleanupParser();
if(failed)
exit(EXIT_FAILURE);
return fullcount;
}