<commit_before>/*
  Copyright 2008 <NAME> and the other authors and contributors.
  All Rights Reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the software's owners nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  (This is the Modified BSD License)
*/


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <iterator>

#include <boost/scoped_ptr.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

#include "argparse.h"
#include "strutil.h"
#include "imageio.h"
using namespace OpenImageIO;


static bool help = false;
static bool invert_match = false;
static bool ignore_case = false;
static bool list_files = false;
static bool recursive = false;
static bool file_match = false;
static bool print_dirs = false;
static std::string pattern;
static std::vector<std::string> filenames;



static bool
grep_file (const std::string &filename, boost::regex &re,
           bool ignore_nonimage_files=false)
{
    boost::filesystem::path path (filename);
    if (! boost::filesystem::exists (path)) {
        std::cerr << "igrep: " << filename << ": No such file or directory\n";
        return false;
    }

    if (boost::filesystem::is_directory (path)) {
        if (! recursive)
            return false;
        if (print_dirs) {
            std::cout << "(" << path << "/)\n";
            std::cout.flush();
        }
        bool r = false;
        boost::filesystem::directory_iterator end_itr;  // default is past-end
        for (boost::filesystem::directory_iterator itr(path);  itr != end_itr;  ++itr) {
            // std::cout << "  rec " << itr->path() << "\n";
            r |= grep_file (itr->path().string(), re, true);
        }
        return r;
    }

    boost::scoped_ptr<ImageInput> in (ImageInput::create (filename.c_str()));
    if (in.get()) {
        in->close ();
    } else {
        if (! ignore_nonimage_files)
            std::cerr << OpenImageIO::error_message() << "\n";
        return false;
    }
    ImageSpec spec;
    if (! in->open (filename.c_str(), spec)) {
        std::cerr << "igrep: Could not open \"" << filename << "\" : "
                  << in->error_message() << "\n";
        return false;
    }

    if (file_match) {
        bool match = boost::regex_search (filename, re);
        if (match && ! invert_match) {
            std::cout << filename << "\n";
            return true;
        }
    }

    bool found = false;
    BOOST_FOREACH (const ImageIOParameter &p, spec.extra_attribs) {
        TypeDesc t = p.type();
        if (t.elementtype() == TypeDesc::STRING) {
            int n = t.numelements();
            for (int i = 0;  i < n;  ++i) {
                bool match = boost::regex_search (((const char **)p.data())[i], re);
                found |= match;
                if (match && ! invert_match) {
                    if (list_files) {
                        std::cout << filename << "\n";
                        return found;
                    }
                    std::cout << filename << ": " << p.name() << " = " 
                              << ((const char **)p.data())[i] << "\n";
                }
            }
        }
    }
    if (invert_match) {
        found = !found;
        if (found)
            std::cout << filename << "\n";
    }
    return found;
}



static int
parse_files (int argc, const char *argv[])
{
    for (int i = 0;  i < argc;  i++) {
        if (pattern.empty())
            pattern = argv[i];
        else
            filenames.push_back (argv[i]);
    }
    return 0;
}



int
main (int argc, const char *argv[])
{
    ArgParse ap;
    ap.options ("Usage:  igrep [options] pattern filename...",
                "%*", parse_files, "",
                "-i", &ignore_case, "Ignore upper/lower case distinctions",
                "-l", &list_files, "List the matching files (no detail)",
                "-r", &recursive, "Recurse into directories",
                "-v", &invert_match, "Invert match (select non-matching files)",
                "-f", &file_match, "Match against file name as well as metadata",
                "-d", &print_dirs, "Print directories (when recursive)",
                "--help", &help, "Print help message",
                NULL);
    if (ap.parse(argc, argv) < 0 || pattern.empty() || filenames.empty()) {
        std::cerr << ap.error_message() << std::endl;
        ap.usage ();
        return EXIT_FAILURE;
    }
    if (help) {
        ap.usage ();
        exit (EXIT_FAILURE);
    }

    boost::regex_constants::syntax_option_type flag = boost::regex_constants::grep;
    if (ignore_case)<commit_msg>igrep -E uses extended regex.<commit_after>/*
  Copyright 2008 <NAME> and the other authors and contributors.
  All Rights Reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the software's owners nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  (This is the Modified BSD License)
*/


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <iterator>

#include <boost/scoped_ptr.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

#include "argparse.h"
#include "strutil.h"
#include "imageio.h"
using namespace OpenImageIO;


static bool help = false;
static bool invert_match = false;
static bool ignore_case = false;
static bool list_files = false;
static bool recursive = false;
static bool file_match = false;
static bool print_dirs = false;
static bool extended_regex = false;
static std::string pattern;
static std::vector<std::string> filenames;



static bool
grep_file (const std::string &filename, boost::regex &re,
           bool ignore_nonimage_files=false)
{
    boost::filesystem::path path (filename);
    if (! boost::filesystem::exists (path)) {
        std::cerr << "igrep: " << filename << ": No such file or directory\n";
        return false;
    }

    if (boost::filesystem::is_directory (path)) {
        if (! recursive)
            return false;
        if (print_dirs) {
            std::cout << "(" << path << "/)\n";
            std::cout.flush();
        }
        bool r = false;
        boost::filesystem::directory_iterator end_itr;  // default is past-end
        for (boost::filesystem::directory_iterator itr(path);  itr != end_itr;  ++itr) {
            // std::cout << "  rec " << itr->path() << "\n";
            r |= grep_file (itr->path().string(), re, true);
        }
        return r;
    }

    boost::scoped_ptr<ImageInput> in (ImageInput::create (filename.c_str()));
    if (in.get()) {
        in->close ();
    } else {
        if (! ignore_nonimage_files)
            std::cerr << OpenImageIO::error_message() << "\n";
        return false;
    }
    ImageSpec spec;
    if (! in->open (filename.c_str(), spec)) {
        std::cerr << "igrep: Could not open \"" << filename << "\" : "
                  << in->error_message() << "\n";
        return false;
    }

    if (file_match) {
        bool match = boost::regex_search (filename, re);
        if (match && ! invert_match) {
            std::cout << filename << "\n";
            return true;
        }
    }

    bool found = false;
    BOOST_FOREACH (const ImageIOParameter &p, spec.extra_attribs) {
        TypeDesc t = p.type();
        if (t.elementtype() == TypeDesc::STRING) {
            int n = t.numelements();
            for (int i = 0;  i < n;  ++i) {
                bool match = boost::regex_search (((const char **)p.data())[i], re);
                found |= match;
                if (match && ! invert_match) {
                    if (list_files) {
                        std::cout << filename << "\n";
                        return found;
                    }
                    std::cout << filename << ": " << p.name() << " = " 
                              << ((const char **)p.data())[i] << "\n";
                }
            }
        }
    }
    if (invert_match) {
        found = !found;
        if (found)
            std::cout << filename << "\n";
    }
    return found;
}



static int
parse_files (int argc, const char *argv[])
{
    for (int i = 0;  i < argc;  i++) {
        if (pattern.empty())
            pattern = argv[i];
        else
            filenames.push_back (argv[i]);
    }
    return 0;
}



int
main (int argc, const char *argv[])
{
    ArgParse ap;
    ap.options ("Usage:  igrep [options] pattern filename...",
                "%*", parse_files, "",
                "-i", &ignore_case, "Ignore upper/lower case distinctions",
                "-v", &invert_match, "Invert match (select non-matching files)",
                "-E", &extended_regex, "Pattern is an extended regular expression",
                "-f", &file_match, "Match against file name as well as metadata",
                "-l", &list_files, "List the matching files (no detail)",
                "-r", &recursive, "Recurse into directories",
                "-d", &print_dirs, "Print directories (when recursive)",
                "--help", &help, "Print help message",
                NULL);
    if (ap.parse(argc, argv) < 0 || pattern.empty() || filenames.empty()) {
        std::cerr << ap.error_message() << std::endl;
        ap.usage ();
        return EXIT_FAILURE;
    }
    if (help) {
        ap.usage ();
        exit (EXIT_FAILURE);
    }

    boost::regex_constants::syntax_option_type flag = boost::regex_constants::grep;
    if (extended_regex)
        flag = boost::regex::extended;
    if (ignore_case)
{'fraction_non_alphanumeric': 0.12153032153032153, 'fraction_numerical': 0.0013024013024013025, 'mean_word_length': 2.3901766004415013, 'pattern_counts': {'":': 4, '<': 155, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353666', 'n_tokens_mistral': 3614, 'n_tokens_neox': 3422, 'n_words': 1177}