静态库封装之ComDir类

ComDir.h


/*
@author:EricsT
@data:20241031
@version:V1.0
@history:
	@author @data @version @content
	EricsT 20241031 V1.0 新增ComDir类[判断存在性以及创建目录]
	EricsT 20241101 V1.1 修改isDirExists函数,新增getFilesFromDir函数
*/

#pragma once

#include <string>
#include <fstream>
#include <vector>
using namespace std;

class ComDir
{
public:

	/*
	func:判断文件夹是否存在
	@strDirPath:文件夹路径
	@csDirPath:文件夹路径
	@pchDirPath:文件夹路径
	ret:存在true,不存在false
	*/
	static bool isDirExists(string strDirPath);
	static bool isDirExists(CString csDirPath);
	static bool isDirExists(PCHAR pchDirPath);

	/*
	func:创建文件夹
	@strDirPath:文件夹路径
	@csDirPath:文件夹路径
	@pchDirPath:文件夹路径
	ret:创建成功true,创建失败false
	*/
	static bool creatDir(string strDirPath);
	static bool creatDir(CString csDirPath);
	static bool creatDir(PCHAR pchDirPath);

	/*
	func:获取文件夹[包括子文件夹]内的文件
	@strDirPath/csDirPath/pchDirPath:文件夹路径
	@strExtension/csExtension/pchExtension:扩展名
	ret:vector<string>文件容器
	*/
	static vector<string> getFilesFromDir(string strDirPath, string strExtension = "*");
	static vector<string> getFilesFromDir(CString csDirPath, CString csExtension = CString("*"));
	static vector<string> getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension = "*");

};

ComDir.cpp


/*
@author:EricsT
@data:20241031
@version:V1.1
*/

#include "stdafx.h"
#include "ComDir.h"
#include <io.h>
#include <direct.h>

bool ComDir::isDirExists(string strDirPath)
{
	struct _finddata_t fileInfo;

	if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))//取文件信息,不存在则为-1
		return false;

	if (fileInfo.attrib & _A_SUBDIR)
		return true;

	return false;
}

 bool ComDir::isDirExists(CString csDirPath)
{
	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);

	if (pchSize == wstring::npos)
	{
		delete pch;
		return "";
	}

	string strDirPath(pch);

	delete pch;

	struct _finddata_t fileInfo;

	if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))
		return false;

	if (fileInfo.attrib & _A_SUBDIR)
		return true;

	return false;
 }

 bool ComDir::isDirExists(PCHAR pchDirPath)
 {
	 struct _finddata_t fileInfo;

	 if (-1 == _findfirst(pchDirPath, &fileInfo))//取文件信息,不存在则为-1
		 return false;

	 if (fileInfo.attrib & _A_SUBDIR)
		 return true;

	 return false;
 }

bool ComDir::creatDir(string strDirPath)
{
	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);//取系统盘

	strCreate = strCreate.substr(3);//除去系统盘之后的路径

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);//按层级取目录

		if (-1 == _access(strCoplete.c_str(), 0)) //判断该目录是否存在
		{
			if (0 != _mkdir(strCoplete.c_str()))//不存在就创建目录
				return false;
		}

		if (strCreate.npos == iPos)//最后一级目录
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

bool ComDir::creatDir(CString csDirPath)
{
	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);

	if (pchSize == wstring::npos)
	{
		delete pch;
		return "";
	}

	string strDirPath(pch);

	delete pch;

	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);

	strCreate = strCreate.substr(3);

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);

		if (-1 == _access(strCoplete.c_str(), 0))
		{
			if (0 != _mkdir(strCoplete.c_str()))
				return false;
		}

		if (strCreate.npos == iPos)
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

bool ComDir::creatDir(PCHAR pchDirPath)
{
	string strDirPath(pchDirPath);
	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);

	strCreate = strCreate.substr(3);

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);

		if (-1 == _access(strCoplete.c_str(), 0))
		{
			if (0 != _mkdir(strCoplete.c_str()))
				return false;
		}

		if (strCreate.npos == iPos)
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

std::vector<std::string> ComDir::getFilesFromDir(string strDirPath, string strExtension /*= "*"*/)
{
	vector<string> strRetVec;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))//判断路径是否存在
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)//判断是否是目录
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;
			
			vector<string> vec = getFilesFromDir(strDirPath + "\\" + fileInfo.name, strExtension);//递归获取子目录文件
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());//将子容器合并到母容器内
			continue;
		}

		if ("*" == strExtension)//无指定扩展名
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == strExtension)
				strRetVec.push_back(fileInfo.name);//目标文件

			continue;
		}

		if (strExtension != strName.substr(iPos + 1))//和指定扩展名不一致
			continue;

		strRetVec.push_back(fileInfo.name);//目标文件

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

std::vector<std::string> ComDir::getFilesFromDir(CString csDirPath, CString csExtension /*= CString("*")*/)
{
	vector<string> strRetVec;

	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);
	
	if (pchSize == wstring::npos)
	{
		delete pch;
		return strRetVec;
	}

	string strDirPath(pch);

	delete pch;

	//CStringToString
	int len_1 = csExtension.GetLength();
	PCHAR pch_1 = new char[len_1 + 1];
	size_t pchSize_1 = wcstombs(pch_1, csExtension, len_1 + 1);

	if (pchSize_1 == wstring::npos)
	{
		delete pch_1;
		return strRetVec;
	}

	string strExtension(pch_1);

	delete pch_1;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;

			CString csPath = csDirPath + CString("\\") + CString(fileInfo.name);
			vector<string> vec = getFilesFromDir(csPath, csExtension);
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
			continue;
		}

		if ("*" == strExtension)
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == strExtension)
				strRetVec.push_back(fileInfo.name);

			continue;
		}

		if (strExtension != strName.substr(iPos + 1))
			continue;

		strRetVec.push_back(fileInfo.name);

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

std::vector<std::string> ComDir::getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension /*= "*"*/)
{
	vector<string> strRetVec;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((string(pchDirPath) + "\\*.*").c_str(), &fileInfo)))
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;

			string strPath = string(pchDirPath) + "\\" + string(fileInfo.name);
			vector<string> vec = getFilesFromDir(strPath.c_str(), pchExtension);
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
			continue;
		}

		if ("*" == string(pchExtension))
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == string(pchExtension))
				strRetVec.push_back(fileInfo.name);

			continue;
		}

		if (string(pchExtension) != strName.substr(iPos + 1))
			continue;

		strRetVec.push_back(fileInfo.name);

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

在VS编译器内会报C4996错误,解决见下文:

C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 博客园 (cnblogs.com)

posted @ 2024-10-31 20:26  EricsT  阅读(48)  评论(0编辑  收藏  举报