C++ win平台路径管理类

2023-11-03

#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;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ win平台路径管理类 的相关文章

  • 结构体如何存储在内存中?

    我有一个struct iof header在我的代码中 我确定它的宽度是 24 字节 我执行 sizeof iof header 它返回 32 字节宽 问题1为什么是 32 字节宽而不是 24 字节宽 问题2包括其成员在内 结构体如何存储在
  • fopen_s 怎么会比 fopen 更安全呢?

    我正在处理遗留代码Windows平台 当我编译代码时VS2013 它给出以下警告 错误 C4996 fopen 该函数或变量可能不安全 考虑使用fopen s反而 要禁用弃用 请使用 CRT SECURE NO WARNINGS 详情请参见
  • 是否有可能劫持标准输出

    我正在尝试使用 C 重定向 Windows XP 上已运行进程的标准输出 我知道如果我自己生成进程 我可以做到这一点 但对于这个应用程序 我更喜欢一个 监听器 我可以附加到另一个进程 这在纯 Net 中可能吗 如果不可能 在 Win32 中
  • 为什么我会收到未找到分析器的警告?

    我创建了一个玩具项目来检查最新的 NET 7 预览版 5 和正则表达式代码生成 它效果很好 所以我对现有项目应用了相同的更改 不是为了生产 而是为了个人生产力 由于某种原因 我收到这些警告 CS8032 An instance of ana
  • Caliburn.Micro - ShowDialog() 如何关闭对话框?

    EDIT 新信息 刚刚设法让记录器工作 老实说 我不知道 cm 有一个 并且在尝试使用时收到此消息TryClose TryClose requires a parent IConductor or a view with a Close m
  • STL之类的容器typedef快捷方式?

    STL 容器的常见模式是这样的 map
  • 将公历日期转换为儒略日期,然后再转换回来(随着时间)

    我正在编写一个程序 必须将当前的公历日期和时间转换为儒略日期 然后再转换回公历门 最终我需要添加能够添加年 月 日 小时 分钟和秒的功能 但我需要先解决这部分问题 现在我已经从公历日期转换为儒略日期 所以从逻辑上讲 我觉得我应该能够以某种方
  • 最新 .Net MongoDb.Driver 的连接问题

    我创建了一个 MongoLab 沙箱数据库 我与 MongoChef 连接 效果很好 我通过 Nuget 安装了 MongoDB Driver 2 2 2 我编写了一些简单的 C 演示代码 但就是无法使其工作 连接字符串是直接从 Mongo
  • 为什么子函数不销毁GtkWindow?

    这是我的代码 void window first void enter window2 GtkWidget w gpointer data void quit GtkWidget w gpointer data void quit int
  • 在没有 epsilon 的情况下可以将浮点数与 0.0 进行比较吗?

    我知道 要比较两个浮点值 需要使用一些 epsilon 精度 因为它们并不精确 但是 我想知道是否存在边缘情况 我不需要那个 epsilon 特别是 我想知道这样做是否总是安全的 double foo double x if x lt 0
  • 如何从 Qt 应用程序通过 ODBC 连接到 MySQL 数据库?

    我有一个新安装的 MySQL 服务器 它监听 localhost 3306 从 Qt 应用程序连接到它的正确方法是什么 原来我需要将MySQL添加到ODBC数据源 我在遵循这个视频教程后做到了这一点 https youtu be K3GZi
  • 嵌入资源文件的路径

    我的资源文件中有一个图标 我想引用它 这是需要图标文件路径的代码 IWshRuntimeLibrary IWshShortcut MyShortcut MyShortcut IWshRuntimeLibrary IWshShortcut W
  • 将 dataGridView 中选定的行作为对象检索

    我有一堂这样的课 public partial class AdressBokPerson public long Session get set public string F rnamn get set public string Ef
  • 无法加载程序集问题

    我收到以下错误 无法加载程序集 错误详细信息 System BadImageFormatException 无法加载文件或程序集 文件 或其依赖项之一 该程序集是由比当前加载的运行时更新的运行时构建的 无法加载 该程序集是使用 Net Fr
  • 如何将STL容器数据转储到gdb中?

    我无法在 gdb 中转储 STL 无序映射容器值 变量类型是 std unordered map var 我的 gdb 版本 7 7 1 GDB配置 configure host x86 64 linux gnu target x86 64
  • 调用异步方法在视图模型的构造函数中加载数据有警告

    我的视图包含一个 ListView 它显示来自互联网的一些数据 我创建一个异步方法来加载数据并在我的视图模型的构造函数中调用该方法 它有一个警告提示我现在使用await关键字 还有其他解决方案可以在构造函数中异步加载数据吗 有几种可以应用的
  • 您对“大规模 C++ 软件设计”的看法 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 正在阅读亚马逊评论 https rads stackoverflow com amzn click com 0201633620 and ACC
  • 在 C# 中使用命名空间别名有什么好处? [复制]

    这个问题在这里已经有答案了 使用命名空间别名有什么好处 仅仅是为了简化编码吗 仅当与类发生冲突时我才使用名称空间别名 对我来说 这根本没有简化 我的意见是 如果没有必要 就不要使用
  • 如何在 C# 中更改公共 IP 地址

    我正在创建一个 C winform 应用程序 我想在其中更改公共 IP 地址 而不是像 Hotspot Shield ZenMate OpenVPN 等那样更改 IPv4 地址 我已经检查了以下链接 但没有找到足够的帮助 所以我发布了这个问
  • 从脚本启用/禁用 GameObject 组件 [Unity3D]

    我需要获取一个脚本中设置的布尔值 放入名为 bouclier 的变量 以启用或禁用游戏对象 该变量位于游戏对象 Player 中 此处右下角 我需要启用或禁用这个游戏对象 Bouclier01 为此 我将脚本附加到游戏对象 Bouclier

随机推荐

  • 【满分】【华为OD机试真题2023 JAVA&JS】最优资源分配

    华为OD机试真题 2023年度机试题库全覆盖 刷题指南点这里 最优资源分配 知识点数组贪心 时间限制 1s 空间限制 32MB 限定语言 不限 题目描述 某块业务芯片最小容量单位为1 25G 总容量为M 1 25G 对该芯片资源编号为1 2
  • win10 VS code 编译运行 C/C++的方法

    win10 VS code 编译运行 C C 的方法 具体配置过程如下链接 https zhuanlan zhihu com p 35178331 但中间出了点问题 CTRL ALT n 运行后 PS D C gt cd d C if gc
  • R语言apply()函数

    apply 函数是一种很强大的机制 apply 可把函数应用到数组的某个维度上 其函数的的一般格式为 apply x MARGIN FUN 其中 x为数据对象 MARGIN是维度的下标 FUN是由你指定的函 数 而 则包括了任何想传递给FU
  • Animator动画混合树

    Unity中的BlendTree BlendTree介绍 BlendTree BlendTree创建 一维混合 1D Blending 二维混合树 每个混合树的动画有一些要注意的地方 BlendTree介绍 Blend Tree用于多个动画
  • ScrollView简单自动滚动问题总结

    今天参考网上的资料写了一个简单的动画 刚开始的时候 确实困难重重 1 当我们在Activity里面获得View对象的时候 无论是getMeasuredHeight 还是getHehgit 方法 放在Activity里的onCreate on
  • 联想拯救者r720自带win10安装linux(ubuntu)双系统

    联想拯救者R720自带win10安装linux ubuntu 双系统 准备事项 ubuntu的u盘启动 网上有教程 下个比较新的版本 本人用的ubuntu16 04 关闭win10的快速启动 也可以不关闭 不关闭的话可能会导致以后ubunt
  • 规律化递归

    递归思想 具体案例 package Java project 1 import java util Scanner public class RecursionDemo public static void main String args
  • k8s知识点拾遗

    目录 Headless和Service ClusterIP模式 Headless模式 Deployment 简述 更新Deployment 回退Deployment Deployment扩容 暂停和恢复Deployment 编写Deploy
  • 通过GitHub Blame深入分析Redux源码

    文章首发于GitHub Blog 说明 本文所分析的Redux版本为3 7 2 分析直接写在了注释里 放在了GitHub上 gt 仓库地址 分析代码时通过查看Github blame 参考了Redux的issue及PR来分析各个函数的意图而
  • 配置SSH Key连接GitLab

    Git配置ssh连接相关命令 1 配置账号 git config global user name cwh git config global user email cwh xxx com 邮箱需要GitLab上账号配置相对应的邮箱 否则拉
  • 2022年「博客之星」参赛博主:落寞的魚丶

    诚信五星 五星必回 https bbs csdn net topics 611387242 spm 1001 2014 3001 6377 诚信五星 五星必回
  • noip 2008 双栈排序

    题目大意 给定n和一串数字 这串数字是一个1 n的排列 现在要用两个栈给这些数字排序 首先先判断是否有解 有解的话再输出字典序最小的方案 入栈1 输出a 出栈1 输出b 入栈2 输出c 出栈2 输出d 分析 首先必然要先考虑是否有解 对于没
  • 国产超低功耗华大MCU资料汇总

    华大单片机最新最全内容请访问 芯虎论坛 http tigerchip com cn 目录 点击直接跳转 开发工具下载 离线编程器 仿真器 MDK IAR 选型表 封装库 芯片资料 HC32F003 HC32F005 HC32L110 HC3
  • tree.plot_tree()函数里面具体的参数作用

    sklearn tree plot tree decision tree max depth None feature names None class names None label all filled False impurity
  • STM32通用定时器的个人总结

    STM32104ZET6系列的芯片中 定时器一共有一下三种分类 高级定时器 通用定时器 基本定时器 这里主要讲一下通用定时器 首先 计数器模式一共有三种 分别为向上 向下或向上向下计数 其中 向上计数的通俗意思就是 计数器从0开始计数 一直
  • 如何找出U盘中隐藏的文件夹

    背景 这周二我们老师说我一个问题就是 说她的U盘里有一大堆东西但是打开U盘的时候显示为空 这个问题困扰了她好长时间 解决办法 第一步 用电脑管家杀毒 打开电脑管家 然后点击 病毒查杀 之后点击指定位置杀毒 杀毒位置为U盘所在盘符 首先排除了
  • Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) 解决方法

    有时用apt安装软件 莫名中断会导致apt被lock 接着再用apt就会报错 E Could not get lock var lib dpkg lock open 11 Resource temporarily unavailable E
  • Transformer代码讲解(最最最最......详细)

    Transformer代码讲解 最最最最 详细 整个代码主要分为两部分去讲解 一 完整代码 二 部分代码剖析 1 主函数if name main 2 从整体网路结构来看 分为三个部分 编码层 解码层 输出层 3 Encoder 部分包含三个
  • 解决IE 浏览器无法访问网站的问题

    今天电脑不知道咋地 一直访问不了网站 访问什么网站都报无法访问 如下图所示 经过几分摸索 发现是DNS的问题 通过设置IE浏览器可以自动设置 按照下面步骤设置即可 1 打开IE浏览器右上角的齿轮 2 选择 Internet 选项 3 选择
  • C++ win平台路径管理类

    ifndef PATH MANAGER H define PATH MANAGER H include