Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
376 lines (297 loc) · 9.42 KB

File metadata and controls

376 lines (297 loc) · 9.42 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <SDL/SDL.h>
#include "xml.h"
#include "logger_helpers.h"
/*! Size for xml element buffer */
#define bufferSize 1024
/*! Buffer for storing the xml element under construction */
static char buffer[bufferSize];
/*! Pointer to XML root element's tag */
static const char *root;
/*!
* Defines structure used for "counting" open XML-tags
*/
typedef struct TagList {
May 18, 2013
May 18, 2013
42
43
const char *tag;
struct TagList *next;
44
45
46
47
48
49
50
51
52
53
54
55
} TagList;
static TagList *openTags = NULL;
/*!
* Prepend the open tags list
*
* \return On error returns non-zero value, otherwise zero will returned
*/
static int
AddOpenTag(const char *tag)
{
May 18, 2013
May 18, 2013
56
57
58
59
60
TagList *openTag = SDL_malloc(sizeof(TagList));
if(openTag == NULL) {
return 1;
}
memset(openTag, 0, sizeof(TagList));
May 18, 2013
May 18, 2013
62
63
64
65
66
67
const int tagSize = SDL_strlen(tag) + 1;
openTag->tag = SDL_malloc(tagSize);
if(openTag->tag == NULL) {
SDL_free(openTag);
return 1;
}
May 18, 2013
May 18, 2013
69
strncpy((char *)openTag->tag, (char *)tag, tagSize);
May 18, 2013
May 18, 2013
71
openTag->next = openTags;
May 18, 2013
May 18, 2013
73
openTags = openTag;
May 18, 2013
May 18, 2013
75
return 0;
76
77
78
79
80
81
82
83
84
85
}
/*!
* Removes the first tag from the open tag list
*
* \return On error returns non-zero value, otherwise zero will returned
*/
static int
RemoveOpenTag(const char *tag)
{
May 18, 2013
May 18, 2013
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if(openTags == NULL || ValidateString(tag) == 0) {
return 1;
}
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_strncmp(tempTag, tag, size) == 0) {
TagList *openTag = openTags;
SDL_free((char *)openTag->tag);
openTags = openTags->next;
SDL_free(openTag);
} else {
//printf("Debug | xml.c:RemoveOpenTag(): open/end tag mismatch");
retVal = 1;
}
return retVal;
110
111
112
113
114
115
116
117
}
/*!
* Debug function. Prints the contents of the open tags list.
*/
static void
PrintOpenTags()
{
May 18, 2013
May 18, 2013
118
printf("\nOpen tags:\n");
May 18, 2013
May 18, 2013
120
121
122
123
TagList *openTag = NULL;
for(openTag = openTags; openTag; openTag = openTag->next) {
printf("\ttag: %s\n", openTag->tag);
}
124
125
126
127
128
129
130
131
132
133
134
135
136
}
/*!
* Converts the special characters ', ", <, >, and & to
* corresponding entities: &apos; &quot; &lt; &gt; and &amp;
*
* \param string String to be escaped
* \return Newly allocated escaped string
*/
const char *
EscapeString(const char *string)
{
May 18, 2013
May 18, 2013
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Calculate the size of the escaped string
int totalSize = 0;
const int maxCount = SDL_strlen(string);
int counter = 0;
for(; counter < maxCount; ++counter) {
char character = string[counter];
switch(character) {
case '&': totalSize += 5; //SDL_strlen("&amp;");
break;
case '\'': totalSize += 6; //SDL_strlen("&apos;");
break;
case '"': totalSize += 6; //SDL_strlen("&quot;");
break;
case '<': totalSize += 4; //SDL_strlen("&lt;");
break;
case '>': totalSize += 4; //SDL_strlen("&gt;");
break;
default:
totalSize += 1;
break;
}
}
totalSize += 1; // for '\0'
char *retBuffer = SDL_malloc(totalSize * sizeof(char));
if(retBuffer == NULL) {
return NULL;
}
// escape the string
char *curRetBuffer = retBuffer;
const char *curString = string;
char character = *curString;
while( (character = *curString++) ) {
switch(character) {
case '&':
memcpy((void *)curRetBuffer, (void *)"&amp;", 5);
curRetBuffer += 5;
break;
case '\'':
memcpy((void *)curRetBuffer, (void *)"&apos;", 6);
curRetBuffer += 6;
break;
case '"':
memcpy((void *)curRetBuffer, (void *)"&quot;", 6);
curRetBuffer += 6;
break;
case '<':
memcpy((void *)curRetBuffer, (void *)"&lt;", 4);
curRetBuffer += 4;
break;
case '>':
memcpy((void *)curRetBuffer, (void *)"&gt;", 4);
curRetBuffer += 4;
break;
default:
*curRetBuffer = character;
curRetBuffer += 1;
break;
}
}
*curRetBuffer = '\0';
return retBuffer;
207
208
209
210
211
212
}
/*
===================
May 18, 2013
May 18, 2013
213
Functions to handle creation of XML elements
214
215
216
217
218
219
220
===================
*/
char *
XMLOpenDocument(const char *rootTag, const char *xslStyle)
{
May 18, 2013
May 18, 2013
221
const char *doctype = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
May 18, 2013
May 18, 2013
223
224
225
226
227
//! \todo refactor this mess
char *style = NULL;
if(xslStyle) {
const char *styleStart = "<?xml-stylesheet type=\"text/xsl\" href=\"";
const char *styleEnd = "\"?>\n";
May 18, 2013
May 18, 2013
229
230
231
const int sizeStyleStart = SDL_strlen(styleStart);
const int sizeStyleEnd = SDL_strlen(styleEnd);
const int sizeStyleSheetName = SDL_strlen(xslStyle);
May 18, 2013
May 18, 2013
233
234
235
236
237
const int tempSize = sizeStyleStart + sizeStyleEnd + sizeStyleSheetName + 1;
style = SDL_malloc(tempSize);
memset(style, 0, tempSize);
SDL_snprintf(style, tempSize, "%s%s%s", styleStart, xslStyle, styleEnd);
}
May 18, 2013
May 18, 2013
239
240
memset(buffer, 0, bufferSize);
SDL_snprintf(buffer, bufferSize, "<%s>", rootTag);
May 18, 2013
May 18, 2013
242
AddOpenTag(rootTag);
May 18, 2013
May 18, 2013
244
root = rootTag; // it's fine, as long as rootTag points to static memory?
May 18, 2013
May 18, 2013
246
247
248
249
250
char *retBuf = NULL;
if(xslStyle) {
const int doctypeSize = SDL_strlen(doctype);
const int styleSize = SDL_strlen(style);
const int tagSize = SDL_strlen(buffer);
May 18, 2013
May 18, 2013
252
253
const int size = doctypeSize + styleSize + tagSize + 1; // extra byte for '\0'
retBuf = SDL_malloc(size);
May 18, 2013
May 18, 2013
255
SDL_snprintf(retBuf, size, "%s%s%s", doctype, style, buffer);
May 18, 2013
May 18, 2013
257
258
259
260
SDL_free(style);
} else {
const int doctypeSize = SDL_strlen(doctype);
const int tagSize = SDL_strlen(buffer);
May 18, 2013
May 18, 2013
262
263
const int size = doctypeSize + tagSize + 1; // extra byte for '\0'
retBuf = SDL_malloc(size);
May 18, 2013
May 18, 2013
265
266
SDL_snprintf(retBuf, size, "%s%s", doctype, buffer);
}
May 18, 2013
May 18, 2013
268
return retBuf;
269
270
271
272
}
char *
XMLCloseDocument() {
May 18, 2013
May 18, 2013
273
return XMLCloseElement(root);
274
275
276
277
278
}
char *
XMLOpenElement(const char *tag)
{
May 18, 2013
May 18, 2013
279
280
memset(buffer, 0, bufferSize);
SDL_snprintf(buffer, bufferSize, "<%s>", tag);
May 18, 2013
May 18, 2013
282
AddOpenTag(tag);
May 18, 2013
May 18, 2013
284
285
286
287
const int size = SDL_strlen(buffer);
char *ret = SDL_malloc(size + 1);
strncpy(ret, buffer, size);
ret[size] = '\0';
May 18, 2013
May 18, 2013
289
return ret;
290
291
292
293
294
}
char *
XMLAddContent(const char *content)
{
May 18, 2013
May 18, 2013
295
296
297
if(ValidateString(content) == 0) {
return NULL;
}
May 18, 2013
May 18, 2013
299
const char *escapedContent = EscapeString(content);
May 18, 2013
May 18, 2013
301
302
303
if(SDL_strlen(escapedContent) >= bufferSize) {
return NULL;
}
May 18, 2013
May 18, 2013
305
306
307
memset(buffer, 0, bufferSize);
SDL_snprintf(buffer, bufferSize, "%s", escapedContent);
SDL_free((char *)escapedContent);
May 18, 2013
May 18, 2013
309
310
311
312
const int size = SDL_strlen(buffer);
char *ret = SDL_malloc(size + 1);
strncpy(ret, buffer, size);
ret[size] = '\0';
May 18, 2013
May 18, 2013
314
return ret;
315
316
317
318
319
}
char *
XMLCloseElement(const char *tag)
{
May 18, 2013
May 18, 2013
320
321
322
if(ValidateString(tag) == 0) {
return NULL;
}
May 18, 2013
May 18, 2013
324
325
326
int retBufferSize = 150;
char *ret = SDL_malloc(retBufferSize);
memset(ret, 0, retBufferSize);
May 18, 2013
May 18, 2013
328
329
// \todo check that element we're trying to close is actually open,
// otherwise it'll cause nesting problems
May 18, 2013
May 18, 2013
331
332
333
334
335
// 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;
while(openTag) {
TagList *temp = openTag->next;
May 18, 2013
May 18, 2013
337
338
char *lowOpenTag = ToLowerCase(openTag->tag);
char *lowTag = ToLowerCase(tag);
May 18, 2013
May 18, 2013
340
341
342
const int openTagSize = SDL_strlen(lowOpenTag);
const int tagSize = SDL_strlen(lowTag);
const int compSize = (openTagSize > tagSize) ? openTagSize : tagSize;
May 18, 2013
May 18, 2013
344
memset(buffer, 0, bufferSize);
May 18, 2013
May 18, 2013
346
347
348
349
350
351
352
int breakOut = 0;
if(SDL_strncmp(lowOpenTag, lowTag, compSize) == 0) {
breakOut = 1;
SDL_snprintf(buffer, bufferSize, "</%s>", tag);
} else {
SDL_snprintf(buffer, bufferSize, "</%s>", openTag->tag);
}
May 18, 2013
May 18, 2013
354
355
SDL_free(lowOpenTag);
SDL_free(lowTag);
May 18, 2013
May 18, 2013
357
358
359
360
361
362
363
int bytesLeft = bufferSize - SDL_strlen(ret);
if(bytesLeft) {
strncat(ret, buffer, bytesLeft);
} else {
// \! todo there's probably better way to report an error?
fprintf(stderr, "xml.c | XMLCloseElement: Buffer is full");
}
May 18, 2013
May 18, 2013
365
RemoveOpenTag(openTag->tag);
May 18, 2013
May 18, 2013
367
openTag = temp;
May 18, 2013
May 18, 2013
369
370
371
372
if(breakOut) {
break;
}
}
May 18, 2013
May 18, 2013
374
return ret;