Ignore:
Timestamp:
Dec 9, 2014, 8:08:34 AM (5 years ago)
Author:
cameron
Message:

-e -f options for multiple regular expressions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4327 r4328  
    66
    77#include <string>
     8#include <iostream>
     9#include <fstream>
    810
    911#include "icgrep.h"
     
    1618
    1719int main(int argc, char *argv[]) {
    18     static cl::opt<std::string> regexp1(cl::Positional, cl::Required, cl::desc("<regexp>"));
    19     static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
     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);
    2022   
    21     static cl::opt<bool>CountOnly("c", cl::desc("Count and display the matching lines per file only."));
     23    cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."));
    2224    cl::alias CountOnlyLong("-count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
    2325   
    24     static cl::opt<bool>ShowFileNames("H", cl::desc("Show the file name with each matching line."));
     26    cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."));
    2527    cl::alias ShowFileNamesLong("-with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
    2628   
    27     static cl::opt<bool>ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
     29    cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
    2830    cl::alias ShowLineNumbersLong("-line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
    2931   
    30     //cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("<filename>"));
     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(""));
    3135 
    3236    // Does -b mean the byte offset of the line, or the byte offset of the match start within the line?
     
    3741
    3842    cl::ParseCommandLineOptions(argc, argv);
    39 
    40     const auto llvm_codegen = icgrep::compile(encoding, regexp1, false, false);
     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);
    4168
    4269    if (llvm_codegen.process_block_fptr != 0) {
     
    4673        grepEngine.setCountOnlyOption(CountOnly);
    4774        grepEngine.setShowLineNumberOption(ShowLineNumbers);
    48         if (inputFiles.size() > 1 || ShowFileNames) grepEngine.setShowFileNameOption();
    49         for (unsigned i = 0; i != inputFiles.size(); ++i) {
     75            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
     76            grepEngine.setShowFileNameOption();
     77        }
     78        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
    5079            grepEngine.doGrep(inputFiles[i].c_str());
    5180        }
Note: See TracChangeset for help on using the changeset viewer.