您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

6 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <memory.h>
  11. #include <set>
  12. #include <deque>
  13. #include "Util/CMD.h"
  14. #include "Util/util.h"
  15. #include "Util/logger.h"
  16. #include "Util/File.h"
  17. using namespace std;
  18. using namespace toolkit;
  19. class CMD_main : public CMD {
  20. public:
  21. CMD_main() {
  22. _parser.reset(new OptionParser(nullptr));
  23. (*_parser) << Option('f',/*该选项简称,如果是\x00则说明无简称*/
  24. "filter",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  25. Option::ArgRequired,/*该选项后面必须跟值*/
  26. "c,cpp,cxx,c,h,hpp",/*该选项默认值*/
  27. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  28. "文件后缀过滤器",/*该选项说明文字*/
  29. nullptr);
  30. (*_parser) << Option('i',/*该选项简称,如果是\x00则说明无简称*/
  31. "in",/*该选项全称,每个选项必须有全称;不得为null或空字符串*/
  32. Option::ArgRequired,/*该选项后面必须跟值*/
  33. nullptr,/*该选项默认值*/
  34. true,/*该选项是否必须赋值,如果没有默认值且为ArgRequired时用户必须提供该参数否则将抛异常*/
  35. "文件夹或文件",/*该选项说明文字*/
  36. nullptr);
  37. }
  38. virtual ~CMD_main() {}
  39. };
  40. vector<string> split(const string& s, const char *delim) {
  41. vector<string> ret;
  42. size_t last = 0;
  43. auto index = s.find(delim, last);
  44. while (index != string::npos) {
  45. if (index - last >= 0) {
  46. ret.push_back(s.substr(last, index - last));
  47. }
  48. last = index + strlen(delim);
  49. index = s.find(delim, last);
  50. }
  51. if (!s.size() || s.size() - last >= 0) {
  52. ret.push_back(s.substr(last));
  53. }
  54. return ret;
  55. }
  56. void process_file(const char *file) {
  57. auto str = File::loadFile(file);
  58. if (str.empty()) {
  59. return;
  60. }
  61. auto lines = ::split(str, "\n");
  62. deque<string> lines_copy;
  63. for (auto &line : lines) {
  64. if(line.empty()){
  65. lines_copy.push_back("");
  66. continue;
  67. }
  68. string line_copy;
  69. bool flag = false;
  70. int i = 0;
  71. for (auto &ch : line) {
  72. ++i;
  73. switch (ch) {
  74. case '\t' :
  75. line_copy.append(" ");
  76. break;
  77. case ' ':
  78. line_copy.push_back(ch);
  79. break;
  80. default:
  81. line_copy.push_back(ch);
  82. flag = true;
  83. break;
  84. }
  85. if (flag) {
  86. line_copy.append(line.substr(i));
  87. break;
  88. }
  89. }
  90. lines_copy.push_back(line_copy);
  91. }
  92. str.clear();
  93. for (auto &line : lines_copy) {
  94. str.append(line);
  95. str.push_back('\n');
  96. }
  97. if(!lines_copy.empty()){
  98. str.pop_back();
  99. }
  100. File::saveFile(str, file);
  101. }
  102. /// 这个程序是为了统一替换tab为4个空格
  103. int main(int argc, char *argv[]) {
  104. CMD_main cmd_main;
  105. try {
  106. cmd_main.operator()(argc, argv);
  107. } catch (std::exception &ex) {
  108. cout << ex.what() << endl;
  109. return -1;
  110. }
  111. string path = cmd_main["in"];
  112. string filter = cmd_main["filter"];
  113. auto vec = ::split(filter, ",");
  114. set<string> filter_set;
  115. for (auto ext : vec) {
  116. filter_set.emplace(ext);
  117. }
  118. bool no_filter = filter_set.find("*") != filter_set.end();
  119. //设置日志
  120. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  121. File::scanDir(path, [&](const string &path, bool isDir) {
  122. if (isDir) {
  123. return true;
  124. }
  125. if (!no_filter) {
  126. //开启了过滤器
  127. auto pos = strstr(path.data(), ".");
  128. if (pos == nullptr) {
  129. //没有后缀
  130. return true;
  131. }
  132. auto ext = pos + 1;
  133. if (filter_set.find(ext) == filter_set.end()) {
  134. //后缀不匹配
  135. return true;
  136. }
  137. }
  138. //该文件匹配
  139. process_file(path.data());
  140. return true;
  141. }, true);
  142. return 0;
  143. }