/*
  Name: TCFP 0.0.1 Example code
  Copyright: Tiiffi 2008
  Author: Tiiffi
  Date: 06.10.08 01:11
  Description: Intro to library functions.
*/


#include <stdio.h>
#include <string.h>

#include "../tcfp_parser.h"
/* #include "../tcfp_parser.c" */


#define CFG_FILE "example.conf" /* define configuration file filename */



int main(void)
{
    /* here goes the config data */
    TCFP_config_data *config_data;   

    /* 
     * here goes the values we get from the config file
     * basically these are just null terminated c-strings
     */
    TCFP_config_value  *cfg_value1;
    TCFP_config_value  *cfg_value2;
    TCFP_config_value  *cfg_value3;

    /* we convert two keyvalues to these datatypes later */
    int   cValue2;
    float cValue3;


    /* ================================================= * 
     *      read config file to buffer                   *
     * ================================================= */
    
    config_data = TCFP_load_config(CFG_FILE);
    if(config_data == NULL) /* something went wrong */ 
    {
        puts("Cannot read the config file.\n");
        #ifdef _WIN32 /* This is for windows systems                 */
         getchar();   /* Makes the program pause till key is pressed */
        #endif
        return -1;
    }


    /* ================================================= * 
     *      get config values from buffer                *
     * ================================================= */

    cfg_value1 = TCFP_get_value(config_data, "value1", '=');
    if(cfg_value1 == NULL) /* retrieving failed so lets bail out */
    {
        /* print something nice to inform about failure */
        puts("Retrieving keyvalue failed.\n");
        #ifdef _WIN32
         getchar();
        #endif
        return -1;
    }

    cfg_value2 = TCFP_get_value(config_data, "value2", '=');
    if(cfg_value2 == NULL) /* same here */
    {
        puts("Retrieving keyvalue failed.\n");
        #ifdef _WIN32
         getchar();
        #endif
        return -1;
    }

    /* lets try different delimiter */
    cfg_value3 = TCFP_get_value(config_data, "value3", ':'); 
    if(cfg_value3 == NULL) /* same here */
    {
        puts("Retrieving keyvalue failed.\n");
        #ifdef _WIN32
         getchar();
        #endif
        return -1;
    }

    /* we dont need config data anymore so lets free it */
    TCFP_free_config_data( config_data );

    /* Convert keyvalue strings to integer and float numbers */
    cValue2 = atoi(cfg_value2);
    cValue3 = atof(cfg_value3);

    /* No need for these anymore */
    TCFP_free_config_value( cfg_value2 );
    TCFP_free_config_value( cfg_value3 );

  
    /* ================================================= * 
     *      use the values as you please                 *
     * ================================================= */

    /* check the results */
    printf("\nResults:\n"
           "\n  (char)  cfg_value1\t->\t%s"      /* string  */
           "\n   (int)  cfg_value2\t->\t%d"      /* integer */
           "\n (float)  cfg_value3\t->\t%f\n\n", /* float   */
        cfg_value1,
        cValue2,
        cValue3
    );
 
    /* free last buffer */
    TCFP_free_config_value( cfg_value1 );


    #ifdef _WIN32
     getchar();
    #endif

    return 0;
}