VC 工程vcproj 代码目录生成工具

有时候我们想阅读开源的代码,在windows下vs是最好的IDE,但是开源代码不提供vs的工程文件,新建工程简单但是上百个文件的添加就比较傻逼了。下面提供一段代码爬目录修改vcproj 的xml描述文件,有需求可以参考下,丰富下功能:

#include <iostream>
#include <list>
#include <map>
#include <io.h>
#include <windows.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <sys/stat.h>

#include "tinyxml2.h"

using namespace std;
using namespace tinyxml2;

////////////////////////////////////////////////////////////////////////
class fileNode
{
public:
	fileNode(){
		fileList.clear();
		dirList.clear();
	}
public:
	list<string>	fileList;
	map<string, fileNode>	dirList;
};

////////////////////////////////////////////////////////////////////////
void getFileList(const string& sPath,fileNode& rNode);
void getFile(const string& sPath,fileNode& rNode,_finddata_t& file)
{
	string filename = file.name;
	if(filename == "." || filename == "..")
	{
		return ;
	}
	string sAddPath = sPath;
	sAddPath += filename;

	struct stat st;
	stat(sAddPath.c_str(), &st);

	if(st.st_mode & S_IFDIR == S_IFDIR)
	{
		sAddPath += "\\";
		getFileList(sAddPath, rNode.dirList[filename]);
	}
	else
	{
		rNode.fileList.push_back(sAddPath);
	}
}

void getFileList(const string& sPath,fileNode& rNode)
{
	struct _finddata_t file;
	long hFile;
	string sPathLast = sPath + "*"; // sPathLast = "c:\test\*.*"
	hFile = _findfirst(sPathLast.c_str(), &file);
	if(hFile == -1)
	{
		return;
	}
	else
	{
		getFile(sPath,rNode,file);
	}

	while(_findnext(hFile, &file) != -1)
	{
		getFile(sPath,rNode,file);
	}
}

void xmlEleFileList(XMLElement* elemXml, map<string, bool>& fileMap)
{
	fileMap.clear();
	if(elemXml == NULL)
	{
		return;
	}
	XMLElement* pNode = elemXml->FirstChildElement("File");
	while(pNode != NULL)
	{
		const char* pName = pNode->Attribute("RelativePath");
		if(pName != NULL)
		{
			fileMap[pName] = false;
		}
		pNode = pNode->NextSiblingElement("File");
	}
}

void xmlEleAddFile(XMLElement* elemXml, const string& fileName)
{
	if(elemXml == NULL)
		return;
	tinyxml2::XMLDocument* doc = elemXml->GetDocument();
	XMLElement* pNode = doc->NewElement("File");
	pNode->SetAttribute( "RelativePath", fileName.c_str());
	elemXml->InsertEndChild(pNode);
}

void xmlEleRemoveFile(XMLElement* elemXml, map<string, bool>& fileMap)
{
	if(elemXml == NULL)
	{
		return;
	}
	XMLElement* pNode = elemXml->FirstChildElement("File");
	while(pNode != NULL)
	{
		string rName = pNode->Attribute("RelativePath");
		if(rName.length() > 0 && fileMap.find(rName) != fileMap.end() && fileMap[rName] == false)
		{
			XMLElement* pOld = pNode;
			pNode = pOld->NextSiblingElement("File");
			elemXml->DeleteChild(pOld);
			continue;
		}
		pNode = pNode->NextSiblingElement("File");
	}
}

void saveFileList(fileNode& rNode, XMLElement* elemXml);
void xmlEleCheckDir(XMLElement* elemXml,const string& dirName, fileNode& rNode)
{
	if(elemXml == NULL)
		return;

	bool bfind = false;
	XMLElement* pNode = elemXml->FirstChildElement("Filter");
	while(pNode != NULL)
	{
		string rName = pNode->Attribute("Name");
		if(rName == dirName)
		{
			bfind = true;
			break;
		}
		pNode = pNode->NextSiblingElement("Filter");
	}
	if(bfind == false)
	{
		pNode = NULL;
	}
	bool binsert = false;
	if(pNode == NULL)
	{
		tinyxml2::XMLDocument* doc = elemXml->GetDocument();
		pNode = doc->NewElement("Filter");
		pNode->SetAttribute( "Name", dirName.c_str());
		binsert = true;
	}

	saveFileList(rNode, pNode);

	if(binsert == true)
	{
		elemXml->InsertEndChild(pNode);
	}
}

void xmlEleRemoveDir(XMLElement* elemXml, map<string, bool>& dirMap)
{
	if(elemXml == NULL)
	{
		return;
	}
	XMLElement* pNode = elemXml->FirstChildElement("Filter");
	while(pNode != NULL)
	{
		string rName = pNode->Attribute("Name");
		if(rName.length() > 0 && dirMap.find(rName) == dirMap.end())
		{
			XMLElement* pOld = pNode;
			pNode = pOld->NextSiblingElement("Filter");
			elemXml->DeleteChild(pOld);
			continue;
		}
		pNode = pNode->NextSiblingElement("Filter");
	}
}

void saveFileList(fileNode& rNode, XMLElement* elemXml)
{
	if(elemXml == NULL)
		return;

	map<string, bool> fileMap;
	xmlEleFileList(elemXml, fileMap);

	for(list<string>::iterator iter = rNode.fileList.begin(); iter != rNode.fileList.end(); iter++)
	{
		string& filename = *iter;
		if(fileMap.find(filename) == fileMap.end())
		{
			xmlEleAddFile(elemXml, filename);
		}
		fileMap[filename] = true;
	}

	xmlEleRemoveFile(elemXml, fileMap);
	fileMap.clear();

	for(map<string, fileNode>::iterator iter = rNode.dirList.begin(); iter != rNode.dirList.end(); iter ++)
	{
		xmlEleCheckDir(elemXml, iter->first, iter->second);
		//fileMap[iter->first] = true;
	}

	//xmlEleRemoveDir(elemXml, fileMap);
	//fileMap.clear();
}

////////////////////////////////////////////////////////////////////////
int main(int argc,char** argv)
{
	if(argc > 2)
	{
		string xmlfile(argv[1]);
		string filedir(argv[2]);

		//获得列表
		fileNode root;
		getFileList(filedir,root);

		tinyxml2::XMLDocument doc;
		if(doc.LoadFile(xmlfile.c_str()) != 0)
		{
			cout<<"读取文件:"<<xmlfile<<"失败!"<<endl;
			return -1;
		}
		XMLElement* elemProj = doc.FirstChildElement("VisualStudioProject");
		if(elemProj == NULL)
		{
			cout<<"读取文件:"<<xmlfile<<"失败,VisualStudioProject节点不存在!"<<endl;
			return -2;
		}
		bool badd = false;
		XMLElement* elemFiles = elemProj->FirstChildElement("Files");
		if(elemFiles == NULL)
		{
			elemFiles = doc.NewElement("Files");
			badd = true;
		}
		saveFileList(root, elemFiles);
		if(badd == true)
		{
			elemProj->InsertEndChild(elemFiles);
		}
		doc.SaveFile(xmlfile.c_str());
	}
	else
	{
		cout<<"usetage: addvsdir 工程文件 相对目录"<<endl;
	}
	return 0;
}
时间: 2024-08-04 07:58:50

VC 工程vcproj 代码目录生成工具的相关文章

代码自动生成工具,2小时搞定智能硬件产品Demo

常见的智能硬件设备多是由单片机.微处理器.微控制器等构成的嵌入式系统,通过WIFI.蓝牙.GPRS等无线通信技术连接云服务器,传统的开发方式需要开发人员根据自己的产品功能完成MCU 通过无线通信/芯片模组与云服务器交互的协议,而通过MCU代码自动生成工具,云端会根据产品定义的数据点生成对应产品的设备端代码.使用自动生成的代码开发产品,就不必再处理协议相关的部分,开发者可以将节省出来的精力集中在产品的核心功能开发上,不必重复"造轮子". 使用MCU.手机APP代码自动生成工具,2小时搞定

iBatis 代码自动生成工具 iBator 及 Example 使用

iBator的下载和安装 官方下载地址:http://people.apache.org/builds/ibatis/ibator/ 安装:见<Eclipse 插件安装> 安装完成后,“File” —> "New" —> "Other..." iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间 选择项目名 —> "New" —> "Other..." —> “N

STM32代码自动生成工具使用说明

1.什么是"代码自动生成工具" 为了降低开发者的开发门槛,缩短开发周期,降低开发资源投入,机智云推出了代码自动生成服务.云端会根据产品定义的数据点生成对应产品的设备端代码. 自动生成的代码实现了机智云通信协议的解析与封包.传感器数据与通信数据的转换逻辑,并封装成了简单的API,且提供了多种平台的实例代码.当设备收到云端或APP端的数据后,程序会将数据转换成对应的事件并通知到应用层,开发者只需要在对应的事件处理逻辑中添加传感器的控制函数,就可以完成产品的开发. 使用自动生成的代码开发产品

GhostDoc Pro v4.9.14093.Cracked.By.SubMain 一款好用的代码注释生成工具——VS插件

原文:GhostDoc Pro v4.9.14093.Cracked.By.SubMain 一款好用的代码注释生成工具--VS插件 一款比较好用的 VS 插件,能够快速生成注释. 这是 Pro 版本,与标准版本相比,支持对类.文件批量生成注释并且可以生成 CHM 帮助文件. 具体差异请转到: http://submain.com/products/ghostdoc.aspx 简化你的XML注释! GhostDoc是一个Visual Studio扩展,自动生成基于其类型,参数,名称和其他上下文信息

Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展

Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导> 是生存Web Form的. 这次看到网上有生成MVC Saffolding扩展原作者的代码 https://github.com/robinli/MVC5-

canvas-js贝塞尔曲线代码在线生成工具

详细内容请点击 canvas贝塞尔曲线代码在线生成工具 可以快速生成二次.三次贝塞尔曲线的源码生成器,方便经常使用到canvas画图的同学使用,可以直接预览效果随意画出自己想要的图像. 生成源码效果预览: canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d") ctx.lineWidth = 6; ctx.strokeStyle = "#0090D2";

canvas-js贝塞尔曲代码在线生成工具

canvas贝塞尔曲代码在线生成工具 可以快速生成二次.三次贝塞尔曲线的源码生成器,方便经常使用到canvas画图的同学使用,可以直接预览效果随意画出自己想要的图像. 生成源码效果预览: canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d") ctx.lineWidth = 6; ctx.strokeStyle = "#0090D2"; ctx.begin

mybaits generator 代码自动生成工具使用

mybaits generator 代码自动生成工具使用MyBatis Generator (MBG) 是一个Mybatis的代码生成器,它可以帮助我们根据数据库中表的设计生成对应的实体类,xml Mapper文件,接口以及帮助类(也就是我们可以借助该类来进行简单的CRUD操作),这样就避免了我们每使用到一张表的数据就需要手动去创建对应的类和xml文件,这就帮我们节约了大量的时间去开发和业务逻辑有关的功能,但是如果对联合查询和存储过程您仍然需要手写SQL和对象. 生成方式主要有 Maven 和

mybatis-generator 代码自动生成工具(maven方式)

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的dao.bean.mapper xml文件. 这里主要通过eclipse工具,来讲解实现; 1.建表语句 CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, PRI