diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-06-02 02:24:55 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-06-02 02:24:55 +0200 |
commit | 72965bda2ea93d56e8e0b443826304192e22b5cc (patch) | |
tree | 5ce0a5f542f9c0ffaead0be901af12082f0d0c27 | |
parent | 6dd916eaedfd23a93ba7aa2e2b39511e7e907dec (diff) |
Attempting to use text writer instead of building an XML tree and writing it.
-rw-r--r-- | export_xml.c | 126 |
1 files changed, 98 insertions, 28 deletions
diff --git a/export_xml.c b/export_xml.c index 7bc9c3d..daebe14 100644 --- a/export_xml.c +++ b/export_xml.c @@ -1,37 +1,50 @@ -#include <libxml/tree.h> -#include <libxml/xmlsave.h> +#include <libxml/xmlwriter.h> #include <string.h> #include "export_xml.h" #include "word.h" -#if !defined(LIBXML_TREE_ENABLED) -#error "libxml must be compiled with tree support" +#if !defined(LIBXML_WRITER_ENABLED) +#error "libxml must be compiled with text writer support" #endif int export_xml(struct channel_t *channel, struct user_t *users) { /* Create document and set root node. */ - xmlDocPtr doc = xmlNewDoc((const xmlChar*)"1.0"); + /*xmlDocPtr doc = xmlNewDoc((const xmlChar*)"1.0"); xmlNodePtr root_node = xmlNewNode(NULL, (const xmlChar*)"channel"); - xmlDocSetRootElement(doc, root_node); + xmlDocSetRootElement(doc, root_node);*/ + xmlTextWriterPtr writer = xmlNewTextWriterFilename(channel->xmlpath, 0); + xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"channel"); /* Set the channel name. */ - xmlNewChild(root_node, NULL, (const xmlChar*)"name", (const xmlChar*)channel->name); + //xmlNewChild(root_node, NULL, (const xmlChar*)"name", (const xmlChar*)channel->name); + xmlTextWriterStartElement(writer, (const xmlChar*)"name"); + xmlTextWriterWriteString(writer, (const xmlChar*)channel->name); + xmlTextWriterEndElement(writer); /* Add lines. */ - xmlNodePtr lines_node = xmlNewChild(root_node, NULL, (const xmlChar*)"lines", NULL); + //xmlNodePtr lines_node = xmlNewChild(root_node, NULL, (const xmlChar*)"lines", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"lines"); for(int h = 0; h < 24; h++) { - xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, (const xmlChar*)"hour", NULL); + //xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, (const xmlChar*)"hour", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"hour"); for(int q = 0; q < 4; q++) { - char s[0xf]; + /*char s[0xf]; snprintf(s, 0xf, "%lu", channel->hours[h*4 + q]); - xmlNewChild(hour_node, NULL, (const xmlChar*)"quarter", (const xmlChar*)s); + xmlNewChild(hour_node, NULL, (const xmlChar*)"quarter", (const xmlChar*)s);*/ + xmlTextWriterStartElement(writer, (const xmlChar*)"quarter"); + xmlTextWriterWriteFormatString(writer, "%lu", channel->hours[h*4 + q]); + xmlTextWriterEndElement(writer); } + xmlTextWriterEndElement(writer); } + xmlTextWriterEndElement(writer); /* Add users. */ - xmlNodePtr users_node = xmlNewChild(root_node, NULL, (const xmlChar*)"users", NULL); + //xmlNodePtr users_node = xmlNewChild(root_node, NULL, (const xmlChar*)"users", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"users"); for(int u = 0; u < USERS_MAX; u++) { struct user_t *user = &users[u]; /* Iterate while we have a user (nick is non-NULL). */ @@ -41,10 +54,39 @@ int export_xml(struct channel_t *channel, struct user_t *users) { user = user->next; continue; } - char s[0xf]; - xmlNodePtr user_node = xmlNewChild(users_node, NULL, (const xmlChar*)"user", NULL); + //char s[0xf]; + //xmlNodePtr user_node = xmlNewChild(users_node, NULL, (const xmlChar*)"user", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"user"); - xmlNewChild(user_node, NULL, (const xmlChar*)"nick", (xmlChar*)user->nick); + xmlTextWriterStartElement(writer, (const xmlChar*)"nick"); + xmlTextWriterWriteString(writer, (const xmlChar*)user->nick); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"words"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"characters"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"kicks"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"kicked"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"monolog_lines"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + xmlTextWriterStartElement(writer, (const xmlChar*)"monologs"); + xmlTextWriterWriteFormatString(writer, "%llu", users->words); + xmlTextWriterEndElement(writer); + + /*xmlNewChild(user_node, NULL, (const xmlChar*)"nick", (xmlChar*)user->nick); snprintf(s, 0xf, "%llu", user->words); xmlNewChild(user_node, NULL, (const xmlChar*)"words", (const xmlChar*)s); snprintf(s, 0xf, "%llu", user->characters); @@ -56,38 +98,60 @@ int export_xml(struct channel_t *channel, struct user_t *users) { snprintf(s, 0xf, "%llu", user->monolog_lines); xmlNewChild(user_node, NULL, (const xmlChar*)"monolog_lines", (const xmlChar*)s); snprintf(s, 0xf, "%llu", user->monologs); - xmlNewChild(user_node, NULL, (const xmlChar*)"monologs", (const xmlChar*)s); + xmlNewChild(user_node, NULL, (const xmlChar*)"monologs", (const xmlChar*)s);*/ /* Add lines for this user. */ - xmlNodePtr lines_node = xmlNewChild(user_node, NULL, (const xmlChar*)"lines", NULL); + //xmlNodePtr lines_node = xmlNewChild(user_node, NULL, (const xmlChar*)"lines", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"lines"); for(int h = 0; h < 24; h++) { - xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, (const xmlChar*)"hour", NULL); + //xmlNodePtr hour_node = xmlNewChild(lines_node, NULL, (const xmlChar*)"hour", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"hour"); for(int q = 0; q < 4; q++) { - snprintf(s, 0xf, "%lu", user->lines[h*4 + q]); - xmlNewChild(hour_node, NULL, (const xmlChar*)"quarter", (const xmlChar*)s); + //snprintf(s, 0xf, "%lu", user->lines[h*4 + q]); + //xmlNewChild(hour_node, NULL, (const xmlChar*)"quarter", (const xmlChar*)s); + xmlTextWriterStartElement(writer, (const xmlChar*)"quarter"); + xmlTextWriterWriteFormatString(writer, "%lu", channel->hours[h*4 + q]); + xmlTextWriterEndElement(writer); } + xmlTextWriterEndElement(writer); } + xmlTextWriterEndElement(writer); user = user->next; + + xmlTextWriterEndElement(writer); } } + xmlTextWriterEndElement(writer); /* Add words. */ - xmlNodePtr words_node = xmlNewChild(root_node, NULL, (const xmlChar*)"words", NULL); + //xmlNodePtr words_node = xmlNewChild(root_node, NULL, (const xmlChar*)"words", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"words"); for(int w = 0; w < WORDS_MAX; w++) { struct word_t *word = &words[w]; while(word && word->name) { - char s[0xf]; - xmlNodePtr word_node = xmlNewChild(words_node, NULL, (const xmlChar*)"word", NULL); + //char s[0xf]; + //xmlNodePtr word_node = xmlNewChild(words_node, NULL, (const xmlChar*)"word", NULL); + xmlTextWriterStartElement(writer, (const xmlChar*)"word"); + + //xmlNewChild(word_node, NULL, (const xmlChar*)"name", (const xmlChar*)word->name); + //snprintf(s, 0xf, "%llu", word->count); + //xmlNewChild(word_node, NULL, (const xmlChar*)"count", (const xmlChar*)s); + xmlTextWriterStartElement(writer, (const xmlChar*)"name"); + xmlTextWriterWriteString(writer, (const xmlChar*)word->name); + xmlTextWriterEndElement(writer); - xmlNewChild(word_node, NULL, (const xmlChar*)"name", (const xmlChar*)word->name); - snprintf(s, 0xf, "%llu", word->count); - xmlNewChild(word_node, NULL, (const xmlChar*)"count", (const xmlChar*)s); + xmlTextWriterStartElement(writer, (const xmlChar*)"name"); + xmlTextWriterWriteFormatString(writer, "%llu", word->count); + xmlTextWriterEndElement(writer); word = word->next; + + xmlTextWriterEndElement(writer); } } + xmlTextWriterEndElement(writer); - xmlSaveCtxtPtr save_context = xmlSaveToFilename(channel->xmlpath, "UTF-8", 0); + /*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")); @@ -96,6 +160,12 @@ int export_xml(struct channel_t *channel, struct user_t *users) { } xmlSaveDoc(save_context, doc); xmlSaveClose(save_context); - xmlFreeDoc(doc); + xmlFreeDoc(doc);*/ + + xmlTextWriterEndElement(writer); + + xmlTextWriterEndDocument(writer); + xmlFreeTextWriter(writer); + return 1; } |