Ignore:
Timestamp:
Apr 5, 2018, 2:39:58 PM (15 months ago)
Author:
cameron
Message:

Common command line file selection utility for icgrep, wc ...

File:
1 edited

Legend:

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

    r5938 r5944  
    2424#include <re/re_toolchain.h>
    2525#include <pablo/pablo_toolchain.h>
    26 #include <boost/filesystem.hpp>
     26#include <util/file_select.h>
    2727#include <sys/stat.h>
    2828#include <fcntl.h>
     
    106106
    107107
    108 // This is a stub, to be expanded later.
    109 bool excludeDirectory(boost::filesystem::path dirpath) { return dirpath.filename() == ".svn";}
    110 
    111 // Determine whether to skip a path based on -D skip or -d skip settings.
    112 bool skip_path(boost::filesystem::path p) {
    113     using namespace boost::filesystem;
    114     switch (status(p).type()) {
    115         case directory_file: return grep::DirectoriesFlag == grep::Skip;
    116         case block_file:
    117         case character_file:
    118         case fifo_file:
    119         case socket_file:
    120             return grep::DevicesFlag == grep::Skip;
    121         default:
    122             return false;
    123     }
    124 }
    125 
    126 std::vector<std::string> getFullFileList(cl::list<std::string> & inputFiles) {
    127     using namespace boost::filesystem;
    128     symlink_option follow_symlink = grep::DereferenceRecursiveFlag ? symlink_option::recurse : symlink_option::none;
    129     std::vector<std::string> expanded_paths;
    130     boost::system::error_code errc;
    131     for (const std::string & f : inputFiles) {
    132         //        if (f == "-") {
    133         //            continue;
    134         //        }
    135         path p(f);
    136         if (skip_path(p)) {
    137             continue;
    138         }
    139         if (LLVM_UNLIKELY((grep::DirectoriesFlag == grep::Recurse) && is_directory(p))) {
    140             if (!excludeDirectory(p)) {
    141                 recursive_directory_iterator di(p, follow_symlink, errc), end;
    142                 if (errc) {
    143                     // If we cannot enter the directory, keep it in the list of files.
    144                     expanded_paths.push_back(f);
    145                     continue;
    146                 }
    147                 while (di != end) {
    148                     auto & e = di->path();
    149                     if (is_directory(e)) {
    150                         if (LLVM_UNLIKELY(excludeDirectory(e))) {
    151                             di.no_push();
    152                         }
    153                     } else {
    154                         if (!skip_path(e)) expanded_paths.push_back(e.string());
    155                     }
    156                     di.increment(errc);
    157                     if (errc) {
    158                         expanded_paths.push_back(e.string());
    159                     }
    160                 }
    161             }
    162         } else {
    163             expanded_paths.push_back(p.string());
    164         }
    165     }
    166     return expanded_paths;
    167 }
    168 
    169108int main(int argc, char *argv[]) {
    170109
     
    173112    auto REs = readExpressions();
    174113
    175     std::vector<std::string> allFiles = getFullFileList(inputFiles);
     114    std::vector<std::string> allFiles = argv::getFullFileList(inputFiles);
    176115    if (allFiles.empty()) {
    177116        allFiles = { "-" };
Note: See TracChangeset for help on using the changeset viewer.