From 3e485113185bf230ee408e921de958dfa804fc0e Mon Sep 17 00:00:00 2001 From: Miika Turkia Date: Sat, 19 Oct 2013 20:35:36 +0300 Subject: Export dives in UDDF format Implement exporting in UDDF format as was done in Gtk version. File menu exports all the dives, right click on selection exports the selected ones. Signed-off-by: Miika Turkia Signed-off-by: Dirk Hohndel --- save-xml.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'save-xml.c') diff --git a/save-xml.c b/save-xml.c index 6d95cb4f7..ab81c1590 100644 --- a/save-xml.c +++ b/save-xml.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "dive.h" #include "device.h" @@ -601,3 +602,62 @@ void save_dives_logic(const char *filename, const bool select_only) fprintf(f, "\n\n"); fclose(f); } + +void export_dives_uddf(const char *filename, const bool selected) +{ + FILE *f; + size_t streamsize; + char *membuf; + xmlDoc *doc; + xsltStylesheetPtr xslt = NULL; + xmlDoc *transformed; + + if (!filename) + return; + + /* Save XML to file and convert it into a memory buffer */ + save_dives_logic(filename, selected); + f = fopen(filename, "r"); + fseek(f, 0, SEEK_END); + streamsize = ftell(f); + rewind(f); + + membuf = malloc(streamsize + 1); + if (!membuf || !fread(membuf, streamsize, 1, f)) { + fprintf(stderr, "Failed to read memory buffer\n"); + return; + } + membuf[streamsize] = 0; + fclose(f); + unlink(filename); + + /* + * Parse the memory buffer into XML document and + * transform it to UDDF format, finally dumping + * the XML into a character buffer. + */ + doc = xmlReadMemory(membuf, strlen(membuf), "divelog", NULL, 0); + if (!doc) { + fprintf(stderr, "Failed to read XML memory\n"); + return; + } + free((void *)membuf); + + /* Convert to UDDF format */ + xslt = get_stylesheet("uddf-export.xslt"); + + if (!xslt) { + fprintf(stderr, "Failed to open UDDF conversion stylesheet\n"); + return; + } + transformed = xsltApplyStylesheet(xslt, doc, NULL); + xsltFreeStylesheet(xslt); + xmlFreeDoc(doc); + + /* Write the transformed XML to file */ + f = fopen(filename, "w"); + xmlDocFormatDump(f, transformed, 1); + xmlFreeDoc(transformed); + + fclose(f); +} -- cgit v1.2.3-70-g09d2