source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 4328

Last change on this file since 4328 was 4328, checked in by cameron, 5 years ago

-e -f options for multiple regular expressions

File size: 3.4 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <string>
8#include <iostream>
9#include <fstream>
10
11#include "icgrep.h"
12#include "utf_encoding.h"
13#include "compiler.h"
14#include "pablo/pablo_compiler.h"
15#include "do_grep.h"
16
17#include "llvm/Support/CommandLine.h"
18
19int main(int argc, char *argv[]) {
20    int firstInputFile = 1;  // Normal case when first positional arg is a regex. 
21    cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
22   
23    cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."));
24    cl::alias CountOnlyLong("-count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
25   
26    cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."));
27    cl::alias ShowFileNamesLong("-with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
28   
29    cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
30    cl::alias ShowLineNumbersLong("-line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
31   
32    cl::list<std::string> regexVector("e", cl::desc("Regular exprssion"), cl::ZeroOrMore);
33   
34    cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""));
35 
36    // Does -b mean the byte offset of the line, or the byte offset of the match start within the line?
37    //static cl::opt<bool>ShowByteOffsets("b", cl::desc("Show the byte offset with each matching line."));
38    //cl::alias ShowByteOffsetsLong("-byte-offset", cl::desc("Alias for -b"), cl::aliasopt(ShowByteOffsets));
39
40    Encoding encoding(Encoding::Type::UTF_8, 8);
41
42    cl::ParseCommandLineOptions(argc, argv);
43   
44    //std::vector<std::string> regexVector;
45    if (RegexFilename != "") {
46        std::ifstream regexFile(RegexFilename.c_str());
47        std::string r;
48        if (regexFile.is_open()) {
49            while (std::getline(regexFile, r)) {
50                regexVector.push_back(r);
51            }
52            regexFile.close();
53        }
54    }
55   
56    // if there are no regexes specified through -e or -f, the first positional argument
57    // must be a regex, not an input file.
58   
59    if (regexVector.size() == 0) {
60        regexVector.push_back(inputFiles[0]);
61        firstInputFile = 1;
62    }
63    else {
64        firstInputFile = 0;
65    }
66   
67    const auto llvm_codegen = icgrep::compile(encoding, regexVector, false);
68
69    if (llvm_codegen.process_block_fptr != 0) {
70        void (*FP)(const Basis_bits &basis_bits, BitBlock carry_q[], BitBlock advance_q[], Output &output) = 
71           (void (*)(const Basis_bits &basis_bits, BitBlock carry_q[], BitBlock advance_q[], Output &output))(void*)llvm_codegen.process_block_fptr;
72        GrepExecutor grepEngine = GrepExecutor(llvm_codegen.carry_q_size, llvm_codegen.advance_q_size, FP);
73        grepEngine.setCountOnlyOption(CountOnly);
74        grepEngine.setShowLineNumberOption(ShowLineNumbers);
75            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
76            grepEngine.setShowFileNameOption();
77        }
78        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
79            grepEngine.doGrep(inputFiles[i].c_str());
80        }
81    }
82   
83    return 0;
84}
85
Note: See TracBrowser for help on using the repository browser.