[PATCH 4/9] Hoist the getting of the XSLT style-sheet out of the loop

Thiago Macieira thiago at macieira.org
Wed Dec 11 17:56:30 UTC 2013


We don't need to get it for every file, since it's the same. This also
avoids leaking temporary files and memory if the stylesheet cannot be
found.

Signed-off-by: Thiago Macieira <thiago at macieira.org>
---
 qt-ui/subsurfacewebservices.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index cdf1f49..6be63fb 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -118,6 +118,12 @@ static char *prepare_dives_for_divelogs(const bool selected)
 	struct zip_source *s[dive_table.nr];
 	struct zip *zip;
 
+	xslt = get_stylesheet("divelogs-export.xslt");
+	if (!xslt) {
+		qDebug() << errPrefix << "missing stylesheet";
+		return NULL;
+	}
+
 	/* generate a random filename and create/open that file with zip_open */
 	QString tempfileQ = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
 	tempfile = strdup(tempfileQ.toLocal8Bit().data());
@@ -166,15 +172,7 @@ static char *prepare_dives_for_divelogs(const bool selected)
 			return NULL;
 		}
 		free((void *)membuf);
-		// this call is overriding our local variable tempfile! not a good sign!
-		xslt = get_stylesheet("divelogs-export.xslt");
-		if (!xslt) {
-			qDebug() << errPrefix << "missing stylesheet";
-			free((void *)tempfile);
-			return NULL;
-		}
 		transformed = xsltApplyStylesheet(xslt, doc, NULL);
-		xsltFreeStylesheet(xslt);
 		xmlDocDumpMemory(transformed, (xmlChar **) &membuf, (int *)&streamsize);
 		xmlFreeDoc(doc);
 		xmlFreeDoc(transformed);
@@ -190,6 +188,7 @@ static char *prepare_dives_for_divelogs(const bool selected)
 		}
 	}
 	zip_close(zip);
+	xsltFreeStylesheet(xslt);
 	return tempfile;
 }
 
-- 
1.7.11.7



More information about the subsurface mailing list