Commit 2dd346fa authored by Markus Kauppila's avatar Markus Kauppila

Changed XML-generator to use static buffer instead of dynamically

allocated memory.
parent a3b21d2e
...@@ -114,6 +114,11 @@ PrintOpenTags() ...@@ -114,6 +114,11 @@ PrintOpenTags()
static const char *root; static const char *root;
/*! Size for xml element buffer */
#define bufferSize 1024
/*! Buffer for storing the xml element under construction */
static char buffer[bufferSize];
void void
XMLOpenDocument(const char *rootTag, LogOutputFp log) XMLOpenDocument(const char *rootTag, LogOutputFp log)
{ {
...@@ -122,11 +127,9 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log) ...@@ -122,11 +127,9 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log)
logger("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); logger("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
size_t size = SDL_strlen(rootTag) + 3 + 1; /* one extra for '\0', '<' and '>' */ memset(buffer, 0, bufferSize);
char *buffer = SDL_malloc(size); snprintf(buffer, bufferSize, "<%s>", rootTag);
snprintf(buffer, size, "<%s>", rootTag);
logger(buffer); logger(buffer);
SDL_free(buffer);
// add open tag // add open tag
AddOpenTag(rootTag); AddOpenTag(rootTag);
...@@ -142,11 +145,9 @@ XMLCloseDocument() { ...@@ -142,11 +145,9 @@ XMLCloseDocument() {
void void
XMLOpenElement(const char *tag) XMLOpenElement(const char *tag)
{ {
size_t size = SDL_strlen(tag) + 2 + 1; /* one extra for '\0', '<' */ memset(buffer, 0, bufferSize);
char *buffer = SDL_malloc(size); snprintf(buffer, bufferSize, "<%s>", tag);
snprintf(buffer, size, "<%s>", tag);
logger(buffer); logger(buffer);
SDL_free(buffer);
AddOpenTag(tag); AddOpenTag(tag);
} }
...@@ -155,13 +156,9 @@ XMLOpenElement(const char *tag) ...@@ -155,13 +156,9 @@ XMLOpenElement(const char *tag)
void void
XMLOpenElementWithAttribute(const char *tag, Attribute *attribute) XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
{ {
const int bufferSize = 1024;
char buffer[bufferSize];
memset(buffer, 0, bufferSize); memset(buffer, 0, bufferSize);
snprintf(buffer, bufferSize, "<%s %s='%s'>", tag, snprintf(buffer, bufferSize, "<%s %s='%s'>", tag,
attribute->attribute, attribute->value); attribute->attribute, attribute->value);
logger(buffer); logger(buffer);
AddOpenTag(tag); AddOpenTag(tag);
...@@ -170,11 +167,10 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute) ...@@ -170,11 +167,10 @@ XMLOpenElementWithAttribute(const char *tag, Attribute *attribute)
void void
XMLAddContent(const char *content) XMLAddContent(const char *content)
{ {
size_t size = SDL_strlen(content) + 1 + 1; memset(buffer, 0, bufferSize);
char *buffer = SDL_malloc(size); snprintf(buffer, bufferSize, "%s", content);
snprintf(buffer, size, "%s", content);
logger(buffer); logger(buffer);
SDL_free(buffer);} }
void void
XMLCloseElement(const char *tag) XMLCloseElement(const char *tag)
...@@ -185,11 +181,9 @@ XMLCloseElement(const char *tag) ...@@ -185,11 +181,9 @@ XMLCloseElement(const char *tag)
while(openTag) { while(openTag) {
TagList *temp = openTag->next; TagList *temp = openTag->next;
size_t size = SDL_strlen(openTag->tag) + 4 + 1; /* one extra for '\0', '<', '/' and '>' */ memset(buffer, 0, bufferSize);
char *buffer = SDL_malloc(size); snprintf(buffer, bufferSize, "<%s>", openTag->tag);
snprintf(buffer, size, "<%s>", openTag->tag);
logger(buffer); logger(buffer);
SDL_free(buffer);
const int openTagSize = SDL_strlen(openTag->tag); const int openTagSize = SDL_strlen(openTag->tag);
const int tagSize = SDL_strlen(tag); const int tagSize = SDL_strlen(tag);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment