c++ 常用函数

#pragma once
#include 
#include 
#include 
#include 
#include 
class CUtils
{
public:
	CUtils();
	~CUtils();
public:
	static std::wstring Ansi_To_Unicode(const std::string& str);
	static std::string Unicode_To_Ansi(const std::wstring& wstr);
	static std::wstring Utf8_To_Unicode(const std::string& str);
	static std::string Unicode_To_Utf8(const std::wstring& wstr);
	static std::string Utf8_To_Ansi(const std::string& str);
	static std::string Ansi_To_Utf8(const std::string& str);
	
	static std::wstring getAppFullpath();//获取当前进程全路径
	static std::wstring getFileExtension(const std::wstring & strPath);//获取文件的扩展名
	static std::wstring removeExtension(const std::wstring & strPath);//去除文件的扩展名
	static std::wstring getFileName(const std::wstring & strPath);//获取文件名,不包含扩展名
	static std::wstring getFileFolder(const std::wstring & strPath);//获取文件所在的文件夹路径
	static void copyFile(const char* src, const char* dst);//复制文件
	static int createDirectory(const wchar_t* dir);//创建目录,如果存在,则不会覆盖原来的文件
	static bool isFileExist(const wchar_t* strPath);//判断文件,目录是否存在
	static bool isFileExistA(const char* strPath);//判断文件,目录是否存在
	static void getFiles(std::string path, std::list& files);//获取指定路径下的所有文件
	static bool deleteFiles(std::string path);//删除指定目录下的所有文件,不删除该目录
	static bool deleteFilesEx(std::string path);//删除指定目录下的所有文件,并且删除该目录
	static bool moveFile(std::string srcPath, std::string dstPath);//移动指定文件到指定路径
};

#include "Utils.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma warning(disable: 4996)
CUtils::CUtils()
{
}

CUtils::~CUtils()
{
}

std::wstring CUtils::Ansi_To_Unicode(const std::string& str) 
{
	int unilen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pWch = new wchar_t[unilen + 1];
	memset(pWch, 0, sizeof(wchar_t)*(unilen + 1));
	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pWch, unilen);
	std::wstring rt;
	rt = (wchar_t*)pWch;
	delete[]pWch;
	return rt;
}

std::string CUtils::Unicode_To_Ansi(const std::wstring& wstr) 
{
	int newlen = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
	char* pCh = new char[newlen + 1];
	memset(pCh, 0, (newlen + 1) * sizeof(char));
	::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.length(), pCh, newlen, NULL, NULL);
	std::string str = pCh;
	delete[]pCh;
	return str;
}

std::wstring CUtils::Utf8_To_Unicode(const std::string& str) 
{
	int newlen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	wchar_t* wCh = new wchar_t[newlen + 1];
	memset(wCh, 0, (newlen + 1) * sizeof(wchar_t));
	::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), wCh, newlen);
	std::wstring wstr = wCh;
	delete[]wCh;
	return wstr;
}

std::string CUtils::Unicode_To_Utf8(const std::wstring& wstr) 
{
	int newlen = ::WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
	char* ch = new char[newlen + 1];
	memset(ch, 0, (newlen + 1) * sizeof(char));
	::WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), ch, newlen, NULL, NULL);
	std::string str = ch;
	delete[]ch;
	return str;
}

std::string CUtils::Utf8_To_Ansi(const std::string& str) 
{
	int newlen1 = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pWch = new wchar_t[newlen1 + 1];
	memset(pWch, 0, (newlen1 + 1) * sizeof(wchar_t));
	::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pWch, newlen1);
	int newlen2 = ::WideCharToMultiByte(CP_ACP, 0, pWch, -1, NULL, 0, NULL, NULL);
	char* pch = new char[newlen2 + 1];
	memset(pch, 0, (newlen2 + 1) * sizeof(char));
	::WideCharToMultiByte(CP_ACP, 0, pWch, newlen1, pch, newlen2, NULL, NULL);
	std::string strrt = pch;
	delete[]pch;
	delete[]pWch;
	return strrt;
}

std::string CUtils::Ansi_To_Utf8(const std::string& str) 
{
	int newlen1 = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pWch = new wchar_t[newlen1 + 1];
	memset(pWch, 0, (newlen1 + 1) * sizeof(wchar_t));
	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pWch, newlen1);
	int newlen2 = ::WideCharToMultiByte(CP_UTF8, 0, pWch, -1, NULL, 0, NULL, NULL);
	char* pch = new char[newlen2 + 1];
	memset(pch, 0, (newlen2 + 1) * sizeof(char));
	::WideCharToMultiByte(CP_UTF8, 0, pWch, newlen1, pch, newlen2, NULL, NULL);
	std::string rt = pch;
	delete[]pch;
	delete[]pWch;
	return rt;
}

std::wstring CUtils::getAppFullpath() 
{
	std::wstring ret = L"";
	wchar_t path[MAX_PATH] = { 0 };
	GetModuleFileNameW(NULL, path, MAX_PATH);
	ret = std::wstring(path);
	return ret;
}

std::wstring CUtils::getFileExtension(const std::wstring & strPath) 
{
	std::wstring ret = L"";
	int length = strPath.length();
	int index = strPath.rfind(L'.');
	if (index == -1)
		return L"";
	ret = strPath.substr(index + 1, length - index - 1);
	return ret;
}

//去除文件的扩展名
std::wstring CUtils::removeExtension(const std::wstring & strPath)
{
	std::wstring ret = L"";
	int length = strPath.length();
	int index = strPath.rfind(L'.');
	if (index == -1)
		return L"";
	ret = strPath.substr(0, index);
	return ret;
}

std::wstring CUtils::getFileName(const std::wstring & strPath) 
{
	std::wstring ret = L"";
	int length = strPath.length();
	int index1 = strPath.rfind(L'\\');
	int index2 = strPath.rfind(L'.');
	if (index1 == -1 || index2 == -1 || index1 > index2)
		return ret;
	ret = strPath.substr(index1 + 1, index2 - index1 - 1);
	return ret;
}

std::wstring CUtils::getFileFolder(const std::wstring & strPath) 
{
	std::wstring ret;
	int length = strPath.length();
	int index = strPath.rfind(L'\\');
	if (index == -1)
		return L"";
	ret = strPath.substr(0, index);
	return ret;
}

void CUtils::copyFile(const char* src, const char* dst)
{
	using namespace std;
	ifstream in(src, ios::binary);
	ofstream out(dst, ios::binary);
	if (!in.is_open())
	{
		exit(EXIT_FAILURE);
	}
	if (!out.is_open())
	{
		exit(EXIT_FAILURE);
	}
	if (src == dst)
	{
		exit(EXIT_FAILURE);
	}
	char buf[2048];
	long long totalBytes = 0;
	while (in)
	{
		in.read(buf, 2048);
		out.write(buf, in.gcount());
		totalBytes += in.gcount();
	}
	in.close();
	out.close();
}

int CUtils::createDirectory(const wchar_t* dir)
{
	return SHCreateDirectoryExW(NULL, dir, NULL);
}

bool CUtils::isFileExist(const wchar_t* strPath)
{
	if (_waccess(strPath, 0) == -1)
		return false;
	else
		return true;
}

bool CUtils::isFileExistA(const char* strPath)
{
	if (_access(strPath, 0) == -1)
		return false;
	else
		return true;
}

void CUtils::getFiles(std::string path, std::list& files)
{
	//文件句柄  
	long   hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之  
			//如果不是,加入列表  
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

//not include path
bool CUtils::deleteFiles(std::string path)
{
	//文件句柄  
	long   hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之  
			//如果不是,加入列表  
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					deleteFiles(p.assign(path).append("\\").append(fileinfo.name));
					RemoveDirectoryA(p.assign(path).append("\\").append(fileinfo.name).c_str());
				}
			}
			else
			{
				DeleteFileA(p.assign(path).append("\\").append(fileinfo.name).c_str());
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
	//bool ret = RemoveDirectoryA(path.c_str());//如果需要删除当前目录,则去除该行注释
	return true;
}

//删除指定目录下的所有文件,并且删除该目录
bool CUtils::deleteFilesEx(std::string path)
{
	//文件句柄  
	long   hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//如果是目录,迭代之  
			//如果不是,加入列表  
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					deleteFiles(p.assign(path).append("\\").append(fileinfo.name));
					RemoveDirectoryA(p.assign(path).append("\\").append(fileinfo.name).c_str());
				}
			}
			else
			{
				DeleteFileA(p.assign(path).append("\\").append(fileinfo.name).c_str());
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
	RemoveDirectoryA(path.c_str());//如果需要删除当前目录,则去除该行注释
	return true;
}

bool CUtils::moveFile(std::string srcPath, std::string dstPath)
{
	if (srcPath.empty() || dstPath.empty() || _stricmp(srcPath.c_str(), dstPath.c_str()) == 0)
		return false;
	if (isFileExistA(dstPath.c_str()))
		DeleteFileA(dstPath.c_str());
	return MoveFileA(srcPath.c_str(), dstPath.c_str());
}

 

你可能感兴趣的:(C++,VC)