当前位置: 首页 > news >正文

MFC中对编码文件的操作01

1、自定义类获取项目中全部对话框ID和头文件子路径

(1)、创建单例类

public:
static SetAllChinesefunctions*Instance();
private:
static SetAllChinesefunctions*_Instance;


SetAllChinesefunctions*SetAllChinesefunctions::Instance()
{
    if(nullptr == _Instance)
    {
        _Instance = new SetAllChinesefunctions;
    }
    return _Instance;
}

(2)、查找CFileFind类

这个类用于查找指定根目录下面的文件和文件夹,或者特定的文件。

1)、FindFile 函数会返回一个BOOL值,如果为1说明当前路径下还有文件或文件夹。参数是你指定的目录。

2)、FindNextFile函数,无参数,查找下一个文件或者文件夹。

3)、IsDots函数,无参数,用于判断当前文件夹是不是.或者..目录,是返回1。

4)、IsDirectory函数,无参数,用于判断当前是不是文件夹,是文件夹返回1。

CFileFind finder;
	CString searchPath = sPrjRoot + _T("\\*");//指定要去查找的路径
	BOOL bWorking = finder.FindFile(searchPath);//查找当前路径下面的文件和文件夹
	while (bWorking)
	{
		bWorking = finder.FindNextFile();

		if (finder.IsDots()) continue;

		if (finder.IsDirectory())
		{
			_iterfind(finder.GetFilePath());
			continue;
		}

		// 查找.h文件
		CString sFileName = finder.GetFileName();
		if (sFileName.Right(2).MakeLower() == _T(".h"))
		{
			_findDlgID(finder.GetFilePath());
		}
	}

(3)、当前类CFindDlgIDAndClassFile全部代码

#pragma once
#include <map>
#include"../TMLCHelper/StdioFileCodePage.h"

class CFindDlgIDAndClassFile
{
public:
	CFindDlgIDAndClassFile();
	~CFindDlgIDAndClassFile();
	static CFindDlgIDAndClassFile* Instance();
	void Find(const CString& sPrjRoot);

	bool IsExistDlgID(const CString& sID)
	{
		return(m_mapDlgIDAndClassFile.find(sID) != m_mapDlgIDAndClassFile.end());
	}
	
private:
	static CFindDlgIDAndClassFile* _Instance ;
	void _iterfind(const CString& sPrjRoot);
	void _findDlgID(const CString& sFileH);
	CString m_sRootPath;
public:
	std::map<CString, CString> m_mapDlgIDAndClassFile;   // 对话框ID - 类文件子路径
};

#define FINDDLGANDCLASS (*CFindDlgIDAndClassFile::Instance())

#include "stdafx.h"
#include "FindDlgIDAndClassFile.h"
#include <fstream>
#include <string>

//CFindDlgIDAndClassFile* _Instance = nullptr;

CFindDlgIDAndClassFile::CFindDlgIDAndClassFile()
{
}


CFindDlgIDAndClassFile::~CFindDlgIDAndClassFile()
{
}
CFindDlgIDAndClassFile* CFindDlgIDAndClassFile::_Instance = nullptr;  // 初始化 _Instance 为 nullptr
CFindDlgIDAndClassFile* CFindDlgIDAndClassFile::Instance()
{
	if (nullptr == _Instance)
	{
		_Instance = new CFindDlgIDAndClassFile;
	}

	return _Instance;
}

void CFindDlgIDAndClassFile::Find(const CString& sPrjRoot)
{
	m_sRootPath = sPrjRoot;
	m_mapDlgIDAndClassFile.clear();
	_iterfind(sPrjRoot);
}


void CFindDlgIDAndClassFile::_iterfind(const CString& sPrjRoot)
{
	CFileFind finder;
	CString searchPath = sPrjRoot + _T("\\*");//指定要去查找的路径
	BOOL bWorking = finder.FindFile(searchPath);//查找当前路径下面的文件和文件夹
	while (bWorking)
	{
		bWorking = finder.FindNextFile();

		if (finder.IsDots()) continue;

		if (finder.IsDirectory())
		{
			_iterfind(finder.GetFilePath());
			continue;
		}

		// 查找.h文件
		CString sFileName = finder.GetFileName();
		if (sFileName.Right(2).MakeLower() == _T(".h"))
		{
			_findDlgID(finder.GetFilePath());
		}
	}
}

void CFindDlgIDAndClassFile::_findDlgID(const CString& sFileH)
{
	CString sFilePath = sFileH;
	bool bDesignTime = false;

	CString stxt;
	CStdioFileCodePage file;
	file.OpenWithoutType(sFileH, CFile::modeRead);
	file.JumpBOM();
	while (file.ReadStringWithOutType(stxt))
	{
			int ipos1 = stxt.Find(_T("IDD ="));
			int ipos2 = stxt.Find(_T("IDD="));
			if (ipos1 != -1 || ipos2 != -1)
			{
				int ipos = (ipos1 > 0) ? (ipos1 + 5) : (ipos2 + 4);
				int ipose = stxt.Find('}', ipos);
				CString sID;
				if (ipose == -1)
				{
					 sID=stxt.Mid(ipos);
				}
				else
				{
					 sID = stxt.Mid(ipos, ipose - ipos);
				}
				
				sID.TrimLeft();
				sID.TrimRight();
				sFilePath.Replace(m_sRootPath, _T(""));
				sFilePath.Replace(L".h", _T(""));
				m_mapDlgIDAndClassFile[sID] = sFilePath;
				file.Close();
				return;
			}
	}
}


相关文章:

  • 图片搜索接口的应用展示说明
  • 阿里云ECS香港服务器性能强大、cn2高速网络租用价格表
  • Python学习笔记——自定义函数(基础知识)
  • qt-交通路口仿真
  • 数学的雨伞下:理解世界的乐趣
  • openEuler2203 LTS安装并远程桌面接VMware WorkStation Pro 17
  • 找座位 - 华为OD统一考试(C卷)
  • 虽迟但到,Postman终于支持Websocket接口了
  • 备战蓝桥杯 Day8(最长上升子序列LIS模型)
  • 第六章:纹理贴图
  • 认识K8S
  • Job 和 DaemonSet
  • Promise
  • SpringBoot+WebSocket实现即时通讯(四)
  • 二.自定义头文件
  • HTTP的详细介绍
  • 3、windows环境下vscode开发c/c++环境配置(二)
  • 通过Docker Compose的方式在Docker中安装Maven环境
  • 配置基于 AWS CRT 的 HTTP 客户端
  • git常用命令
  • 做网站推广的是什么职位/公司网站设计定制
  • 政府 门户 网站建设/合肥网络seo
  • wordpress国内博客主题/seo蜘蛛屯
  • head first wordpress 中文版/南宁seo主管
  • 企业网站的在线推广方法有哪些/网页制作软件
  • 宣武网站开发/链爱交易平台