C++ win平台路径管理类

#ifndef __PATH_MANAGER_H__
#define __PATH_MANAGER_H__
#include <string>
#include <vector>

class PathManager
{
public:
	PathManager(void) {};
	PathManager(const PathManager&) {};
	PathManager& operator=(const PathManager &) {};
public:
	~PathManager(void);

	static PathManager* getInstance();

	//判断是否是目录
	bool isDirectory(const std::string& path);

	//由路径获取文件名
	std::string getFileName(const std::string& path);

	//由路径获取目录
	std::string getFileDirectory(const std::string& path);

	//将一条完整路径进行分割,获取每一级目录,层次依次加深
	//path:文件或文件夹路径
	std::vector<std::string> getDirectoryHierarchy(const std::string& path);

	//将传入的所有完整路径进行分割,获取每一级目录并合并重复目录,层次依次加深
	//paths:文件或文件夹路径
	std::vector<std::string> getDirectoryHierarchy(const std::vector<std::string>& paths);

	//由完整文件名获取不带后缀的文件名
	std::string getNameWithoutSuffix(const std::string& fileName);

	//由完整文件名获取后缀
	std::string getSuffix(const std::string& fileName);

	//由一条完整路径创建目录
	//path:文件或文件夹路径
	//return:成功创建路径的数量
	int createDirectory(const std::string& path);

	//由多条完整路径创建目录
	//paths:文件或文件夹路径
	//return:成功创建路径的数量
	int createDirectory(const std::vector<std::string>& paths);

private:
	static	PathManager*	_pathManager;
};
#endif	//__PATH_MANAGER_H__

#include "stdafx.h"
#include "PathManager.h"
#include <algorithm>

using namespace std;

PathManager*	PathManager::_pathManager = nullptr;

PathManager::~PathManager(void)
{
	if (_pathManager)
	{
		delete (_pathManager);
		_pathManager = nullptr;
	}
}

PathManager* PathManager::getInstance()
{
	if (_pathManager == nullptr)
	{
		_pathManager = new	PathManager;
	}
	return	_pathManager;
}

bool PathManager::isDirectory(const std::string& path)
{
	int index = path.length() - 1;
	if (path.find_last_of('\\') != index && path.find_last_of('/') != index)
	{
		return	false;
	}

	return	true;
}

std::string PathManager::getFileName(const std::string& path)
{
	int indexP = path.find_last_of('\\');
	int indexN = path.find_last_of('/');
	return	path.substr((indexP > indexN ? indexP : indexN) + 1, path.length());
}

std::string PathManager::getFileDirectory(const std::string& path)
{
	int indexP = path.find_last_of('\\');
	int indexN = path.find_last_of('/');
	return	path.substr(0, (indexP > indexN ? indexP : indexN) + 1);
}

//将一条完整路径进行分割,获取每一级目录,层次依次减小
//路径可以为文件或文件夹路径
std::vector<std::string> getDirectoryHierarchyReverse(const std::string& path)
{
	std::vector<std::string>	directoryHierarchy;
	int index = path.find_last_of('\\');
	char separator = (index > 0) ? ('\\') : ('/');

	string directory = path;
	int indexF = path.find_first_of(separator);
	int indexL = path.find_last_of(separator);
	while (indexF < indexL)
	{
		directory = path.substr(0, directory.find_last_of(separator));
		directoryHierarchy.push_back(directory + separator);
		indexL = directory.find_last_of(separator);
	}

	return	directoryHierarchy;
}

std::vector<std::string> PathManager::getDirectoryHierarchy(const std::string& path)
{
	std::vector<std::string>	directoryHierarchy;
	directoryHierarchy = getDirectoryHierarchyReverse(path);
	reverse(directoryHierarchy.begin(), directoryHierarchy.end());

	return	directoryHierarchy;
}

std::vector<std::string> PathManager::getDirectoryHierarchy(const std::vector<std::string>& paths)
{
	std::vector<std::string>	directoryHierarchy;
	std::vector<std::string>	directorys;
	for (auto& path : paths)
	{
		directorys.clear();
		directorys = getDirectoryHierarchyReverse(path);
		directoryHierarchy.insert(directoryHierarchy.end(), directorys.begin(), directorys.end());
	}

	sort(directoryHierarchy.begin(), directoryHierarchy.end());
	directoryHierarchy.erase(unique(directoryHierarchy.begin(), directoryHierarchy.end()), directoryHierarchy.end());

	return	directoryHierarchy;
}

std::string PathManager::getNameWithoutSuffix(const std::string& fileName)
{
	int index = fileName.find_last_of('.');
	return	fileName.substr(0, index);
}

std::string PathManager::getSuffix(const std::string& fileName)
{
	int index = fileName.find_last_of('.');
	return	fileName.substr(index + 1, fileName.length());
}

int PathManager::createDirectory(const std::string& path)
{
	int pathCount = 0;
	auto directories = getDirectoryHierarchy(path);
	if (directories.size() > 0)
	{
		for (auto& directory : directories)
		{
			::CreateDirectory(directory.c_str(), NULL);
			pathCount++;
		}
	}

	return	pathCount;
}

int PathManager::createDirectory(const std::vector<std::string>& paths)
{
	int pathCount = 0;
	for (auto& path : paths)
	{
		pathCount += createDirectory(path);
	}
	return	pathCount;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值