#pragma once
#include <iomanip>
#include <sstream>
#include <vector>

using namespace std;

#ifndef _UNICODE
#define tstring std::string
#else
#define tstring std::wstring
#endif

#ifndef _UNICODE
#define tstringstream std::stringstream
#else
#define tstringstream std::wstringstream
#endif


template<class T = char>
class autoptr
{
public:
	autoptr(T *buf) : m_buf(buf) {}
	~autoptr() {
		if (m_buf)
		{
			delete m_buf;
			m_buf = NULL;
		}
	}
private:
	T *m_buf;
};

template<class T = char>
class autoptr_arr
{
public:
	autoptr_arr(T *buf) : m_buf(buf) {}
	~autoptr_arr() {
		delete[]m_buf;
	}
private:
	T *m_buf;
};

bool IsLeap(int year);
bool IsWeekEnd(int year, int month, int day);
bool Nextday(int& year, int& month, int& day);

void Full2Half(wchar_t* str);
bool split(const tstring& src, tstring delimit,vector<tstring> &v, tstring null_subst=_T(""));
bool split(const string& src, string delimit, vector<string> &v, string null_subst = "");
vector<tstring> splitEx(const tstring& src, tstring separate_character);
vector<string> splitEx(const string& src, string separate_character);
int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len);
void Gb2312_2_Unicode(unsigned short* dst, const char* src);
void Unicode_2_UTF8(char* dst, unsigned short* src);
string ConvertUTF8toGB2312(const char *pData, size_t size);
string ConvertGB2312toUTF8(const char *pData);
bool TransToPriceFormat(const tstring& strSrc, tstring& strDes, double& dDes);

wstring GB2312ToUnicode(const char *pData);
wstring UTF8ToUnicode(const char* szU8);
void UTF8toANSI(string &strUTF8);
string UnicodeToUtf8(const wchar_t* unicode);
string UnicodeToGB2312(TCHAR* pData);
string UnicodeToGB2312(const tstring& strSrc);
string UnicodeToGB2312(const CString& strSrc);
tstring GB2312ToTstring(string str);
string	TstringToGB2312(tstring tstr);
string TstringToUTF8(tstring tstr);
tstring UTF8ToTstring(string str);

inline bool IsChinese(unsigned char c) { return ((c>=0xa1)&&(c<=0xfe)); }
int CalcWord(const char *ps, const size_t length);

void Int64DateToStr(const __int64& llDate, string& out);
void Int64DateToYMDStr(const __int64& llDate, string& out);
void Int64DateToHMStr(const __int64& llDate, string& out);

void GetSubStr(TCHAR *szDestination, const TCHAR *szSource, const int nWidth, HDC hDC);

bool IsNumAndPoint(const CString& strText);

unsigned char ToHex(unsigned char x);
unsigned char FromHex(unsigned char x);
string UrlEncode(const string& str);
string UrlDecode(const string& str);
void DeleteDirectory(const CString &strPath);

void toUpper(char *szDestination, const char *szSource);
void toUpper(TCHAR *szDestination, const TCHAR *szSource);

void FormatPrice(char *szDes, const double dSource);  // 格式化价格 小数部分至少两位

bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark);

bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark = NULL
	, const char *szDanger = NULL, const char *szOCO = NULL, const char *szPACK = NULL);

string replace(const char *szContext, char oldChar, const char *szNew);

__int64 StringToInt64(const TCHAR *szSourse);

void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src);
void SafeCopyData(TCHAR dest[], int destSize, tstring str);

tstring GetRequestByKey(const tstring& strKey,const tstring& strReuest);  // 解析URL中的参数

void GetKeysByUrl(const tstring & strUrl, vector<tstring> & vctKey);

__int64 GetCharNumber(TCHAR* szText, size_t n);	// 获取字符串所有的数据			

CString  AddNumberSeparate(CString strSrc);

bool RegexStdMatch(string src, string regular);  // std 正则表达式校验
bool RegexStdMatch(tstring src, tstring regular);
tstring   replace_all(tstring&   str, const   tstring&   old_value, const   tstring&   new_value);

template<typename T>
tstring  tostring(T t, int bit = 4)
{
	tstring result;
	wstringstream ss;
	ss << fixed << setprecision(bit) << t;
	ss >> result;
	CString strTemp = result.c_str();

	int nIndex = strTemp.Find(_T("."));
	if (nIndex != -1)
	{
		strTemp.TrimRight(_T("0"));
		if (nIndex == strTemp.GetLength() - 1)
		{
			strTemp.TrimRight(_T("."));
		}
	}
	if (strTemp == _T("-0"))
		strTemp = _T("0");
	return tstring(strTemp);
}
tstring ANSIToUnicode(const string& str);

CString FormatNumberString(CString strData);

#define CLOSE_IMAGE(pImage) if (pImage)\
	{\
		delete pImage;\
		pImage = NULL;\
	}\


#define CLOSE_ICON(hIcon) if (hIcon)\
	{\
		::DestroyIcon(hIcon);\
		hIcon = NULL; \
	}\
	
//格式化手机号码
#define FormatPhoneNumber(strPhone)\
if (strPhone.GetLength() > 5)\
{\
	strPhone = strPhone.Mid(0, 3) + "******" + strPhone.Mid(strPhone.GetLength() - 2, 2);\
}
string FormatInt32Value2(int nValue);
tstring FormatInt32Value(int nValue);
tstring FormatInt64Value(__int64 nValue);
tstring FormatFloatValue(double fValue);
string FormatFloatValue2(double fValue);
CString GetAppPath();