/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#pragma once
#include "common.h"
#include "file.h"

#define XML_DOING_NOTHING 0x00
#define XML_PARSING_TAG_NAME 0x01
#define XML_LOOKING_FOR_ATTRIBUTE 0x02
#define XML_PARSING_ATTRIBUTE_NAME 0x03
#define XML_LOOKING_FOR_ATTRIBUTE_VALUE 0x04
#define XML_PARSING_ATTRIBUTE_VALUE 0x05
#define XML_PARSING_VALUE 0x06
#define XML_PARSING_CHILD 0x07
#define XML_PARSING_CLOSE 0x08

#define XML_TEXT_BUFFER_MAX 256
#define XML_CHILD_COUNT_MAX 16
#define XML_ATTRIBUTE_MAX 16

typedef struct _xml_t xml_t;

typedef struct _xml_t {
  char *node;
  char *value;

  char *attributeNames[XML_ATTRIBUTE_MAX];
  char *attributeDatas[XML_ATTRIBUTE_MAX];
  uint8_t attributeCount;

  xml_t *children;
  uint8_t childrenCount;
} xml_t;

/**
 * Load an XML child from a string buffer.
 * 
 * @param xml XML to load.
 * @param data Data to parse
 * @param i Character index within the data
 * @return The index in the data string this XML node ends.
 */
int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i);

/**
 * Load an XML String into an XML memory.
 * 
 * @param xml XML to load into.
 * @param data XML string.
 */
void xmlLoad(xml_t *xml, char *data);

/**
 * Dispose a previously loaded XML.
 * 
 * @param xml XML to dispose.
 */
void xmlDispose(xml_t *xml);

int16_t xmlGetAttributeByName(xml_t *xml, char *name);

bool xmlIsWhitespace(char c);