Added case insensitivity to XML elements.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Fri, 24 Jun 2011 14:35:14 +0300
changeset 5712 dabff57811e4
parent 5711 63487fb415ee
child 5713 08edd9b3ec32
Added case insensitivity to XML elements.
test/test-automation/logger.c
test/test-automation/logger.h
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/logger.c	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/logger.c	Fri Jun 24 14:35:14 2011 +0300
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <stdarg.h>
 
+#include <SDL/SDL.h>
+
 #include "logger.h"
 #include "xml_logger.h"
 #include "plain_logger.h"
@@ -25,9 +27,15 @@
  * \return Possible error value (\todo)
  */
 int
-LogGenericOutput(const char *message)
+LogGenericOutput(const char *message, ...)
 {
-	fprintf(stderr, "%s\n", message);
+	va_list list;
+	va_start(list, message);
+
+	char buffer[1024];
+	SDL_vsnprintf(buffer, sizeof(buffer), message, list);
+
+	fprintf(stderr, "%s\n", buffer);
 	fflush(stderr);
 }
 
--- a/test/test-automation/logger.h	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/logger.h	Fri Jun 24 14:35:14 2011 +0300
@@ -24,7 +24,7 @@
 #include <time.h>
 
 // Function pointer to function which handles to output
-typedef int (*LogOutputFp)(const char *);
+typedef int (*LogOutputFp)(const char *, ...);
 
 
 /*!
--- a/test/test-automation/plain_logger.c	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/plain_logger.c	Fri Jun 24 14:35:14 2011 +0300
@@ -25,14 +25,14 @@
 void
 PlainSuiteStarted(const char *suiteName, time_t eventTime)
 {
-	printf("Executing tests in %s\n", suiteName);
+	logger("Executing tests in %s\n", suiteName);
 }
 
 void
 PlainSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
            double endTime, time_t totalRuntime)
 {
-	printf("Suite executed. %d passed, %d failed and %d skipped\n", testsPassed, testsFailed, testsSkipped);
+	logger("Suite executed. %d passed, %d failed and %d skipped\n", testsPassed, testsFailed, testsSkipped);
 }
 
 void
@@ -44,8 +44,8 @@
 PlainTestEnded(const char *testName, const char *suiteName,
           int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
 {
-	printf("Asserts:%d\n", numAsserts);
-	printf("%s: ok\n", testName);
+	logger("Asserts:%d\n", numAsserts);
+	logger("%s: ok\n", testName);
 }
 
 void
@@ -53,7 +53,7 @@
        time_t eventTime)
 {
 	const char *result = (assertResult) ? "passed" : "failed";
-	printf("%s %d: %s\n", assertName, assertResult, assertMessage);
+	logger("%s %d: %s\n", assertName, assertResult, assertMessage);
 }
 
 void
--- a/test/test-automation/xml.c	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/xml.c	Fri Jun 24 14:35:14 2011 +0300
@@ -53,7 +53,10 @@
 	}
 	memset(openTag, 0, sizeof(TagList));
 
-	openTag->tag = tag; // Should be fine without malloc?
+	const int tagSize = SDL_strlen(tag) + 1;
+	openTag->tag = SDL_malloc(tagSize);
+	strncpy(openTag->tag, tag, tagSize);
+
 	openTag->next = openTags;
 
 	openTags = openTag;
@@ -75,15 +78,27 @@
 
 	int retVal = 0;
 
+	const int size = SDL_strlen(tag);
+	char *tempTag = SDL_malloc(size);
+	strncpy(tempTag, tag, size);
+
 	// Tag should always be the same as previously opened tag
 	// It prevents opening and ending tag mismatch
-	if(SDL_strcmp(openTags->tag, tag) == 0) {
+	if(SDL_strcmp(tempTag, tag) == 0) {
 		TagList *openTag = openTags;
-		openTags  = openTags->next;
+		SDL_free(openTag->tag);
 
-		free(openTag);
+		/*
+		int counter = 0;
+		for(; counter < strlen(buffer); ++counter) {
+			buffer[counter] = tolower(buffer[counter]);
+		}
+		*/
+
+		openTags  = openTags->next;
+		SDL_free(openTag);
 	} else {
-		printf("Debug | RemoveOpenTag(): open/end tag mismatch");
+		//printf("Debug | xml.c:RemoveOpenTag(): open/end tag mismatch");
 		retVal = 1;
 	}
 
@@ -153,7 +168,30 @@
 	return stringBuffer;
 }
 
+/*! Turns all the characters of the given
+ * string to lowercase and returns the resulting string.
+ *
+ * \param string String to be converted
+ * \return Lower-case version of the given string
+ */
+char *
+ToLowerCase(char *string)
+{
+	const int size = SDL_strlen(string);
+	char *ret = SDL_malloc(size + 1);
+	strncpy(ret, string, size);
+	ret[size] = '\0';
 
+	// turn the tag to lower case for case-insensitive comparation
+	int counter = 0;
+	for(; counter < size; ++counter) {
+		ret[counter] = tolower(ret[counter]);
+	}
+
+	// printf("Debug: %s == %s\n", string, ret);
+
+	return ret;
+}
 
 /*
 ===================
@@ -177,7 +215,6 @@
 
 	memset(buffer, 0, bufferSize);
 	snprintf(buffer, bufferSize, "<%s>", rootTag);
-	//logger(buffer);
 
 	AddOpenTag(rootTag);
 
@@ -217,25 +254,6 @@
 	return ret;
 }
 
-
-char *
-XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
-{
-	memset(buffer, 0, bufferSize);
-	snprintf(buffer, bufferSize, "<%s %s='%s'>", tag,
-			attribute->attribute, attribute->value);
-	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;
-}
-
 char *
 XMLAddContent(const char *content)
 {
@@ -265,21 +283,28 @@
 	while(openTag) {
 		TagList *temp = openTag->next;
 
+		char *lowOpenTag = ToLowerCase(openTag->tag);
+		char *lowTag = ToLowerCase(tag);
+
+		const int openTagSize = SDL_strlen(lowOpenTag);
+		const int tagSize = SDL_strlen(lowTag);
+		const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize;
+
 		memset(buffer, 0, bufferSize);
-		snprintf(buffer, bufferSize, "</%s>", openTag->tag);
+
+		int breakOut = 0;
+		if(SDL_strncmp(lowOpenTag, lowTag, compSize) == 0) {
+			breakOut = 1;
+			snprintf(buffer, bufferSize, "</%s>", tag);
+		} else {
+			snprintf(buffer, bufferSize, "</%s>", openTag->tag);
+		}
+
+		SDL_free(lowOpenTag);
+		SDL_free(lowTag);
 
 		// \todo use strNcat
 		strcat(ret, buffer);
-		//logger(buffer);
-
-		const int openTagSize = SDL_strlen(openTag->tag);
-		const int tagSize = SDL_strlen(tag);
-		const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize;
-
-		int breakOut = 0;
-		if(SDL_strncmp(openTag->tag, tag, compSize) == 0) {
-			breakOut = 1;
-		}
 
 		RemoveOpenTag(openTag->tag);
 
--- a/test/test-automation/xml.h	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/xml.h	Fri Jun 24 14:35:14 2011 +0300
@@ -54,11 +54,6 @@
 char *XMLOpenElement(const char *tag);
 
 /*!
- * Opens XML-element with given attributes
- */
-char *XMLOpenElementWithAttribute(const char *tag, Attribute *attribute);
-
-/*!
  * Add content to currently open element.
  *
  * \param content Content for the currently open element
--- a/test/test-automation/xml_logger.c	Thu Jun 23 22:00:03 2011 +0300
+++ b/test/test-automation/xml_logger.c	Fri Jun 24 14:35:14 2011 +0300
@@ -32,11 +32,11 @@
 {
 	logger = outputFn;
 
-	char *output = XMLOpenDocument("testlog");
+	char *output = XMLOpenDocument("teSTtlog");
 	logger(output);
 	SDL_free(output);
 
-	output = XMLOpenElement("parameters");
+	output = XMLOpenElement("paRameters");
 	logger(output);
 	SDL_free(output);
 
@@ -44,7 +44,7 @@
 	logger(output);
 	SDL_free(output);
 
-	output = XMLCloseElement("parameters");
+	output = XMLCloseElement("Parameters");
 	logger(output);
 	SDL_free(output);
 }
@@ -53,7 +53,7 @@
 XMLRunEnded(int testCount, int suiteCount, int testPassCount, int testFailCount,
             time_t endTime, time_t totalRuntime)
 {
-	char *output = XMLCloseDocument("testlog");
+	char *output = XMLCloseDocument("testlOg");
 	logger(output);
 	SDL_free(output);
 }
@@ -65,12 +65,12 @@
 	logger(output);
 	SDL_free(output);
 
-	output = XMLOpenElement("eventTime");
+	output = XMLOpenElement("EVENTTime");
 	logger(output);
 	SDL_free(output);
 
 	//XMLAddContent(evenTime);
-	output = XMLCloseElement("eventTime");
+	output = XMLCloseElement("eventTIME");
 	logger(output);
 	SDL_free(output);
 }
@@ -91,7 +91,6 @@
 	logger(output);
 	SDL_free(output);
 
-
 	//Attribute attribute = {"test", "value"};
 	//XMLOpenElementWithAttribute("name", &attribute);
 	output = XMLOpenElement("name");
@@ -111,7 +110,6 @@
 	logger(output);
 	SDL_free(output);
 
-
 	output = XMLAddContent(testDescription);
 	logger(output);
 	SDL_free(output);