#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

#define DOFREE(obj)								\
{												\
	if(obj)										\
	{											\
	MEM_free(obj);							\
	obj = NULL;								\
	}											\
}
std::vector<std::string> split2(const std::string &str, const std::string &pattern)
{
	std::vector<std::string> resVec;

	if ("" == str)
	{
		return resVec;
	}
	//方便截取最后一段数据
	std::string strs = str + pattern;

	size_t pos = strs.find(pattern);
	size_t size = strs.size();

	while (pos != std::string::npos)
	{
		std::string x = strs.substr(0, pos);
		resVec.push_back(x);
		strs = strs.substr(pos + pattern.size(), size);
		pos = strs.find(pattern);
	}

	return resVec;
};
std::vector<std::string> split(const std::string &str, const std::string &pattern)
{
	std::vector<std::string> resVec;

	if ("" == str)
	{
		return resVec;
	}
	//方便截取最后一段数据
	std::string strs = str + pattern;

	size_t pos = strs.find(pattern);
	size_t size = strs.size();

	while (pos != std::string::npos)
	{
		std::string x = strs.substr(0, pos);
		resVec.push_back(x);
		strs = strs.substr(pos + pattern.size(), size);
		pos = strs.find(pattern);
	}

	return resVec;
};
//分割字符串
void split(char *src, const char *separator, char **dest, int *num) {

	vector<std::string> resultVector = split(src, separator);
	int count = 0;
	for (auto result : resultVector) {
		int nLen = strlen(result.c_str());
		char *pCh = new char[nLen + 1];
		strcpy(pCh, result.c_str());
		dest[count] = pCh;
		++count;
	}
	*num = count;
	///*
	//src 源字符串的首地址(buf的地址)
	//separator 指定的分割字符
	//dest 接收子字符串的数组
	//num 分割后子字符串的个数
	//*/
	//char *pNext;
	//int count = 0;
	//if (src == NULL || strlen(src) == 0)  //如果传入的地址为空或长度为0，直接终止 
	//	return;
	//if (separator == NULL || strlen(separator) == 0) //如未指定分割的字符串，直接终止 
	//	return;


	////printf("分割前的字符=======================%s\n", src);

	//pNext = strtok(src, separator); //必须使用(char *)进行强制类型转换(虽然不写有的编译器中不会出现指针错误)
	//while (pNext != NULL) {


	//	//printf("分割后的字符=======================%s\n", pNext);

	//	dest[count] = pNext;
	//	++count;
	//	pNext = strtok(NULL, separator);  //必须使用(char *)进行强制类型转换
	//}
	//*num = count;
}

char* U2G(const char* utf8)
{

	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);

	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);

	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);

	char* str = new char[len + 1];
	memset(str, 0, len + 1);

	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);

	if (wstr)
	{
		delete[] wstr;
		wstr = NULL;
	}
	return str;
}

void removeChar(char* str, char c)
{
	char *tmp = str;   //定义tmp，临时存储str的字符串；
	int i = 0;
	while (*tmp)  //当tmp指向字符串末尾‘\0’时结束循环
	{
		if (*tmp != c)  //判断字符是否为空格，不是则将该字符存到str中；
		{
			str[i++] = *tmp;
		}
		tmp++;
	}
	str[i] = '\0';  //字符串用结束符标识
	//puts(str);   //输出得到的新字符串
	//DOFREE(tmp);
}

/**********************************************************************
* 函数名称：    gbk_strlen
* 功能描述：  计算含有汉字的字符串长度，汉字作为一个字符处理
* 输入参数：  需要计算的字符串
* 输出参数：
* 返 回 值：  字符串长度
* 其它说明：
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
*
***********************************************************************/
int gbk_strlen(const char* str)
{
	const char* p = str;        //p用于后面遍历
	while (*p)                    //若是结束符0，则结束循环
	{
		if (*p < 0 && (*(p + 1)<0 || *(p + 1) < 63))            //中文汉字情况
		{
			str++;                //str移动一位，p移动移动2位，因此长度加1
			p += 2;
		}
		else
		{
			p++;                //str不动，p移动一位，长度加1
		}
	}
	return p - str;                //返回地址之差
}

void stringToLenth(int value, int num, char ** result)
{
	string num_str = to_string(value);
	if (num_str.size() > num)
	{
		num_str = num_str.substr(0, num);
	}
	else
	{
		while (num_str.size() < num) {
			num_str = "0" + num_str;
		}
	}
	char * resultValue = new char[num];
	strcpy(resultValue, num_str.c_str());
	*result = resultValue;
}

bool count(vector<char *> values, char * value)
{
	for (auto referencedValue : values) {
		if (strcmp(referencedValue, value) == 0)
		{
			return true;
		}
	}
	return false;
}

void split(std::string s, const char* delim, std::vector<std::string>* ret)
{
	size_t last = 0;
	size_t index = s.find(delim, last);
	int size = strlen(delim);
	while (index != std::string::npos) {
		ret->push_back(s.substr(last, index - last));
		last = index + size;
		index = s.find(delim, last);
	}
	if (index - last > 0) {
		ret->push_back(s.substr(last, index - last));
	}
}



void vecToArray(vector<string> &vec, char ***arr)
{
	(*arr) = (char **)MEM_alloc(vec.size()*sizeof(char *));
	for (auto i = 0; i<vec.size(); i++)
	{
		(*arr)[i] = (char *)MEM_alloc((vec[i].size() + 1)*sizeof(char));
		strcpy((*arr)[i], vec[i].c_str());
	}
}
const char* newGUID()
{
	static char buf[64] = { 0 };
	GUID guid;
	if (S_OK == ::CoCreateGuid(&guid))
	{
		_snprintf(buf, sizeof(buf)
			, "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X"
			, guid.Data1
			, guid.Data2
			, guid.Data3
			, guid.Data4[0], guid.Data4[1]
			, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
			, guid.Data4[6], guid.Data4[7]
			);
	}
	return (const char*)buf;

}

string replace_all_distinct(char* strC, char*  old_valueC, char*  new_valueC)
{

	string str = strC;
	string old_value = old_valueC;
	string new_value = new_valueC;

	for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
		if ((pos = str.find(old_value, pos)) != string::npos)
			str.replace(pos, old_value.length(), new_value);
		else   break;
	}
	return str;
	//strC = bstr;
	//strcpy(strC,bstr);
}

logical isVecHasStr(vector<string> vec,char * str)
{
	for each (string s in vec)
	{
		if (stricmp(s.c_str(), str) == 0)
		{
			return true;
		}
	}
	return false;
}
char* G2U(const char* gb2312)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}