#include "stdafx.h"
#include "Global.h"
#include <SetupAPI.h>
#include <InitGuid.h>
#include <WinIoCtl.h>
#include <direct.h>
#include <fcntl.h>
#include <share.h>
#include <sys\stat.h>
#pragma comment(lib, "SetupAPI.lib")

namespace Global
{
	//////////////////////////////////////////////////////////////////////////
	// 全局变量;
	TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
	TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
	TCHAR g_szFna[MAX_PATH] = { 0 };
	TCHAR g_szConfig[MAX_PATH] = { 0 };
	TCHAR g_szLogPath[MAX_PATH] = { 0 };
	bool g_bEnableLog = false;
	COleDateTime g_lastTime;
	time_t g_time = time(NULL);
	ThreadSection g_critSection;
	FILE *gp_log_fp = NULL;
	
	//////////////////////////////////////////////////////////////////////////
	void MKDIR(LPCTSTR dir) 
	{
		//////////////////////////////////////////////////////////////////////////
		// 创建目录;
		int nleft = 0;
		int nIndex = -1;
		string strdir = dir;
		strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));

		if (strdir.at(strdir.size() - 1) != _T('\\'))
			strdir.append(_T("\\"));

		// 共享路径和硬盘盘符;
		if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
			nleft = strdir.find_first_of(_T("\\"), 2) + 1;	// 去除共享主机名;
		else if (strdir.at(2) == _T('\\'))
			nleft = 3;

		do
		{
			nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));

			if (nIndex != string::npos)
			{
				if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
				{
					WriteTextLog(_T("创建目录失败：%s，错误码：%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
					break;
				}

				nleft += nIndex + 1;
			}
		} while (nIndex != -1);
	};

	// 全局函数;
	/************************************************************************/
	/*  函数：WriteTextLog[7/28/2009 Jeff];
	/*  描述：写文本日志;
	/*  参数：;
	/*  	[IN] ：;
	/*  返回：void;
	/*  注意：;
	/*  示例：;
	/*
	/*  修改：;
	/*  日期：;
	/*  内容：;
	/************************************************************************/
	void WriteTextLog(const TCHAR* format, ...)
	{
		static ThreadSection _critSection;
		AutoThreadSection aSection(&_critSection);
		// 将日志内容输入到文件中;
		// 获取今年年份;
		__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
		struct tm gmtm = { 0 };
		localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;

		// 解析出日志路径;
		TCHAR szlogpath[MAX_PATH] = { 0 };
		_stprintf_s(szlogpath, _T("%slog %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
		// 打开或创建文件;
		FILE* fp = NULL;
		//MKDIR(g_szLogPath);
		//if (_taccess(szlogpath, 0) != -1)
#ifndef UNICODE
		if (_access(szlogpath, 0) != -1)
#else
		if (_taccess(szlogpath, 0) != -1)
#endif
		{ // 存在;
			if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
				// 移动到末尾;
				fseek(fp, 0, SEEK_END);
		}
		else
		{ // 不存在;
			_tfopen_s(&fp, szlogpath, _T("w+"));
		}

		if (fp == NULL)
			return;

		// 格式化前设置语言区域;
		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
		_tsetlocale(LC_CTYPE, _T("chs")); //设定中文;

		// 格式化日志内容;
		va_list args = NULL;
		int len = 0;
		TCHAR* buffer = NULL;
		va_start(args, format);
		// _vscprintf doesn't count. terminating '\0'
		len = _vsctprintf(format, args) + 1;
		buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
		_vstprintf_s(buffer, len, format, args);
		_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);

		// 关闭文件，释放资源并设置回原语言区域;
		free(buffer);
		fclose(fp);
		_tsetlocale(LC_CTYPE, old_locale);
		free(old_locale); //还原区域设定;
	}

	void WritePythonLog(const TCHAR* msg)
	{
		AutoThreadSection aSection(&g_critSection);
		// 将日志内容输入到文件中;
		// 获取今年年份;
		__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
		struct tm gmtm = { 0 };
		localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;

		int nErr = -1;
		// 打开或创建文件;
		static TCHAR szLastLog[MAX_PATH] = {0};
		if ( _tcsicmp(szLastLog, g_szLogPath) != 0 )
		{
			MKDIR(g_szLogPath);
			_tcscpy_s(szLastLog, g_szLogPath);
			if ( gp_log_fp != NULL )
			{	
				fclose(gp_log_fp);
				gp_log_fp = NULL;
			}
#if 0
			int fd = 0;
			if (_tsopen_s(&fd, g_szLogPath, _O_CREAT | _O_WRONLY | _O_APPEND, _SH_DENYRW, _S_IREAD | _S_IWRITE))
			{
				WriteTextLog(_T("创建文件描述符失败：%s,%d,%d"), g_szLogPath, nErr, GetLastError());
				return;
			}

			// 创建并打开文件;
			gp_log_fp = _tfdopen(fd, _T("w+"));
			if (gp_log_fp == NULL )
			{
				WriteTextLog(_T("创建文件失败：%s,%d,%d"), g_szLogPath, nErr, GetLastError());
				return;
			}
#else
			nErr = _tfopen_s(&gp_log_fp, g_szLogPath, "w+");
			if ( gp_log_fp == NULL )
			{
				WriteTextLog(_T("创建文件失败：%s,%d,%d"), g_szLogPath, nErr, GetLastError());
				return;
			}
#endif
		}

		if (gp_log_fp == NULL )
		{
			WriteTextLog(_T("追加文件失败：%s,%d,%d"), g_szLogPath, nErr, GetLastError());
			return;
		}
		
		fseek(gp_log_fp, 0, SEEK_END);
		// 格式化前设置语言区域;
		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
		_tsetlocale(LC_CTYPE, _T("chs")); //设定中文;

		_ftprintf(gp_log_fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);

		fflush(gp_log_fp);
		_tsetlocale(LC_CTYPE, old_locale);
		free(old_locale); //还原区域设定;
	}

	void ClosePythonLog()
	{
		AutoThreadSection aSection(&g_critSection);
		if ( gp_log_fp )
		{
			fclose(gp_log_fp);
			gp_log_fp = NULL;
		}
	}

	// 去除空格;
	std::string& trim(std::string& str)
	{
		int nIndex = 0;
		while ((nIndex = str.find_first_of(' ')) != std::string::npos)
			str.erase(nIndex, 1);

		return str;
	}

	// hModule 模块句柄 NULL表示当前模块;
	bool GetVersion(IN const TCHAR* fname, OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
	{
		VS_FIXEDFILEINFO* pVi = NULL;
		DWORD dwHandle = 0;
		int size = GetFileVersionInfoSize(fname, &dwHandle);
		if (size > 0)
		{
			BYTE* buffer = new BYTE[size];
			memset(buffer, 0, size);

			if (GetFileVersionInfo(fname, 0, size, buffer))
			{
				if (VerQueryValue(buffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& size))
				{
					pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
					pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
					pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
					pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);

					pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
					pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
					pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
					pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);

					delete[] buffer;
					return true;
				}
			}

			delete[] buffer;
		}

		return false;
	}

	BOOL GetVersion(IN HMODULE hModule, OUT DWORD(&dwFVArray)[4], OUT DWORD(&dwPVArray)[4])
	{
		TCHAR fname[MAX_PATH];
		VS_FIXEDFILEINFO* pVi;
		DWORD dwHandle;

		if (GetModuleFileName(hModule, fname, MAX_PATH))
		{
			INT nSize = GetFileVersionInfoSize(fname, &dwHandle);

			if (nSize > 0)
			{
				BYTE* pBuffer = new BYTE[nSize];
				memset(pBuffer, 0, nSize);

				if (GetFileVersionInfo(fname, 0, nSize, pBuffer))
				{
					if (VerQueryValue(pBuffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& nSize))
					{
						dwFVArray[0] = HIWORD(pVi->dwFileVersionMS);
						dwFVArray[1] = LOWORD(pVi->dwFileVersionMS);
						dwFVArray[2] = HIWORD(pVi->dwFileVersionLS);
						dwFVArray[3] = LOWORD(pVi->dwFileVersionLS);

						dwPVArray[0] = HIWORD(pVi->dwProductVersionMS);
						dwPVArray[1] = LOWORD(pVi->dwProductVersionMS);
						dwPVArray[2] = HIWORD(pVi->dwProductVersionLS);
						dwPVArray[3] = LOWORD(pVi->dwProductVersionLS);

						delete[]pBuffer;
						return TRUE;
					}
				}

				if (pBuffer)
					delete[]pBuffer;
			}
		}

		return FALSE;
	}

	WCHAR* ASCII2UNICODE(IN LPCCH lpASCIIStr)
	{
		if (lpASCIIStr == NULL)
			return NULL;

		// 获取宽字符字节数;
		int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
		if (cchWideChar == 0)
			return NULL;

		// 转换成宽字符串;
		WCHAR* pWideChar = new WCHAR[cchWideChar + 1];
		memset(pWideChar, 0, sizeof(WCHAR) * (cchWideChar + 1));
		int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pWideChar, cchWideChar);
		if (nWriteNum != cchWideChar)
		{
			if (pWideChar)
				delete[] pWideChar;
			return NULL;
		}

		return pWideChar;
	}

	BOOL UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT string& strResult)
	{
		if (lpUNICODEStr == NULL)
			return FALSE;

		// 获取多字节字符字节数;
		int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
		if (cbMultiByte == 0)
			return FALSE;

		// 转换成多字节字符;
		CHAR* pResult = new CHAR[cbMultiByte];
		memset(pResult, 0, cbMultiByte);
		int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
		if (nWriteNum != cbMultiByte)
			return FALSE;

		strResult = pResult;
		if (pResult)
			delete[] pResult;

		return TRUE;
	}

	BOOL ASCII2UTF8(IN LPCCH lpASCIIStr, OUT string& strResult)
	{
		// 将ASCII字符串转成UNICODE字符串;
		WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
		if (pWideChar == NULL)
			return FALSE;

		// 再将UICODE转成UTF8;
		BOOL bResult = UNICODE2UTF8(pWideChar, strResult);

		if (pWideChar)
			delete[] pWideChar;

		return bResult;
	}

	string EnCode_UTF8URL(IN const CHAR* pText)
	{
		string tt = "";
		string dd = "";
		ASCII2UTF8(pText, tt);

		size_t len = tt.length();
		for (size_t i = 0; i < len; i++)
		{
			if (isalnum((BYTE)tt.at(i)))
			{
				char tempbuff[2] = { 0 };
				sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
				dd.append(tempbuff);
			}
			else if (isspace((BYTE)tt.at(i)))
			{
				dd.append("+");
			}
			else
			{
				char tempbuff[4];
				sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
				dd.append(tempbuff);
			}
		}
		return dd;
	}

	void EnCode_UTF8URL(IN const CHAR* pText, OUT string& strResult)
	{
		string tt = "";
		ASCII2UTF8(pText, tt);

		size_t len = tt.length();
		for (size_t i = 0; i < len; i++)
		{
			if (isalnum((BYTE)tt.at(i)))
			{
				char tempbuff[2] = { 0 };
				sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
				strResult.append(tempbuff);
			}
			else if (isspace((BYTE)tt.at(i)))
			{
				strResult.append("+");
			}
			else
			{
				char tempbuff[4];
				sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
				strResult.append(tempbuff);
			}
		}
	}

	//做为解Url使用
	char CharToInt(char ch)
	{
		if (ch >= '0' && ch <= '9')return (char)(ch - '0');
		if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
		if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
		return -1;
	}

	char StrToBin(IN char(&str)[2])
	{
		char tempWord[2];
		char chn;

		tempWord[0] = CharToInt(str[0]);                         //make the B to 11 -- 00001011
		tempWord[1] = CharToInt(str[1]);                         //make the 0 to 0  -- 00000000

		chn = (tempWord[0] << 4) | tempWord[1];                //to change the BO to 10110000

		return chn;
	}

	CHAR* UNICODE2ASCII(IN LPWCH lpUNICODEStr)
	{
		if (lpUNICODEStr == NULL)
			return NULL;

		// 获取多字节字符字节数;
		int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
		if (cbMultiByte == 0)
			return NULL;

		// 转换成多字节字符;
		CHAR *pMultiByteStr = new CHAR[cbMultiByte + 1];
		memset(pMultiByteStr, 0, cbMultiByte + 1);
		int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
		if (nWriteNum != cbMultiByte)
		{
			if (pMultiByteStr)
				delete[]pMultiByteStr;
			return NULL;
		}

		pMultiByteStr[cbMultiByte] = '\0';

		return pMultiByteStr;
	}

	BOOL UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
	{
		if (lpUNICODEStr == NULL)
			return FALSE;

		// 获取多字节字符字节数;
		int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
		if (cbMultiByte == 0 || cbMultiByte >= nASCIIStrLen)
			return FALSE;

		// 转换成多字节字符;
		memset((void*)pASCIIStr, 0, nASCIIStrLen);
		int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pASCIIStr, cbMultiByte, NULL, NULL);
		if (nWriteNum != cbMultiByte)
		{
			return FALSE;
		}

		return TRUE;
	}

	/************************************************************************/
	/*  函数：[7/26/2016 IT];
	/*  描述：;
	/*  参数：;
	/*  	[IN] ：;
	/*  	[OUT] ：;
	/*  	[IN/OUT] ：;
	/*  返回：void;
	/*  注意：;
	/*  示例：;
	/*
	/*  修改：;
	/*  日期：;
	/*  内容：;
	/************************************************************************/
	string DeCode_URLUNICODE(IN const CHAR* pURLText)
	{
		string str = pURLText;
		string strResult = "";

		INT nIndex = 0;
		string strTemp = "";

		while (str.find("\\u", 0, 2) != string::npos)
		{
			nIndex = str.find("\\u", 0, 2);
			strResult.append(str.substr(0, nIndex));
			strTemp = str.substr(nIndex + 2, 4);
			str = str.substr(nIndex + 2 + 4);
			CHAR szReturn[10] = { 0 };

			union  __UNION_VAR_INT {
				BYTE   ch[2];
				int	   value;
			}unionVarInt;

			unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
			unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);

			WCHAR szWide[2] = { 0 };
			szWide[0] = unionVarInt.value;
			UNICODE2ASCII(szWide, szReturn, 10);
			strResult.append(szReturn);
		}
		strResult.append(str);

		return strResult;
	}

	// 通过注册表查找系统当前串口信息;
	
	BOOL IsValidString(LPCTSTR lpszString)
	{
		if (lpszString == NULL)
			return FALSE;

		do
		{
			// ASCII可显示的字符;
			if (*lpszString < 32 || *lpszString > 126)
			{
				return FALSE;
			}
		} while (*++lpszString);

		return TRUE;
	}
} // namespace Global