/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : ft_pltpcardmst.pc
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 :
 *
 *========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <sqlca.h>
#include <unistd.h>
#include <sys/stat.h>
#include "pl_mst.h"
//#include "pl_info.h"
#include "chg_struc.h"
#include "ora_sql.h"
#include "file_no.h"
#include "dir_path.h"
#include "file_lb.h"
#include "tcs_db_def.h"
#include "ft_plmst.h"

int make_pl_file_FFAW(char *pcFileName);
int get_aliasno(char *pcFileNAme, int iNo);
int get_ppd_bit(char cData, char *pcUse, char *pcStudent);
int get_chg_bit(char cData, char *pcUse);
long Get_File_Size_Char(char* pcFile);
long Read_File_Char(char* pcDesc, char* pcFile);
int Write_File_Char(unsigned char *data, long len, char *file);
int Add_Write_File_Char(char *data, long len, char *file);

static    struct     pl_mst_tbl    pl_mst_tbl_f;
//static    struct     pl_tcrd_info       pl_tcrd_info_f;
static    int    errflg=0;
char     msg[128];

/*********************************************************************
// PL 파일 추가 시... (Alias No 추가 시..)
// 	1) ../../include/tcs/file_common.h에 MAX_PL_FILE_CNT 를 증가
// 	2) ../../include/tcs/file_common.h에 enum FILE_ID 에 추가
//  3) ../../include/tcs/file_common.h에 #define [파밀명]_START_NO_GET(a), #define [파일명]_END_NO 추가
// 	4) 아래 gastPlFileCheckList에 Alias No 순서대로 파일을 등록
//	5) PL_make_file.cc의 get_aliasno() -> switch()안에 추가
*********************************************************************/
PL_FILE_CHECK_LIST	gastPlFileCheckList [] = 
{
	 {(char*)"WSSF", 0x00, WSSF}
	,{(char*)"WTSF", 0x00, WTSF}
	,{(char*)"WXSF", 0x00, WXSF}
	,{(char*)"WYSF", 0x00, WYSF}
	,{(char*)"WBSF", 0x00, WBSF}
	,{(char*)"WZSF", 0x00, WZSF}
	,{(char*)"VWSF", 0x00, VWSF}
	,{(char*)"VXSF", 0x00, VXSF}
	,{(char*)"WASF", 0x00, WASF}
	,{(char*)"NVSF", 0x00, NVSF}
	,{(char*)"NUSF", 0x00, NUSF}
	,{(char*)"NTSF", 0x00, NTSF}
	,{(char*)"NSSF", 0x00, NSSF}
	,{(char*)"NRSF", 0x00, NRSF}
	,{(char*)"NQSF", 0x00, NQSF}
	,{(char*)"NPSF", 0x00, NPSF}
	
	,{(char*)"NOSF", 0x00, NOSF}
	
	,{(char*)"BHSF", 0x00, BHSF}
	,{(char*)"EPSF", 0x00, EPSF}
	
	,{(char*)"EQSF", 0x00, EQSF}
	,{(char*)"HLSF", 0x00, HLSF}

	,{(char*)"V3SF", 0x00, V3SF}
	,{(char*)"V4SF", 0x00, V4SF}
	,{(char*)"V5SF", 0x00, V5SF}
	,{(char*)"V6SF", 0x00, V6SF}
	,{(char*)"V7SF", 0x00, V7SF}
	
	,{(char*)0x00, NULL, UNKNOWN}
};

int term_PL(char* pcData, PL_RECORD* pstPlRecord)
{
	if(pcData != 0x00){
		proc_log("[mem chk] term_PL pcData free\n");
		free(pcData);
	}
	if(pstPlRecord != 0x00){
		proc_log("[mem chk] term_PL pstPlRecord free\n");
		free(pstPlRecord);
	}
	pcData = 0x00;
	pstPlRecord = 0x00;

	return 0;
}

/*
 *========================================================================
 *
 *   함 수 명 : chng_t2f_plmst
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 : PL테이블을 파일로
 *
 *========================================================================
*/
chng_t2f_plmst(struct    chg_struc    *cs)
{
    char path[128], path2[128], spath[128], tpath[128];
    char rcvpath[128];
    int  isfd, isrc, isfd2, isrc2;
    int  ii, jj, kk, ret;
    int  from_alias_no=0, to_alias_no=0;
    int  cnt=0;
    int  block_st_no=0, block_pl_cnt=0, block_cnt=0, block_order=0, total_frame_cnt=0, frame_cnt=0;
	long lReadSize, size;
#pragma pack(push, 1)
    unsigned char s_aliase_no[10+1], s_use_yn[1+1];
    unsigned char tmp_pre_ali[4];
	unsigned char tmp_pre_stdali[4];
    unsigned char tmp_aft_alli[8];
    unsigned char cmd[128]; memset(cmd, 0x00, sizeof(cmd));
	unsigned char ali;
	unsigned char buff[2];
#pragma pack(pop)
    unsigned int  aliase_no, use_yn, chk_cnt = 0, start = 0, limit = 0;
    FILE *fp;
    FILE *ofp;
	struct dirent *entry;
    struct stat file_info;
	PL_RECORD *pstPlRecord = 0x00;
	int  iAliasNo, iAliasNoTmp, iAliasNoTmpSub, iAliasSubFlag, iWXSF_flag, iWBSF_flag;
	int  iOffSet = 0;
	int  iTotAliasCnt = 0;
	int  iChgUsrOffSet = 0;
	char acChgUsrD[5000];
	char pcFileName[20];
	char *pcData = 0x00;

    memset(path, 0x00, sizeof(path));
	memset(path2, 0x00, sizeof(path2));
	memset(pcFileName, 0x00, sizeof(pcFileName));
    sprintf(path, "%s/%s/PLTPCARDMST_S", DIR_TCS, DIR_BASEINFO);
    unlink(path);
    sprintf(path2, "%s/%s/PLTPCARDMST", DIR_TCS, DIR_BASEINFO);
	
    sprintf(rcvpath, "%s/FILE/RECV", DIR_TCS);

    DIR *dir = opendir(rcvpath);
    if (dir == NULL) {
        return 0;
    }

    while ((entry = readdir(dir)) != NULL) 
    {
#if 1
        if( strncmp((char *)entry->d_name, "WSSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WTSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WXSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WYSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WBSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WZSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "VWSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "VXSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "WASF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NVSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NUSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NTSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NSSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NRSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NQSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NPSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "NOSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "BHSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "EPSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "EQSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "HLSF", 4) == 0 ||
			strncmp((char *)entry->d_name, "V3SF", 4) == 0 ||
			strncmp((char *)entry->d_name, "V4SF", 4) == 0 ||
			strncmp((char *)entry->d_name, "V5SF", 4) == 0 ||
			strncmp((char *)entry->d_name, "V6SF", 4) == 0 ||
			strncmp((char *)entry->d_name, "V7SF", 4) == 0 )
#else
		if( strncmp((char *)entry->d_name, gastPlFileCheckList[cnt].pcFileName, 4) == 0 )
#endif
		{
			cnt++;
			strncpy(pcFileName, entry->d_name, strlen(entry->d_name));
            sprintf(spath, "%s/FILE/RECV/%s", DIR_TCS, pcFileName);
			sprintf(tpath, "%s/FILE/RECV/SAVE/%s", DIR_TCS, pcFileName);
proc_log("PL Start [%s]\n", pcFileName);
            size = Get_File_Size_Char(spath);
			pcData = (char*)malloc(size+1);
			memset(pcData, 0x00, size+1);
			lReadSize = Read_File_Char(pcData, spath);
			if(lReadSize < 512)
			{
				proc_log("File Size Error ... [size:%d]\n", lReadSize);
				proc_log("[mem chk] go term_PL\n");
				term_PL(pcData, pstPlRecord);
				return -1;
			}

			iAliasNo = 0;
			iAliasNoTmp = 1;
			iAliasNoTmpSub = 1;
			iAliasSubFlag = 0;
			iWXSF_flag = 0;
			iWBSF_flag = 0;
			
			memset(tmp_aft_alli, 0x00, sizeof(tmp_aft_alli));
			memset(tmp_pre_ali, 0x00, sizeof(tmp_pre_ali));
			memset(tmp_pre_stdali, 0x00, sizeof(tmp_pre_stdali));

			if(strncmp((char *)pcFileName, "WXSF", 4) == 0)
			{
				iWXSF_flag = 1;
			}

			if(strncmp((char *)pcFileName, "WBSF", 4) == 0)
			{
				iWBSF_flag = 1;
			}

			block_cnt = (int)ceil((double)size/sizeof(PL_RECORD));
			pstPlRecord = (PL_RECORD *)malloc(sizeof(PL_RECORD)*block_cnt);

            for(ii = 0; ii < block_cnt; ii++)
            {
				if(size >= sizeof(pstPlRecord[ii].acData))
				{
					memcpy(pstPlRecord[ii].acData, pcData+iOffSet, sizeof(pstPlRecord[ii].acData));
					iOffSet += sizeof(pstPlRecord[ii].acData);
					size -= sizeof(pstPlRecord[ii].acData);
					
					iChgUsrOffSet = 0;
					memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
					
					for(jj = 0; jj < 510; jj++)
					{
						if( strncmp((char *)pcFileName, "WSSF", 4) == 0 ||
							strncmp((char *)pcFileName, "WZSF", 4) == 0 ||
							strncmp((char *)pcFileName, "VWSF", 4) == 0 ||
							strncmp((char *)pcFileName, "WASF", 4) == 0 ||
							strncmp((char *)pcFileName, "NTSF", 4) == 0 ||
							strncmp((char *)pcFileName, "BHSF", 4) == 0 ||
							strncmp((char *)pcFileName, "EPSF", 4) == 0 ||
							strncmp((char *)pcFileName, "EQSF", 4) == 0 ||
							strncmp((char *)pcFileName, "V3SF", 4) == 0 ||
							strncmp((char *)pcFileName, "V4SF", 4) == 0 )
						{
							memset(tmp_aft_alli, 0x00, sizeof(tmp_aft_alli));
							get_chg_bit(pstPlRecord[ii].acData[jj], tmp_aft_alli);

							for(kk = 0; kk < sizeof(tmp_aft_alli); kk++)
							{
								iAliasNo = get_aliasno((char *)pcFileName, iAliasNoTmp);
								iAliasNoTmp++;
								
								if(iAliasNo <= 0)
								{
									break;
								}

								acChgUsrD[iChgUsrOffSet++] = tmp_aft_alli[jj];
								iTotAliasCnt++;
							}
						}else
						{
							memset(tmp_pre_ali, 0x00, sizeof(tmp_pre_ali));
							memset(tmp_pre_stdali, 0x00, sizeof(tmp_pre_stdali));
							
							get_ppd_bit(pstPlRecord[ii].acData[jj], tmp_pre_ali, tmp_pre_stdali);

							for (kk =0; kk <sizeof(tmp_pre_ali); kk++)
							{
								if(strncmp((char *)pcFileName, "WXSF", 4)==0)
								{
									if(iAliasSubFlag == 0)
									{
										iAliasNo = WXSF_SUB1_START_NO_GET(iAliasNoTmp);
										if(iAliasNo > WXSF_SUB1_END_NO){
											iAliasSubFlag = 1;
										}else{
											iAliasNoTmp++;
										}
									}
									if(iAliasSubFlag == 1)
									{
										iAliasNo = WXSF_SUB2_START_NO_GET(iAliasNoTmpSub);
										iAliasNoTmpSub++;
		
										if(iAliasNo > WXSF_SUB2_END_NO){
											iAliasNo = -1;
										}
									}
								}
								else if (strncmp((char *)pcFileName, "WBSF", 4)==0)
								{
									if(iAliasSubFlag == 0)
									{
										iAliasNo = WBSF_SUB1_START_NO_GET(iAliasNoTmp);
										if(iAliasNo > WBSF_SUB1_END_NO){
											iAliasSubFlag = 1;
										}else{
											iAliasNoTmp++;
										}
									}
									if(iAliasSubFlag == 1)
									{
										iAliasNo = WBSF_SUB2_START_NO_GET(iAliasNoTmpSub);
										iAliasNoTmpSub++;
		
										if(iAliasNo > WBSF_SUB2_END_NO){
											iAliasNo = -1;
										}
									}
								}
								else
								{
									iAliasNo = get_aliasno((char *)pcFileName, iAliasNoTmp);
									iAliasNoTmp++;
								}
								
								if(iAliasNo <= 0)
								{
									break;
								}

								acChgUsrD[iChgUsrOffSet++] = ((tmp_pre_ali[kk] == 0x30 && tmp_pre_stdali[kk] == 0x30) ? 0x30 : 0x31);
		
								iTotAliasCnt++;
								
								if(iWXSF_flag == 1)
								{
									if(iAliasNo == WXSF_SUB1_END_NO){
										if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, path) != 0){
											proc_log("File Write Error.. [%s]", path);
											return -1;
										}
										proc_log("[mem chk] go term_PL");
										term_PL(pcData, pstPlRecord);
										return iTotAliasCnt;
									}
								}

								if(iWBSF_flag == 1)
								{
									if(iAliasNo == WBSF_SUB1_END_NO){
										if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, path) != 0){
											proc_log("File Write Error.. [%s]", path);
											proc_log("[mem chk] return");
											return -1;
										}
		
										memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
										iChgUsrOffSet = 0;
		
										iTotAliasCnt += make_pl_file_WBSF((char *)pcFileName);
									}
								}
								
							}
						}
					}
					if(iChgUsrOffSet > 0){
						if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, path) != 0){
							proc_log("File Write Error.. [%s]", path);
							proc_log("[mem chk] return");
							return -1;
						}

						memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
						iChgUsrOffSet = 0;
					}
				}else
				{
					proc_log("Data in file struct Error.. [%d/%d]", ii, block_cnt);
					proc_log("[mem chk] go term_PL");
					term_PL(pcData, pstPlRecord);
					return -1;
				}
            }
			lb_rename(spath, tpath);
proc_log("PL 완료[%d]", ret);
		}else
		{
			continue;
		}
	}
    //lb_rename(path, path2);
proc_log("PL 마스터화 완료");

#if 0
    EXEC SQL DECLARE i_cursor CURSOR FOR
    SELECT
        ALIAS_NO,
        USE_YN
    FROM BLTM_TRFSPLCDB
    WHERE ALIAS_NO < '0800000001'
    AND SUBSTR(PROC_TM,1,8) = (SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') FROM DUAL);

    EXEC SQL OPEN i_cursor;

    while(1)
    {
        EXEC SQL FETCH i_cursor INTO :s_aliase_no, :s_use_yn;

        if(sqlca.sqlcode) break;

        ii=0;
        frame_cnt=0;

        aliase_no = atoi(s_aliase_no);
        use_yn = atoi(s_use_yn);

/*        proc_log("\naliase_no : (%d) use_yn : (%d) 검색 시작\n\n", aliase_no, use_yn); */

        sprintf(path2, "%s/%s/PLTPCARDMST", DIR_TCS, DIR_BASEINFO);  

        fp = fopen(path2, "r+");

        if(fp < 0)
        {
            proc_log("PLTPCARDMST open error %d\n", fp);
        }

        fseek(fp, aliase_no-1, SEEK_SET);
        fread(buff, sizeof(char), 1, fp);
/*
        proc_log("파일명 : %s\n", path2);
        proc_log("위치값 : %d\n", aliase_no);
        proc_log("변경 : %c -> %d\n\n", *buff, use_yn);
*/

    fseek(fp, aliase_no-1, SEEK_SET);
    if(use_yn == 1)
    {
        fprintf(fp, "1");
        }
    else if(use_yn == 0)
    {
        fprintf(fp, "0");
        }

        fclose(fp);
    }
#endif
    return iTotAliasCnt;
}

/******************************************************************************
 * make_pl_file_WBSF
 * parameter
 * Return
 ******************************************************************************/
int make_pl_file_WBSF(char *pcFileName)
{
	char				*pcData = 0x00;
	char				acSrcFileFullPath[512];
	char				acDstFileFullPath[512];
	char				acMakeFile[512];
	PL_RECORD		*pstPlRecord = 0x00;
	int					iTotRecordCnt = 0;
	int					iOffSet = 0; 
	int					iTotAliasCnt = 0;
	int 	i, a, q;
	int	 	iAliasNo = 0;
	int		iAliasNoTmp = 1;
	int	 	iAliasNoTmpSub = 1;
	int  	iAliasSubFlag = 0;
	char 	acChgUse[8];
	char	acPpdUse[4];
	char	acPpdStudent[4];
	char 	acChgUsrD[5000];
	int  	iChgUsrOffSet = 0;
	long 	lReadSize, lFileSize;

	memset(acSrcFileFullPath, 0x00, sizeof(acSrcFileFullPath));
	memset(acDstFileFullPath, 0x00, sizeof(acDstFileFullPath));
	memset(acMakeFile, 0x00, sizeof(acMakeFile));
	
	sprintf(acSrcFileFullPath, 	"%s/FILE/RECV/%.8s", 	DIR_TCS, pcFileName);
	sprintf(acDstFileFullPath, 	"%s/FILE/RECV/%.8s", 	DIR_TCS, pcFileName);
	sprintf(acMakeFile, 		"%s/%s/PLTPCARDMST_S", 	DIR_TCS, DIR_BASEINFO);
	
	lFileSize = Get_File_Size_Char(acSrcFileFullPath);
	
	if(lFileSize > 0)
	{
		proc_log("WXSF file continuous use...");
		proc_log("PL FILE READ START [name:%s, size:%d byte]", pcFileName, lFileSize);

		pcData = (char *) malloc(lFileSize+1);
		memset(pcData, 0x00, lFileSize+1);
		proc_log("[mem chk] 1-3 pcData malloc");

		long lReadSize = Read_File_Char(pcData, acSrcFileFullPath);
		
		if(lReadSize < 512){
			proc_log("File Size Error ... [size:%d]", lReadSize);
			proc_log("[mem chk] go term_PL");
			term_PL(pcData, pstPlRecord);
			return 0;
		}
		
		iAliasNo = 0;
		iAliasNoTmp = 1;
		iAliasNoTmpSub = 1;
		iAliasSubFlag = 0;
		
		memset(acChgUse, 0x00, sizeof(acChgUse));
		memset(acPpdUse, 0x00, sizeof(acPpdUse));
		memset(acPpdStudent, 0x00, sizeof(acPpdStudent));
		
		iTotRecordCnt = (int)ceil((double)lFileSize/sizeof(PL_RECORD));
		pstPlRecord = (PL_RECORD *)malloc(sizeof(PL_RECORD)*iTotRecordCnt);
		proc_log("[mem chk] 1-4 pstPlRecord malloc");
		
		proc_log("Record Cnt [%d]", iTotRecordCnt);
		
		for (i=0; i<iTotRecordCnt; i++)
		{
			if (lFileSize >= sizeof(pstPlRecord[i].acData))
			{
				memcpy(pstPlRecord[i].acData, pcData+iOffSet, sizeof(pstPlRecord[i].acData));
				iOffSet += sizeof(pstPlRecord[i].acData);
				lFileSize -= sizeof(pstPlRecord[i].acData);

				iChgUsrOffSet = 0;
				memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
				
				for (a=0; a<510; a++)
				{
					memset(acPpdUse, 0x00, sizeof(acPpdUse));
					memset(acPpdStudent, 0x00, sizeof(acPpdStudent));
					
					get_ppd_bit(pstPlRecord[i].acData[a], acPpdUse, acPpdStudent);
					
					for (q=0; q<sizeof(acPpdUse); q++)
					{
						if(iAliasSubFlag == 0)
						{
							iAliasNo = WXSF_SUB1_START_NO_GET(iAliasNoTmp);
							if(iAliasNo > WXSF_SUB1_END_NO)
							{
								iAliasSubFlag = 1;
							}
							else
							{
								iAliasNoTmp++;
							}
						}
						if(iAliasSubFlag == 1)
						{
							iAliasNo = WXSF_SUB2_START_NO_GET(iAliasNoTmpSub);
							iAliasNoTmpSub++;

							if(iAliasNo > WXSF_SUB2_END_NO)
							{
								iAliasNo = -1;
							}
						}
						
						if(iAliasNo <= 0)
						{
							break;
						}
						
						if (iAliasNo >= 170000001)
						{
							
							// CARD TEST
							if (iAliasNo == 170910240)
							{
								proc_log("before alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
								acPpdUse[q] = 0x31;
								acPpdStudent[q] = 0x31;
								proc_log("after  alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
							}

							//acChgUsrD[iChgUsrOffSet++] = acPpdUse[q];
							acChgUsrD[iChgUsrOffSet++] = ((acPpdUse[q] == 0x30 && acPpdStudent[q] == 0x30) ? 0x30 : 0x31);

							iTotAliasCnt++;
						}
						
					} // for (int q=0; q<sizeof(ppd_use); q++)
					
				} // for (int a=0; a<510; a++)
				
				if (iChgUsrOffSet > 0)
				{
					if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, acMakeFile) != 0)
					{
						proc_log("File Write Error.. [%s]", acMakeFile);
						proc_log("[mem chk] return");
						return 0;
					}
				}

				memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
				iChgUsrOffSet = 0;
				
			} // if (file_size >= sizeof(record_data[i].data))
			else
			{
				proc_log("Data in file struct Error.. [%d/%d]", i, iTotRecordCnt);
				proc_log("[mem chk] go term_PL");
				term_PL(pcData, pstPlRecord);
				return 0;
			}
			
		} // for (int i=0; i<tot_record_cnt; i++)
		
	} // if(lFileSize > 0)
	else
	{
		proc_log("[mem chk] return");
		return 0;
	}
	
	proc_log("WXSF File Continuous Use END..");
	
	// 처리 완료된 파일은 옮긴다
	char acCmd[256];
	memset(acCmd, 0x00, sizeof(acCmd));
		
	sprintf(acCmd, "mv -f %s %s", acSrcFileFullPath, acDstFileFullPath);
	system(acCmd);
		
	proc_log("FILE MOVE [%s]", acCmd);
		
	proc_log("[mem chk] go term_PL");
	term_PL(pcData, pstPlRecord);

	return iTotAliasCnt;
}

/******************************************************************************
 * get_aliasno
 * parameter
 * Return
 ******************************************************************************/
int get_aliasno(char *pcFileName, int iNo)
{
	int	iAliasNo = 0;
	int iCnt = 0;
	enum FILE_ID iFileId = UNKNOWN;
	
	while(1)
	{
		if(gastPlFileCheckList[iCnt].pcFileName == (char*)0x00)
		{
			break;
		}
		
		if(strncmp(gastPlFileCheckList[iCnt].pcFileName, pcFileName, 4) == 0)
		{
			iFileId = gastPlFileCheckList[iCnt].iId;
			break;
		}
		
		iCnt++;
	}
	
	switch(iFileId)
	{
		case WSSF:
			iAliasNo = WSSF_START_NO_GET(iNo);
			if(iAliasNo > WSSF_END_NO)	iAliasNo = -1;	
			break;

		case WTSF:
			iAliasNo = WTSF_START_NO_GET(iNo);
			if(iAliasNo > WTSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WYSF:
			iAliasNo = WYSF_START_NO_GET(iNo);
			if(iAliasNo > WYSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WZSF:
			iAliasNo = WZSF_START_NO_GET(iNo);
			if(iAliasNo > WZSF_END_NO)	iAliasNo = -1;	
			break;
			
		case VWSF:
			iAliasNo = VWSF_START_NO_GET(iNo);
			if(iAliasNo > VWSF_END_NO)	iAliasNo = -1;	
			break;
			
		case VXSF:
			iAliasNo = VXSF_START_NO_GET(iNo);
			if(iAliasNo > VXSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WASF:
			iAliasNo = WASF_START_NO_GET(iNo);
			if(iAliasNo > WASF_END_NO)	iAliasNo = -1;	
			break;
			
		case NVSF:
			iAliasNo = NVSF_START_NO_GET(iNo);
			if(iAliasNo > NVSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NUSF:
			iAliasNo = NUSF_START_NO_GET(iNo);
			if(iAliasNo > NUSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NTSF:
			iAliasNo = NTSF_START_NO_GET(iNo);
			if(iAliasNo > NTSF_END_NO)	iAliasNo = -1;	
			break;
			
		case BHSF:
			iAliasNo = BHSF_START_NO_GET(iNo);
			if(iAliasNo > BHSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NSSF:
			iAliasNo = NSSF_START_NO_GET(iNo);
			if(iAliasNo > NSSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NRSF:
			iAliasNo = NRSF_START_NO_GET(iNo);
			if(iAliasNo > NRSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NQSF:
			iAliasNo = NQSF_START_NO_GET(iNo);
			if(iAliasNo > NQSF_END_NO)	iAliasNo = -1;	
			break;

		case HLSF:
			iAliasNo = HLSF_START_NO_GET(iNo);
			if(iAliasNo > HLSF_END_NO)	iAliasNo = -1;	
			break;
		
		case NPSF:
			iAliasNo = NPSF_START_NO_GET(iNo);
			if(iAliasNo > NPSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NOSF:
			iAliasNo = NOSF_START_NO_GET(iNo);
			if(iAliasNo > NOSF_END_NO)	iAliasNo = -1;	
			break;
			
		case EPSF:
			iAliasNo = EPSF_START_NO_GET(iNo);
			if(iAliasNo > EPSF_END_NO)	iAliasNo = -1;	
			break;
			
		case EQSF:
			iAliasNo = EQSF_START_NO_GET(iNo);
			if(iAliasNo > EQSF_END_NO)	iAliasNo = -1;	
			break;

		case V3SF:
			iAliasNo = V3SF_START_NO_GET(iNo);
			if(iAliasNo > V3SF_END_NO)	iAliasNo = -1;	
			break;

		case V4SF:
			iAliasNo = V4SF_START_NO_GET(iNo);
			if(iAliasNo > V4SF_END_NO)	iAliasNo = -1;	
			break;

		case V5SF:
			iAliasNo = V5SF_START_NO_GET(iNo);
			if(iAliasNo > V5SF_END_NO)	iAliasNo = -1;	
			break;

		case V6SF:
			iAliasNo = V6SF_START_NO_GET(iNo);
			if(iAliasNo > V6SF_END_NO)	iAliasNo = -1;	
			break;

		case V7SF:
			iAliasNo = V7SF_START_NO_GET(iNo);
			if(iAliasNo > V7SF_END_NO)	iAliasNo = -1;	
			break;
			
		default:
			break;
	}
	
	return iAliasNo;
}

/******************************************************************************
 * Get_File_Size_Char
 * parameter
 * Return
 ******************************************************************************/
long Get_File_Size_Char(char* pcFile)
{
	FILE* pFp = 0x00;
	long lSize = 0;
	char acFilePath[512];

	memset(acFilePath, 0x00, sizeof(acFilePath));
	memcpy(acFilePath, pcFile, sizeof(acFilePath));

	pFp = fopen(acFilePath, "rb");
	if(0x00 == pFp)
	{
		return -1;
	}
	else
	{
		fseek(pFp, 0L, SEEK_END);
		lSize = ftell(pFp);
	}
	fclose(pFp);

	return lSize;
}

/******************************************************************************
 * Read_File_Char
 * parameter
 * Return
 ******************************************************************************/
long Read_File_Char(char* pcDesc, char* pcFile)
{
	FILE* pFp = 0x00;
	long lSize = 0;
	char acFilePath[512];
	memset(acFilePath, 0x00, sizeof(acFilePath));
	memcpy(acFilePath, pcFile, sizeof(acFilePath));

	pFp = fopen(acFilePath, "rb");
	if(0x00 == pFp)
	{
		return -1;
	}
	else
	{
		fseek(pFp, 0L, SEEK_END);
		lSize = ftell(pFp);
		fseek(pFp, 0L, SEEK_SET);

		fread(pcDesc, sizeof(char), lSize+1, pFp);
	}
	fclose(pFp);

	return lSize;
}

/******************************************************************************
 * Write_File_Char
 * parameter
 * Return
 ******************************************************************************/
int Write_File_Char(unsigned char *data, long len, char *file)
{
	FILE *fp = NULL;
	char filePath[512];
	memset(filePath, 0x00, sizeof(filePath));
	memcpy(filePath, file, sizeof(filePath));

	fp = fopen(filePath,"wb");
	if (fp == NULL)		return -1;
	else{
		char *buff = (char *) malloc(len+1);
		memset(buff, 0x00, len+1);
		memcpy(buff, data, len);
		fputs(buff, fp);
		free(buff);
	}
	fflush(fp);
	fclose(fp);

	proc_log("파일 생성 : %s", filePath);
	proc_log("파일 크기 : %d byte", len);

	return 0;
}

/******************************************************************************
 * Add_Write_File_Char
 * parameter
 * Return
 ******************************************************************************/
int Add_Write_File_Char(char *data, long len, char *file)
{
	FILE *fp = NULL;
	char filePath[512];
	memset(filePath, 0x00, sizeof(filePath));
	memcpy(filePath, file, sizeof(filePath));

	fp = fopen(filePath,"ab");
	if (fp == NULL)		return -1;
	else{
		char *buff = (char *) malloc(len+1);
		memset(buff, 0x00, len+1);
		memcpy(buff, data, len);
		fputs(buff, fp);
		free(buff);
	}
	fflush(fp);
	fclose(fp);

	return 0;
}

/******************************************************************************
 * get_chg_bit
 * parameter
 * Return
 ******************************************************************************/
int get_chg_bit(char cData, char *pcUse)
{
	char acUseTmp[8];
	memset(acUseTmp, 0x30, sizeof(acUseTmp));

	if((cData & 0x80) == 0x80) acUseTmp[0] = 0x31;
	if((cData & 0x40) == 0x40) acUseTmp[1] = 0x31;
	if((cData & 0x20) == 0x20) acUseTmp[2] = 0x31;
	if((cData & 0x10) == 0x10) acUseTmp[3] = 0x31;
	if((cData & 0x08) == 0x08) acUseTmp[4] = 0x31;
	if((cData & 0x04) == 0x04) acUseTmp[5] = 0x31;
	if((cData & 0x02) == 0x02) acUseTmp[6] = 0x31;
	if((cData & 0x01) == 0x01) acUseTmp[7] = 0x31;

	memcpy(pcUse, acUseTmp, sizeof(acUseTmp));

	return 0;
}


/******************************************************************************
 * get_ppd_bit
 * parameter
 * Return
 ******************************************************************************/
int get_ppd_bit(char cData, char *pcUse, char *pcStudent)
{
	char acUseTmp[4], acStudentTmp[4];
	memset(acUseTmp, 0x30, sizeof(acUseTmp));
	memset(acStudentTmp, 0x30, sizeof(acStudentTmp));

	if((cData & 0x80) == 0x80) acUseTmp		[0]	= 0x31;
	if((cData & 0x40) == 0x40) acStudentTmp	[0]	= 0x31;
	if((cData & 0x20) == 0x20) acUseTmp		[1]	= 0x31;
	if((cData & 0x10) == 0x10) acStudentTmp	[1]	= 0x31;
	if((cData & 0x08) == 0x08) acUseTmp		[2]	= 0x31;
	if((cData & 0x04) == 0x04) acStudentTmp	[2]	= 0x31;
	if((cData & 0x02) == 0x02) acUseTmp		[3]	= 0x31;
	if((cData & 0x01) == 0x01) acStudentTmp	[3]	= 0x31;

	memcpy(pcUse, acUseTmp, sizeof(acUseTmp));
	memcpy(pcStudent, acStudentTmp, sizeof(acStudentTmp));

	return 0;
}
