2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <memory.h>
|
2020-03-20 11:22:05 +08:00
|
|
|
|
#include <set>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
#include <deque>
|
2020-03-20 11:22:05 +08:00
|
|
|
|
#include "Util/CMD.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/File.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
class CMD_main : public CMD {
|
|
|
|
|
public:
|
|
|
|
|
CMD_main() {
|
|
|
|
|
_parser.reset(new OptionParser(nullptr));
|
|
|
|
|
(*_parser) << Option('f',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"filter",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
"c,cpp,cxx,c,h,hpp",/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"文件后缀过滤器",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
|
|
(*_parser) << Option('i',/*该选项简称,如果是\x00则说明无简称*/
|
|
|
|
|
"in",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
|
|
|
|
|
Option::ArgRequired,/*该选项后面必须跟值*/
|
|
|
|
|
nullptr,/*该选项默认值*/
|
|
|
|
|
true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
|
|
|
|
|
"文件夹或文件",/*该选项说明文字*/
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~CMD_main() {}
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
vector<string> split(const string& s, const char *delim) {
|
|
|
|
|
vector<string> ret;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t last = 0;
|
|
|
|
|
auto index = s.find(delim, last);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
while (index != string::npos) {
|
|
|
|
|
if (index - last >= 0) {
|
|
|
|
|
ret.push_back(s.substr(last, index - last));
|
|
|
|
|
}
|
|
|
|
|
last = index + strlen(delim);
|
|
|
|
|
index = s.find(delim, last);
|
|
|
|
|
}
|
|
|
|
|
if (!s.size() || s.size() - last >= 0) {
|
|
|
|
|
ret.push_back(s.substr(last));
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 11:22:05 +08:00
|
|
|
|
void process_file(const char *file) {
|
|
|
|
|
auto str = File::loadFile(file);
|
|
|
|
|
if (str.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
auto lines = ::split(str, "\n");
|
2020-03-20 11:22:05 +08:00
|
|
|
|
deque<string> lines_copy;
|
|
|
|
|
for (auto &line : lines) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if(line.empty()){
|
|
|
|
|
lines_copy.push_back("");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-20 11:22:05 +08:00
|
|
|
|
string line_copy;
|
|
|
|
|
bool flag = false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
int i = 0;
|
2020-03-20 11:22:05 +08:00
|
|
|
|
for (auto &ch : line) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
++i;
|
2020-03-20 11:22:05 +08:00
|
|
|
|
switch (ch) {
|
|
|
|
|
case '\t' :
|
|
|
|
|
line_copy.append(" ");
|
|
|
|
|
break;
|
|
|
|
|
case ' ':
|
|
|
|
|
line_copy.push_back(ch);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
line_copy.push_back(ch);
|
|
|
|
|
flag = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flag) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
line_copy.append(line.substr(i));
|
2020-03-20 11:22:05 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lines_copy.push_back(line_copy);
|
|
|
|
|
}
|
|
|
|
|
str.clear();
|
|
|
|
|
for (auto &line : lines_copy) {
|
|
|
|
|
str.append(line);
|
|
|
|
|
str.push_back('\n');
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if(!lines_copy.empty()){
|
|
|
|
|
str.pop_back();
|
|
|
|
|
}
|
2020-03-20 11:22:05 +08:00
|
|
|
|
File::saveFile(str, file);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// / 这个程序是为了统一替换tab为4个空格 [AUTO-TRANSLATED:ecb3b523]
|
|
|
|
|
// / This program is for unified replacement of tabs with 4 spaces
|
2020-03-20 11:22:05 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
CMD_main cmd_main;
|
|
|
|
|
try {
|
|
|
|
|
cmd_main.operator()(argc, argv);
|
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
cout << ex.what() << endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string path = cmd_main["in"];
|
|
|
|
|
string filter = cmd_main["filter"];
|
2020-03-20 11:51:24 +08:00
|
|
|
|
auto vec = ::split(filter, ",");
|
2020-03-20 11:22:05 +08:00
|
|
|
|
|
|
|
|
|
set<string> filter_set;
|
|
|
|
|
for (auto ext : vec) {
|
|
|
|
|
filter_set.emplace(ext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool no_filter = filter_set.find("*") != filter_set.end();
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置日志 [AUTO-TRANSLATED:50372045]
|
|
|
|
|
// Set log
|
2020-03-20 11:22:05 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
|
|
|
|
File::scanDir(path, [&](const string &path, bool isDir) {
|
|
|
|
|
if (isDir) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!no_filter) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 开启了过滤器 [AUTO-TRANSLATED:331a77dd]
|
|
|
|
|
// Filter enabled
|
2020-03-20 11:22:05 +08:00
|
|
|
|
auto pos = strstr(path.data(), ".");
|
|
|
|
|
if (pos == nullptr) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 没有后缀 [AUTO-TRANSLATED:2273522f]
|
|
|
|
|
// No suffix
|
2020-03-20 11:22:05 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
auto ext = pos + 1;
|
|
|
|
|
if (filter_set.find(ext) == filter_set.end()) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 后缀不匹配 [AUTO-TRANSLATED:7e30f0b4]
|
|
|
|
|
// Suffix does not match
|
2020-03-20 11:22:05 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 该文件匹配 [AUTO-TRANSLATED:9dce5098]
|
|
|
|
|
// File matches
|
2020-03-20 11:22:05 +08:00
|
|
|
|
process_file(path.data());
|
|
|
|
|
return true;
|
|
|
|
|
}, true);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|