From 9f187b46ae761e8275362d6c1533e9fe79028c7b Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 1 Nov 2009 05:24:54 +0000 Subject: Improved memory management using APR memory pools, changed frame allocation in eval library to support forward references and fixed memory leak in XML parsing code. Also simplified a bit the printing of lists to make them easier to read. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@831639 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/sca/kernel/xml.hpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'cpp/sca/kernel/xml.hpp') diff --git a/cpp/sca/kernel/xml.hpp b/cpp/sca/kernel/xml.hpp index afc3f5cc38..b7611b8477 100644 --- a/cpp/sca/kernel/xml.hpp +++ b/cpp/sca/kernel/xml.hpp @@ -39,6 +39,22 @@ namespace tuscany { +/** + * Initializes the libxml2 library. + */ +class XMLParser { +public: + XMLParser() { + xmlInitParser(); + } + + ~XMLParser() { + xmlCleanupParser(); + } +}; + +XMLParser xmlParser; + /** * Encapsulates a libxml2 xmlTextReader and its state. */ @@ -54,6 +70,7 @@ public: } ~XMLReader() { + xmlTextReaderClose(xml); xmlFreeTextReader(xml); } @@ -298,9 +315,9 @@ const failable write(const list& l, const xmlTextWrite */ template class XMLWriteContext { public: - XMLWriteContext(const lambda& reduce, const R& accum) : reduce(reduce), accum(accum) { + XMLWriteContext(const lambda& reduce, const R& accum) : reduce(reduce), accum(accum) { } - const lambda reduce; + const lambda reduce; R accum; }; @@ -309,36 +326,35 @@ public: */ template int writeCallback(void *context, const char* buffer, int len) { XMLWriteContext& cx = *static_cast*>(context); - cx.accum = cx.reduce(cx.accum, std::string(buffer, len)); + cx.accum = cx.reduce(std::string(buffer, len), cx.accum); return len; } /** * Convert a list of values to an XML document. */ -template const failable writeXML(const lambda& reduce, const R& initial, const list& l) { +template const failable writeXML(const lambda& reduce, const R& initial, const list& l) { XMLWriteContext cx(reduce, initial); xmlOutputBufferPtr out = xmlOutputBufferCreateIO(writeCallback, NULL, &cx, NULL); + if (out == NULL) + return std::string("xmlOutputBufferCreateIO failed"); xmlTextWriterPtr xml = xmlNewTextWriter(out); if (xml == NULL) return std::string("xmlNewTextWriter failed"); const failable w = write(l, xml); - if (!hasValue(w)) + xmlFreeTextWriter(xml); + if (!hasValue(w)) { return std::string(w); - + } return cx.accum; } /** * Convert a list of values to a list of strings representing an XML document. */ -const list writeXMLList(const list& listSoFar, const std::string& s) { - return cons(s, listSoFar); -} - const failable, std::string> writeXML(const list& l) { - const failable, std::string> ls = writeXML >(writeXMLList, list(), l); + const failable, std::string> ls = writeXML >(rcons, list(), l); if (!hasValue(ls)) return ls; return reverse(list(ls)); -- cgit v1.2.3