/**********************************************************************
// source file name    : util.c
// date of preperation : 2016.03.17
// date of last update : 2016.03.17
// developer           : mskim
**********************************************************************/

#include "util.h"
#include "MessageInfo.h"
#include <inttypes.h>

int Cto0NdC(char *dst, char *src, int len)
{
	char acZero[1024];
	char acDst[1024];
	int	iTmp=0;
	int iSrcLen=0;
	int i;

	memset(acZero, 0x30, len);
	memset(acDst, 0x00, sizeof(acDst));
	
	for(i=0; i<strlen(src); i++)
	{
		if(src[i] == 0x00 || src[i] == 0x20)
		{
			break;
		}
		iSrcLen++;
	}

	iTmp = len - iSrcLen;
	if(iTmp < 0)
	{
		memcpy(dst, src, iSrcLen);
		return -1;
	}

	memcpy(acDst, acZero, iTmp); 
	memcpy(acDst+iTmp, src, iSrcLen);

	memcpy(dst, acDst, strlen(acDst));
	
	return 0;
}

int64_t S64(const char *s) 
{
  int64_t i;
  char c ;
  //int scanned = sscanf(s, "%d" SCNd64 "%c", &i, &c);
  int scanned = sscanf(s, "%d %c", &i, &c);
  if (scanned == 1) return i;
  if (scanned > 1) {
    // TBD about extra data found
    return i;
    }
  // TBD failed to scan;  
  return 0;  
}

/******************************************************************************
 * 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);
		if(lSize < 0)
			perror("Get_File_Size_Char Error");
	}
	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);

	WriteLog(LogType_StandardOut, LogLevel_Info, "파일 생성 : %s", filePath);
	WriteLog(LogType_StandardOut, LogLevel_Info, "파일 크기 : %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;
}

/******************************************************************************
 * Byte_Ordering
 * parameter
 * Return
 ******************************************************************************/
void Byte_Ordering(void *dest,const void *src,size_t n)
{
	int              i, j;
	int              cur_stat = 0x01020304;
	unsigned char *  pp  = (unsigned char *)src;
	unsigned char *  rtn = (unsigned char *)dest;

	if (n <= 0) return;

	if (((unsigned char *)&cur_stat)[0] == 0x01) 
	{
		for (i = 0, j = 0; i < (int)n; i++, j++) rtn[i] = pp[j];
	} 
	else 
	{
		for (i = 0, j = n-1; i < (int)n; i++, j--) rtn[i] = pp[j];
	} 
	return;
}
