Changeset 5163 for icGREP


Ignore:
Timestamp:
Sep 25, 2016, 9:07:58 PM (3 years ago)
Author:
cameron
Message:

Initial support for -r/-R recursive file processing

File:
1 edited

Legend:

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

    r5161 r5163  
    2121#include <pablo/pablo_toolchain.h>
    2222#include <mutex>
    23 
     23#include <boost/filesystem.hpp>
    2424
    2525#include <iostream> // MEEE
     
    3535static cl::OptionCategory EnhancedGrepOptions("B. Enhanced Grep Options",
    3636                                       "These are additional options for icgrep functionality and performance.");
    37 static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(LegacyGrepOptions));
     37static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(LegacyGrepOptions), cl::Grouping);
    3838static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
    3939
    4040static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
    4141
    42 static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(LegacyGrepOptions));
     42static cl::opt<bool> EnterDirectoriesRecursively("r", cl::desc("Recursively process files within directories, (but follow only top-level symlinks unless -R)."), cl::cat(LegacyGrepOptions), cl::Grouping);
     43static cl::opt<bool> FollowSubdirectorySymlinks("R", cl::desc("Recursively process files within directories, following symlinks at all levels."), cl::cat(LegacyGrepOptions), cl::Grouping);
     44static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(LegacyGrepOptions), cl::Grouping);
    4345
    4446
     
    5254         Gives you colored output + back-referencing capability."), cl::cat(EnhancedGrepOptions));
    5355
     56
     57static std::vector<std::string> allFiles;
    5458//
    5559// Handler for errors reported through llvm::report_fatal_error.  Report
     
    139143    count_mutex.unlock();
    140144
    141     while (fileIdx < inputFiles.size()){
    142         grepEngine->doGrep(inputFiles[fileIdx], fileIdx, CountOnly, total_CountOnly, UTF_16);
     145    while (fileIdx < allFiles.size()){
     146        grepEngine->doGrep(allFiles[fileIdx], fileIdx, CountOnly, total_CountOnly, UTF_16);
    143147       
    144148        count_mutex.lock();
     
    182186// Filters out the command line strings that shouldn't be passed on to IcGrep
    183187bool isArgUnwantedForIcGrep(char *argument) {
    184     bool isUnwated = false;
     188    bool isUnwanted = false;
    185189    std::vector<std::string> unwantedFlags = {"-c"};
    186190
    187191    for (unsigned i = 0; i < unwantedFlags.size(); ++i){
    188192        if (strcmp(argument, unwantedFlags[i].c_str()) == 0) {
    189             isUnwated = true;
    190         }
    191     }
    192 
    193     return isUnwated;
     193            isUnwanted = true;
     194        }
     195    }
     196
     197    return isUnwanted;
    194198}
    195199
     
    234238
    235239
     240// This is a stub, to be expanded later.
     241bool excludeDirectory(boost::filesystem::path dirpath) { return dirpath.filename() == ".svn";}
     242
     243std::vector<std::string> getFullFileList(cl::list<std::string> & inputFiles) {
     244    using namespace boost::filesystem;
     245    symlink_option follow_symlink = FollowSubdirectorySymlinks ? symlink_option::recurse : symlink_option::none;
     246    std::vector<std::string> expanded_paths;
     247    boost::system::error_code errc;
     248    if (FollowSubdirectorySymlinks) {
     249        EnterDirectoriesRecursively = true;
     250    }
     251    for (auto & f : inputFiles) {
     252        path p(f);
     253        if (EnterDirectoriesRecursively && is_directory(p)) {
     254            if (!excludeDirectory(p)) {
     255                recursive_directory_iterator di(p, follow_symlink, errc), end;
     256                if (errc) {
     257                    // If we cannot enter the directory, keep it in the list of files.
     258                    expanded_paths.push_back(f);
     259                    continue;
     260                }
     261                while (di != end) {
     262                    auto & e = di->path();
     263                    if (is_directory(e)) {
     264                        if (excludeDirectory(e)) di.no_push();
     265                    }
     266                    else expanded_paths.push_back(e.string());
     267                    di.increment(errc);
     268                    if (errc) {
     269                        expanded_paths.push_back(e.string());
     270                    }
     271                }
     272            }
     273        }
     274        else expanded_paths.push_back(p.string());
     275    }
     276    return expanded_paths;
     277}
     278
     279
    236280int main(int argc, char *argv[]) {
    237281    llvm::install_fatal_error_handler(&icgrep_error_handler);
     
    252296
    253297    releaseSlabAllocatorMemory();
    254     initResult(inputFiles);
    255     for (unsigned i=0; i<inputFiles.size(); ++i){
     298   
     299    allFiles = getFullFileList(inputFiles);
     300   
     301    initResult(allFiles);
     302    for (unsigned i=0; i < allFiles.size(); ++i){
    256303        total_CountOnly.push_back(0);
    257304    }
     
    264311        papi::PapiCounter<4> papiCounters({PAPI_RES_STL, PAPI_STL_CCY, PAPI_FUL_CCY, PAPI_MEM_WCY});
    265312        #endif
    266         for (unsigned i = 0; i != inputFiles.size(); ++i) {
     313        for (unsigned i = 0; i != allFiles.size(); ++i) {
    267314            #ifdef PRINT_TIMING_INFORMATION
    268315            papiCounters.start();
    269316            const timestamp_t execution_start = read_cycle_counter();
    270317            #endif
    271             grepEngine.doGrep(inputFiles[i], i, CountOnly, total_CountOnly, UTF_16);
     318            grepEngine.doGrep(allFiles[i], i, CountOnly, total_CountOnly, UTF_16);
    272319            #ifdef PRINT_TIMING_INFORMATION
    273320            const timestamp_t execution_end = read_cycle_counter();
    274321            papiCounters.stop();
    275             std::cerr << "EXECUTION TIME: " << inputFiles[i] << ":" << "CYCLES|" << (execution_end - execution_start) << papiCounters << std::endl;
     322            std::cerr << "EXECUTION TIME: " << allFiles[i] << ":" << "CYCLES|" << (execution_end - execution_start) << papiCounters << std::endl;
    276323            #endif
    277324        }       
Note: See TracChangeset for help on using the changeset viewer.