summaryrefslogtreecommitdiffstats
path: root/save-xml.c
diff options
context:
space:
mode:
authorGravatar Miika Turkia <miika.turkia@gmail.com>2013-10-19 20:35:36 +0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-10-20 15:26:02 +0200
commit3e485113185bf230ee408e921de958dfa804fc0e (patch)
tree4c388c92cb54e3b918844fdb154964f9efb3eb13 /save-xml.c
parentbdedf46e4c53e6b8e8134c4ab465b32ae1043e2e (diff)
downloadsubsurface-3e485113185bf230ee408e921de958dfa804fc0e.tar.gz
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 <miika.turkia@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'save-xml.c')
-rw-r--r--save-xml.c60
1 files changed, 60 insertions, 0 deletions
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 <stdlib.h>
#include <errno.h>
#include <time.h>
+#include <unistd.h>
#include "dive.h"
#include "device.h"
@@ -601,3 +602,62 @@ void save_dives_logic(const char *filename, const bool select_only)
fprintf(f, "</dives>\n</divelog>\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);
+}