#include "StdAfx.h"
#include "filehelp.h"
#include <shlwapi.h>

// 排序和乱序使用stl;
#include <cstdlib>
#include <ctime>
#include <algorithm>
// _mkdir头文件;
#include <direct.h>

int myrandom(int i) { return std::rand() % i; }

filehelpImpl::filehelpImpl(void)
{
	
}

filehelpImpl::~filehelpImpl(void)
{

}

/************************************************************************/
/*  函数：[2/21/2017 Jeff];
/*  描述：;
/*  参数：;
/*  	[IN] ：;
/*  	[OUT] ：;
/*  	[IN/OUT] ：;
/*  返回：void;
/*  注意：;
/*  示例：;
/*
/*  修改：;
/*  日期：;
/*  内容：;
/************************************************************************/
void filehelpImpl::random(IN STR_VEC& vtContent, IN BOOL bSort /* = TRUE */)
{
	if (bSort)
	{//排序;
		std::sort(vtContent.begin(), vtContent.end());
	}
	else
	{//乱序;
		//设置随即数生成器的种子;
		std::srand(unsigned(std::time(0)));
		std::random_shuffle(vtContent.begin(), vtContent.end(), myrandom);
	}
}

/************************************************************************/
/*  函数：[2/21/2017 Jeff];
/*  描述：;
/*  参数：;
/*  	[IN] ：;
/*  	[OUT] ：;
/*  	[IN/OUT] ：;
/*  返回：void;
/*  注意：;
/*  示例：;
/*
/*  修改：;
/*  日期：;
/*  内容：;
/************************************************************************/
void filehelpImpl::split(OUT vector<TString>& vtSplit, IN const TString str, IN const TString strSplit)
{
	if (str.size() == 0 || strSplit.size() == 0)
		return;

	INT nIndex = 0;
	vtSplit.clear();
	TString strtmp = str;
	TString strtmp2;
	do 
	{
		if (TString::npos != (nIndex = strtmp.find_first_of(strSplit)))
		{
			strtmp2 = strtmp.substr(0, nIndex);
			if (strtmp2.size())vtSplit.push_back(strtmp2);
			strtmp = strtmp.substr(nIndex + strSplit.size());
		}
	} while (strtmp.find_first_of(strSplit) != TString::npos);

	if (strtmp.size())
		vtSplit.push_back(strtmp);
}

/************************************************************************/
/*  函数：[2/19/2017 Jeff];
/*  描述：保留指定的扩展名文件;
/*  参数：;
/*  	[IN] keepExt：要保留的扩展名;
/*  	[OUT] vtfiles：要提取保留扩展名的文件数组;
/*  	[IN/OUT] ：;
/*  返回：void;
/*  注意：;
/*  示例：;
/*
/*  修改：;
/*  日期：;
/*  内容：;
/************************************************************************/
void filehelpImpl::keepdownbyext(IN const TString &keepExt, IN STR_VEC &vtfiles)
{
	if (keepExt.find(_T("*.*")) != TString::npos) return;
	if (vtfiles.size() == 0) return;

	// 获取复制扩展名;
	int nIndex = 0;
	TString strtmp;
	TString strRemainExt(keepExt);
	strRemainExt.append(_T("|"));
	STR_VEC vtRemainExt;

	// 将所有扩展名解析到数组里;
	do
	{
		nIndex = strRemainExt.find(_T('|'));
		if (nIndex != TString::npos)
		{
			strtmp = strRemainExt.substr(0, nIndex);
			strRemainExt = strRemainExt.substr(nIndex + 1);

			if (strtmp.compare(_T("*.*")))
				vtRemainExt.push_back(strtmp);
		}
	} while (strRemainExt.find(_T('|')) != TString::npos);

	STR_VEC vtresult;
	// 过滤非复制扩展名的文件,同时去掉缩略图,保留指定复制扩展名的文件;
	for (STR_VEC::iterator itExt = vtRemainExt.begin(); itExt != vtRemainExt.end(); itExt++)
	{
		for (STR_VEC::iterator it = vtfiles.begin(); it != vtfiles.end();)
		{
			if (match(itExt->substr(1), it->c_str())) 
			{
				vtresult.push_back(*it);
				it = vtfiles.erase(it);
			}
			else
				it++;
		}
	}

	vtfiles.clear();
	for ( STR_VEC::iterator it = vtresult.begin(); it != vtresult.end(); it++ )
	{
		vtfiles.push_back(*it);
	}
}

void filehelpImpl::keepdownbyext(IN const TString &keepExt, IN const STR_VEC &vtfiles, OUT STR_VEC &vtresult)
{
	if (keepExt.find(_T("*.*")) != TString::npos) return;
	if (vtfiles.size() == 0) return;

	// 获取复制扩展名;
	int nIndex = 0;
	TString strtmp;
	TString strRemainExt(keepExt);
	strRemainExt.append(_T("|"));
	STR_VEC vtRemainExt;

	// 将所有扩展名解析到数组里;
	do
	{
		nIndex = strRemainExt.find(_T('|'));
		if (nIndex != TString::npos)
		{
			strtmp = strRemainExt.substr(0, nIndex);
			strRemainExt = strRemainExt.substr(nIndex + 1);

			if (strtmp.compare(_T("*.*")))
				vtRemainExt.push_back(strtmp);
		}
	} while (strRemainExt.find(_T('|')) != TString::npos);

#if 0
	// 过滤非复制扩展名的文件,同时去掉缩略图,保留指定复制扩展名的文件;
	for (STR_VEC::iterator itExt = vtRemainExt.begin(); itExt != vtRemainExt.end(); itExt++)
	{
		for (STR_VEC::const_iterator it = vtfiles.begin(); it != vtfiles.end(); it++)
		{
			if (match(itExt->substr(1), it->c_str())) 
			{
				vtresult.push_back(*it);
			}
		}
	}
#else
	STR_VEC vt_files;
	for (STR_VEC::const_iterator it = vtfiles.begin(); it != vtfiles.end(); it++ )
	{// 不能直接用vtresult来复制,因为如果vtresult不为空的情况下,会有异常出现;
		vt_files.push_back(*it);
	}

	for (STR_VEC::iterator itExt = vtRemainExt.begin(); itExt != vtRemainExt.end(); itExt++)
	{
		for (STR_VEC::iterator it = vt_files.begin(); it != vt_files.end(); )
		{
			if (match(itExt->substr(1), it->c_str()))
			{
				vtresult.push_back(*it);
				it = vt_files.erase(it);
			}
			else
				it++;
		}
	}
#endif
}

/************************************************************************/
/*  函数：keepoutext[2/19/2017 Jeff];
/*  描述：移除指定扩展名的文件;
/*  参数：;
/*  	[IN] ：;
/*  	[OUT] ：;
/*  	[IN/OUT] ：;
/*  返回：void;
/*  注意：;
/*  示例：;
/*
/*  修改：;
/*  日期：;
/*  内容：;
/************************************************************************/
void filehelpImpl::keepoutbyext(IN const TString &removeext, IN STR_VEC &vtfiles)
{
	if (removeext.find(_T("*.*")) != TString::npos) return;
	if (vtfiles.size() == 0) return;

	// 获取复制扩展名;
	int nIndex = 0;
	TString strtmp;
	TString strRemoveExt(removeext);
	strRemoveExt.append(_T("|"));
	STR_VEC vtRemoveExt;

	// 将所有扩展名解析到数组里;
	do
	{
		nIndex = strRemoveExt.find(_T('|'));
		if (nIndex != TString::npos)
		{
			strtmp = strRemoveExt.substr(0, nIndex);
			strRemoveExt = strRemoveExt.substr(nIndex + 1);

			if (strtmp.compare(_T("*.*")))
				vtRemoveExt.push_back(strtmp);
		}
	} while (strRemoveExt.find(_T('|')) != TString::npos);

	// 过滤非复制扩展名的文件,同时去掉缩略图,保留指定复制扩展名的文件;
	for (STR_VEC::iterator itExt = vtRemoveExt.begin(); itExt != vtRemoveExt.end(); itExt++)
	{
		for (STR_VEC::iterator it = vtfiles.begin(); it != vtfiles.end();)
		{
			if (match(itExt->substr(1), it->c_str())) 
			{
				it = vtfiles.erase(it);
			}
			else
				it++;
		}
	}
}

void filehelpImpl::keepoutbyext(IN const TString &removeext, IN const STR_VEC &vtfiles, OUT STR_VEC &vtresult)
{
	if (removeext.find(_T("*.*")) != TString::npos) return;
	if (vtfiles.size() == 0) return;

	// 获取复制扩展名;
	int nIndex = 0;
	TString strtmp;
	TString strRemoveExt(removeext);
	strRemoveExt.append(_T("|"));
	STR_VEC vtRemoveExt;

	// 将所有扩展名解析到数组里;
	do
	{
		nIndex = strRemoveExt.find(_T('|'));
		if (nIndex != TString::npos)
		{
			strtmp = strRemoveExt.substr(0, nIndex);
			strRemoveExt = strRemoveExt.substr(nIndex + 1);

			if (strtmp.compare(_T("*.*")))
				vtRemoveExt.push_back(strtmp);
		}
	} while (strRemoveExt.find(_T('|')) != TString::npos);

	// 复制到新容器中;
	STR_VEC vt_files;
	for (STR_VEC::const_iterator it = vtfiles.begin(); it != vtfiles.end(); it++ )
	{// 不能直接用vtresult来复制,因为如果vtresult不为空的情况下,会有异常出现;
		vt_files.push_back(*it);
	}

	// 过滤非复制扩展名的文件,同时去掉缩略图,保留指定复制扩展名的文件;
	for (STR_VEC::iterator itExt = vtRemoveExt.begin(); itExt != vtRemoveExt.end(); itExt++)
	{
		for (STR_VEC::iterator it = vt_files.begin(); it != vt_files.end();)
		{
			if (match(itExt->substr(1), it->c_str()))
			{
				vtresult.push_back(*it);
				it = vt_files.erase(it);
			}
			else
				it++;
		}
	}
}

void filehelpImpl::keepdowbyname(IN const STR_VEC &vtnames)
{
	if ( m_pvtfiles == NULL || m_pvtnames == &vtnames )
		return;

	INT nIndex = 0;
	STR_VEC vt_result_files;
	if ( m_pvtnames == NULL)
	{
		STR_VEC::const_iterator it_name = vtnames.begin();
		for ( ; it_name != vtnames.end(); it_name++ )
		{
			STR_VEC::iterator it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(); )
			{
				nIndex = it_file->find_last_of(_T('\\'));
				if (TString::npos != nIndex)
				{
					if (_tcsicmp(it_file->substr(nIndex + 1).c_str(), it_name->c_str()) == 0)
					{
						vt_result_files.push_back(*it_file);
						it_file = m_pvtfiles->erase(it_file);
						continue;
					}
				}
				it_file++;
			}
		}

		m_pvtfiles->clear();
		for ( STR_VEC::iterator it = vt_result_files.begin(); it != vt_result_files.end(); it++ )
		{
			m_pvtfiles->push_back(*it);
		}
	}
	else 
	{
		if ( m_pvtnames->size() != m_pvtfiles->size() )
			return;

		STR_VEC::iterator it_name;
		STR_VEC::iterator it_file;
		STR_VEC vt_result_names;
		for ( STR_VEC::const_iterator it = vtnames.begin(); it != vtnames.end(); it++ )
		{
			it_name = m_pvtnames->begin();
			it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(), it_name != m_pvtnames->end();)
			{
				if ( _tcsicmp(it_name->c_str(), it->c_str()) == 0 )
				{
					vt_result_files.push_back(*it_file);
					vt_result_names.push_back(*it_name);
					it_file = m_pvtfiles->erase(it_file);
					it_name = m_pvtnames->erase(it_name);
					continue;
				}

				it_file++;
				it_name++;
			}
		}

		m_pvtfiles->clear();
		m_pvtnames->clear();
		it_name = vt_result_names.begin();
		it_file = vt_result_files.begin();
		for ( ; it_file != vt_result_files.end(), it_name != vt_result_names.end(); it_file++, it_name++ )
		{
			m_pvtfiles->push_back(*it_file);
			m_pvtnames->push_back(*it_name);
		}
	}
}

void filehelpImpl::keepdowbyname(IN const STR_VEC &vtnames, IN STR_VEC &vtfiles)
{
	if ( vtnames.size() == 0 )
		return;

	INT nIndex = 0;
	STR_VEC vt_result_files;
	STR_VEC::const_iterator it_name = vtnames.begin();
	for ( ; it_name != vtnames.end(); it_name++ )
	{
		STR_VEC::iterator it_file = vtfiles.begin();
		for ( ; it_file != vtfiles.end(); )
		{
			nIndex = it_file->find_last_of(_T('\\'));
			if (TString::npos != nIndex)
			{
				if (_tcsicmp(it_file->substr(nIndex + 1).c_str(), it_name->c_str()) == 0)
				{
					vt_result_files.push_back(*it_file);
					it_file = vtfiles.erase(it_file);
					continue;
				}
			
			}
			it_file++;
		}
	}

	vtfiles.clear();
	for ( STR_VEC::iterator it = vt_result_files.begin(); it != vt_result_files.end(); it++ )
	{
		vtfiles.push_back(*it);
	}
}

void filehelpImpl::keepdowbyname(IN const STR_VEC &vtnames, IN const STR_VEC &vtfiles, OUT STR_VEC &vt_result)
{
	if ( vtnames.size() == 0 )
		return;

	INT nIndex = 0;
	STR_VEC::const_iterator it_name = vtnames.begin();
	for ( ; it_name != vtnames.end(); it_name++ )
	{
		STR_VEC::const_iterator it_file = vtfiles.begin();
		for ( ; it_file != vtfiles.end(); it_file++)
		{
			nIndex = it_file->find_last_of(_T('\\'));
			if (TString::npos != nIndex)
			{
				if (_tcsicmp(it_file->substr(nIndex + 1).c_str(), it_name->c_str()) == 0)
				{
					vt_result.push_back(*it_file);
				}
			}
		}
	}
}

void filehelpImpl::keepdowbyname_noext(IN const STR_VEC &vtnames)
{
	if ( m_pvtfiles == NULL || m_pvtnames == &vtnames )
		return;

	INT nIndex = 0;
	STR_VEC vt_result_files;
	if ( m_pvtnames == NULL)
	{
		INT nIndex2 = 0;
		STR_VEC::const_iterator it_name = vtnames.begin();
		for ( ; it_name != vtnames.end(); it_name++ )
		{
			STR_VEC::iterator it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(); )
			{
				nIndex = it_file->find_last_of(_T('\\'));
				nIndex2 = it_file->find_last_of(_T('.'));
				if (TString::npos != nIndex && TString::npos != nIndex2 && nIndex > nIndex2 )
				{
					if ( _tcsicmp(it_file->substr(nIndex+1,nIndex2 - nIndex - 1).c_str(), it_name->c_str()) == 0)
					{
						vt_result_files.push_back(*it_file);
						it_file = m_pvtfiles->erase(it_file);
						continue;
					}
				}
				it_file++;
			}
		}

		m_pvtfiles->clear();
		for ( STR_VEC::iterator it = vt_result_files.begin(); it != vt_result_files.end(); it++ )
		{
			m_pvtfiles->push_back(*it);
		}
	}
	else 
	{
		if ( m_pvtnames->size() != m_pvtfiles->size() )
			return;

		STR_VEC::iterator it_name;
		STR_VEC::iterator it_file;
		STR_VEC vt_result_names;
		for ( STR_VEC::const_iterator it = vtnames.begin(); it != vtnames.end(); it++ )
		{
			it_name = m_pvtnames->begin();
			it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(), it_name != m_pvtnames->end();)
			{
				if ( TString::npos != ( nIndex = it_name->find_last_of(_T('.'))))
				{
					if ( _tcsicmp(it_name->substr(0, nIndex).c_str(), it->c_str()) == 0 )
					{
						vt_result_files.push_back(*it_file);
						vt_result_names.push_back(*it_name);
						it_file = m_pvtfiles->erase(it_file);
						it_name = m_pvtnames->erase(it_name);
						continue;
					}
				}
				it_file++;
				it_name++;
			}
		}

		m_pvtfiles->clear();
		m_pvtnames->clear();
		it_name = vt_result_names.begin();
		it_file = vt_result_files.begin();
		for ( ; it_file != vt_result_files.end(), it_name != vt_result_names.end(); it_file++, it_name++ )
		{
			m_pvtfiles->push_back(*it_file);
			m_pvtnames->push_back(*it_name);
		}
	}
}

void filehelpImpl::keepdowbyname_noext(IN const STR_VEC &vtnames, IN STR_VEC &vtfiles)
{
	if ( vtnames.size() == 0 )
		return;

	INT nIndex = 0;
	TString strName;
	STR_VEC vt_result_files;
	STR_VEC::const_iterator it_name = vtnames.begin();
	for ( ; it_name != vtnames.end(); it_name++ )
	{
		STR_VEC::iterator it_file = vtfiles.begin();
		for ( ; it_file != vtfiles.end(); )
		{
			nIndex = it_file->find_last_of(_T('\\'));
			if (TString::npos != nIndex)
			{
				strName = it_file->substr(nIndex+1);
				if ( TString::npos != ( nIndex = strName.find_last_of(_T('.'))))
				{
					if ( _tcsicmp(strName.substr(0,nIndex).c_str(), it_name->c_str()) == 0)
					{
						vt_result_files.push_back(*it_file);
						it_file = vtfiles.erase(it_file);
						continue;
					}
				}
			}
			it_file++;
		}
	}

	vtfiles.clear();
	for ( STR_VEC::iterator it = vt_result_files.begin(); it != vt_result_files.end(); it++ )
	{
		vtfiles.push_back(*it);
	}
}

void filehelpImpl::keepdowbyname_noext(IN const STR_VEC &vtnames, IN const STR_VEC &vtfiles, OUT STR_VEC &vt_result)
{
	if ( vtnames.size() == 0 )
		return;

	INT nIndex = 0;
	TString strName;
	STR_VEC::const_iterator it_name = vtnames.begin();
	for ( ; it_name != vtnames.end(); it_name++ )
	{
		STR_VEC::const_iterator it_file = vtfiles.begin();
		for ( ; it_file != vtfiles.end(); it_file++)
		{
			nIndex = it_file->find_last_of(_T('\\'));
			if (TString::npos != nIndex)
			{
				strName = it_file->substr(nIndex+1);
				if ( TString::npos != ( nIndex = strName.find_last_of(_T('.'))))
				{
					if ( _tcsicmp(strName.substr(0,nIndex).c_str(), it_name->c_str()) == 0)
					{
						vt_result.push_back(*it_file);
					}
				}
			}
		}
	}
}

void filehelpImpl::subgroupExt(IN const TString &ext1, IN STR_VEC &vt1, IN const TString &ext2, IN STR_VEC &vt2)
{
	// 		if (ext1.find(_T("*.*")) != TString::npos ) return;
	// 		if ( !iscorrectext(ext1) || vt1.size() == 0 ) return;
	// 
	// 		// 获取复制扩展名;
	// 		int nIndex = 0;
	// 		TString strtmp;
	// 		TString strExt1(ext1);
	// 		strExt1.append(_T("|"));
	// 		STR_VEC vtExt1;
	// 
	// 		// 将所有扩展名解析到数组里;
	// 		do 
	// 		{
	// 			nIndex = strExt1.find(_T('|'));
	// 			if ( nIndex != TString::npos )
	// 			{
	// 				strtmp = strExt1.substr(0,nIndex);
	// 				strExt1 = strExt1.substr(nIndex+1);
	// 
	// 				if(strtmp.compare(_T("*.*")))
	// 					vtExt1.push_back(strtmp);
	// 			}
	// 		} while ( strExt1.find(_T('|')) != TString::npos );
}

/************************************************************************/
/*  函数：filterbyname[2/21/2017 Jeff];
/*  描述：根据有后缀的文件名(如:001.jpg)来过虑文件;;
/*  参数：;
/*  	[IN] vtfiles：要被过滤的文件路径数组;
/*  	[IN] vtnames：要过滤的文件名，每个文件名都有后缀，如：001.jpg;
/*  返回：void;
/*  注意：;
/*  示例：;
/*
/*  修改：;
/*  日期：;
/*  内容：;
/************************************************************************/
void filehelpImpl::filterbyname(IN STR_VEC &vtfiles, IN const STR_VEC &vtnames)
{// 再按名称过滤;
	INT nIndex = 0;
	TString strName;
	for ( STR_VEC::const_iterator it_name = vtnames.begin(); it_name != vtnames.end(); it_name++ )
	{
		for ( STR_VEC::iterator it_file = vtfiles.begin(); it_file != vtfiles.end(); )
		{
			nIndex = it_file->find_last_of(_T('\\'));
			if (TString::npos != nIndex)
			{
				strName = it_file->substr(nIndex+1);
				if ( _tcsicmp(strName.c_str(), it_name->c_str()) == 0 )
				{
					it_file = vtfiles.erase(it_file);
					continue;
				}				
			}
			it_file++;
		}
	}
}

void filehelpImpl::filterbyname(IN const STR_VEC &vtnames)
{
	if ( m_pvtfiles == NULL || m_pvtnames == &vtnames )
		return;

	INT nIndex = 0;
	TString strName;
	if ( m_pvtnames == NULL)
	{
		STR_VEC::const_iterator it_name = vtnames.begin();
		for ( ; it_name != vtnames.end(); it_name++ )
		{
			STR_VEC::iterator it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(); )
			{
				nIndex = it_file->find_last_of(_T('\\'));
				if (TString::npos != nIndex)
				{
					strName = it_file->substr(nIndex+1);
					if ( _tcsicmp(strName.c_str(), it_name->c_str()) == 0)
					{
						it_file = m_pvtfiles->erase(it_file);
						continue;
					}
				}
				it_file++;
			}
		}
	}
	else 
	{
		if ( m_pvtnames->size() != m_pvtfiles->size() )
			return;
		for ( STR_VEC::const_iterator it = vtnames.begin(); it != vtnames.end(); it++ )
		{
			STR_VEC::iterator it_name = m_pvtnames->begin();
			STR_VEC::iterator it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(), it_name != m_pvtnames->end();)
			{
				if ( _tcsicmp(it_name->c_str(), it->c_str()) == 0 )
				{
					it_file = m_pvtfiles->erase(it_file);
					it_name = m_pvtnames->erase(it_name);
					continue;
				}

				it_file++;
				it_name++;
			}
		}
	}
}

// 根据无后缀的文件名(如:001)来过虑文件;
void filehelpImpl::filterbyname_noext(IN STR_VEC &vtfiles, IN const STR_VEC &vtnames)
{// 再按名称过滤;
	INT nIndex = 0;
	INT nIndex2 = 0;
	TString strName;
	for ( STR_VEC::iterator it_file = vtfiles.begin(); it_file != vtfiles.end(); )
	{
		nIndex = it_file->find_last_of(_T('\\'));
		nIndex2 = it_file->find_last_of(_T('.'));
		for ( STR_VEC::const_iterator it_name = vtnames.begin(); it_name != vtnames.end(); it_name++ )
		{
			if (TString::npos != nIndex && TString::npos != nIndex2 && nIndex2 > nIndex)
			{
				if ( _tcsicmp(it_file->substr(nIndex+1,nIndex2-nIndex-1).c_str(), it_name->c_str()) == 0)
				{
					it_file = vtfiles.erase(it_file);
					continue;
				}
			}
			it_file++;
		}
	}
}

void filehelpImpl::filterbyname_noext(IN const STR_VEC &vtnames)
{
	if ( m_pvtfiles == NULL || m_pvtnames == &vtnames )
		return;

	INT nIndex = 0;
	TString strName;
	if ( m_pvtnames == NULL )
	{
		INT nIndex2 = 0;
		STR_VEC::iterator it_file = m_pvtfiles->begin();
		for ( ; it_file != m_pvtfiles->end(); )
		{
			nIndex = it_file->find_last_of(_T('\\'));
			nIndex2 = it_file->find_last_of(_T('.'));
			for ( STR_VEC::const_iterator it_name = vtnames.begin(); it_name != vtnames.end(); it_name++ )
			{
				if (TString::npos != nIndex && TString::npos != nIndex2 && nIndex2 > nIndex)
				{
					if ( _tcsicmp(it_file->substr(nIndex+1,nIndex2-nIndex-1).c_str(), it_name->c_str()) == 0 )
					{
						it_file = m_pvtfiles->erase(it_file);
						continue;
					}
				}
				it_file++;
			}
		}
	}
	else
	{
		if (m_pvtnames->size() != m_pvtfiles->size())
			return;

		for ( STR_VEC::const_iterator it = vtnames.begin(); it != vtnames.end(); it++ )
		{
			STR_VEC::iterator it_name = m_pvtnames->begin();
			STR_VEC::iterator it_file = m_pvtfiles->begin();
			for ( ; it_file != m_pvtfiles->end(), it_name != m_pvtnames->end();)
			{
				if ( TString::npos != (nIndex = it_name->find_last_of(_T('.'))) )
				{
					if ( _tcsicmp(it_name->substr(0, nIndex).c_str(), it->c_str()) == 0 )
					{
						it_file = m_pvtfiles->erase(it_file);
						it_name = m_pvtnames->erase(it_name);
						continue;
					}

					it_file++;
					it_name++;
				}
			}
		}
	}

}
//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::getall(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN OUT STR_VEC *pvtfiles, IN OUT STR_VEC *pvtnames, IN OUT STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (!pvtfiles || !pvtnames || !pvtfolders ) return FALSE;

	m_pvtfiles = pvtfiles;
	m_pvtnames = pvtnames;
	m_pvtfolders = pvtfolders;

#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif

	findall(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
		keepdownbyext(lpfindext, *pvtnames);
	}

	return TRUE;
}

BOOL filehelpImpl::getsubfolder(IN LPCTSTR lpfolder, IN STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfolders == NULL) return FALSE;
	m_pvtfolders = pvtfolders;

	findsubfolder(lpfolder);

	return TRUE;
}

BOOL filehelpImpl::getallsubfolder(IN LPCTSTR lpfolder, IN STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfolders == NULL) return FALSE;
	m_pvtfolders = pvtfolders;

	findallsubfolder(lpfolder);

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::getfiles_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;

#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfiles_findin_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

BOOL filehelpImpl::getfiles_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfiles_findout_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

BOOL filehelpImpl::getfiles_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;
	m_pvtfolders = pvtfolders;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfiles_within_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::getfiles_bynames_findin_subfolder( IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC &vtnames, IN STR_VEC *pvtfiles )
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;

#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfiles_findin_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	// 再按名称过滤;
	filterbyname(*pvtfiles, vtnames);

	return TRUE;
}

BOOL filehelpImpl::getfiles_bynames_findout_subfolder( IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC &vtnames, IN OUT STR_VEC *pvtfiles )
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;

#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif

	findfiles_findout_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	// 再按名称过滤;
	filterbyname(*pvtfiles, vtnames);

	return TRUE;
}

BOOL filehelpImpl::getfiles_bynames_within_subfolder( IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC &vtnames, IN STR_VEC *pvtfiles, IN STR_VEC *pvtfolders )
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtfiles == NULL) return FALSE;
	m_pvtfiles = pvtfiles;
	m_pvtfolders = pvtfolders;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif

	findfiles_within_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtfiles);
	}

	// 再按名称过滤;
	filterbyname(*pvtfiles, vtnames);

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::getnames_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames == NULL) return FALSE;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	m_pvtnames = pvtnames;
	findnames_findin_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *m_pvtnames);
	}

	return TRUE;
}

BOOL filehelpImpl::getnames_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames == NULL) return FALSE;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	m_pvtnames = pvtnames;
	findnames_findout_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *m_pvtnames);
	}

	return TRUE;
}

BOOL filehelpImpl::getnames_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtnames, IN STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames == NULL || pvtfolders == NULL ) 
		return FALSE;

	m_pvtfolders = pvtfolders;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	m_pvtnames = pvtnames;
	m_pvtfolders = pvtfolders;
	findnames_within_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *m_pvtnames);
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::getfilesnames_findin_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames || !pvtnames) return FALSE;
	m_pvtfiles = pvtfiles;
	m_pvtnames = pvtnames;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfilesnames_findin_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtnames);
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

BOOL filehelpImpl::getfilesnames_findout_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames || !pvtnames) return FALSE;
	m_pvtfiles = pvtfiles;
	m_pvtnames = pvtnames;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfilesnames_findout_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtnames);
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

BOOL filehelpImpl::getfilesnames_within_subfolder(IN LPCTSTR lpfolder, IN LPCTSTR lpfindext, IN STR_VEC *pvtfiles, IN STR_VEC *pvtnames, IN STR_VEC *pvtfolders)
{
	// 路径不存在;
	if (!PathFileExists(lpfolder))
		return FALSE;

	// 指针空;
	if (pvtnames || !pvtnames) return FALSE;
	m_pvtfiles = pvtfiles;
	m_pvtnames = pvtnames;
	m_pvtfolders = pvtfolders;
#if	USE_IMGEXT
	// 判断扩展名有效性;
	if (!iscorrectext(lpfindext)) return FALSE;
#endif
	findfilesnames_within_subfolder(lpfolder);
	if ( _tcsstr(lpfindext, _T("*.*")) == NULL )
	{// 需要保留指定后缀;
		keepdownbyext(lpfindext, *pvtnames);
		keepdownbyext(lpfindext, *pvtfiles);
	}

	return TRUE;
}

#if 1
//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::comparename_2file(IN LPCTSTR file1, IN LPCTSTR file2)
{
	if (!file1 || !file2) return FALSE;

	if (!PathFileExists(file1) || !PathFileExists(file2))
		return FALSE;

	TString strfile1 = file1;
	TString strfile2 = file2;

	strfile1 = getfilename(strfile1);
	strfile2 = getfilename(strfile2);

	return !strfile1.compare(strfile2);
}

BOOL filehelpImpl::comparename_findin_names(IN LPCTSTR name)
{
	if (!m_pvtnames || !name) return FALSE;
	for (STR_VEC::iterator it = m_pvtnames->begin(); it != m_pvtnames->end(); it++)
	{
		if ( _tcsicmp(name, it->c_str()) == 0)
		{
			// 打印日志,或提示用户同名文件路径在哪;
			return TRUE;
		}
	}

	return FALSE;
}

BOOL filehelpImpl::comparename_findin_names(IN LPCTSTR name, IN STR_VEC &vtnames)
{
	if (!name) return FALSE;
	for (STR_VEC::iterator it = vtnames.begin(); it != vtnames.end(); it++)
	{
		if ( _tcsicmp(name, it->c_str()) == 0)
		{
			// 打印日志,或提示用户同名文件路径在哪;
			return TRUE;
		}
	}

	return FALSE;
}

BOOL filehelpImpl::comparename_findin_files(IN LPCTSTR name, IN STR_VEC &vtfiles)
{
	if (!name) return FALSE;

	int nIndex = 0;
	for (STR_VEC::iterator it = vtfiles.begin(); it != vtfiles.end(); it++)
	{
		if ((nIndex = it->find_last_of(_T('\\'))) != TString::npos)
		{
			if (_tcsicmp(name, it->substr(nIndex+1).c_str()) == 0)
			{
				// 打印日志,或提示用户同名文件路径在哪;
				return TRUE;
			}
		}
	}

	return FALSE;
}

BOOL filehelpImpl::comparename_findin_folder(IN LPCTSTR name, IN LPCTSTR folder, IN LPCTSTR findext, IN BOOL bsubfolder)
{
#if	USE_IMGEXT
	if (!name || !iscorrectext(findext)) return FALSE;
#else
	if ( !name ) return FALSE;
#endif
	STR_VEC vtnames;
	if (bsubfolder)
	{
		if (!getnames_findin_subfolder(folder, findext, &vtnames))
			return FALSE;
	}
	else
	{
		if (!getnames_findout_subfolder(folder, findext, &vtnames))
			return FALSE;
	}

	return comparename_findin_names(name, vtnames);
}

INT filehelpImpl::comparenames_findin_names(IN STR_VEC vtSrcnames, IN STR_VEC vtDesnames, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	if (vtSrcnames.size() == 0 || vtDesnames.size() == 0) return 0;

	INT nCount = 0;
	for (STR_VEC::iterator it1 = vtSrcnames.begin(); it1 != vtSrcnames.end(); it1++)
	{
		for (STR_VEC::iterator it2 = vtDesnames.begin(); it2 != vtDesnames.end(); it2++)
		{
			if (_tcsicmp(it1->c_str(), it2->c_str()) == 0)
			{
				nCount++;
				if (pvtIsonym)
					pvtIsonym->push_back(*it1);

				break;
			}
		}
	}

	return nCount;
}

INT filehelpImpl::comparenames_findin_files(IN STR_VEC vtnames, IN STR_VEC vtfiles, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	if (vtnames.size() == 0 || vtfiles.size() == 0) return 0;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return 0;
#endif
	INT nCount = 0;
	for (STR_VEC::iterator it = vtnames.begin(); it != vtnames.end(); it++)
	{
		if (comparename_findin_files(it->c_str(), vtfiles))
		{
			if (pvtIsonym)
				pvtIsonym->push_back(*it);
			nCount++;
		}
	}

	return nCount;
}

INT filehelpImpl::comparenames_findin_folder(IN STR_VEC vtnames, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	if (vtnames.size() == 0 || !PathFileExists(folder)) return 0;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return 0;
#endif
	STR_VEC vtnames2;
	if (bsubfolder)
	{
		getnames_findin_subfolder(folder, findext, &vtnames2);
	}
	else
	{
		getnames_findout_subfolder(folder, findext, &vtnames2);
	}

	return comparenames_findin_names(vtnames, vtnames2, pvtIsonym);
}

//////////////////////////////////////////////////////////////////////////
BOOL filehelpImpl::comparefile_findin_names(IN LPCTSTR file, IN STR_VEC& vtnames)
{
	if (vtnames.size() == 0) return FALSE;
	if (!file || !PathFileExists(file)) return FALSE;

	TString strfile = file;
	TString name = getfilename(strfile);

	return comparename_findin_names(name.c_str(), vtnames);
}

BOOL filehelpImpl::comparefile_findin_files(IN LPCTSTR file, IN STR_VEC& vtfiles, IN LPCTSTR findext)
{
	if (!file || !PathFileExists(file)) return FALSE;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return FALSE;
#endif
	TString strfile = file;
	TString name = getfilename(strfile);

	// 过滤掉不需要的扩展名;
	keepdownbyext(findext, vtfiles);

	return comparename_findin_files(name.c_str(), vtfiles);
}

BOOL filehelpImpl::comparefile_findin_folder(IN LPCTSTR file, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext)
{
	if (!file || !PathFileExists(file) || !folder || !PathFileExists(folder)) return FALSE;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return FALSE;
#endif
	TString strfile = file;
	TString name = getfilename(strfile);

	STR_VEC vtnames;
	if (bsubfolder)
	{
		getnames_findin_subfolder(folder, findext, &vtnames);
	}
	else
	{
		getnames_findout_subfolder(folder, findext, &vtnames);
	}

	return comparename_findin_names(name.c_str(), vtnames);
}

INT filehelpImpl::comparefiles_findin_files(IN STR_VEC &vtfiles1, IN STR_VEC &vtfiles2, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	if (vtfiles1.size() == 0 || vtfiles2.size() == 0) return 0;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return 0;
#endif
	// 过滤掉不需要的扩展名;
	keepdownbyext(findext, vtfiles1);

	// 过滤掉不需要的扩展名;
	keepdownbyext(findext, vtfiles2);

	INT nCount = 0;
	TString name;
	for (STR_VEC::iterator it = vtfiles1.begin(); it != vtfiles1.end(); it++)
	{
		name = getfilename(*it);
		if (comparename_findin_files(name.c_str(), vtfiles2))
		{
			if (pvtIsonym)
				pvtIsonym->push_back(name);
			nCount++;
		}
	}

	return nCount;
}

INT filehelpImpl::comparefiles_findin_folder(IN STR_VEC &vtfiles, IN LPCTSTR folder, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	if (vtfiles.size() == 0 || !folder || !PathFileExists(folder)) return 0;
#if	USE_IMGEXT
	if (!iscorrectext(findext)) return 0;
#endif
	STR_VEC vtnames;
	if (bsubfolder)
	{
		getnames_findin_subfolder(folder, findext, &vtnames);
	}
	else
	{
		getnames_findout_subfolder(folder, findext, &vtnames);
	}

	return comparenames_findin_files(vtnames, vtfiles, findext, pvtIsonym);
}

INT filehelpImpl::comparefolderself(IN LPCTSTR folder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	OutputDebugString(_T("\n"));
#if	USE_IMGEXT
	if (!folder || !PathFileExists(folder) || !iscorrectext(findext)) return 0;
#else
	if (!folder || !PathFileExists(folder) ) return 0;
#endif

	STR_VEC vtsubfolders;
	vtsubfolders.push_back(folder);	// 包含当前目录;
	getsubfolder(folder, &vtsubfolders);

	int n = vtsubfolders.size();
	STR_VEC *pvtfiles = new STR_VEC[n];

	for (STR_VEC::iterator it = vtsubfolders.begin(); it != vtsubfolders.end(); it++)
	{
		getfiles_findin_subfolder(it->c_str(), findext, &pvtfiles[--n]);
	}

	STR_VEC vtnames;
	n = vtsubfolders.size();
	TString name;
	for (int i = 0; i < n; i++)
	{
		for (STR_VEC::iterator it = pvtfiles[i].begin(); it != pvtfiles[i].end(); it++)
		{
			name = getfilename(*it);
			if (comparename_findin_names(name.c_str(), vtnames))
			{
				// 打印日志,提示客户重名的相片;
				OutputDebugString(it->c_str());
				OutputDebugString(_T("\n"));
				pvtIsonym->push_back(it->c_str());
			}
			else
			{
				vtnames.push_back(name);
			}
		}
	}

	if (pvtfiles)
		delete []pvtfiles;
	pvtfiles = NULL;

	return 0;
}

INT filehelpImpl::comparefolder_findin_folder(IN LPCTSTR folder1, IN LPCTSTR folder2, IN BOOL bsubfolder, IN LPCTSTR findext, OUT STR_VEC *pvtIsonym /* = NULL */)
{
	return 0;
}

INT filehelpImpl::copyfolder(IN LPCTSTR from, IN LPCTSTR to)
{
	if (to == NULL || from == NULL)
		return -1;

	// 创建目录;
	int nleft = 0;
	int nIndex = -1;
	TString strTo = to;
	if (strTo.at(strTo.size()-1) != _T('\\'))
		strTo.append(_T("\\"));
	// 共享路径和硬盘盘符;
	if (_tcscmp(strTo.substr(0, 2).c_str(), _T("\\\\")) == 0)
		nleft = 2;
	else if (strTo.at(2) == _T('\\'))
		nleft = 3;

	do 
	{
		nIndex = strTo.substr(nleft, -1).find_first_of(_T("\\"));
		if (nIndex != TString::npos)
		{
			if (_tmkdir(strTo.substr(0, nIndex + nleft).c_str()) == -1 && errno != EEXIST)				
				break;
			nleft += nIndex + 1;
		}		
	} while (nIndex!=-1);
	

	return 0;
}
#endif


