静态库封装之ComDir类

ComDir.h


/*
@author:EricsT
@data:20241031
@version:V1.0
@history:
	@author @data @version @content
	EricsT 20241031 V1.0 新增ComDir类[判断存在性以及创建目录]
*/

#pragma once

#include <string>
#include <fstream>
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);

};

ComDir.cpp


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

#include "stdafx.h"
#include "ComDir.h"
#include <io.h>
#include <sys/stat.h>
#include <direct.h>


bool ComDir::isDirExists(string strDirPath)
{
	if (!access(strDirPath.c_str(), 0))//判断路径是否存在
		return false;

	struct stat status;

	stat(strDirPath.c_str(), &status);//获取状态变量

	if (status.st_mode & S_IFDIR)//取模式判断是否是目录
		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;

	if (!access(strDirPath.c_str(), 0))
		return false;

	struct stat status;

	stat(strDirPath.c_str(), &status);

	if (status.st_mode & S_IFDIR)
		return true;

	return false;
}

bool ComDir::isDirExists(PCHAR pchDirPath)
{
	if (!access(pchDirPath, 0))
		return false;

	struct stat status;

	stat(pchDirPath, &status);

	if (status.st_mode & S_IFDIR)
		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;
}

在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)

请登录后发表评论

    没有回复内容