/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*----------------------------------------------------------------------------*/
/*! @file u_xml.h 
 *  $RCSfile: $
 *  $Revision: #1 $
 *  $Date: 2015/11/10 $
 *  $Author: dtvbm11 $
 *  
 *  @par Description:
 *         This header file contains xml specific
 *         definitions, which are exported.
 */
/*----------------------------------------------------------------------------*/

/*!
* @defgroup    groupMW_INET_XML XML API
* @ingroup     groupMW_INET
* @brief       Provide an external API about XML.
* %INET_XML API would be used by user who wants to parse xml.
* 
*
* @{
* */

#ifndef _U_XML_H
#define _U_XML_H

#include "u_common.h"

/*------------------------------------------------------------------*/
/*! @enum XML_ERROR_CODE
 *  @brief XML error code
 *  @code
 *  typedef enum
 *  { 
 *      XML_SUCCESS = 0,            
 *      XML_INVALID_STATE_ERR = -1, 
 *      XML_OUT_OF_MEMORY_ERR = -2, 
 *      XML_PARSE_FAILURE = -3      
 *  } XML_ERROR_CODE;
 *  @endcode
 *  @li@c  XML_SUCCESS                       -Success
 *  @li@c  XML_INVALID_STATE_ERR      -XML parser state error
 *  @li@c  XML_OUT_OF_MEMORY_ERR    -Out of memory
 *  @li@c  XML_PARSE_FAILURE              -Other error 
 */
/*------------------------------------------------------------------*/
typedef enum
{ 
  XML_SUCCESS = 0,
  XML_INVALID_STATE_ERR = -1,
  XML_OUT_OF_MEMORY_ERR = -2,
  XML_PARSE_FAILURE = -3
} XML_ERROR_CODE;


/*------------------------------------------------------------------*/
/*! @struct XML_ATTRIBUTE_T
 *  @brief XML Attribute
 *  @code
 *  typedef struct _XML_ATTRIBUTE_T
 *  { 
 *      struct _XML_ATTRIBUTE_T      *pt_next;            
 *      struct _XML_ATTRIBUTE_T      *pt_prev;
 *      CHAR                         *ps_name; 
 *      CHAR                         *ps_value;     
 *  } XML_ATTRIBUTE_T;
 *  @endcode
 *  @li@c  pt_next                    -Linked list next
 *  @li@c  pt_prev                    -Linked list prev
 *  @li@c  ps_name                  -Attribute name
 *  @li@c  ps_value                  -Attribute value 
 */
/*------------------------------------------------------------------*/
typedef struct _XML_ATTRIBUTE_T
{
    struct _XML_ATTRIBUTE_T      *pt_next; 
    struct _XML_ATTRIBUTE_T      *pt_prev;
  
    CHAR                         *ps_name;
    CHAR                         *ps_value; 
    INT32                        i4_type;

} XML_ATTRIBUTE_T;

/*------------------------------------------------------------------*/
/*! @struct XML_T
 *  @brief XML structure
 *  @code
 *  typedef struct _XML_T
 *  { 
 *      struct _XML_T             *pt_next;           
 *      struct _XML_T             *pt_prev;
 *      UINT32                      ui4_reference_counter;
 *      struct _XML_T             *pt_parent;  
 *        CHAR                         *ps_name; 
 *        CHAR                         *ps_value;  
 *        struct _XML_T             *pt_ref;     
 *        struct
 *        {
 *            struct _XML_T          *head;
 *            struct _XML_T          *tail;
 *            UINT32                   ui4_count;
 *        } child_ele_list; 
 *        struct
 *        {
 *            XML_ATTRIBUTE_T        *head;
 *            XML_ATTRIBUTE_T        *tail;
 *            UINT32                         ui4_count;
 *        } attrlist; 
 *  } XML_T;
 *  @endcode
 *  @li@c  pt_next                                -Linked list next
 *  @li@c  pt_prev                                -Linked list prev
 *  @li@c  ui4_reference_counter           -Reference counter
 *  @li@c  pt_parent                             -Parent element 
 *  @li@c  ps_name                              -Element name
 *  @li@c  ps_value                              -Element  Value
 *  @li@c  pt_ref                                   -Reference XML element
 *  @li@c  child_ele_list                         -Child elements
 *  @li@c  attrlist                                   -Attribute list
 */
/*------------------------------------------------------------------*/
typedef struct _XML_T
{

    struct _XML_T             *pt_next;
    struct _XML_T             *pt_prev;
  
    UINT32                     ui4_reference_counter; 

    struct _XML_T              *pt_parent;
 
    CHAR                       *ps_name;
    CHAR                       *ps_value;
    INT32                       i4_type;

    struct _XML_T              *pt_ref;

    struct
    {
        struct _XML_T          *head;
        struct _XML_T          *tail;
        UINT32                 ui4_count;
    } child_ele_list;
   
    struct
    {
      XML_ATTRIBUTE_T        *head;
      XML_ATTRIBUTE_T        *tail;
      UINT32                 ui4_count;
    } attrlist;
   
} XML_T;


typedef struct _XML_T  XML_Element;                /**<  Typedef XML for XML element  */

/*------------------------------------------------------------------*/
/*! @struct XML_NODE_T
 *  @brief XML node for more than one element
 *  @code
 *  typedef struct _XML_NODE_T
 *  { 
 *      UINT32                    ui4_max;          
 *      UINT32                    ui4_alloced; 
 *  #define XML_NODE_INIT_SIZE   (4)
 *      VOID                      **ppv_index; 
 *  } XML_NODE_T;
 *  @endcode
 *  @li@c  ui4_max                           -Number of the node
 *  @li@c  ui4_alloced                       -Allocation of the index
 *  @li@c  XML_NODE_INIT_SIZE       -Initial allocation size of XML node index
 *  @li@c  ppv_index                        -Index to each element
 */
/*------------------------------------------------------------------*/
typedef struct _XML_NODE_T 
{
    UINT32                    ui4_max;
    UINT32                    ui4_alloced; 

#define XML_NODE_INIT_SIZE   (4)

    VOID                      **ppv_index;
} XML_NODE_T;


/*------------------------------------------------------------------*/
/*! @struct XML_DEQUE_T
 *  @brief XML deque structure
 *  @code
 *  typedef struct _XML_DEQUE_T
 *  { 
 *      struct _XML_DEQUE_T         *pt_prev;            
 *      XML_T                               *pt_xml_element;    
 *  } XML_DEQUE_T;
 *  @endcode
 *  @li@c  pt_prev                           -Previous pointer 
 *  @li@c  pt_xml_element               -XML element
 */
/*------------------------------------------------------------------*/
typedef struct _XML_DEQUE_T
{
   
    struct _XML_DEQUE_T         *pt_prev; 
      
    XML_T                       *pt_xml_element;
} XML_DEQUE_T;


/*------------------------------------------------------------------*/
/*! @struct XML_USER_DATA_T
 *  @brief XML user data structure
 *  @code
 *  typedef struct _XML_USER_DATA_T
 *  { 
 *      XML_T                        *pt_doc;  
 *      XML_T                        *pt_xml_current;     
 *      XML_DEQUE_T                  *pt_deque;
 *      INT32                         i4_error;
 *  #define XML_FLAG_CDATA  (1 << 0)
 *      INT32                         i4_flags;
 *      VOID                          *pv_parser; 
 *  } XML_USER_DATA_T;
 *  @endcode
 *  @li@c  ps_version                       -XML version 
 *  @li@c  ps_encoding                     -XML encoding
 *  @li@c  pt_doc                             -XML document
 *  @li@c  pt_xml_current                 -Current XML element
 *  @li@c  pt_deque                          -Deque of XML element
 *  @li@c  XML_FLAG_CDATA             - 
 *  @li@c  i4_error                            -Error code
 *  @li@c  i4_flags                             -Flags 
 *  @li@c  pv_parser                         -XML parser
 */
/*------------------------------------------------------------------*/
typedef struct _XML_USER_DATA_T
{
    XML_T                        *pt_doc;
    XML_T                        *pt_xml_current;
    XML_DEQUE_T                  *pt_deque;

    INT32                         i4_error;

#define XML_FLAG_CDATA  (1 << 0)

    INT32                         i4_flags;
     
    VOID                          *pv_parser;

    VOID                          *pv_user_private;
} XML_USER_DATA_T;

#define XML_CHECK_FLAG(A, B)      ((A) & (B))            /**<   check flag A has flag B     */
#define XML_SET_FLAG(A, B)        ((A) |= (B))           /**<   flag A set flag B     */
#define XML_UNSET_FLAG(A, B)      ((A) &= ~(B))          /**<   flag A unset flag B     */

#define XML_Name(N)               ((N)->ps_name)         /**<   get name information     */
#define XML_Value(N)              ((N)->ps_value)        /**<   get value information     */
#define XML_NodeLength(N)         ((N)->ui4_max)         /**<   get number of the node     */
#define XML_NodeItem(N,I)         ((N)->ppv_index[I])    /**<   get node of index     */

#endif

/*! @} */
