#include "stdafx.h"
#include "Global.h"
#include "crc32.h"


TCHAR g_szServerIP[100] = _T("");
TCHAR g_szCmdPort[10] = _T("");
TCHAR g_szFilePort[10] = _T("");
DWORD g_dwCmdPort = 0;
DWORD g_dwFilePort = 0;

// 1.
TCHAR g_szDBSource[MAX_PATH+1] = _T("");
DWORD g_dwDBServerPort = 0;
TCHAR g_szDBAccount[MAX_PATH+1] = _T("");
TCHAR g_szDBPassWord[MAX_PATH+1] = _T("");
TCHAR g_szDBName[MAX_PATH+1] = _T("");

// 2.
TCHAR g_ModulePath[_MAX_PATH] = _T("");
TCHAR g_ModuleFileName[_MAX_PATH] = _T("");
TCHAR g_szConnectString[MAX_PATH] = _T("");		// DB连接串;

// 3.
int  g_nType = 0;
long g_nMachineNumber = 0;
long g_nLicense = 0;
long g_nComPort = 0;
long g_nBaudRate = 0;
char g_szTelNumber[MAX_PATH+1] = _T("");
long g_nWaitDialTime = 0;
char g_szIpAddress[MAX_PATH+1] = _T("");
long g_nPort = 0;
long g_nTimeOut = 0;
long g_nProtocolType = 0;
long g_nNetPassword = 0;

STFileInfobody *g_pSTFileInfobody = NULL;
DWORD		   g_dwRecived;	// 已接收的长度;

BOOL g_bGetRoutinePath = FALSE;
void GetRoutinePath()
{
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath)/sizeof(TCHAR));
	sprintf_s(g_ModuleFileName,"%s",g_ModulePath);

	_tsplitpath(g_ModulePath, szDrive, szDir, NULL, NULL);
	_tcscpy(g_ModulePath, szDrive);
	_tcscat(g_ModulePath, szDir);
}

/*
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext );

path：全路径（IN）
drive：盘符（OUT）
dir：除去盘符和文件名，中间的那段路径（OUT）
fname：文件名，不包含扩展名（OUT）
ext：文件扩展名，包含那个点.（OUT）
*/
void GetFileName(IN const TCHAR *pFullName,OUT TCHAR *pFileName)
{
	TCHAR szExt[_MAX_EXT];
	_tsplitpath(pFullName, NULL, NULL, pFileName, szExt);
	_tcscat(pFileName, szExt);
}

unsigned int VerifyIntegrityPacket(IN void *pIntegrityPacket,IN unsigned int nPacketSize)
{
	unsigned int checksum = 0;
	//if ( nPacketSize <= sizeof(STProtocolheader) )
	//{
	//	return 0;
	//}
	unsigned char *pBody = &((unsigned char*)pIntegrityPacket)[sizeof(STProtocolheader)];

	if( pBody )
		checksum = crc32( 0, pBody, nPacketSize-sizeof(STProtocolheader) );

	return checksum;
}

int GetServerIniInfo(const char *szPath/*=NULL*/, const char *szIniName/*=NULL*/)
{
	if ( !g_bGetRoutinePath )
		GetRoutinePath();
	
	TCHAR szFile[MAX_PATH + 1] = _T("");
	if( szPath != NULL && szIniName != NULL)
		wsprintf(szFile, "%s\\%s", szPath, szIniName);
	else
		wsprintf(szFile,"%s\\lyfzServiceInfo.ini",g_ModulePath);

	OFSTRUCT ofStruct;
	OpenFile(szFile,&ofStruct,OF_EXIST);
	if( ERROR_FILE_NOT_FOUND == GetLastError())
	{
		return -1;
	}

	g_dwCmdPort = GetPrivateProfileInt("ServerInfo", "cmdPort", 0, szFile);
	g_dwFilePort = GetPrivateProfileInt("ServerInfo", "filePort", 0, szFile);

	GetPrivateProfileString("ServerInfo", "cmdPort", "", g_szCmdPort, 10, szFile);
	GetPrivateProfileString("ServerInfo", "filePort", "", g_szFilePort, 10, szFile);

	GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, szFile);
	g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, szFile);

	if( g_dwDBServerPort != 0)
		sprintf(g_szConnectString,"driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s",
		g_szDBSource,g_dwDBServerPort,g_szDBName,g_szDBAccount,g_szDBPassWord);
	else
		sprintf(g_szConnectString,"driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s",
		g_szDBSource,g_szDBName,g_szDBAccount,g_szDBPassWord);

	return 0;
}

int GetClientIniInfo(const char *szPath/*=NULL*/, const char *szIniName/*=NULL*/)
{
	if ( !g_bGetRoutinePath )
		GetRoutinePath();

	TCHAR szFile[MAX_PATH + 1] = _T("");
	if( szPath != NULL && szIniName != NULL)
		wsprintf(szFile, "%s\\%s", szPath, szIniName);
	else
		wsprintf(szFile,"%s\\ServiceInfo.ini",g_ModulePath);

	OFSTRUCT ofStruct;
	OpenFile(szFile,&ofStruct,OF_EXIST);
	if( ERROR_FILE_NOT_FOUND == GetLastError())
	{
		return -1;
	}

	//g_dwCmdPort = GetPrivateProfileInt("ServerInfo", "cmdPort", 0, szFile);
	//g_dwFilePort = GetPrivateProfileInt("ServerInfo", "filePort", 0, szFile);

	GetPrivateProfileString("ServerInfo", "IP", "", g_szServerIP, 100, szFile);
	GetPrivateProfileString("ServerInfo", "cmdPort", "", g_szCmdPort, 10, szFile);
	GetPrivateProfileString("ServerInfo", "filePort", "", g_szFilePort, 10, szFile);

	g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, szFile);
	GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, szFile);

	return 0;
}