/******************************************************************************
*
* Copyright (C) 2019  Brian Berlin
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* 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 the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************
*
* FILENAME: toml.h
*
* DESCRIPTION:  This contains function declarations for toml config file 
*               handling.  We do our own since since all the parsers out there
*               don't let you emit in the same style you wrote in
*
* AUTHORS:  bberlin
*
* CHANGE LOG
*
*  VER     DATE     CHANGED BY   DESCRIPTION OF CHANGE
* ------  --------  ----------   ------------------------------------
* 0.9.0   01/29/19  bberlin      Creation
******************************************************************************/
#ifndef toml_h
#define toml_h

#include <stdio.h>

typedef enum {
    TOML_NONE,
    TOML_FILE,
    TOML_INTERNAL_ERROR,
    TOML_INVALID,
    TOML_INVALID_TABLE_DECLARATION,
    TOML_INVALID_KEY_DECLARATION,
    TOML_INVALID_KEY,
    TOML_INVALID_ARRAY,
    TOML_INVALID_ARRAY_MIXED_TYPES,
} e_toml_error;

typedef enum {
    TOML_UNKNOWN,
    TOML_ROOT,
    TOML_TABLE,
    TOML_TABLE_ARY,
    TOML_COMMENT,
    TOML_NEWLINE,
    TOML_COMMA,
    TOML_KEY,
    TOML_ARRAY,
    TOML_ARRAY_END,
    TOML_INT,
    TOML_FLOAT,
    TOML_DQ_STRING,
    TOML_SQ_STRING,
    TOML_TSQ_STRING,
    TOML_TDQ_STRING,
    TOML_BOOLEAN,
    TOML_DATE,
    TOML_TIME
} e_toml_type;

struct toml_node {
    e_toml_type type;
    char *value;
    char *indent_initial;
    char *indent_to_equals;
    char *to_newline;
    int values_per_line;
    int has_ending_backslash;
    struct toml_node *child;
    struct toml_node *parent;
    struct toml_node *next;
    struct toml_node *previous;
};

typedef struct toml_node t_toml_node;

typedef struct {
    FILE *fp;
    int in_t_dquote;
    int in_t_squote;
    int in_key_ary;
    int line_no;
    t_toml_node *current_node;
    t_toml_node *current_table;
} t_toml_parser;

/*
 * Function Declarations
 */
t_toml_node * toml_parse_file ( char *filename );
int toml_write_file ( t_toml_node *tree, const char *filename );
void toml_free_node_tree ( t_toml_node *tree );
void toml_free_single_node ( t_toml_node *node );
int toml_get_error ( int *line_no );
const char * toml_get_error_string ( e_toml_error err_val );
t_toml_node * toml_search_for_node ( t_toml_node *node, const char *key_tag, 
                                     const char *table_tag );
t_toml_node * toml_find_node ( t_toml_node *node, const char *tag );
int toml_set_value ( t_toml_node *node, const char *tag, const char *value );
char * toml_get_key_value ( t_toml_node *node, const char *tag );
t_toml_node* toml_get_key_array_values_start ( t_toml_node *node, 
                                                 const char *tag );

char * toml_get_value_by_index_from_array_node ( t_toml_node *node, int index );
char * toml_get_array_value_by_index ( t_toml_node *node, const char *tag, 
                                       int index );
int toml_get_num_array_elements ( t_toml_node *node );
int toml_start_array_with_value ( t_toml_node *node, const char *tag, int type, 
                                    const char *value, const char *indent_initial, 
                                    const char *indent_to_equals );
int toml_insert_array_value_by_index ( t_toml_node *node, const char *tag, 
                                    const char *value, const char *indent_initial, 
                                    const char *comma_to_newline, 
                                    const char *prev_indent_initial, int index );
t_toml_node * toml_get_table_array_by_key ( t_toml_node *node, const char *table, 
        const char *key, const char *tag );
int toml_add_key ( t_toml_node *table, const char *key, e_toml_type type, 
                   const char *value, const char *indent_initial, 
                   const char *indent_to_equals );
t_toml_node * toml_add_table ( t_toml_node *parent, const char *name, e_toml_type type,
                     const char *indent_initial );
t_toml_node * toml_add_generic_node ( t_toml_node *table, e_toml_type type, 
                    const char *value, const char *indent_initial );
int toml_delete_node_and_children ( t_toml_node *parent, const char *name);
int toml_delete_array_value_by_index ( t_toml_node *node, const char *tag, 
                                       int index );
#endif
