gfxEngine_documentation
simplexml.c File Reference
#include <stdlib.h>
#include <string.h>
#include "simplexml.h"

Data Structures

struct  simplexml_value_buffer
 
struct  simplexml_user_data
 
struct  simplexml_parser_state
 

Macros

#define FAIL   0
 
#define SUCCESS   1
 
#define NO_ERROR   0
 
#define NOT_PARSED   1
 
#define OUT_OF_MEMORY   2
 
#define EARLY_TERMINATION   3
 
#define ILLEGAL_AMPERSAND   4
 
#define NO_UNICODE_SUPPORT   5
 
#define GREATER_THAN_EXPECTED   6
 
#define QUOTE_EXPECTED   7
 
#define ILLEGAL_HANDLER   8
 
#define NOT_INITIALIZED   9
 
#define NO_DOCUMENT_TAG   10
 
#define MISMATCHED_END_TAG   11
 
#define ATTRIBUTE_EXPECTED   12
 
#define EQUAL_SIGN_EXPECTED   13
 
#define TAG_BEGIN_OPENING   0
 
#define TAG_BEGIN_CLOSING   1
 
#define TAG_END   2
 
#define PROCESSING_INSTRUCTION   3
 
#define DOCTYPE   4
 
#define COMMENT   5
 
#define ATTRIBUTE   6
 
#define CONTENT   7
 
#define UNKNOWN   8
 
#define SPACE   ' '
 
#define LF   '\xa'
 
#define CR   '\xd'
 

Typedefs

typedef struct simplexml_value_buffer TSimpleXmlValueBuffer
 
typedef struct simplexml_value_bufferSimpleXmlValueBuffer
 
typedef struct simplexml_user_data TSimpleXmlUserData
 
typedef struct simplexml_user_dataSimpleXmlUserData
 
typedef struct simplexml_parser_state TSimpleXmlParserState
 
typedef struct simplexml_parser_stateSimpleXmlParserState
 

Functions

SimpleXmlParserState createSimpleXmlParser (const char *sData, long nDataSize)
 
void destroySimpleXmlParser (SimpleXmlParserState parser)
 
int initializeSimpleXmlParser (SimpleXmlParserState parser, const char *sData, long nDataSize)
 
char * getSimpleXmlParseErrorDescription (SimpleXmlParserState parser)
 
int parseSimpleXml (SimpleXmlParserState parser, SimpleXmlTagHandler handler)
 
int parseOneTag (SimpleXmlParserState parser, SimpleXmlTagHandler parentHandler)
 
int readNextTagToken (SimpleXmlParserState parser)
 
int readNextContentToken (SimpleXmlParserState parser)
 
int readChar (SimpleXmlParserState parser)
 
char peekInputCharAt (SimpleXmlParserState parser, int nOffset)
 
char peekInputChar (SimpleXmlParserState parser)
 
int skipWhitespace (SimpleXmlParserState parser)
 
void skipInputChars (SimpleXmlParserState parser, int nAmount)
 
void skipInputChar (SimpleXmlParserState parser)
 
char readInputChar (SimpleXmlParserState parser)
 
int addNextTokenCharValue (SimpleXmlParserState parser, char c)
 
int addNextTokenStringValue (SimpleXmlParserState parser, char *szInput)
 
SimpleXmlValueBuffer createSimpleXmlValueBuffer (long nInitialSize)
 
void destroySimpleXmlValueBuffer (SimpleXmlValueBuffer vb)
 
int growSimpleXmlValueBuffer (SimpleXmlValueBuffer vb)
 
int appendCharToSimpleXmlValueBuffer (SimpleXmlValueBuffer vb, char c)
 
int appendStringToSimpleXmlValueBuffer (SimpleXmlValueBuffer vb, const char *szInput)
 
int zeroTerminateSimpleXmlValueBuffer (SimpleXmlValueBuffer vb)
 
int clearSimpleXmlValueBuffer (SimpleXmlValueBuffer vb)
 
int getSimpleXmlValueBufferContentLength (SimpleXmlValueBuffer vb)
 
int getSimpleXmlValueBufferContents (SimpleXmlValueBuffer vb, char *szOutput, long nMaxLen)
 
char * getInternalSimpleXmlValueBufferContents (SimpleXmlValueBuffer vb)
 
SimpleXmlParser simpleXmlCreateParser (const char *sData, long nDataSize)
 
void simpleXmlDestroyParser (SimpleXmlParser parser)
 
int simpleXmlInitializeParser (SimpleXmlParser parser, const char *sData, long nDataSize)
 
int simpleXmlParse (SimpleXmlParser parser, SimpleXmlTagHandler handler)
 
char * simpleXmlGetErrorDescription (SimpleXmlParser parser)
 
long simpleXmlGetLineNumber (SimpleXmlParser parser)
 
void simpleXmlParseAbort (SimpleXmlParser parser, int nErrorCode)
 
int simpleXmlPushUserData (SimpleXmlParser parser, void *pData)
 
void * simpleXmlPopUserData (SimpleXmlParser parser)
 
void * simpleXmlGetUserDataAt (SimpleXmlParser parser, int nLevel)
 
void * simpleXmlGetUserData (SimpleXmlParser parser)
 
void * simpleXmlNopHandler (SimpleXmlParser parser, SimpleXmlEvent event, const char *szName, const char *szAttribute, const char *szValue)
 

Macro Definition Documentation

#define ATTRIBUTE   6
#define ATTRIBUTE_EXPECTED   12
#define COMMENT   5
#define CONTENT   7
#define CR   '\xd'
#define DOCTYPE   4
#define EARLY_TERMINATION   3
#define EQUAL_SIGN_EXPECTED   13
#define FAIL   0
#define GREATER_THAN_EXPECTED   6
#define ILLEGAL_AMPERSAND   4
#define ILLEGAL_HANDLER   8
#define LF   '\xa'
#define MISMATCHED_END_TAG   11
#define NO_DOCUMENT_TAG   10
#define NO_ERROR   0
#define NO_UNICODE_SUPPORT   5
#define NOT_INITIALIZED   9
#define NOT_PARSED   1
#define OUT_OF_MEMORY   2
#define PROCESSING_INSTRUCTION   3
#define QUOTE_EXPECTED   7
#define SPACE   ' '
#define SUCCESS   1
#define TAG_BEGIN_CLOSING   1
#define TAG_BEGIN_OPENING   0
#define TAG_END   2
#define UNKNOWN   8

Typedef Documentation

SimpleXmlParser internal state.

This structure holds all data necessary for the simple xml parser to operate.

This struct describes the internal representation of a SimpleXmlParserState.

User data stack. This structure is a simple stack for user data.

Value buffer. This structure resembles a string buffer that grows automatically when inserting data.

Function Documentation

int addNextTokenCharValue ( SimpleXmlParserState  parser,
char  c 
)

Appends a character to the next token value string.

Parameters
parserthe parser whose next token value string is to be modified.
cthe character to append.
Returns
SUCCESS or FAIL (if there is not enough memory).
int addNextTokenStringValue ( SimpleXmlParserState  parser,
char *  szInput 
)

Appends a zero terminated string to the next token value string.

Parameters
parserthe parser whose next token value string is to be modified.
szInputthe zero terminated string to append.
Returns
SUCCESS or FAIL (if there is not enough memory).
int appendCharToSimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb,
char  c 
)

Appends a character to the value buffer.

Parameters
vbthe value buffer to append to.
cthe character to append.
Returns
SUCCESS or FAIL (if there is not enough memory).
int appendStringToSimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb,
const char *  szInput 
)

Appends a zero terminated string to the value buffer.

Parameters
vbthe value buffer to append to.
szInputthe input string to append.
Returns
SUCCESS or FAIL (if there is not enough memory).
int clearSimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb)

Resets the append location of the value buffer.

Parameters
vbthe value buffer to clear.
Returns
SUCCESS or FAIL.
SimpleXmlParserState createSimpleXmlParser ( const char *  sData,
long  nDataSize 
)

Creates a new simple xml parser for the specified input data.

Parameters
sDatathe input data to parse (must no be NULL).
nDataSizethe size of the input data buffer (sData) to parse (must be greater than 0).
Returns
the new simple xml parser or NULL if there is not enough memory or the input data specified cannot be parsed.
SimpleXmlValueBuffer createSimpleXmlValueBuffer ( long  nInitialSize)

Creates a new value buffer of the specified size.

The value buffer automatically grows when appending characters if it is not large enough.

The value buffer uses 'malloc' to allocate buffer space. The user is responsible for freeing the value buffer created using destroySimpleXmlValueBuffer.

Parameters
nInitialSizethe initial size of the value buffer in chars.
Returns
NULL if the value buffer could not be allocated, the newly allocated value buffer otherwise (to be freed by the caller).
See also
destroySimpleXmlValueBuffer
void destroySimpleXmlParser ( SimpleXmlParserState  parser)

Destroys the specified simple xml parser.

Parameters
parserthe parser to destroy (must have been created using createSimpleXmlParser).
void destroySimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb)

Destroys a value buffer created using createSimpleXmlValueBuffer.

Parameters
vbthe value buffer to destroy.
See also
destroySimpleXmlValueBuffer
char * getInternalSimpleXmlValueBufferContents ( SimpleXmlValueBuffer  vb)

Returns the zero terminated internal string buffer of the value buffer specified.

Warning: Modifying the array returned modifies the internal data of the value buffer!

Parameters
vbthe value buffer whose string buffer should be returned.
Returns
the string buffer or NULL (if there is not enough memory).
char * getSimpleXmlParseErrorDescription ( SimpleXmlParserState  parser)

Returns a description of the error that occured during parsing.

Parameters
parserthe parser for which to get the error description.
Returns
an error description or NULL if there was no error during parsing.
int getSimpleXmlValueBufferContentLength ( SimpleXmlValueBuffer  vb)

Returns the content length of the value buffer (including a trailing zero termination character).

Parameters
vbthe value buffer whose content length should be determined.
Returns
the content length (i.e. 1 if the content is empty, 0 in case of a failure).
int getSimpleXmlValueBufferContents ( SimpleXmlValueBuffer  vb,
char *  szOutput,
long  nMaxLen 
)

Retrieves the buffer content and stores it to the specified output array.

Parameters
vbthe value buffer whose content should be retrieved.
szOutputthe output character array to store it to, the output array is in any case zero terminated!
nMaxLenthe maximum number of characters to write to the output array.
Returns
SUCCESS or FAIL.
int growSimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb)

Grows the internal data buffer of the value buffer.

Parameters
vbthe value buffer to grow.
Returns
SUCCESS or FAIL (if there is not enough memory).
int initializeSimpleXmlParser ( SimpleXmlParserState  parser,
const char *  sData,
long  nDataSize 
)

Reinitializes the specified simple xml parser for parsing the specified input data.

Parameters
parserthe parser to initialize.
sDatathe input data to parse (must no be NULL).
nDataSizethe size of the input data buffer (sData) to parse (must be greater than 0).
Returns
0 if the parser could not be initialized,

0 if the parser was initialized successfully.

int parseOneTag ( SimpleXmlParserState  parser,
SimpleXmlTagHandler  parentHandler 
)

Parses exactly one tag.

int parseSimpleXml ( SimpleXmlParserState  parser,
SimpleXmlTagHandler  handler 
)

Starts an initialized (or newly created) xml parser with the specified document tag handler.

Parameters
parserthe parser to start.
handlerthe handler to use for the document tag.
Returns
0 if there was no error, and error code

0 if there was an error.

char peekInputChar ( SimpleXmlParserState  parser)

Peeks at the current input character at the read cursor position of the specified parser.

Parameters
parserthe parser for which to peek.
Returns
the peeked character or '\0' if there are no more data.
char peekInputCharAt ( SimpleXmlParserState  parser,
int  nOffset 
)

Peeks at the character with the specified offset from the cursor (i.e. the last character read).

Note: To peek at the next character that will be read use and offset of 0.

Parameters
parserthe parser for which to peek.
nOffsetthe peek offset relative to the position of the last char read.
Returns
the peeked character or '\0' if there are no more data.
int readChar ( SimpleXmlParserState  parser)

Reads the next character from the input data and appends it to the next token value buffer of the parser.

Note: This method does not support unicode and 8-bit characters are read using the default platform encoding.

Parameters
parserthe parser for which to read the next input character.
Returns
SUCCESS or FAIL.
char readInputChar ( SimpleXmlParserState  parser)

Reads the next input character from the specified parser and returns it.

Note: If an error is encountered '\0' is returned and the nError flag of the parser is set to EARLY_TERMINATION.

Parameters
parserthe parser from which to read the next input character.
Returns
the next input character or '\0' if there is none.
int readNextContentToken ( SimpleXmlParserState  parser)

Scanner that reads the next token type and sets the nNextToken type and the value buffer of the parser. Must not be invoked when the last token read was a TAG_BEGIN (use readNextTagToken in such a case).

The following token types are supported:

Type | ValueBuffer | Example --------------------—+----------—+----------— TAG_BEGIN_OPENING | foo | <foo TAG_END | foo | </foo> CONTENT | foo | foo PROCESSING_INSTRUCTION | XML | <?XML?> UNKNOWN | WHATEVER | <!WHATEVER> COMMENT | foo | DOCTYPE | foo | <!DOCTYPEfoo>

Parameters
parserthe parser for which to read the next token.
Returns
SUCCESS or FAIL.
int readNextTagToken ( SimpleXmlParserState  parser)

Scanner that reads the contents of a tag and sets the nNextToken type and the value buffer of the parser. Must not be invoked unless the last token read was a TAG_BEGIN (see readNextContentToken in such a case).

The following token types are supported:

Type | ValueBuffer | Example --------------------—+----------—+----------— TAG_END | <unchanged> | /> TAG_BEGIN_CLOSING | <unchanged> | > ATTRIBUTE | bar | foo="bar"

Note: The name of an attribute (e.g. foo in the above example) is stored in the attribute name field of the parser (szAttributeName).

Parameters
parserthe parser for which to read the next token.
Returns
SUCCESS or FAIL.
SimpleXmlParser simpleXmlCreateParser ( const char *  sData,
long  nDataSize 
)

Creates a new simple xml parser for the specified input data.

The input data may be parsed with simpleXmlParse and the parser returned by this function as parameter.

Note: The parser will not copy the input data or in any way modify it. However any modifications of the input data in a callback handler while parsing will have an undefined result!

Parameters
sDatathe input data to parse (must no be NULL).
nDataSizethe size of the input data buffer (sData) to parse (must be greater than 0).
Returns
the new simple xml parser or NULL if there is not enough memory or the input data specified cannot be parsed.
void simpleXmlDestroyParser ( SimpleXmlParser  parser)

Destroys the specified simple xml parser.

Parameters
parserthe parser to destroy (must have been created using simpleXmlCreateParser).
char* simpleXmlGetErrorDescription ( SimpleXmlParser  parser)

Returns a description of the error that occured during parsing.

Parameters
parserthe parser for which to get the error description.
Returns
an error description or NULL if there was no error during parsing.
long simpleXmlGetLineNumber ( SimpleXmlParser  parser)

Returns the line number of the current input line that the parser has read.

In case of an error this method will return the line number on which the error was encountered after a call to simpleXmlParse.

If called from a handler during parsing this function will return the current line number.

If called after a successfull simpleXmlParse run this function will return the line number of the last line parsed in the xml data.

Returns
the current input line number of the parser or -1 if it is unknown.
void* simpleXmlGetUserData ( SimpleXmlParser  parser)

Peeks at the top of the user data stack.

The last pointer pushed on the user data stack is returned.

Note: This function does not modify the stack.

This function is a convenience function for simpleXmlGetUserDataAt(parser, 0);

Parameters
parserthe parser from which to get the user data pointer.
Returns
the value of the user data pointer or NULL if the pointer has not been set yet.
void* simpleXmlGetUserDataAt ( SimpleXmlParser  parser,
int  nLevel 
)

Peeks at a specified location from to the top of the user data stack.

The level indicates the level from the top of the stack that is inspected.

Note: This method does not modify the stack.

Parameters
parserthe parser from which to get the user data pointer.
nLevelthe level (from the top of the stack) to inspect.
Returns
the value of the user data pointer at the specified level or NULL if there is no such entry (i.e. the level is higher than the stack depth).
int simpleXmlInitializeParser ( SimpleXmlParser  parser,
const char *  sData,
long  nDataSize 
)

Reinitializes the specified simple xml parser for parsing the specified input data.

Parameters
parserthe parser to initialize.
sDatathe input data to parse (must no be NULL).
nDataSizethe size of the input data buffer (sData) to parse (must be greater than 0).
Returns
0 if the parser could not be initialized, > 0 if the parser was initialized successfully and parsing may be started using simpleXmlParse.
void* simpleXmlNopHandler ( SimpleXmlParser  parser,
SimpleXmlEvent  event,
const char *  szName,
const char *  szAttribute,
const char *  szValue 
)

No operation handler (used internally).

int simpleXmlParse ( SimpleXmlParser  parser,
SimpleXmlTagHandler  handler 
)

Starts an initialized (or newly created) xml parser with the specified document tag handler.

Note: This function may only be called once after creation or initialization of a parser. To reuse the parser it has to be freshly initialized (using simpleXmlInitializeParser) prior to calling the function again.

Parameters
parserthe parser to start.
handlerthe handler to use for the document tag.
Returns
0 if there was no error, and error code > 0 if there was an error.
void simpleXmlParseAbort ( SimpleXmlParser  parser,
int  nErrorCode 
)

Causes the simple xml parser to abort parsing of the input data.

This method may only be called from a tag handler.

The active simpleXmlParse run will be aborted and the simpleXmlParse function will return with the specified error code.

Parameters
nErrorCodethe error code with which to abort (the error code must be >= SIMPLE_XML_USER_ERROR else the abort request is ignored!)
void* simpleXmlPopUserData ( SimpleXmlParser  parser)

Pops the last pointer pushed on the user data stack of the parser.

Parameters
parserthe parser on which to pop the user data pointer.
Returns
the last pointer pushed or NULL if the stack is empty.
int simpleXmlPushUserData ( SimpleXmlParser  parser,
void *  pData 
)

Pushes the specified pointer on the user data stack of the parser.

Parameters
parserthe parser on which to push the user data pointer.
pDatapointer to the user data to set (NULL is not a valid value).
Returns
> 0 on success, 0 if there is not enough memory left or it was tried to push a NULL data pointer.
void skipInputChar ( SimpleXmlParserState  parser)

Skips the current input read character.

Parameters
parserthe parser whose read cursor should be incremented.
See also
skipInputChars
peekInputChar
void skipInputChars ( SimpleXmlParserState  parser,
int  nAmount 
)

Moves the read cursor of the specified parser by the specified amount.

Parameters
parserthe parser whose read cursor is to be moved.
nAmountthe amount by which to move the cursor (> 0).
int skipWhitespace ( SimpleXmlParserState  parser)

Skips any whitespace at the cursor position of the parser specified.

Note: All characters smaller than the space character are considered to be whitespace.

Parameters
parserthe parser for which to skip whitespace.
Returns
SUCCESS or FAIL.
int zeroTerminateSimpleXmlValueBuffer ( SimpleXmlValueBuffer  vb)

Zero terminates the internal buffer without appending any characters (i.e. the append location is not modified).

Parameters
vbthe value buffer to zero terminate.
Returns
SUCCESS or FAIL (if there is not enough memory).