/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE LIBRARY
*   FILE     : lb_ini_mgr.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   :
 *              
 *========================================================================
*/

#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

#include "path_def.h"
#include "lb_func_list.h"

/*
 *========================================================================
 *
 *   Function : lb_get_inifile()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/

char *lb_get_inifile(file_name, sec_name, var_name)
char *file_name;
char *sec_name;
char *var_name;
{
    unsigned int i;
    int j, len, iflag=0, vflag=0;
    char buf[256], cvar_name[32], ctmp[32];
    static char cvalue[32];
    FILE *fp;

    if((fp = fopen(file_name, "r")) == NULL)
    {
        printf("ini file not found [%s]", file_name);
        return NULL;
    }

    memset(buf, 0x00, sizeof(buf));
    while(fgets(buf, sizeof buf, fp) != NULL)
    {
        len = strlen(buf);
        buf[len-1] = 0x00;

        if((buf[0]&0xFF) == 0x23)
        {
            continue;
        }
        else if(((buf[0]&0xFF) == 0x20 || (buf[0]&0xFF) == 0x09) && iflag == 1)
        {

            memset(cvar_name, 0x00, sizeof(cvar_name));
            memset(cvalue,   0x00, sizeof(cvalue));
            memset(ctmp,     0x00, sizeof(ctmp));

            for(i=1; i<strlen(buf); i++)
            {

                if((buf[i]&0xFF) == 0x23) break;
                else if((buf[i]&0xFF) == 0x20 || (buf[i]&0xFF) == 0x09)
                {
                    if(vflag == 0) continue;

                    ctmp[j] = 0x00;
                    strncpy(cvar_name, ctmp, strlen(ctmp));

                    if(strncmp(cvar_name, var_name, strlen(var_name)) == 0)
                    {
                        j = 0;
                        vflag = 1;
                        memset(ctmp, 0x00, sizeof(ctmp));
                    }
                    else
                        continue;
                }
                else if((buf[i]&0xFF) >= 0x21 && (buf[i]&0xFF) <= 0x7E)
                {
                    if(vflag == 0)
                    {
                        ctmp[j++] = buf[i];
                        ctmp[j]   = 0x00;
                    }
                    else
                    {
                        strncpy(cvalue, (char*)&buf[i], strlen(buf)-i);
                        fclose(fp);
                        return cvalue;
                    }
                }
            }
        }
        else
        {
            if(strncmp(sec_name, buf, strlen(sec_name)) == 0)
            {
                iflag = 1;
            }
       }
       memset(buf, 0x00, sizeof buf);
       j = 0;
    }
    fclose(fp);
    return NULL;
}
