Sometimes we want to read the open source code, under Windows vs is the best IDE, but the open source code does not provide the VS project file, the new project is simple but hundreds of files added is more silly. Below provides a section of code Crawl directory modification vcproj XML description file, there is a need to refer to the following, rich under the function:
#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 ();d irlist.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");p node->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 ();p node = doc->newelement ("Filter");p node->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]);//Get list Filenode root;getfilelist (filedir,root); TINYXML2:: XMLDocument Doc;if (Doc. LoadFile (XMLFILE.C_STR ())! = 0) {cout<< "read file:" <<xmlfile<< "Failed! "<<ENDL;RETURN-1;} xmlelement* elemproj = doc. Firstchildelement ("Visualstudioproject"); if (elemproj = = NULL) {cout<< "read file:" <<xmlfile<< "failed, Visualstudioproject node does not exist! "<<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 project file relative catalogue" <<ENDL;} return 0;}
VC Project vcproj Code Catalog Generation tool