/* Copyright (c) 2013-2014 Yoran Heling12Permission is hereby granted, free of charge, to any person obtaining3a copy of this software and associated documentation files (the4"Software"), to deal in the Software without restriction, including5without limitation the rights to use, copy, modify, merge, publish,6distribute, sublicense, and/or sell copies of the Software, and to7permit persons to whom the Software is furnished to do so, subject to8the following conditions:910The above copyright notice and this permission notice shall be included11in all copies or substantial portions of the Software.1213THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,14EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY17CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,18TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE19SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.20*/2122#ifndef YXML_H23#define YXML_H2425#include <stdint.h>26#include <stddef.h>2728#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline)29#define inline __inline30#endif3132/* Full API documentation for this library can be found in the "yxml.pod" file33* in the yxml git repository, or online at http://dev.yorhel.nl/yxml/man */3435typedef enum {36YXML_EEOF = -5, /* Unexpected EOF */37YXML_EREF = -4, /* Invalid character or entity reference (&whatever;) */38YXML_ECLOSE = -3, /* Close tag does not match open tag (<Tag> .. </OtherTag>) */39YXML_ESTACK = -2, /* Stack overflow (too deeply nested tags or too long element/attribute name) */40YXML_ESYN = -1, /* Syntax error (unexpected byte) */41YXML_OK = 0, /* Character consumed, no new token present */42YXML_ELEMSTART = 1, /* Start of an element: '<Tag ..' */43YXML_CONTENT = 2, /* Element content */44YXML_ELEMEND = 3, /* End of an element: '.. />' or '</Tag>' */45YXML_ATTRSTART = 4, /* Attribute: 'Name=..' */46YXML_ATTRVAL = 5, /* Attribute value */47YXML_ATTREND = 6, /* End of attribute '.."' */48YXML_PISTART = 7, /* Start of a processing instruction */49YXML_PICONTENT = 8, /* Content of a PI */50YXML_PIEND = 9 /* End of a processing instruction */51} yxml_ret_t;5253/* When, exactly, are tokens returned?54*55* <TagName56* '>' ELEMSTART57* '/' ELEMSTART, '>' ELEMEND58* ' ' ELEMSTART59* '>'60* '/', '>' ELEMEND61* Attr62* '=' ATTRSTART63* "X ATTRVAL64* 'Y' ATTRVAL65* 'Z' ATTRVAL66* '"' ATTREND67* '>'68* '/', '>' ELEMEND69*70* </TagName71* '>' ELEMEND72*/737475typedef struct {76/* PUBLIC (read-only) */7778/* Name of the current element, zero-length if not in any element. Changed79* after YXML_ELEMSTART. The pointer will remain valid up to and including80* the next non-YXML_ATTR* token, the pointed-to buffer will remain valid81* up to and including the YXML_ELEMEND for the corresponding element. */82char *elem;8384/* The last read character(s) of an attribute value (YXML_ATTRVAL), element85* data (YXML_CONTENT), or processing instruction (YXML_PICONTENT). Changed86* after one of the respective YXML_ values is returned, and only valid87* until the next yxml_parse() call. Usually, this string only consists of88* a single byte, but multiple bytes are returned in the following cases:89* - "<?SomePI ?x ?>": The two characters "?x"90* - "<![CDATA[ ]x ]]>": The two characters "]x"91* - "<![CDATA[ ]]x ]]>": The three characters "]]x"92* - "&#N;" and "&#xN;", where dec(n) > 127. The referenced Unicode93* character is then encoded in multiple UTF-8 bytes.94*/95char data[8];9697/* Name of the current attribute. Changed after YXML_ATTRSTART, valid up to98* and including the next YXML_ATTREND. */99char *attr;100101/* Name/target of the current processing instruction, zero-length if not in102* a PI. Changed after YXML_PISTART, valid up to (but excluding)103* the next YXML_PIEND. */104char *pi;105106/* Line number, byte offset within that line, and total bytes read. These107* values refer to the position _after_ the last byte given to108* yxml_parse(). These are useful for debugging and error reporting. */109uint64_t byte;110uint64_t total;111uint32_t line;112113114/* PRIVATE */115int state;116unsigned char *stack; /* Stack of element names + attribute/PI name, separated by \0. Also starts with a \0. */117size_t stacksize, stacklen;118unsigned reflen;119unsigned quote;120int nextstate; /* Used for '@' state remembering and for the "string" consuming state */121unsigned ignore;122unsigned char *string;123} yxml_t;124125126#ifdef __cplusplus127extern "C" {128#endif129130void yxml_init(yxml_t *, void *, size_t);131132133yxml_ret_t yxml_parse(yxml_t *, int);134135136/* May be called after the last character has been given to yxml_parse().137* Returns YXML_OK if the XML document is valid, YXML_EEOF otherwise. Using138* this function isn't really necessary, but can be used to detect documents139* that don't end correctly. In particular, an error is returned when the XML140* document did not contain a (complete) root element, or when the document141* ended while in a comment or processing instruction. */142yxml_ret_t yxml_eof(yxml_t *);143144#ifdef __cplusplus145}146#endif147148149/* Returns the length of the element name (x->elem), attribute name (x->attr),150* or PI name (x->pi). This function should ONLY be used directly after the151* YXML_ELEMSTART, YXML_ATTRSTART or YXML_PISTART (respectively) tokens have152* been returned by yxml_parse(), calling this at any other time may not give153* the correct results. This function should also NOT be used on strings other154* than x->elem, x->attr or x->pi. */155static inline size_t yxml_symlen(yxml_t *x, const char *s) {156return (x->stack + x->stacklen) - (const unsigned char*)s;157}158159#endif160161/* vim: set noet sw=4 ts=4: */162163164