XML component no longer outputs the generated XML.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Thu, 23 Jun 2011 22:00:03 +0300
changeset 5711 63487fb415ee
parent 5710 2f48255db3d8
child 5712 dabff57811e4
XML component no longer outputs the generated XML. Fixed a bunch of compiler warnings.
test/test-automation/plain_logger.c
test/test-automation/xml.c
test/test-automation/xml.h
test/test-automation/xml_logger.c
--- a/test/test-automation/plain_logger.c	Thu Jun 23 21:11:48 2011 +0300
+++ b/test/test-automation/plain_logger.c	Thu Jun 23 22:00:03 2011 +0300
@@ -53,7 +53,7 @@
        time_t eventTime)
 {
 	const char *result = (assertResult) ? "passed" : "failed";
-	printf("%s %s: %s\n", assertName, assertResult, assertMessage);
+	printf("%s %d: %s\n", assertName, assertResult, assertMessage);
 }
 
 void
--- a/test/test-automation/xml.c	Thu Jun 23 21:11:48 2011 +0300
+++ b/test/test-automation/xml.c	Thu Jun 23 22:00:03 2011 +0300
@@ -118,7 +118,7 @@
 	memset(buffer, 0, bufferSize);
 
 	// prevents the code doing a 'bus error'
-	char stringBuffer[bufferSize];
+	char *stringBuffer = SDL_malloc(bufferSize);
 	strncpy(stringBuffer, string, bufferSize);
 
 	// Ampersand (&) must be first, otherwise it'll mess up the other entities
@@ -170,40 +170,55 @@
 /*! Buffer for storing the xml element under construction */
 static char buffer[bufferSize];
 
-void
-XMLOpenDocument(const char *rootTag, LogOutputFp log)
+char *
+XMLOpenDocument(const char *rootTag)
 {
-	assert(log != NULL);
-	logger = log;
-
-	logger("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
+	const char *doctype = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
 
 	memset(buffer, 0, bufferSize);
 	snprintf(buffer, bufferSize, "<%s>", rootTag);
-	logger(buffer);
+	//logger(buffer);
 
 	AddOpenTag(rootTag);
 
 	root = rootTag; // it's fine, as long as rootTag points to static memory?
+
+	const int doctypeSize = SDL_strlen(doctype);
+	const int tagSize = SDL_strlen(buffer);
+
+	const int size = doctypeSize + tagSize + 1; // extra byte for '\0'
+	char *ret = SDL_malloc(size);
+	// copy doctype
+	strncpy(ret, doctype, doctypeSize);
+	// copy tag
+	strncpy(ret + doctypeSize, buffer, tagSize);
+	ret[size] = '\0';
+	return ret;
 }
 
-void
+char *
 XMLCloseDocument() {
-	XMLCloseElement(root);
+	return XMLCloseElement(root);
 }
 
-void
+char *
 XMLOpenElement(const char *tag)
 {
 	memset(buffer, 0, bufferSize);
 	snprintf(buffer, bufferSize, "<%s>", tag);
-	logger(buffer);
 
 	AddOpenTag(tag);
+
+	const int size = SDL_strlen(buffer);
+	char *ret = SDL_malloc(size + 1);
+	strncpy(ret, buffer, size);
+	ret[size] = '\0';
+
+	return ret;
 }
 
 
-void
+char *
 XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
 {
 	memset(buffer, 0, bufferSize);
@@ -212,21 +227,38 @@
 	logger(buffer);
 
 	AddOpenTag(tag);
+
+	const int size = SDL_strlen(buffer);
+	char *ret = SDL_malloc(size + 1);
+	strncpy(ret, buffer, size);
+	ret[size] = '\0';
+
+	return ret;
 }
 
-void
+char *
 XMLAddContent(const char *content)
 {
 	const char *escapedContent = EscapeString(content);
 
 	memset(buffer, 0, bufferSize);
 	snprintf(buffer, bufferSize, "%s", escapedContent);
-	logger(buffer);
+	SDL_free((char *)escapedContent);
+
+	const int size = SDL_strlen(buffer);
+	char *ret = SDL_malloc(size + 1);
+	strncpy(ret, buffer, size);
+	ret[size] = '\0';
+
+	return ret;
 }
 
-void
+char *
 XMLCloseElement(const char *tag)
 {
+	char *ret = SDL_malloc(bufferSize);
+	memset(ret, 0, bufferSize);
+
 	// Close the open tags with proper nesting. Closes tags until it finds
 	// the given tag which is the last tag that will be closed
 	TagList *openTag = openTags;
@@ -235,7 +267,10 @@
 
 		memset(buffer, 0, bufferSize);
 		snprintf(buffer, bufferSize, "</%s>", openTag->tag);
-		logger(buffer);
+
+		// \todo use strNcat
+		strcat(ret, buffer);
+		//logger(buffer);
 
 		const int openTagSize = SDL_strlen(openTag->tag);
 		const int tagSize = SDL_strlen(tag);
@@ -254,5 +289,7 @@
 			break;
 		}
 	}
+
+	return ret;
 }
 
--- a/test/test-automation/xml.h	Thu Jun 23 21:11:48 2011 +0300
+++ b/test/test-automation/xml.h	Thu Jun 23 22:00:03 2011 +0300
@@ -35,34 +35,35 @@
  * Creates header and start tag for root element.
  *
  * \param rootTag Root tag for the XML document
+ * \return The generated XML output
  */
-void XMLOpenDocument(const char *rootTag, LogOutputFp log);
+char *XMLOpenDocument(const char *rootTag);
 
 /*!
  * Closes the XML-document.
  * Creates end tag for root element and closes other open elements
  * with correct nesting.
  */
-void XMLCloseDocument();
+char *XMLCloseDocument();
 
 /*!
  *  Opens XML-element.
  *
  *  \param tag Element to be opened
  */
-void XMLOpenElement(const char *tag);
+char *XMLOpenElement(const char *tag);
 
 /*!
  * Opens XML-element with given attributes
  */
-void XMLOpenElementWithAttribute(const char *tag, Attribute *attribute);
+char *XMLOpenElementWithAttribute(const char *tag, Attribute *attribute);
 
 /*!
  * Add content to currently open element.
  *
  * \param content Content for the currently open element
  */
-void XMLAddContent(const char *content);
+char *XMLAddContent(const char *content);
 
 /*!
  * Closes previously opened element until tag given as parameter is met.
@@ -73,7 +74,7 @@
  *
  * \param tag Element to close
  */
-void XMLCloseElement(const char *tag);
+char *XMLCloseElement(const char *tag);
 
 #endif
 
--- a/test/test-automation/xml_logger.c	Thu Jun 23 21:11:48 2011 +0300
+++ b/test/test-automation/xml_logger.c	Thu Jun 23 22:00:03 2011 +0300
@@ -18,95 +18,165 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
+#include <SDL/SDL.h>
+
 #include "xml.h"
 #include "logger.h"
 
 #include "xml_logger.h"
 
+LogOutputFp logger;
+
 void
 XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
 {
-	//! \todo Giving outputFn to the function is awful, fix it
-	//! Make the outputting differently
-	XMLOpenDocument("testlog", outputFn);
+	logger = outputFn;
+
+	char *output = XMLOpenDocument("testlog");
+	logger(output);
+	SDL_free(output);
 
-	XMLOpenElement("parameters");
-	XMLAddContent(runnerParameters);
-	XMLCloseElement("parameters");
+	output = XMLOpenElement("parameters");
+	logger(output);
+	SDL_free(output);
+
+	output = XMLAddContent(runnerParameters);
+	logger(output);
+	SDL_free(output);
+
+	output = XMLCloseElement("parameters");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLRunEnded(int testCount, int suiteCount, int testPassCount, int testFailCount,
             time_t endTime, time_t totalRuntime)
 {
-	XMLCloseDocument("testlog");
+	char *output = XMLCloseDocument("testlog");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLSuiteStarted(const char *suiteName, time_t eventTime)
 {
-	XMLOpenElement("suite");
+	char *output = XMLOpenElement("suite");
+	logger(output);
+	SDL_free(output);
 
-	XMLOpenElement("eventTime");
+	output = XMLOpenElement("eventTime");
+	logger(output);
+	SDL_free(output);
+
 	//XMLAddContent(evenTime);
-	XMLCloseElement("eventTime");
+	output = XMLCloseElement("eventTime");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
            double endTime, time_t totalRuntime)
 {
-	XMLCloseElement("suite");
+	char *output = XMLCloseElement("suite");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLTestStarted(const char *testName, const char *suiteName, const char *testDescription, time_t startTime)
 {
-	XMLOpenElement("test");
+	char * output = XMLOpenElement("test");
+	logger(output);
+	SDL_free(output);
 
-	Attribute attribute = {"test", "value"};
 
-	XMLOpenElementWithAttribute("name", &attribute);
-	XMLAddContent(testName);
-	XMLCloseElement("name");
+	//Attribute attribute = {"test", "value"};
+	//XMLOpenElementWithAttribute("name", &attribute);
+	output = XMLOpenElement("name");
+	logger(output);
+	SDL_free(output);
+
+	output = XMLAddContent(testName);
+	logger(output);
+	SDL_free(output);
+
+	output = XMLCloseElement("name");
+	logger(output);
+	SDL_free(output);
+
 
-	XMLOpenElement("description");
-	XMLAddContent(testDescription);
-	XMLCloseElement("description");
+	output = XMLOpenElement("description");
+	logger(output);
+	SDL_free(output);
+
+
+	output = XMLAddContent(testDescription);
+	logger(output);
+	SDL_free(output);
 
-	XMLOpenElement("starttime");
+	output = XMLCloseElement("description");
+	logger(output);
+	SDL_free(output);
+
+	output = XMLOpenElement("starttime");
+	logger(output);
+	SDL_free(output);
+
 	//XMLAddContent(startTime);
-	XMLCloseElement("starttime");
+	output = XMLCloseElement("starttime");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLTestEnded(const char *testName, const char *suiteName,
           int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
 {
-	XMLCloseElement("test");
+	char *output = XMLCloseElement("test");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
        time_t eventTime)
 {
-	XMLOpenElement("assert");
+	char *output = XMLOpenElement("assert");
+	logger(output);
+	SDL_free(output);
+
+	output = XMLOpenElement("result");
+	logger(output);
+	SDL_free(output);
 
-	XMLOpenElement("result");
-	XMLAddContent((assertResult) ? "pass" : "failure");
-	XMLOpenElement("result");
+	output = XMLAddContent((assertResult) ? "pass" : "failure");
+	logger(output);
+	SDL_free(output);
 
+	output = XMLOpenElement("result");
+	logger(output);
+	SDL_free(output);
 
-	XMLCloseElement("assert");
+	output = XMLCloseElement("assert");
+	logger(output);
+	SDL_free(output);
 }
 
 void
 XMLLog(const char *logMessage, time_t eventTime)
 {
-	XMLOpenElement("log");
+	char *output = XMLOpenElement("log");
+	logger(output);
+	SDL_free(output);
 
-	XMLAddContent(logMessage);
+	output = XMLAddContent(logMessage);
+	logger(output);
+	SDL_free(output);
 
-	XMLCloseElement("log");
+	output = XMLCloseElement("log");
+	logger(output);
+	SDL_free(output);
 }