linux内核源码工程,除了源码,makefile,之外,还有其它很多乱七八遭的东西,而当用vscode看源码时,无关的文件会大大拖慢vscode响应速度,简直是太卡了,所以,非常有必要把源码,makefile等重要东西筛选复制出来,单独成一个新的文件夹,然后再用用vscode来看低码,就流畅多了。这个小工具源码如下:
#include <io.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
struct DirectoryStruct
{
vector<string> FileName;
vector<string> DirectoryName;
vector<DirectoryStruct*> pNext;
};
void GetFilesType(string path, DirectoryStruct &Directory)
{
//文件信息,声明一个存储文件信息的结构体
struct _finddata_t fileinfo;
long hFile = _findfirst((path + "\\*").c_str(), &fileinfo);
if (hFile != -1)//若查找成功,则进入
{
do
{
//如果是目录,迭代之(即文件夹内还有文件夹)
if ((fileinfo.attrib & _A_SUBDIR))
{
//文件名不等于"."&&文件名不等于".."
//.表示当前目录
//..表示当前目录的父目录
//判断时,两者都要忽略,不然就无限递归跳不出去了!
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
Directory.DirectoryName.push_back(fileinfo.name);
DirectoryStruct *p = new DirectoryStruct;
Directory.pNext.push_back(p);
GetFilesType(path + "\\" + fileinfo.name, *Directory.pNext[Directory.DirectoryName.size()-1]);
}
}
//如果不是,加入列表
else
{
Directory.FileName.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile); //函数结束查找
}
}
bool CheckFileType(string Filename, string Type)
{
if (Filename.length() < Type.length())return false;
int j = Filename.length() - Type.length();
for (int i=0; i<Type.length(); i++, j++)
{
if (Filename[j] != Type[i])
return false;
}
return true;
}
bool CheckFileType(string Filename, vector<string> vFileType)
{
for (int i=0; i<vFileType.size(); i++)
{
if (CheckFileType(Filename, vFileType[i]) == true)//if (str.find(vFileType[i]) != string::npos)
{
return true;
}
}
return false;
}
bool FilterDirectory(DirectoryStruct &Directory, vector<string> vFileType)
{
for (int i=0; i<Directory.DirectoryName.size(); i++)
{
if (Directory.pNext[i] != NULL)
{
if (FilterDirectory(*Directory.pNext[i], vFileType) == true)
{
// 删除空文件夹
delete Directory.pNext[i];
Directory.pNext.erase(Directory.pNext.begin() + i);
Directory.DirectoryName.erase(Directory.DirectoryName.begin() + i);
i--;
}
}
}
for (int i=0; i<Directory.FileName.size(); i++)
{
if (CheckFileType(Directory.FileName[i], vFileType) == false)
{
// 删除非源文件。
Directory.FileName.erase(Directory.FileName.begin() + i);
i--;
}
}
if (Directory.pNext.empty() == true && Directory.FileName.empty() == true && Directory.DirectoryName.empty() == true)
{
return true;// 返回是空文件夹
}
return false; // 返回不是空文件夹
}
#include <io.h>
#include <direct.h>
#define MAX_PATH_LEN 2048//256
#define ACCESS(fileName,accessMode) _access(fileName, accessMode)
#define MKDIR(path) _mkdir(path)
// 从左到右依次判断文件夹是否存在,不存在就创建
// example: /home/root/mkdir/1/2/3/4/
// 注意:最后一个如果是文件夹的话,需要加上 '\' 或者 '/'
int createDirectory(string &directoryPath)
{
if (directoryPath[directoryPath.length()-1] != '\\' || directoryPath[directoryPath.length()-1] != '/')
directoryPath += '/';
int dirPathLen = directoryPath.length();
if (dirPathLen > MAX_PATH_LEN)
{
return -1;
}
char tmpDirPath[MAX_PATH_LEN] = { 0 };
for (int i = 0; i < dirPathLen; ++i)
{
tmpDirPath[i] = directoryPath[i];
if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
{
if (ACCESS(tmpDirPath, 0) != 0)
{
int ret = MKDIR(tmpDirPath);
if (ret != 0)
{
return ret;
}
}
}
}
return 0;
}
string to_string_path(vector<string> &vPrePath)
{
string path;
if (vPrePath.size() > 0)
{
path = vPrePath[0];
for (int i=1; i<vPrePath.size(); i++)
{
path = path + "\\" + vPrePath[i];
}
}
return path;
}
void CopyFile(string in_path, string out_path)
{
ifstream fin;
ofstream fout;
int pos = out_path.find_last_of('.');
if (pos != string::npos)
{
}
setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
fin.open(in_path.c_str(), ios::binary);
fout.open(out_path.c_str(), ios::binary);
setlocale(LC_ALL,"C");//还原
if (!fin)
{
printf("Error: Open in file <<%s>> filed!!!\n", in_path.c_str());
return;
}
if (!fout)
{
printf("Error: Open out file <<%s>> filed!!!\n", out_path.c_str());
return;
}
fin.seekg(0, ios::end);
long Size = fin.tellg();
fin.seekg(0, ios::beg);
char *buffer = new char[Size];
fin.read(buffer, Size);
fout.write(buffer, Size);
fin.close();
fout.close();
}
void OutputDirectoryBase(DirectoryStruct Directory, vector<string> &vInputPath, vector<string> &vOutputPath)
{
string strInputPrePath = to_string_path(vInputPath);
string strOutputPrePath = to_string_path(vOutputPath);
//string strOutputPrePath = vOutputPath[0] + "\\";
createDirectory(strOutputPrePath);
for (int i=0; i<Directory.DirectoryName.size(); i++)
{
if (Directory.pNext[i] != NULL)
{
vInputPath.push_back(Directory.DirectoryName[i]);
vOutputPath.push_back(Directory.DirectoryName[i]);
OutputDirectoryBase(*Directory.pNext[i], vInputPath, vOutputPath);
}
}
for (int i=0; i<Directory.FileName.size(); i++)
{
CopyFile(strInputPrePath + "\\" + Directory.FileName[i], strOutputPrePath + "\\" + Directory.FileName[i]);
}
if (vInputPath.empty() == false)vInputPath.pop_back();
if (vOutputPath.empty() == false)vOutputPath.pop_back();
}
void OutputDirectory(DirectoryStruct Directory, string InputPrePath, string OutputPrePath)
{
vector<string> vInPath;
vector<string> vOutPath;
vInPath.push_back(InputPrePath);
vOutPath.push_back(OutputPrePath);
OutputDirectoryBase(Directory, vInPath, vOutPath);
}
void Init_FileType(vector<string> &vFileType)
{
vFileType.push_back("Kconfig");
vFileType.push_back("Makefile");
vFileType.push_back("README");
vFileType.push_back("Kbuild");
vFileType.push_back(".h");
vFileType.push_back(".H");
vFileType.push_back(".c");
vFileType.push_back(".C");
vFileType.push_back(".cpp");
vFileType.push_back(".CPP");
vFileType.push_back(".s");
vFileType.push_back(".S");
}
void Filter_and_copy(string InputPrePath, string OutputPrePath, vector<string> &vFileType)
{
printf("-------------------------\r\n");
获取该路径下的所有文件
DirectoryStruct RootDirectory;
GetFilesType(InputPrePath, RootDirectory);
printf("Pick end.\r\n");
FilterDirectory(RootDirectory, vFileType);
printf("filter end.\r\n");
OutputDirectory(RootDirectory, InputPrePath, OutputPrePath);
printf("Out End\r\n");
}
int main()
{
printf("Start\r\n");
vector<string> vFileType;
Init_FileType(vFileType);
//Filter_and_copy("F:\\test_src", "F:\\test_dst", vFileType);
//Filter_and_copy("F:/Workspace_linux_0.11", "F:/test_dst", vFileType);
Filter_and_copy("C:/test", "F:/test_dst", vFileType);
printf("End\r\n");
system("pause");
}
附件:
同类型代码:
精确制导 --- 把linux内核源码中需要的代码导入Source Insight