test/test-automation/xml.c
author Markus Kauppila <markus.kauppila@gmail.com>
Tue, 21 Jun 2011 19:31:46 +0300
changeset 5705 b320f7a66ac4
child 5706 9da43acca81f
permissions -rw-r--r--
Creating flexible logging system which supports XML and plain text. Work under progress.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5705
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     1
/*
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     2
  Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     3
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     6
  arising from the use of this software.
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     7
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    10
  freely, subject to the following restrictions:
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    11
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    12
  1. The origin of this software must not be misrepresented; you must not
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    13
     claim that you wrote the original software. If you use this software
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    14
     in a product, an acknowledgment in the product documentation would be
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    15
     appreciated but is not required.
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    16
  2. Altered source versions must be plainly marked as such, and must not be
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    17
     misrepresented as being the original software.
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    18
  3. This notice may not be removed or altered from any source distribution.
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    19
*/
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    20
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    21
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    22
#include <stdio.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    23
//#include <stdlib.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    24
#include <string.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    25
//#include <stdarg.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    26
#include <assert.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    27
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    28
#include <SDL/SDL.h>
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    29
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    30
#include "xml.h"
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    31
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    32
/*! Points the function which handles the output */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    33
static LogOutputFp logger = 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    34
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    35
/*!
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    36
 * Defines structure used for "counting" open XML-tags
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    37
 */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    38
typedef struct TagList {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    39
	const char *tag;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    40
	struct TagList *next;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    41
} TagList;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    42
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    43
static TagList *openTags = NULL;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    44
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    45
/*!
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    46
 * Prepend the open tags list
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    47
 *
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    48
 * \return On error returns non-zero value, otherwise zero will returned
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    49
 */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    50
static int
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    51
AddOpenTag(const char *tag)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    52
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    53
	TagList *openTag = SDL_malloc(sizeof(TagList));
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    54
	if(openTag == NULL) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    55
		return 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    56
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    57
	memset(openTag, 0, sizeof(TagList));
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    58
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    59
	openTag->tag = tag; // Should be fine without malloc?
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    60
	openTag->next = openTags;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    61
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    62
	openTags = openTag;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    63
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    64
	return 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    65
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    66
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    67
/*!
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    68
 * Removes the first tag from the open tag list
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    69
 *
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    70
 * \return On error returns non-zero value, otherwise zero will returned
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    71
 */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    72
static int
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    73
RemoveOpenTag(const char *tag)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    74
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    75
	if(openTags == NULL) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    76
		return 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    77
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    78
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    79
	int retVal = 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    80
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    81
	// Tag should always be the same as previously opened tag
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    82
	// It prevents opening and ending tag mismatch
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    83
	if(SDL_strcmp(openTags->tag, tag) == 0) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    84
		TagList *openTag = openTags;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    85
		openTags  = openTags->next;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    86
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    87
		free(openTag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    88
	} else {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    89
		printf("Debug | RemoveOpenTag(): open/end tag mismatch");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    90
		retVal = 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    91
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    92
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    93
	return retVal;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    94
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    95
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    96
/*!
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    97
 * Debug function. Prints the contents of the open tags list.
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    98
 */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
    99
static void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   100
PrintOpenTags()
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   101
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   102
	printf("\nOpen tags:\n");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   103
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   104
	TagList *openTag = NULL;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   105
	for(openTag = openTags; openTag; openTag = openTag->next) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   106
		printf("\ttag: %s\n", openTag->tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   107
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   108
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   109
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   110
/*
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   111
===================
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   112
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   113
	XML
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   114
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   115
===================
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   116
*/
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   117
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   118
static int has_open_element = 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   119
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   120
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   121
XMLOpenDocument(const char *rootTag, LogOutputFp log)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   122
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   123
	assert(log != NULL);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   124
	logger = log;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   125
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   126
	logger("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   127
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   128
	size_t size = SDL_strlen(rootTag) + 3 + 1; /* one extra for '\0', '<' and '>' */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   129
	char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   130
	snprintf(buffer, size, "%s%s%s", "<", rootTag, ">");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   131
	logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   132
	SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   133
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   134
	// add open tag
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   135
	AddOpenTag(rootTag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   136
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   137
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   138
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   139
XMLCloseDocument() {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   140
	// Close the open tags with proper nesting
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   141
	TagList *openTag = openTags;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   142
	while(openTag) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   143
		TagList *temp = openTag->next;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   144
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   145
		size_t size = SDL_strlen(openTag->tag) + 4 + 1; /* one extra for '\0', '<', '/' and '>' */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   146
		char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   147
		snprintf(buffer, size, "%s%s%s", "</", openTag->tag, ">");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   148
		logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   149
		SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   150
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   151
		RemoveOpenTag(openTag->tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   152
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   153
		openTag = temp;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   154
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   155
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   156
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   157
static const char *currentTag = NULL;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   158
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   159
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   160
XMLOpenElement(const char *tag)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   161
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   162
	size_t size = SDL_strlen(tag) + 2 + 1; /* one extra for '\0', '<' */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   163
	char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   164
	snprintf(buffer, size, "%s%s%s", "<", tag, ">");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   165
	logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   166
	SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   167
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   168
	currentTag = tag;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   169
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   170
	has_open_element = 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   171
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   172
	AddOpenTag(tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   173
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   174
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   175
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   176
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   177
XMLOpenElementWithAttribute(const char *tag, Attribute attribute)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   178
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   179
	size_t size = SDL_strlen(tag) + 2 + 1; /* one extra for '\0', '<' */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   180
	char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   181
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   182
	snprintf(buffer,  size, "%s%s", "<", tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   183
	logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   184
	SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   185
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   186
	currentTag = tag;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   187
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   188
	has_open_element = 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   189
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   190
	AddOpenTag(tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   191
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   192
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   193
//! \todo make this static and remove from interface?
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   194
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   195
XMLAddAttribute(const char *attribute, const char *value)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   196
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   197
	// Requires open element
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   198
	if(has_open_element == 0) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   199
		return ;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   200
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   201
	size_t attributeSize = SDL_strlen(attribute);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   202
	size_t valueSize = SDL_strlen(value);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   203
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   204
	size_t size = 1 + attributeSize + 3 + valueSize + 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   205
	char *buffer = SDL_malloc(size); // 1 for '='
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   206
	snprintf(buffer, size, " %s%s\"%s\"", attribute, "=", value);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   207
	logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   208
	SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   209
}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   210
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   211
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   212
XMLAddContent(const char *content)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   213
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   214
	size_t size = SDL_strlen(content) + 1 + 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   215
	char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   216
	snprintf(buffer, size, "%s", content);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   217
	logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   218
	SDL_free(buffer);}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   219
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   220
void
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   221
XMLCloseElement(const char *tag)
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   222
{
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   223
	// Close the open tags with proper nesting. Closes tags until it finds
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   224
	// the given tag which is the last tag that will be closed
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   225
	TagList *openTag = openTags;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   226
	while(openTag) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   227
		TagList *temp = openTag->next;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   228
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   229
		size_t size = SDL_strlen(openTag->tag) + 4 + 1; /* one extra for '\0', '<', '/' and '>' */
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   230
		char *buffer = SDL_malloc(size);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   231
		snprintf(buffer, size, "%s%s%s", "</", openTag->tag, ">");
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   232
		logger(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   233
		SDL_free(buffer);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   234
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   235
		const int openTagSize = SDL_strlen(openTag->tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   236
		const int tagSize = SDL_strlen(tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   237
		const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   238
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   239
		int breakOut = 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   240
		if(SDL_strncmp(openTag->tag, tag, compSize) == 0) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   241
			breakOut = 1;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   242
		}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   243
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   244
		RemoveOpenTag(openTag->tag);
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   245
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   246
		openTag = temp;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   247
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   248
		if(breakOut) {
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   249
			break;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   250
		}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   251
	}
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   252
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   253
	has_open_element = 0;
b320f7a66ac4 Creating flexible logging system which supports XML and plain
Markus Kauppila <markus.kauppila@gmail.com>
parents:
diff changeset
   254
}