GophHub - kevinboone/epub2txt2/src/sxmlc.h


Raw File

    1	/**
    2		Copyright (c) 2010, Matthieu Labas
    3		All rights reserved.
    4	
    5		Redistribution and use in source and binary forms, with or without modification,
    6		are permitted provided that the following conditions are met:
    7	
    8		1. Redistributions of source code must retain the above copyright notice,
    9		   this list of conditions and the following disclaimer.
   10	
   11		2. Redistributions in binary form must reproduce the above copyright notice,
   12		   this list of conditions and the following disclaimer in the documentation
   13		   and/or other materials provided with the distribution.
   14	
   15		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   16		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   17		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18		IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   19		INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20		NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   21		PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   22		WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   23		ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
   24		OF SUCH DAMAGE.
   25	
   26		The views and conclusions contained in the software and documentation are those of the
   27		authors and should not be interpreted as representing official policies, either expressed
   28		or implied, of the FreeBSD Project.
   29	*/
   30	#ifndef _SXML_H_
   31	#define _SXML_H_
   32	
   33	/**
   34	 * \brief Current SXMLC version, as a `const char[]`.
   35	 */
   36	#define SXMLC_VERSION "4.4.0"
   37	
   38	#ifdef __cplusplus
   39	extern "C" {
   40	#endif

   41	
   42	#include <stdio.h>
   43	
   44	/**
   45	 * \brief Define this to compile unicode support.
   46	 */
   47	#ifdef SXMLC_UNICODE
   48		typedef wchar_t SXML_CHAR;
   49		#define C2SX(c) L ## c
   50		#define CEOF WEOF
   51		#define sx_strcmp wcscmp
   52		#define sx_strncmp wcsncmp
   53		#define sx_strlen wcslen
   54		#define sx_strdup wcsdup
   55		#define sx_strchr wcschr
   56		#define sx_strrchr wcsrchr
   57		#define sx_strcpy wcscpy
   58		#define sx_strncpy wcsncpy
   59		#define sx_strcat wcscat
   60		#define sx_printf wprintf
   61		#define sx_fprintf fwprintf
   62		#define sx_sprintf swprintf
   63		#define sx_fgetc fgetwc
   64		#define sx_fputc fputwc
   65		#define sx_puts putws
   66		#define sx_fputs fputws
   67	    #define sx_isspace iswspace
   68		#if defined(WIN32) || defined(WIN64)
   69			#define sx_fopen _wfopen
   70		#else
   71			#define sx_fopen fopen
   72		#endif
   73	    #define sx_fclose fclose
   74		#define sx_feof feof
   75	#else

   76		typedef char SXML_CHAR;
   77		#define C2SX(c) c
   78		#define CEOF EOF
   79		#define sx_strcmp strcmp
   80		#define sx_strncmp strncmp
   81		#define sx_strlen strlen
   82		#define sx_strdup __sx_strdup
   83		#define sx_strchr strchr
   84		#define sx_strrchr strrchr
   85		#define sx_strcpy strcpy
   86		#define sx_strncpy strncpy
   87		#define sx_strcat strcat
   88		#define sx_printf printf
   89		#define sx_fprintf fprintf
   90		#define sx_sprintf sprintf
   91		#define sx_fgetc fgetc
   92		#define sx_fputc fputc
   93		#define sx_puts puts
   94		#define sx_fputs fputs
   95		#define sx_isspace(c) ((int)c >= 0 && (int)c <= 127 && isspace((int)c))
   96		#if defined(WIN32) || defined(WIN64) // On Windows, if the filename has unicode characters in it, assume them to be UTF8 and convert it to wide char before calling _wfopen()
   97			FILE* sx_fopen(const SXML_CHAR* filename, const SXML_CHAR* mode);
   98		#else // On Linux, simply call fopen()
   99			#define sx_fopen fopen
  100		#endif
  101		#define sx_fclose fclose
  102		#define sx_feof feof
  103	#endif

  104	
  105	#ifdef DBG_MEM
  106		void* __malloc(size_t sz);
  107		void* __calloc(size_t count, size_t sz);
  108		void* __realloc(void* mem, size_t sz);
  109		void __free(void* mem);
  110		char* __sx_strdup(const char* s);
  111	#else

  112		#define __malloc malloc
  113		#define __calloc calloc
  114		#define __realloc realloc
  115		#define __free free
  116		#define __sx_strdup strdup
  117	#endif

  118	
  119	/**
  120	 * \brief The number of bytes to add to currently allocated buffer for line reading. Default to 256 characters (=512
  121	 * 		bytes with unicode support).
  122	 */
  123	#ifndef MEM_INCR_RLA
  124	#define MEM_INCR_RLA (256*sizeof(SXML_CHAR)) /* Initial buffer size and increment for memory reallocations */
  125	#endif

  126	
  127	#ifndef false
  128	#define false 0
  129	#endif

  130	
  131	#ifndef true
  132	#define true 1
  133	#endif

  134	
  135	#define NULC ((SXML_CHAR)C2SX('\0'))
  136	#define isquote(c) (((c) == C2SX('"')) || ((c) == C2SX('\'')))
  137	
  138	/**
  139	 * \brief Buffer data source used by 'read_line_alloc' when required. 'buf' should be 0-terminated.
  140	 */
  141	typedef struct _DataSourceBuffer {
  142		const SXML_CHAR* buf;
  143		int buf_len;
  144		int cur_pos;
  145	} DataSourceBuffer;
  146	
  147	typedef FILE* DataSourceFile;
  148	
  149	/**
  150	 * \brief Describes the type of data source used for parsing.
  151	 */
  152	typedef enum _DataSourceType {
  153		DATA_SOURCE_FILE = 0,
  154		DATA_SOURCE_BUFFER,
  155		DATA_SOURCE_MAX
  156	} DataSourceType;
  157	
  158	/**
  159	 * \brief Node types for `XMLNode`.
  160	 *
  161	 * Types marked *[N/A]* are *not* valid types for an `XMLNode` but are used
  162	 * internally by the parser.
  163	 */
  164	typedef enum _TagType {
  165		TAG_ERROR = -1,
  166		TAG_NONE = 0,
  167		TAG_PARTIAL,	/**< Node containing a legal `>` that stopped file reading. *[N/A]* */
  168		TAG_FATHER,		/**< `<tag>` - Next nodes will be children of this one. */
  169		TAG_SELF,		/**< `<tag/>` - Standalone node. */
  170		TAG_INSTR,		/**< `<?prolog?>` - Processing instructions, or prolog node. */
  171		TAG_COMMENT,	/**< `<!--comment-->` */
  172		TAG_CDATA,		/**< `<![CDATA[ ]]>` - CDATA node */
  173		TAG_DOCTYPE,	/**< `<!DOCTYPE [ ]>` - DOCTYPE node */
  174		TAG_END,		/**< `</tag>` - End of father node. *[N/A]* */
  175		TAG_TEXT,		/**< Special node used as a text container. */
  176						/**< and `node->tag` is `NULL`. */
  177	
  178		TAG_USER = 100	/*!<  User-defined tag start */
  179	} TagType;
  180	
  181	/* TODO: Performance improvement with some fixed-sized strings ??? (e.g. XMLAttribute.name[64], XMLNode.tag[64]).
  182	 * Also better for platforms where allocations can be forbidden (e.g. embedded, space, ...). */
  183	
  184	/**
  185	 * \brief An XML attribute in the form `name="value"`.
  186	 *
  187	 * An attribute can be *deactivated*, in which case it will not be taken into account in functions
  188	 * `XMLNode_print*()`, `XMLNode_get_attribute_count()`.
  189	 */
  190	typedef struct _XMLAttribute {
  191		SXML_CHAR* name;	/**< The attribute name. */
  192		SXML_CHAR* value;	/**< The attribute value. */
  193		int active;			/**< `true` if the attribute is active. */
  194	} XMLAttribute;
  195	
  196	/* Constant to know whether a struct has been initialized (XMLNode or XMLDoc)
  197	 * TODO: Find a better way. */
  198	#define XML_INIT_DONE 0x19770522 /* Happy Birthday ;) */
  199	
  200	/**
  201	 * \brief An XML node.
  202	 *
  203	 * For tag types `TAG_INSTR`, `TAG_COMMENT`, `TAG_CDATA` and `TAG_DOCTYPE`, the text is
  204	 * stored in `node->tag`, with `node->text` being `NULL`.
  205	 *
  206	 * A node can be *deactivated*, in which case it will not be taken into account in functions
  207	 * `XMLNode_print*()`, `XMLNode_get_children_count()`, `XMLNode_get_child()`, `XMLNode_insert_child()`, ...
  208	 *
  209	 * If `text_as_nodes` is zeroed during parsing, the `node->text` is the concatenation of
  210	 * all texts found under the node: `<a>text<b/>other text</a>` will show `node->tag == "a"`
  211	 * and `node->text == "textother text"`.
  212	 * If `text_as_nodes` is non-zeroed, the same node will have 3 children: child [0] type is
  213	 * `TAG_TEXT` (with text `"text"`), child [1] type is `TAG_SELF` and child [2] type is
  214	 * `TAG_TEXT` (with text `"other text"`).
  215	 *
  216	 * *N.B. that when reading a pretty-printed XML, the extra line breaks and spaces will be stored
  217	 * in `node->text`*.
  218	 */
  219	typedef struct _XMLNode {
  220		SXML_CHAR* tag;				/**< Tag name, or text for tag types `TAG_INSTR`, `TAG_COMMENT`, `TAG_CDATA` and `TAG_DOCTYPE`. */
  221		SXML_CHAR* text;			/**< Text inside the node, or `NULL` if empty. */
  222		XMLAttribute* attributes;	/**< Array of attributes. */
  223		int n_attributes;			/**< Number of attributes *in `attributes` array* (might not be the number of *active* attributes). */
  224		
  225		struct _XMLNode* father;	/**< Pointer to father node. `NULL` if root. */
  226		struct _XMLNode** children; /**< Array of children nodes. */
  227		int n_children;				/**< Number of nodes *in `children` array* (might not be the number of *active* children). */
  228		
  229		TagType tag_type;			/**< Node type. */
  230		int active;					/**< 'true' to tell that node is active and should be displayed by 'XMLDoc_print_*()'. */
  231	
  232		void* user;	/**< Pointer for user data associated to the node. */
  233	
  234		/* Keep 'init_value' as the last member */
  235		int init_value;	/**< Initialized to 'XML_INIT_DONE' to indicate that node has been initialized properly. */
  236	} XMLNode;
  237	
  238	#ifndef SXMLC_MAX_PATH
  239	#ifdef _MAX_PATH
  240	#define SXMLC_MAX_PATH _MAX_PATH
  241	#else

  242	#define SXMLC_MAX_PATH 256
  243	#endif

  244	#endif

  245	
  246	/**
  247	 * \brief Describe the types of BOM detected while reading an XML buffer.
  248	 */
  249	typedef enum _BOM_TYPE {
  250		BOM_NONE = 0x00,
  251		BOM_UTF_8 = 0xefbbbf,
  252		BOM_UTF_16BE = 0xfeff,
  253		BOM_UTF_16LE = 0xfffe,
  254		BOM_UTF_32BE = 0x0000feff,
  255		BOM_UTF_32LE = 0xfffe0000
  256	} BOM_TYPE;
  257	    
  258	/**
  259	 * \brief An XML document, basically an array of `XMLNode`.
  260	 *
  261	 * An sxmlc XML document can have several root nodes. It actually usually have a prolog `<?xml version="1.0" ...?>`
  262	 * as the first node, then maybe a few comment nodes, then the first root node, which is the last node in
  263	 * correctly-formed XML, but there can also be some other nodes after it.
  264	 */
  265	typedef struct _XMLDoc {
  266		SXML_CHAR filename[SXMLC_MAX_PATH];
  267		BOM_TYPE bom_type;		/**< BOM type (UTF-8, UTF-16*). */
  268		unsigned char bom[5];	/**< First characters read that might be a BOM when unicode is used. */
  269		int sz_bom;				/**< Number of bytes in BOM. */
  270		XMLNode** nodes;		/* Nodes of the document, including prolog, comments and root nodes */
  271		int n_nodes;			/* Number of nodes in 'nodes' */
  272		int i_root;				/* Index of first root node in 'nodes', -1 if document is empty */
  273	
  274		/* Keep 'init_value' as the last member */
  275		int init_value;	/* Initialized to 'XML_INIT_DONE' to indicate that document has been initialized properly */
  276	} XMLDoc;
  277	
  278	/**
  279	 * \brief Register an XML tag, giving its 'start' and 'end' string, which should include '<' and '>'.
  280	 *
  281	 * \param tag_type is user-given and has to be less than or equal to `TAG_USER`. It will be
  282	 * 		returned as the `tag_type` member of the `XMLNode` struct.
  283	 * 		*Note that no test is performed to check for an already-existing `tag_type`*.
  284	 * \param start The start string used to detect such tag (e.g. `"<![CDATA["`). Should start with `<`.
  285	 * \param end The tag end string (e.g. `"]]>"`). Should end with `>`.
  286	 * \return tag index in user tags table when successful, or -1 if the `tag_type` is invalid or
  287	 * 		the new tag could not be registered (e.g. when `start` does not start with `<` or
  288	 * 		`end` does not end with `>`).
  289	 */
  290	int XML_register_user_tag(TagType tag_type, SXML_CHAR* start, SXML_CHAR* end);
  291	
  292	/**
  293	 * \brief Remove a registered user tag.
  294	 * \param i_tag The user-tag number, as returned by `XML_register_user_tag()`.
  295	 * \return the new number of registered user tags or -1 if `i_tag` is invalid.
  296	 */
  297	int XML_unregister_user_tag(int i_tag);
  298	
  299	/**
  300	 * \brief Get the number of user-defined tags
  301	 * \return the number of registered tags.
  302	 */
  303	int XML_get_nb_registered_user_tags(void);
  304	
  305	/**
  306	 * \brief Search for a user tag based on it type.
  307	 * \param tag_type The tag type, as given in `XML_register_user_tag()`.
  308	 * \return the index of first occurrence of 'tag_type' in registered user tags, or '-1' if not found.
  309	 */
  310	int XML_get_registered_user_tag(TagType tag_type);
  311	
  312	/**
  313	 * \brief The different reasons why parsing would fail.
  314	 */
  315	typedef enum _ParseError {
  316		PARSE_ERR_NONE = 0,					/**< Success. */
  317		PARSE_ERR_MEMORY = -1,				/**< Not enough memory for an `?alloc()` call. */
  318		PARSE_ERR_UNEXPECTED_TAG_END = -2,	/**< When a tag end does not match the tag start (e.g. `<a></b>`). */
  319		PARSE_ERR_SYNTAX = -3,				/**< General syntax error. */
  320		PARSE_ERR_EOF = -4,					/**< Unexpected EOF. */
  321		PARSE_ERR_TEXT_OUTSIDE_NODE = -5,	/**< During DOM loading. */
  322		PARSE_ERR_UNEXPECTED_NODE_END = -6	/**< During DOM loading. */
  323	} ParseError;
  324	
  325	/**
  326	 * \brief Events that can happen when loading an XML document.
  327	 *
  328	 * These will be passed to the `all_event` callback of the SAX parser.
  329	 */
  330	typedef enum _XMLEvent {
  331		XML_EVENT_START_DOC,	/**< Document parsing started. */
  332		XML_EVENT_START_NODE,	/**< New node detected (all attributes are read). */
  333		XML_EVENT_END_NODE,		/**< Last node ended. */
  334		XML_EVENT_TEXT,			/**< Text detected in current node. */
  335		XML_EVENT_ERROR,		/**< Parsing error. */
  336		XML_EVENT_END_DOC		/**< Document parsing finished. */
  337	} XMLEvent;
  338	
  339	/**
  340	 * \brief Structure given as an argument for SAX callbacks to retrieve information about parsing status.
  341	 */
  342	typedef struct _SAX_Data {
  343		const SXML_CHAR* name;	/**< Document name (file name or buffer name). */
  344		int line_num;			/**< Current line number being processed. */
  345		void* user;				/**< User-given data. */
  346		DataSourceType type;	/**< Data source type [DATA_SOURCE_FILE|DATA_SOURCE_BUFFER]. */
  347		void* src;				/**< Data source [DataSourceFile|DataSourceBuffer]. Depends on type. */
  348	} SAX_Data;
  349	
  350	/**
  351	 * \brief User callbacks used for SAX parsing.
  352	 *
  353	 * Return values of these callbacks should be 0 to stop parsing. Some callbacks can be set to `NULL`
  354	 * to disable handling of some events (e.g. everything `NULL` except `all_event()`).
  355	 *
  356	 * All parameters are pointers to structures that will no longer be available after callback returns so
  357	 * it is recommended that the callback uses the information and stores it in its own data structure.
  358	 *
  359	 * *WARNING! SAX PARSING DOES NOT CHECK FOR XML INTEGRITY!* e.g. a tag end without a matching tag start
  360	 * will not be detected by the parser and should be detected by the callbacks instead.
  361	 */
  362	typedef struct _SAX_Callbacks {
  363		/**
  364		 * \fn start_doc
  365		 * \brief Callback called when parsing starts, *before* parsing the first node.
  366		 */
  367		int (*start_doc)(SAX_Data* sd);
  368	
  369		/**
  370		 * \fn start_node
  371		 * \brief Callback called when a new node starts (e.g. `<tag>` or `<tag/>`).
  372		 *
  373		 * Attributes are read and available from `node->attributes`.
  374		 * N.B. "self-contained" ndoes (e.g. `<tag/>`) will trigger an immediate call to the `end_node()` callback
  375		 * after the `start_node()` callback.
  376		 */
  377		int (*start_node)(const XMLNode* node, SAX_Data* sd);
  378	
  379		/**
  380		 * \fn end_node
  381		 * \brief Callback called when a node ends (e.g. `</tag>` or `<tag/>`).
  382		 */
  383		int (*end_node)(const XMLNode* node, SAX_Data* sd);
  384	
  385		/**
  386		 * \fn new_text
  387		 * \brief Callback called when text has been found in the last node (e.g. `<tag>text<...`).
  388		 */
  389		int (*new_text)(SXML_CHAR* text, SAX_Data* sd);
  390	
  391		/**
  392		 * \fn end_doc
  393		 * \brief Callback called when parsing is finished.
  394		 * No other callbacks will be called after it.
  395		 */
  396		int (*end_doc)(SAX_Data* sd);
  397	
  398		/**
  399		 * \fn on_error
  400		 * \brief Callback called when an error occurs during parsing.
  401		 * \param error_num is the error number
  402		 * \param line_number is the line number in the stream being read (file or buffer).
  403		 */
  404		int (*on_error)(ParseError error_num, int line_number, SAX_Data* sd);
  405	
  406		/**
  407		 * \fn all_event
  408		 * \brief Callback called when text has been found in the last node.
  409		 *
  410		 * \param event is the type of event for which the callback was called:
  411		 	 - `XML_EVENT_START_DOC`:
  412		 	 	 - `node` is NULL.
  413		 	 	 - 'text` is the file name if a file is being parsed, `NULL` if a buffer is being parsed.
  414		 	 	 - `n` is 0.
  415		 	 - `XML_EVENT_START_NODE`:
  416		 	 	 - `node` is the node starting, with tag and all attributes initialized.
  417		 	 	 - `text` is NULL.
  418		 	 	 - `n` is the number of lines parsed.
  419		 	 - `XML_EVENT_END_NODE`:
  420		 	 	 - `node` is the node ending, with tag, attributes and text initialized.
  421		 	 	 - `text` is NULL.
  422		 	 	 - `n` is the number of lines parsed.
  423		 	 - `XML_EVENT_TEXT`:
  424		 	 	 - `node` is NULL.
  425		 	 	 - `text` is the text to be added to last node started and not finished.
  426		 	 	 - `n` is the number of lines parsed.
  427		 	 - `XML_EVENT_ERROR`:
  428		 	 	 - Everything is NULL.
  429		 	 	 - `n` is one of the `PARSE_ERR_*`.
  430		 	 - `XML_EVENT_END_DOC`:
  431		 	 	 - `node` is `NULL`.
  432		 	 	 - `text` is the file name if a file is being parsed, NULL if a buffer is being parsed.
  433		 	 	 - `n` is the number of lines parsed.
  434		 */
  435		int (*all_event)(XMLEvent event, const XMLNode* node, SXML_CHAR* text, const int n, SAX_Data* sd);
  436	} SAX_Callbacks;
  437	
  438	/**
  439	 * \brief Helper function to initialize all `sax` members to `NULL`.
  440	 * \param sax The callbacks structure to initialize.
  441	 * \return `false` is `sax` is NULL.
  442	 */
  443	int SAX_Callbacks_init(SAX_Callbacks* sax);
  444	
  445	/**
  446	 * \brief Set of SAX callbacks used by `XMLDoc_parse_file_DOM()`.
  447	 *
  448	 * These are made available to be able to load an XML document using DOM implementation
  449	 * with user-defined code at some point (e.g. counting nodes, running search, ...).
  450	 *
  451	 * In this case, the `XMLDoc_parse_file_SAX()` has to be called instead of the `XMLDoc_parse_file_DOM()`,
  452	 * providing either these callbacks directly, or a functions calling these callbacks.<br>
  453	 * To do that, you should initialize the `doc` member of the `DOM_through_SAX` struct and call the
  454	 * `XMLDoc_parse_file_SAX()` giving this struct as a the `user` data pointer.
  455	 */
  456	typedef struct _DOM_through_SAX {
  457		XMLDoc* doc;		/**< Document to fill up. */
  458		XMLNode* current;	/**< For internal use (current father node). */
  459		ParseError error;	/**< For internal use (parse status). */
  460		int line_error;		/**< For internal use (line number when error occurred). */
  461		int text_as_nodes;	/**< For internal use (store text inside nodes as sequential TAG_TEXT nodes). */
  462	} DOM_through_SAX;
  463	
  464	int DOMXMLDoc_doc_start(SAX_Data* dom);
  465	int DOMXMLDoc_node_start(const XMLNode* node, SAX_Data* dom);
  466	int DOMXMLDoc_node_text(SXML_CHAR* text, SAX_Data* dom);
  467	int DOMXMLDoc_node_end(const XMLNode* node, SAX_Data* dom);
  468	int DOMXMLDoc_parse_error(ParseError error_num, int line_number, SAX_Data* sd);
  469	int DOMXMLDoc_doc_end(SAX_Data* dom);
  470	
  471	/**
  472	 * \brief Initialize `sax` with the "official" DOM callbacks.
  473	 */
  474	int SAX_Callbacks_init_DOM(SAX_Callbacks* sax);
  475	
  476	/* --- XMLNode methods --- */
  477	
  478	/**
  479	 * \brief Parse an attribute to an `XMLAttribute` struct.
  480	 * \param str contains the string to parse, supposed like `attrName[ ]=[ ]["]attr Value["]`.
  481	 * \param to is the position in `str` to stop at, or `-1` to parse until the end of `str`.
  482	 * \param xmlattr filled with `xmlattr->name` to `attrName` and `xmlattr->value` to `attr Value`.
  483	 * \return 0 if not enough memory or bad parameters (`str` or `xmlattr` is `NULL`),
  484			2 if last quote is missing in the attribute value,  1 if `xmlattr` was filled correctly.
  485	 */
  486	int XML_parse_attribute_to(const SXML_CHAR* str, int to, XMLAttribute* xmlattr);
  487	
  488	/**
  489	 * \fn XML_parse_attribute
  490	 * \brief Short for `XML_parse_attribute_to()` with `to=-1`.
  491	 */
  492	#define XML_parse_attribute(str, xmlattr) XML_parse_attribute_to(str, -1, xmlattr)
  493	
  494	/**
  495	 * \brief Reads a string that is supposed to be an xml tag like `<tag (attribName="attribValue")* [/]>` or `</tag>`.
  496	 * \param str The string to parse.
  497	 * \param xmlnode the `XMLNode` structure which tag name and attributes will be filled.
  498	 * \returns 0 if an error occurred (malformed `str` or memory). `TAG_*` when string is recognized.
  499	 */
  500	TagType XML_parse_1string(const SXML_CHAR* str, XMLNode* xmlnode);
  501	
  502	/**
  503	 * \brief Allocate and initialize XML nodes.
  504	 * \param n is the number of contiguous elements to allocate (to create and array).
  505	 * \return `NULL` if not enough memory, or the pointer to the elements otherwise.
  506	 */
  507	XMLNode* XMLNode_allocN(int n);
  508	
  509	/**
  510	 * \fn XMLNode_alloc
  511	 * \brief Shortcut to allocate one node only.
  512	 */
  513	#define XMLNode_alloc() XMLNode_allocN(1)
  514	
  515	/**
  516	 * \brief Allocate and initialize a new `XMLNode` of the given type, tag name and text.
  517	 * \param tag_type The node tag type.
  518	 * \param tag The node tag.
  519	 * \param text The node text.
  520	 * \return `NULL` if not enough memory, or the pointer to the node otherwise.
  521	 */
  522	
  523	XMLNode* XMLNode_new(const TagType tag_type, const SXML_CHAR* tag, const SXML_CHAR* text);
  524	
  525	/**
  526	 * \fn XMLNode_new_node_comment
  527	 * \brief Utility to create a comment: `<!--tag-->`
  528	 */
  529	#define XMLNode_new_node_comment(comment) XMLNode_new(TAG_COMMENT, (comment), NULL)
  530	/**
  531	 * \fn XMLNode_new_comment
  532	 */
  533	#define XMLNode_new_comment XMLNode_new_node_comment
  534	
  535	/**
  536	 * \fn XMLNode_new_node_text
  537	 * \brief Utility to create a simple node with text: `<tag>text</tag>`
  538	 */
  539	#define XMLNode_new_node_text(tag, text) XMLNode_new(TAG_TEXT, (tag), (text))
  540	/**
  541	 * \fn XMLNode_new_text
  542	 */
  543	#define XMLNode_new_text XMLNode_new_node_text
  544	
  545	/**
  546	 * \brief Initialize an already-allocated XMLNode.
  547	 */
  548	int XMLNode_init(XMLNode* node);
  549	
  550	/**
  551	 * \brief Tells whether a node is valid.
  552	 */
  553	#define XMLNode_is_valid(node) ((node) != NULL && (node)->init_value == XML_INIT_DONE)
  554	
  555	/**
  556	 * \brief Free a node and all its children.
  557	 */
  558	int XMLNode_free(XMLNode* node);
  559	
  560	/**
  561	 * \brief Copy a node to another one, optionally including its children.
  562	 * \param dst The node receiving the copy. N.B. thtat the node is freed first!
  563	 * \param src The node to duplicate. If `NULL`, `dst` is freed and initialized.
  564	 * \param copy_children `true` to include `src` children (recursive copy).
  565	 * \return `false` in case of memory error or if `dst` is `NULL` or `src` uninitialized.
  566	 */
  567	int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children);
  568	
  569	/**
  570	 * \brief Duplicate a node, potentially with its children.
  571	 * \param node The node to duplicate.
  572	 * \param copy_children `true` to include `src` children (recursive copy).
  573	 * \return `NULL` if not enough memory, or a pointer to the new node otherwise.
  574	 */
  575	XMLNode* XMLNode_dup(const XMLNode* node, int copy_children);
  576	
  577	/**
  578	 * \brief Set the active/inactive state of `node`.
  579	 *
  580	 * Set `active` to `true` to activate `node` and all its children, and enable its use
  581	 * in other functions (e.g. `XMLDoc_print()`, ...).
  582	 */
  583	int XMLNode_set_active(XMLNode* node, int active);
  584	
  585	/**
  586	 * \brief Set node tag.
  587	 * \param node The node to set.
  588	 * \param tag The tag to set in `node`. A *copy* of `tag` will be assigned to `node->tag`, using `strdup()`.
  589	 * \return `false` for memory error, `true` otherwise.
  590	 */
  591	int XMLNode_set_tag(XMLNode* node, const SXML_CHAR* tag);
  592	
  593	/**
  594	 * \brief Set the node type to one of `TagType` or any user-registered tag.
  595	 * \return 'false' when the node or the 'tag_type' is invalid.
  596	 */
  597	int XMLNode_set_type(XMLNode* node, const TagType tag_type);
  598	
  599	/**
  600	 * \brief Add an attribute to a node or update an existing one.
  601	 * \param node The node to which add/update an attribute.
  602	 * \param attr_name The attribute name. A *copy* will be assigned through `strdup()`.
  603	 * \param attr_value The attribute value. A *copy* will be assigned through `strdup()`.
  604	 * \return the new number of attributes, or -1 for memory problem.
  605	 */
  606	int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_CHAR* attr_value);
  607	
  608	/**
  609	 * \brief Retrieve an attribute value, based on its name, returning a default value if the attribute
  610	 * 		does not exist.
  611	 * \param node The node.
  612	 * \param attr_name The attribute name to search.
  613	 * \param attr_value A pointer receiving a *copy* of the attribute value (from `strdup()`).
  614	 * \param default_attr_value If `attr_name` does not exist in `node`, a *copy* (from `strdup()`)
  615	 * 		of this string will be stored in `attr_value`.
  616	 * \return `false` when the `node` is invalid, `attr_name` is NULL or empty, or `attr_value` is NULL.
  617	 */
  618	int XMLNode_get_attribute_with_default(XMLNode* node, const SXML_CHAR* attr_name, const SXML_CHAR** attr_value, const SXML_CHAR* default_attr_value);
  619	
  620	/**
  621	 * \fn XMLNode_get_attribute
  622	 * \brief Helper macro that retrieve an attribute value, or an empty string if the attribute does not exist.
  623	 */
  624	#define XMLNode_get_attribute(node, attr_name, attr_value) XMLNode_get_attribute_with_default(node, attr_name, attr_value, C2SX(""))
  625	
  626	/**
  627	 * \return the number of active attributes of 'node', or '-1' if 'node' is invalid.
  628	 */
  629	int XMLNode_get_attribute_count(const XMLNode* node);
  630	
  631	/**
  632	 * \brief Search for the active attribute `attr_name` in `node`, starting from index `isearch`
  633	 * and returns its index, or -1 if not found or error.
  634	 */
  635	int XMLNode_search_attribute(const XMLNode* node, const SXML_CHAR* attr_name, int isearch);
  636	
  637	/**
  638	 * \brief Remove attribute index `i_attr`.
  639	 * \return the new number of attributes or -1 on invalid arguments.
  640	 */
  641	int XMLNode_remove_attribute(XMLNode* node, int i_attr);
  642	
  643	/**
  644	 * \brief Remove all attributes from `node`.
  645	 */
  646	int XMLNode_remove_all_attributes(XMLNode* node);
  647	
  648	/**
  649	 * Set node text to a copy of `text` (from `strdup()`), or remove text if set to `NULL`.
  650	 * \return `true` when successful, `false` on error.
  651	 */
  652	int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text);
  653	
  654	/**
  655	 * \fn XMLNode_remove_text
  656	 * \brief Helper macro to remove text from `node`.
  657	 */
  658	#define XMLNode_remove_text(node) XMLNode_set_text(node, NULL);
  659	
  660	/**
  661	 * \brief Add a child to a node.
  662	 * \return `false` for memory problem, `true` otherwise.
  663	 */
  664	int XMLNode_add_child(XMLNode* node, XMLNode* child);
  665	
  666	/**
  667	 * \brief Insert a node at a given position.
  668	 * \param node The node to which inserting the child node.
  669	 * \param child The node to insert.
  670	 * \param index The insert position: if `index <= 0`: will be the first child (0).
  671	 * 		If `index >= child->father->n_children`: will be the last child.
  672	 * \return 'false' if 'node' is not initialized, 'true' otherwise.
  673	 */
  674	int XMLNode_insert_child(XMLNode* node, XMLNode* child, int index);
  675	
  676	/**
  677	 * \fn XMLNode_insert_before
  678	 * \brief Insert a node before the given node (i.e. at its index).
  679	 */
  680	#define XMLNode_insert_before(node, child) XMLNode_insert_child(node, child, XMLNode_get_index(node))
  681	/**
  682	 * \fn XMLNode_insert_after
  683	 * \brief Insert a node after the given node.
  684	 */
  685	#define XMLNode_insert_after(node, child) XMLNode_insert_child(node, child, XMLNode_get_index(node)+1)
  686	
  687	/**
  688	 * \brief Move a child node among its siblings.
  689	 * \param node The node which children should be moved.
  690	 * \param from Position of the node to move.
  691	 * \param to Position to move to. Moved to first position if `to <= 0` or last position
  692	 * 		if `to >= node->n_children`.
  693	 * \return `false` if `node` is not initialized or `from` is invalid. `true` otherwise.
  694	 */
  695	int XMLNode_move_child(XMLNode* node, int from, int to);
  696	
  697	/**
  698	 * \brief Get the number of *active* children of a node.
  699	 * \param node The node.
  700	 * \return the number of active children nodes of `node`, or -1 if `node` is invalid.
  701	 * 		N.B. that it can be different from `node->n_children` if some nodes are deactivated!
  702	 */
  703	int XMLNode_get_children_count(const XMLNode* node);
  704	
  705	/**
  706	 * \brief Get the node position among its *active* siblings.
  707	 * \param node The node.
  708	 * \return `node` position among its siblings, -1 if `node` is invalid or -2 if `node` could
  709	 * 		not be found in its father's children (in which case I'd appreciate a bug report with
  710	 * 		the XML and steps that led to that situation!).
  711	 */
  712	int XMLNode_get_index(const XMLNode* node);
  713	
  714	/**
  715	 * \brief Get an *active* node.
  716	 * \param node The node.
  717	 * \param i_child The active node index to retrieve.
  718	 * \return the `i_child`th *active* node.
  719	 */
  720	XMLNode* XMLNode_get_child(const XMLNode* node, int i_child);
  721	
  722	/**
  723	 * \brief Remove the `i_child`th *active* child of the node.
  724	 * \param node The node.
  725	 * \param i_child The active node index to retrieve.
  726	 * \param free_child if `true`, free the child node itself (and its children, recursively).
  727	 * 		This parameter is usually `true` but should be `false` when child nodes are pointers
  728	 * 		to local or global variables instead of user-allocated memory.
  729	 * \return the new number of children or -1 on invalid arguments.
  730	 */
  731	int XMLNode_remove_child(XMLNode* node, int i_child, int free_child);
  732	
  733	/**
  734	 * \brief Remove (and frees) all children from the node.
  735	 * \param node The node.
  736	 * \return `true`.
  737	 */
  738	int XMLNode_remove_children(XMLNode* node);
  739	
  740	/**
  741	 * \param node1 The first node to test.
  742	 * \param node2 The second node to test.
  743	 * \return `true` if `node1` is the same as `node2` (i.e. same tag, same active attributes)
  744	 * 		but *not necessarily* the same children.
  745	 */
  746	int XMLNode_equal(const XMLNode* node1, const XMLNode* node2);
  747	
  748	/**
  749	 * \brief Get the next sibling node.
  750	 * \param node The node which sibling to retrieve.
  751	 * \return the next sibling of the node, or `NULL` if `node` is invalid or the last child
  752	 * 		or if its father could not be determined (i.e. `node` is a root node).
  753	 */
  754	XMLNode* XMLNode_next_sibling(const XMLNode* node);
  755	
  756	/**
  757	 * \brief Get the "next" node: first child (if any) of next sibling otherwise.
  758	 * \param node The node.
  759	 * \return the next node in XML order, or `NULL` if `node` is invalid or the last node.
  760	 */
  761	XMLNode* XMLNode_next(const XMLNode* node);
  762	
  763	
  764	
  765	/* --- XMLDoc methods --- */
  766	
  767	
  768	/**
  769	 * \brief Initializes an already-allocated XML document.
  770	 * \param doc The document to initialize.
  771	 * \return `false` if `doc` is NULL.
  772	 */
  773	int XMLDoc_init(XMLDoc* doc);
  774	
  775	/**
  776	 * \brief Free an XML document, including all of its nodes, recursively.
  777	 * \param doc The document to initialize.
  778	 * \return `false` if `doc` was not initialized.
  779	 */
  780	int XMLDoc_free(XMLDoc* doc);
  781	
  782	/**
  783	 * \brief Set the new document root node.
  784	 * \param doc The document to initialize.
  785	 * \param i_root The element index to set as root.
  786	 * \return `false` if `doc` is not initialized or `i_root` is invalid, `true` otherwise.
  787	 */
  788	int XMLDoc_set_root(XMLDoc* doc, int i_root);
  789	
  790	/**
  791	 * \brief Add a node to the document.
  792	 *
  793	 * If the node type is `TAG_FATHER`, it also sets the document root node if previously undefined.
  794	 * \param doc The document.
  795	 * \param node The node to add.
  796	 * \return the node index, or -1 for uninitialized `doc` or `node`, or memory error.
  797	 */
  798	int XMLDoc_add_node(XMLDoc* doc, XMLNode* node);
  799	
  800	/**
  801	 * \brief Remove a node from the document root nodes. Inactive nodes can be removed like this.
  802	 * \param doc The XML document.
  803	 * \param i_node The node index to remove
  804	 * \param free_node if `true`, free the node itself. This parameter is usually `true`
  805	 * 		but should be 'false' when the node is a pointer to local or global variable instead of
  806	 * 		user-allocated memory.
  807	 * \return `true` if node was removed or `false` if `doc` or `i_node` is invalid.
  808	 */
  809	int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node);
  810	
  811	#define XMLDoc_remove_root_node XMLDoc_remove_node
  812	
  813	/**
  814	 * \brief Shortcut macro to retrieve root node from a document. Equivalent to `doc->nodes[doc->i_root]`,
  815	 *		or `NULL` if there is no root node.
  816	 */
  817	#define XMLDoc_root(doc) (((doc)->i_root) < 0 ? NULL : ((doc)->nodes[(doc)->i_root]))
  818	
  819	/**
  820	 * \brief Shortcut macro to add a node to 'doc' root node. Equivalent to `XMLDoc_add_child_root(XMLDoc* doc, XMLNode* child)`.
  821	 */
  822	#define XMLDoc_add_child_root(doc, child) XMLNode_add_child((doc)->nodes[(doc)->i_root], (child))
  823	
  824	/**
  825	 * \brief Default quote to use to print attribute value (defaults to a double. quote `"`).
  826	 *
  827	 * User can redefine it with its own character by adding a `-DXML_DEFAULT_QUOTE` compiler option.
  828	 */
  829	#ifndef XML_DEFAULT_QUOTE
  830	#define XML_DEFAULT_QUOTE C2SX('"')
  831	#endif

  832	
  833	/**
  834	 * \brief Print the node and its children to a file (that can be `stdout`).
  835	 *
  836	 * \param node The node to print.
  837	 * \param f The file to print to (can be `stdout`).
  838	 * \param tag_sep The string to use to separate nodes from each other (usually `"\n"`).
  839	 * \param child_sep The additional string to put for each child level (usually `"\t"`).
  840	 * \param attr_sep The additional string to put to separate attributes (usually `" "`).
  841	 * \param keep_text_spaces indicates that text should not be printed if it is composed of
  842	 * 		spaces, tabs or new lines only (e.g. when XML document spans on several lines due to
  843	 * 		pretty-printing).
  844	 * \param sz_line The maximum number of characters that can be put on a single line. The
  845	 * 		node remainder will be output to extra lines.
  846	 * \param nb_char_tab How many characters should be counted for a tab when counting characters
  847	 * 		in the line. It usually is 8 or 4, but at least 1.
  848	 * \return `false` on invalid arguments (`NULL` `node` or `f`), `true` otherwise.
  849	 */
  850	int XMLNode_print_attr_sep(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab);
  851	
  852	/**
  853	 * \brief For backward compatibility (`attr_sep` is a space).
  854	 */
  855	#define XMLNode_print(node, f, tag_sep, child_sep, keep_text_spaces, sz_line, nb_char_tab) XMLNode_print_attr_sep(node, f, tag_sep, child_sep, C2SX(" "), keep_text_spaces, sz_line, nb_char_tab)
  856	
  857	/**
  858	 * \brief Print the node "header": `<tagname attribname="attibval" ...[/]>`, spanning it on several lines if needed.
  859	 * \return `false` on invalid arguments (`NULL` `node` or `f`), `true` otherwise.
  860	 */
  861	int XMLNode_print_header(const XMLNode* node, FILE* f, int sz_line, int nb_char_tab);
  862	
  863	/**
  864	 * \brief Prints the XML document using `XMLNode_print_attr_sep()` on all document nodes.
  865	 */
  866	int XMLDoc_print_attr_sep(const XMLDoc* doc, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab);
  867	
  868	/* For backward compatibility */
  869	#define XMLDoc_print(doc, f, tag_sep, child_sep, keep_text_spaces, sz_line, nb_char_tab) XMLDoc_print_attr_sep(doc, f, tag_sep, child_sep, C2SX(" "), keep_text_spaces, sz_line, nb_char_tab)
  870	
  871	/**
  872	 * \brief Parse a file into an initialized XML document (DOM mode).
  873	 * \param filename The file to parse.
  874	 * \param doc The document to parse into.
  875	 * \param text_as_nodes should be non-zero to put text into separate TAG_TEXT nodes.
  876	 * \return `false` in case of error (memory or unavailable filename, malformed document), `true` otherwise.
  877	 */
  878	int XMLDoc_parse_file_DOM_text_as_nodes(const SXML_CHAR* filename, XMLDoc* doc, int text_as_nodes);
  879	
  880	/**
  881	 * \brief For backward compatibility (`text_as_nodes` is 0)
  882	 */
  883	#define XMLDoc_parse_file_DOM(filename, doc) XMLDoc_parse_file_DOM_text_as_nodes(filename, doc, 0)
  884	
  885	/**
  886	 * \brief Parse a memory buffer into an initialized document (DOM mode).
  887	 * \param buffer The memory buffer to parse.
  888	 * \param name The buffer name (to identify several buffers if run concurrently).
  889	 * \param doc The document to parse into.
  890	 * \param text_as_nodes should be non-zero to put text into separate TAG_TEXT nodes.
  891	 * \return `false` in case of error (memory or unavailable filename, malformed document), `true` otherwise.
  892	 */
  893	int XMLDoc_parse_buffer_DOM_text_as_nodes(const SXML_CHAR* buffer, const SXML_CHAR* name, XMLDoc* doc, int text_as_nodes);
  894	
  895	/**
  896	 * \brief For backward compatibility (`text_as_nodes` is 0)
  897	 */
  898	#define XMLDoc_parse_buffer_DOM(buffer, name, doc) XMLDoc_parse_buffer_DOM_text_as_nodes(buffer, name, doc, 0)
  899	
  900	/**
  901	 * \brief Parse an XML file, calling SAX callbacks.
  902	 * \param filename The file to parse.
  903	 * \param sax The SAX callbacks that will be called by the parser on each XML event.
  904	 * \param user A user-given pointer that will be given back to all callbacks.
  905	 * \return `false` in case of error (memory or unavailable filename, malformed document) or when requested
  906	 * 		by a SAX callback. `true` otherwise.
  907	 */
  908	int XMLDoc_parse_file_SAX(const SXML_CHAR* filename, const SAX_Callbacks* sax, void* user);
  909	
  910	/**
  911	 * \brief Parse an XML buffer, calling SAX callbacks.
  912	 * \param buffer The memory buffer to parse.
  913	 * \param buffer_len The buffer lenght, in *characters* (i.e. can be 2 bytes in unicode).
  914	 * \param name An optional buffer name.
  915	 * \param sax The SAX callbacks that will be called by the parser on each XML event.
  916	 * \param user A user-given pointer that will be given back to all callbacks.
  917	 * \return `false` in case of error (memory or unavailable filename, malformed document) or when requested
  918	 * 		by a SAX callback. `true` otherwise.
  919	 */
  920	int XMLDoc_parse_buffer_SAX_len(const SXML_CHAR* buffer, int buffer_len, const SXML_CHAR* name, const SAX_Callbacks* sax, void* user);
  921	
  922	/**
  923	 * \brief For backward compatibility (buffer length is `strlen(buffer)`).
  924	 */
  925	#define XMLDoc_parse_buffer_SAX(buffer, name, sax, user) XMLDoc_parse_buffer_SAX_len(buffer, sx_strlen(buffer), name, sax, user)
  926	
  927	/**
  928	 * \brief Parse an XML file using the DOM implementation.
  929	 */
  930	#define XMLDoc_parse_file XMLDoc_parse_file_DOM
  931	
  932	
  933	
  934	/* --- Utility functions --- */
  935	
  936	/**
  937	 * \brief Get next byte from data source.
  938	 * \return as `fgetc()` would for `FILE*`.
  939	 */
  940	int _bgetc(DataSourceBuffer* ds);
  941	
  942	/**
  943	 * \brief know if the end has been reached in a data source.
  944	 * \return as `feof()` would for `FILE*`.
  945	 */
  946	int _beob(DataSourceBuffer* ds);
  947	
  948	/**
  949	 * \brief Read a "line" from data source, eventually (re-)allocating a given buffer. A "line" is defined
  950	 * as a portion starting with character `from` (usually `<`) ending at character `to` (usually `>`).
  951	 *
  952	 * Characters read will be stored in `line` starting at `i0` (this allows multiple calls to
  953	 * `read_line_alloc()` on the same `line` buffer without overwriting it at each call).
  954	 * Searches for character `from` until character `to`. If `from` is 0, starts from
  955	 * current position in the data source. If `to` is 0, it is replaced by `\n`.
  956	 *
  957	 * \param in The data source (either `FILE*` if `in_type` is `DATA_SOURCE_FILE` or `SXML_CHAR*`
  958	 * 		if `in_type` is `DATA_SOURCE_BUFFER`).
  959	 * \param in_type specifies the type of data source to be read.
  960	 * \param line can be `NULL`, in which case it will be allocated to `*sz_line` bytes. After the function
  961	 * 		returns, `*sz_line` is the actual buffer size. This allows multiple calls to this function using
  962	 * 		the same buffer (without re-allocating/freeing).
  963	 * \param sz_line is the size of the buffer `line` if previously allocated (in `SXML_CHAR`, not byte!).
  964	 * 		If `NULL` or 0, an internal value of `MEM_INCR_RLA` is used.
  965	 * \param i0 The position where read characters are stored in `line`.
  966	 * \param from The character indicating a start of line.
  967	 * \param to The character indicating an end of line.
  968	 * \param keep_fromto if 0, removes characters `from` and `to` from the line (stripping).
  969	 * \param interest is a special character of interest, usually `\n` so we can count line numbers in the
  970	 * 		data source (valid only if `interest_count` is not `NULL`).
  971	 * \param interest_count if not `NULL`, will receive the count of `interest` characters while searching.
  972	 * \returns the number of characters in the line or 0 if an error occurred.
  973	 */
  974	int read_line_alloc(void* in, DataSourceType in_type, SXML_CHAR** line, int* sz_line, int i0, SXML_CHAR from, SXML_CHAR to, int keep_fromto, SXML_CHAR interest, int* interest_count);
  975	
  976	/**
  977	 * \brief Concatenates the string pointed at by `src1` with `src2` into `*src1` and return it.
  978	 * \return `*src1`, or `NULL` if out of memory.
  979	 */
  980	SXML_CHAR* strcat_alloc(SXML_CHAR** src1, const SXML_CHAR* src2);
  981	
  982	/**
  983	 * \brief Strip whitespaces at the beginning and end of a string, modifying it.
  984	 * \param str The string to strip.
  985	 * \param repl_sq if not null, squeezes spaces to an single character `repl_sq`.
  986	 * \returns the stripped string, which can be `str` directly if there are no whitespaces
  987	 * 		at the beginning.
  988	 * 		N.B. that if `str` was allocated, it should be freed later, *not* the returned
  989	 * 		reference.
  990	 */
  991	SXML_CHAR* strip_spaces(SXML_CHAR* str, SXML_CHAR repl_sq);
  992	
  993	/**
  994	 * \brief Remove `\` characters from `str`, modifying it.
  995	 * \return `str` unescaped.
  996	 */
  997	SXML_CHAR* str_unescape(SXML_CHAR* str);
  998	
  999	/**
 1000	 * \brief Split a string into a left and right part around a given separator.
 1001	 *
 1002	 * The beginning and end indices of left part are stored in `l0` and `l1` while the
 1003	 * right part's are stored in `r0` and `r1`. The separator position is stored at `i_sep`
 1004	 * (whenever these are not `NULL`).
 1005	 * Whenever the right member is empty (e.g. `"attrib"` or `"attrib="`), `*r0` is set to
 1006	 * `strlen(str)` and `*r1` to `*r0-1` (crossed).
 1007	 *
 1008	 * \param str The string to split.
 1009	 * \param sep The separator (e.g. `=`).
 1010	 * \param l0 will contain the index of the left part first character in `str`.
 1011	 * \param l1 will contain the index of left part last character in `str`.
 1012	 * \param i_sep will contain the index of the separator in `str`, or -1 if not found.
 1013	 * \param r0 will contain the index of the left part first character in `str`.
 1014	 * \param r1 will contain the index of left part last character in `str`.
 1015	 * \param ignore_spaces Is `true`, computed indexes will not take into account potential
 1016	 *		spaces around the separator as well as before left part and after right part, so
 1017	 *		`&quot;name=val&quot;` will be equivalent to `&quot;name = val&quot;`.
 1018	 * \param ignore_quotes If `true`, quotes (`&quot;` or `&apos;`) will not be taken into account when parsing left
 1019	 * 		and right members, so `&quot;name = 'val'&quot;` will be equivalent to `&quot;name = val&quot;`.
 1020	 *
 1021	 * \return `false` when `str` is malformed, `true` when splitting was successful.
 1022	 */
 1023	int split_left_right(SXML_CHAR* str, SXML_CHAR sep, int* l0, int* l1, int* i_sep, int* r0, int* r1, int ignore_spaces, int ignore_quotes);
 1024	
 1025	/**
 1026	 Detect a potential BOM at the current file position and read it into 'bom' (if not NULL,
 1027	 'bom' should be at least 5 bytes). It also moves the 'f' beyond the BOM so it's possible to
 1028	 skip it by calling 'freadBOM(f, NULL, NULL)'. If no BOM is found, it leaves 'f' file pointer
 1029	 is reset to its original location.
 1030	 If not null, 'sz_bom' is filled with how many bytes are stored in 'bom'.
 1031	 Return the BOM type or BOM_NONE if none found (empty 'bom' in this case).
 1032	 */
 1033	BOM_TYPE freadBOM(FILE* f, unsigned char* bom, int* sz_bom);
 1034	
 1035	/**
 1036	 * \brief Replace occurrences of special HTML characters escape sequences (e.g. `"&amp;"`)
 1037	 * by its character equivalent (e.g. `"&"`).
 1038	 *
 1039	 * If `html == str`, replacement is made in `str` itself, overwriting it.
 1040	 * If `str` is `NULL`, replacement is made into `html`, overwriting it.
 1041	 *
 1042	 * \param html The string containing the HTML escapes (e.g. `"h&ocirc;tel"`).
 1043	 * \param str The string to receive the unescaped string (e.g. `"hôtel"`).
 1044	 *
 1045	 * \returns The unescaped string (`str`, or `html` if `str` was `NULL`).
 1046	 */
 1047	SXML_CHAR* html2str(SXML_CHAR* html, SXML_CHAR* str);
 1048	
 1049	/**
 1050	 * \brief Replace occurrences of special characters (e.g. `&`) into their XML escaped
 1051	 * equivalent (e.g. `"&amp;"`).
 1052	 *
 1053	 * `html` is supposed allocated to the correct size (e.g. using `__malloc(strlen_html(str)+30)`)
 1054	 * and *different* from `str` (unlike `html2str()`), as the string will expand.
 1055	 * If it is `NULL`, `str` will be analyzed and a string will be allocated to the exact size and
 1056	 * returned. In that case, it is the responsibility of the caller to `free()` the result!
 1057	 * \return `html`, or `NULL` if `str` or `html` are `NULL`, or when `html` is `str`.
 1058	 */
 1059	SXML_CHAR* str2html(SXML_CHAR* str, SXML_CHAR* html);
 1060	
 1061	/**
 1062	 * \brief Compute the length of a string as if all its special character were replaced by their HTML
 1063	 * escapes.
 1064	 * \return 0 if `str` is NULL.
 1065	 */
 1066	int strlen_html(SXML_CHAR* str);
 1067	
 1068	/**
 1069	 * \brief Print a string to a file, transforming special characters into their HTML equivalent.
 1070	
 1071	 * This is more efficient than a call to `fprintf(f, str2html(str))` as it does not need memory
 1072	 * allocation; rather, it converts characters on-the-fly while writing.
 1073	 * If `f` is NULL, does not print `str` but rather counts the number of characters that
 1074	 * would be printed.
 1075	 *
 1076	 * \returns the number of output characters.
 1077	 */
 1078	int fprintHTML(FILE* f, SXML_CHAR* str);
 1079	
 1080	#ifdef __cplusplus
 1081	}
 1082	#endif

 1083	
 1084	#endif

 1085	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.