summaryrefslogtreecommitdiff
path: root/export_xml.c
blob: f66381effbe088bba3f923578b1e3edead60e853 (plain)
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
#include <libxml/tree.h>
#include <libxml/xmlsave.h>

#include "export_xml.h"

#if !defined(LIBXML_TREE_ENABLED)
#error "libxml must be compiled with tree support"
#endif


int export_xml(struct channel_t *channel, struct user_t *users) {
	/* Create document and set root node. */
	xmlDocPtr doc = xmlNewDoc("1.0");
	xmlNodePtr root_node = xmlNewNode(NULL, "channel");
	xmlDocSetRootElement(doc, root_node);

	/* Set the channel name. */
	xmlNewChild(root_node, NULL, "name", channel->name);

	/* Add lines. */
	xmlNodePtr lines_node = xmlNewChild(root_node, NULL, "lines", NULL);
	for(int h = 0; h < 24; h++) {
		xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, "hour", NULL);
		for(int q = 0; q < 4; q++) {
			char s[0xf];
			sprintf(s, "%d", channel->hours[h*4 + q]);
			xmlNewChild(hour_node, NULL, "quarter", s);
		}
	}

	/* Add users. */
	xmlNodePtr users_node = xmlNewChild(root_node, NULL, "users", NULL);
	for(int u = 0; u < USERS_MAX; u++) {
		struct user_t *user = &users[u];
		/* Iterate while we have a user (nick is non-NULL). */
		while(user && user->nick) {
			char s[0xf];
			xmlNodePtr user_node = xmlNewChild(users_node, NULL, "user", NULL);

			xmlNewChild(user_node, NULL, "nick", user->nick);
			sprintf(s, "%d", user->words);
			xmlNewChild(user_node, NULL, "words", s);

			/* Add lines for this user. */
			xmlNodePtr lines_node = xmlNewChild(users_node, NULL, "lines", NULL);
			for(int h = 0; h < 24; h++) {
				xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, "hour", NULL);
				for(int q = 0; q < 4; q++) {
					sprintf(s, "%d", user->lines[h*4 + q]);
					xmlNewChild(hour_node, NULL, "quarter", s);
				}
			}
			user = user->next;
		}
	}

	xmlSaveCtxtPtr save_context = xmlSaveToFilename(channel->xmlpath, "UTF-8", 0);
	if(!save_context) {
		xmlErrorPtr error = xmlGetLastError();
		fprintf(stderr, "Failed to save XML data: %s\n", (error ? error->message : "unknown error"));
		xmlFreeDoc(doc);
		return 0;
	}
	xmlSaveDoc(save_context, doc);
	xmlSaveClose(save_context);
	xmlFreeDoc(doc);
	return 1;
}