|
|
#include <bits/stdc++.h>
|
|
|
#include <io.h>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
/**
|
|
|
* 功能:获取指定目录下所有的文件
|
|
|
* 作者:黄海
|
|
|
* 时间:2019-12-04
|
|
|
* @param cate_dir
|
|
|
* @return
|
|
|
*/
|
|
|
void getFiles(const string &path, vector<std::string> &files) {
|
|
|
//文件句柄
|
|
|
intptr_t hFile = 0;
|
|
|
//文件信息,_finddata_t需要io.h头文件
|
|
|
struct _finddata_t fileinfo;
|
|
|
std::string p;
|
|
|
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
|
|
|
do {
|
|
|
//如果是目录,迭代之
|
|
|
//如果不是,加入列表
|
|
|
if ((fileinfo.attrib & _A_SUBDIR)) {
|
|
|
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
|
|
|
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
|
|
|
} else {
|
|
|
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
|
|
|
}
|
|
|
} while (_findnext(hFile, &fileinfo) == 0);
|
|
|
_findclose(hFile);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int startsWith(string s, string sub) {
|
|
|
return s.find(sub) == 0 ? 1 : 0;
|
|
|
}
|
|
|
|
|
|
int endsWith(string s, string sub) {
|
|
|
return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:替换指定字符串
|
|
|
* 作者:黄海
|
|
|
* 时间:2019-11-21
|
|
|
* @param str
|
|
|
* @param old_value
|
|
|
* @param new_value
|
|
|
* @return
|
|
|
*/
|
|
|
string &replace_all(string &str, const string &old_value, const string &new_value) {
|
|
|
while (true) {
|
|
|
string::size_type pos(0);
|
|
|
if ((pos = str.find(old_value)) != string::npos)
|
|
|
str.replace(pos, old_value.length(), new_value);
|
|
|
else break;
|
|
|
}
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:将字符串进行分隔的工具函数
|
|
|
* 作者:黄海
|
|
|
* 时间:2019-12-04
|
|
|
* @param s
|
|
|
* @param sv
|
|
|
* @param flag
|
|
|
*/
|
|
|
void split(const string &s, vector<string> &sv, const char flag = ' ') {
|
|
|
sv.clear();
|
|
|
istringstream iss(s);
|
|
|
string temp;
|
|
|
|
|
|
while (getline(iss, temp, flag)) {
|
|
|
sv.push_back(temp);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:获取当前exe执行文件的上一级目录
|
|
|
* 作者:黄海
|
|
|
* 时间:2019-12-04
|
|
|
* @return
|
|
|
*/
|
|
|
string getCurrentParentPath() {
|
|
|
//当前目录
|
|
|
char currentPath[256];
|
|
|
_getcwd(currentPath, sizeof(currentPath));
|
|
|
vector<string> pathVector;
|
|
|
split(string(currentPath), pathVector, '\\');
|
|
|
string parentPath = "";
|
|
|
for (int i = 0; i < pathVector.size() - 1; i++) {
|
|
|
parentPath += pathVector[i] + "\\";
|
|
|
}
|
|
|
//去掉最后的分隔符
|
|
|
parentPath = parentPath.substr(0, parentPath.size() - 1);
|
|
|
return parentPath;
|
|
|
}
|
|
|
|
|
|
string inFile = "../../CMakeLists.config";
|
|
|
string configFile = "../CMakeLists.txt";
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
//获取当前目录的上一级目录
|
|
|
string path = getCurrentParentPath();
|
|
|
|
|
|
//中文?
|
|
|
locale loc = locale::global(locale(locale(), "", LC_CTYPE));
|
|
|
//输入文件
|
|
|
ifstream in;
|
|
|
in.open(inFile);
|
|
|
|
|
|
//输出文件
|
|
|
ofstream out;
|
|
|
out.open(configFile);
|
|
|
|
|
|
//遍历
|
|
|
string line;
|
|
|
while (getline(in, line)) {
|
|
|
string str = line;
|
|
|
out << str << endl;
|
|
|
}
|
|
|
|
|
|
//开始处理目录下的所有CPP文件
|
|
|
vector<string> filenames;//用来存储文件名
|
|
|
getFiles(path, filenames);
|
|
|
|
|
|
int count = 0;
|
|
|
for (auto file : filenames) {
|
|
|
if (endsWith(file, ".cpp") && !endsWith(file, "CMakeCXXCompilerId.cpp")) {
|
|
|
string key = replace_all(file, path, "").substr(1);
|
|
|
key = replace_all(key, "\\", "/");
|
|
|
//获取程序名称
|
|
|
vector<string> sv;
|
|
|
split(key, sv, '/');
|
|
|
string fName;
|
|
|
if (sv.size() > 1)fName = sv[1]; else fName = sv[0];
|
|
|
fName = replace_all(fName, ".cpp", "");
|
|
|
out << "add_executable(" + fName + " " + key + ")" << endl;
|
|
|
count++;
|
|
|
}
|
|
|
}
|
|
|
//关闭文件
|
|
|
out.close();
|
|
|
in.close();
|
|
|
|
|
|
cout << "恭喜,配置文件修改成功完成,成功添加.cpp文件共" << count << "个!" << endl;
|
|
|
return 0;
|
|
|
} |