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

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/io.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "type_def.h"
#include "path_def.h"
#include "lb_func_list.h"

FILE   *logfp;
#define GCC 0

#define ONEDAYSEC (60*60*24)

/*
 *========================================================================
 *
 *   Function : get_current_time()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 현재시간을 반환
 *
 *========================================================================
*/

char *get_current_time(time_buf)
char *time_buf;
{
    time_t current_time;
    struct tm *struct_time;

    time(&current_time);
    struct_time = localtime( &current_time);

    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d", 
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday,
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : get_yesterday_date()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 하루전 날짜를 반환
 *
 *========================================================================
*/

char *get_yesterday_date(date_buf)
char *date_buf;
{
    time_t yesterday_time;
    struct tm *struct_time;

    yesterday_time = time(NULL)-ONEDAYSEC;
    struct_time = localtime( &yesterday_time);

    sprintf(date_buf, "%04d%02d%02d", struct_time->tm_year+1900, struct_time->tm_mon + 1, struct_time->tm_mday);

    return date_buf;
}

/*
 *========================================================================
 *
 *   Function : get_gmt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 세계표준시간을 반환
 *
 *========================================================================
*/

char *get_gmt(time_buf,size)
char *time_buf;
int   size;
{
    time_t gmt;
    struct tm *struct_time;

    time( &gmt);
    gmt -= 3600 * 9;
    struct_time = localtime( &gmt);

    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d", 
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday,
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    time_buf[size]=0x0;
    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : conv_localtime()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 받은 시간을 표준지역시간으로 변환
 *
 *========================================================================
*/

char *conv_localtime(raw_time)                                                          
time_t raw_time;
{                                                                                       
    struct tm  *struct_time;
    static char time_buf[15];

    struct_time = localtime( &raw_time);

    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d",                                       
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday,
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    return time_buf;                                                                    
}                                               

/*
 *========================================================================
 *
 *   Function : conv_localtime_ymd()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 받은 시간을 표준지역시간으로 변환 년월일까지 반환
 *
 *========================================================================
*/

char *conv_localtime_ymd(raw_time)
time_t raw_time;
{
    struct tm  *struct_time;
    static char time_buf[15];

    struct_time = localtime( &raw_time);

    sprintf(time_buf, "%04d%02d%02d", 
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday);

    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : conv_localtime_hms()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 받은 시간을 표준지역시간으로 변환하여 시분초 반환
 *
 *========================================================================
*/

char *conv_localtime_hms(raw_time)
time_t raw_time;
{
    struct tm  *struct_time;
    static char time_buf[15];

    struct_time = localtime( &raw_time);

    sprintf(time_buf, "%04d%02d%02d", 
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : get_current_time_sec()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 현재시간을 초단위로 반환
 *
 *========================================================================
*/

char *get_current_time_sec()
{
    time_t current_time;
    static char time_buf[15];

    time(&current_time);

    sprintf(time_buf,"%d", (int)current_time);
    return time_buf;
}



/*
 *========================================================================
 *
 *   Function : conv_localtime2()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 받은 시간을 표준지역시간으로 변환
 *
 *========================================================================
*/

char *conv_localtime2(raw_time,n)                               
time_t raw_time;
int    n;
{                                                      
    struct tm  *struct_time;
    static char time_buf[15];

    struct_time = localtime( &raw_time);

    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d",
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday,
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    time_buf[n]=0x0;
    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : conv_localtime3()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 받은 시간을 표준지역시간으로 변환
 *
 *========================================================================
*/

char *conv_localtime3(raw_time, time_buf)
time_t raw_time;
char  *time_buf;
{
    struct tm *struct_time;

    struct_time = localtime( &raw_time);

    sprintf(time_buf, "%04d%02d%02d%02d%02d%02d", 
            struct_time->tm_year+1900,
            struct_time->tm_mon + 1, struct_time->tm_mday,
            struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);

    return time_buf;
}

/*
 *========================================================================
 *
 *   Function : get_dsnt_time()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 지정한 날짜시간으로 구하기
 *
 *========================================================================
*/

time_t get_dsnt_time(dsnt_buf)
char *dsnt_buf;
{
    time_t current_time;
    struct tm struct_time;
    char      tm_struct[6][5];

    memset(tm_struct,0x0,sizeof(tm_struct));

    strncpy(tm_struct[0],dsnt_buf,4);
    strncpy(tm_struct[1],dsnt_buf+4,2);
    strncpy(tm_struct[2],dsnt_buf+6,2);
    strncpy(tm_struct[3],dsnt_buf+8,2);
    strncpy(tm_struct[4],dsnt_buf+10,2);
    strncpy(tm_struct[5],dsnt_buf+12,2);

    struct_time.tm_year = atoi((const char *)tm_struct[0]) - 1900;
    struct_time.tm_mon = atoi((const char *)tm_struct[1]) - 1;
    struct_time.tm_mday = atoi((const char *)tm_struct[2]);
    struct_time.tm_hour = atoi((const char *)tm_struct[3]);
    struct_time.tm_min = atoi((const char *)tm_struct[4]);
    struct_time.tm_sec = atoi((const char *)tm_struct[5]);

    current_time = mktime(&struct_time);

    return current_time;
}

/*
 *========================================================================
 *
 *   Function : get_week()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 요일가져오기
 *
 *========================================================================
*/

int get_week()
{
    time_t  current_time;
    struct  tm *struct_time;

    time( &current_time);
    struct_time = localtime( &current_time);
    return (struct_time->tm_wday);
}

/*
 *========================================================================
 *
 *   Function : open_log_file()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 로그파일 오픈
 *
 *========================================================================
*/

int open_log_file()
{
    char    log_file[50];
    time_t  current_time;
    struct  tm  *tm_ptr, struct_time, mod_tm;
    struct  stat st_buf;
    int     ret, exist_flag, past_flag;

    memset( log_file, 0x0, sizeof(log_file));

    time( &current_time);
    tm_ptr = localtime( &current_time);
    memcpy( &struct_time, tm_ptr, sizeof(struct tm));

    switch( struct_time.tm_wday   ) {        /* Sunday:0, - Saturday:6 */
    case    0:
        sprintf(log_file, "%s/log/iis.sun", LOG_DIR);
        break;
    case    1:
        sprintf(log_file, "%s/log/iis.mon", LOG_DIR);
        break;
    case    2:
        sprintf(log_file, "%s/log/iis.tue", LOG_DIR);
        break;
    case    3:
        sprintf(log_file, "%s/log/iis.wed", LOG_DIR);
        break;
    case    4:
        sprintf(log_file, "%s/log/iis.thu", LOG_DIR);
        break;
    case    5:
        sprintf(log_file, "%s/log/iis.fri", LOG_DIR);
        break;
    case    6:
        sprintf(log_file, "%s/log/iis.sat", LOG_DIR);
        break;
    }
    exist_flag = 0;
    past_flag = 0;
    if( access( log_file, F_OK ) < 0 ) {
        exist_flag = 0;
    } else {
        exist_flag = 1;
        ret = stat( log_file, &st_buf );
        if( ret < 0 ) {
            printf("stat call error...\n");
            return(-1);
        }

        tm_ptr = localtime( &st_buf.st_mtime );
        memcpy( &mod_tm, tm_ptr, sizeof(struct tm));

        if( struct_time.tm_year != mod_tm.tm_year ||
            struct_time.tm_mon  != mod_tm.tm_mon ||
            struct_time.tm_mday != mod_tm.tm_mday )
        {
            past_flag = 1;
        }
    }

        if( exist_flag ) {
            if( past_flag ) logfp = fopen( log_file, "w" );
            else        logfp = fopen( log_file, "a+" );
        }
        else
            logfp = fopen( log_file, "w" );

        if( logfp == NULL )
        {
             printf("O_O v : %s\n", log_file);
             return(-1);
        }

    return (0);
}

/*
 *========================================================================
 *
 *   Function : close_log_file()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 로그파일 닫기
 *
 *========================================================================
*/

void close_log_file()
{
    fclose( logfp );
}

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

int LOGGING(str)
char    *str;
{
    time_t  current_time;
    struct  tm *struct_time;
    int     year, month, day, hour, minute, sec;
    char    log_time[50];

    if( open_log_file() < 0 ) {
        printf("In LOGGING logfile open error...\n");
        return -1;
    }

    time( &current_time);
    struct_time = localtime( &current_time);

    year = struct_time->tm_year+1900;    /* years since 1900 */
    month = struct_time->tm_mon + 1;     /* months since january [0 - 11]    */
    day = struct_time->tm_mday;          /* day of the month [1 - 31]        */
    hour = struct_time->tm_hour;         /* hour since midnight [0 - 23]     */
    minute = struct_time->tm_min;        /* minite after the hour [0 - 59]   */
    sec = struct_time->tm_sec;           /* seconds after the minute - [0, 61] */

    memset( log_time, 0x0, sizeof(log_time));

    sprintf( log_time, "%4d-%02d-%02d %02d:%02d:%02d : ",year,month,day,hour,
            minute, sec);

    fprintf( logfp, "%s", log_time );
    fprintf( logfp, "%s", str );
    fprintf( logfp, "\n");

    printf( "%s", log_time );
    printf( "%s", str );
    printf( "\n");

    close_log_file( );
    return 0;
}

/*
 *========================================================================
 *
 *   Function : log_write_stderr()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : stderr에 로그 남기기
 *
 *========================================================================
*/

void log_write_stderr(str)
char *str;
{
   long ct;
   char *tmstr;
   char stime[16];

   get_current_time(stime);

   ct=time(0);
   tmstr=asctime(localtime(&ct));

   *(tmstr+strlen(tmstr)-6)='\0';

   fprintf(stderr,"%s %s\n",tmstr, str);
}

/*
 *========================================================================
 *
 *   Function : proc_log()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 파일오류화면에 문자열을 기록
 *
 *========================================================================
*/

void proc_log(char *str, ...)
{
    va_list args;
    char buf[4096];
    FILE *fp;

    char stime[16];
    char dirname[80];

    get_current_time(stime);
    sprintf(dirname, "%s%6.6s.log", LOG_DIR, stime+2);

    va_start(args, str);
    (void) vsprintf(buf, str, args);
    va_end(args);
    printf("%s %s\n",stime,buf);

    if((fp = fopen(dirname,"a"))==NULL) return; 
    fprintf(fp, "%s %s\n",stime+8,buf);
    fclose(fp);
}

/*
 *========================================================================
 *
 *   Function : atoi_str_len()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 문자열을 받은 길이만큼 숫자로 변환하여 반환
 *
 *========================================================================
*/

int atoi_str_len(str, len_str)
char *str;
int len_str;
{
   char  buf[128];

   if (len_str >= (int)sizeof(buf))
      len_str = sizeof(buf) - 1;
   memcpy(buf, str, (size_t)len_str);
   buf[len_str] = '\0';
   return atoi(buf);
}

/*
 *========================================================================
 *
 *   Function : non_block()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 문자열을 받은 길이만큼 숫자로 변환하여 반환
 *
 *========================================================================
*/

void non_block(s)
int s;
{
   if (fcntl(s,F_SETFL,O_NDELAY)==-1) {    /* O_NDELAY */
      perror("Noblock");
      return;
   }
}

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

char *rtrim(str)
register char *str;
{
   register int i = strlen(str);

   while (i && (str[i - 1] == ' ' || str[i - 1] == '\t')) i--;
   str[i] = '\0';
   return (char *)str;
}

/*
 *========================================================================
 *
 *   Function : del_space()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 문자열의 끝의 공백문자를 지워 준다.
 *
 *========================================================================
*/
#if 0
char del_space (str)
#else
void del_space (str)
#endif
char *str;
{
    int i, j, len = strlen(str);

    for(i=0; i<len; i++)
    {
        if(str[i] == ' ' || str[i] == '\t')
        {
            for(j=i; j<len; j++)
            {
                str[j] = str[j+1];
            }
            i--;
        }
    }

    for( i = len-1; i > 0; i-- )
    {
        if(i && (str[i] == ' ' || str[i] == '\t'))
        {
            str[i] =  str[i+1] ;
        }
    }
#if 0
    return (char *)str;
#endif
}

/*
 *========================================================================
 *
 *   Function : get_wday()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : 요일을 가져온다
 *
 *========================================================================
*/ 

int get_wday(daystr)
char *daystr;
{
    time_t  Time;
    struct  tm *Tmptr, Tm;

    Tm.tm_year = atoi_str_len(daystr,4) - 1900;
    Tm.tm_mon = atoi_str_len(daystr+4,2) - 1;
    Tm.tm_mday = atoi_str_len(daystr+6,2);
    Tm.tm_hour = 0;
    Tm.tm_min = 0;
    Tm.tm_sec = 0;

    Time = mktime(&Tm);
    Tmptr = localtime( &Time);
    memcpy( &Tm, Tmptr, sizeof(struct tm));
    return (Tm.tm_wday+1);
}

