tab.cpp 4.79 KB
Newer Older
xiongziliang committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Use of this source code is governed by MIT license that can be found in the
 * 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>
xiongziliang committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <set>
#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() {}
};

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
vector<string> split(const string& s, const char *delim) {
    vector<string> ret;
    int last = 0;
    int index = s.find(delim, last);
    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;
}

xiongziliang committed
62 63 64 65 66
void process_file(const char *file) {
    auto str = File::loadFile(file);
    if (str.empty()) {
        return;
    }
67
    auto lines = ::split(str, "\n");
xiongziliang committed
68 69
    deque<string> lines_copy;
    for (auto &line : lines) {
70 71 72 73
        if(line.empty()){
            lines_copy.push_back("");
            continue;
        }
xiongziliang committed
74 75
        string line_copy;
        bool flag = false;
76
        int i = 0;
xiongziliang committed
77
        for (auto &ch : line) {
78
            ++i;
xiongziliang committed
79 80 81 82 83 84 85 86 87 88 89 90 91
            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) {
92
                line_copy.append(line.substr(i));
xiongziliang committed
93 94 95 96 97 98 99 100 101 102
                break;
            }
        }
        lines_copy.push_back(line_copy);
    }
    str.clear();
    for (auto &line : lines_copy) {
        str.append(line);
        str.push_back('\n');
    }
103 104 105
    if(!lines_copy.empty()){
        str.pop_back();
    }
xiongziliang committed
106 107 108
    File::saveFile(str, file);
}

109
/// 这个程序是为了统一替换tab为4个空格
xiongziliang committed
110 111 112 113 114 115 116 117 118 119 120
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"];
121
    auto vec = ::split(filter, ",");
xiongziliang committed
122 123 124 125 126 127 128 129 130

    set<string> filter_set;
    for (auto ext : vec) {
        filter_set.emplace(ext);
    }

    bool no_filter = filter_set.find("*") != filter_set.end();
    //设置日志
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
131 132
    path = File::absolutePath(path, "");
    DebugL << path;
xiongziliang committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    File::scanDir(path, [&](const string &path, bool isDir) {
        if (isDir) {
            return true;
        }
        if (!no_filter) {
            //开启了过滤器
            auto pos = strstr(path.data(), ".");
            if (pos == nullptr) {
                //没有后缀
                return true;
            }
            auto ext = pos + 1;
            if (filter_set.find(ext) == filter_set.end()) {
                //后缀不匹配
                return true;
            }
        }
        //该文件匹配
        process_file(path.data());
        return true;
    }, true);
    return 0;
}