Example #1
0
bool
Filesystem::get_directory_entries(const std::string& dirname,
                                  std::vector<std::string>& filenames,
                                  bool recursive,
                                  const std::string& filter_regex)
{
    filenames.clear();
    if (dirname.size() && !is_directory(dirname))
        return false;
    filesystem::path dirpath(dirname.size() ? u8path(dirname)
                                            : filesystem::path("."));
    regex re;
    try {
        re = regex(filter_regex);
    } catch (...) {
        return false;
    }

    if (recursive) {
        for (filesystem::recursive_directory_iterator s(dirpath);
             s != filesystem::recursive_directory_iterator(); ++s) {
            std::string file = pathstr(s->path());
            if (!filter_regex.size() || regex_search(file, re))
                filenames.push_back(file);
        }
    } else {
        for (filesystem::directory_iterator s(dirpath);
             s != filesystem::directory_iterator(); ++s) {
            std::string file = pathstr(s->path());
            if (!filter_regex.size() || regex_search(file, re))
                filenames.push_back(file);
        }
    }
    return true;
}
SvmParams::SvmParams(const std::string &filename) throw (IOException) {

    static regex coefficients("betas: ([0-9 .\\-]+)");
    static regex means("means: ([0-9 .\\-]+)");
    static regex sigmas("sigmas: ([0-9 .\\-]+)");
    static regex bias("bias: ([0-9.\\-]+)");

    ifstream file(filename);
    string current_line;
    cmatch matches;

    if (file.is_open()) {
        do {
            getline(file, current_line);

            if (regex_search(current_line.c_str(), matches, coefficients)) {
                this->coefficients = tokenize(matches[1]);
            } else if (regex_search(current_line.c_str(), matches, means)) {
                this->means = tokenize(matches[1]);
            } else if (regex_search(current_line.c_str(), matches, sigmas)) {
                this->sigmas = tokenize(matches[1]);
            } else if (regex_search(current_line.c_str(), matches, bias)) {
                this->bias = stod(matches[1]);
            }
        } while (current_line.length() > 0);
    } else {
        throw IOException("Couldn't open SVM parameters file.");
    }

    file.close();

}
Example #3
0
    CStrRegExprFindInstance(const regex * _regEx, const char * _str, size32_t _from, size32_t _len, bool _keep)
        : regEx(_regEx)
    {
        matched = false;
        sample = NULL;
        try
        {
            if (_keep)
            {
                sample = (char *)rtlMalloc(_len + 1);  //required for findstr
                memcpy(sample, _str + _from, _len);
                sample[_len] = (char)NULL;
                matched = regex_search(sample, subs, *regEx);
            }
            else
            {
                matched = regex_search(_str + _from, _str + _len, subs, *regEx);
            }
        }
        catch (const std::runtime_error & e)
        {
            std::string msg = "Error in regex search: ";
            msg += e.what();
#if defined(_USE_BOOST_REGEX)
            msg += "(regex: ";
            msg += regEx->str();
            msg += ")";
#endif
            rtlFail(0, msg.c_str());
        }

    }
Example #4
0
File: Uri.cpp Project: jpgr87/dart
//==============================================================================
bool Uri::fromStringOrPath(const std::string& _input)
{
  // TODO(JS): Need to check if _input is an "absolute" path?

#ifdef _WIN32

  // Assume that any URI begin with pattern [SINGLE_LETTER]:[/ or \\] is an
  // absolute path.
  static regex windowsPathRegex(R"END([a-zA-Z]:[/|\\])END");
  bool isPath = regex_search(_input, windowsPathRegex, match_continuous);

  if (isPath)
    return fromPath(_input);

#else

  // Assume that any URI without a scheme is a path.
  static regex uriSchemeRegex(R"END(^(([^:/?#]+):))END");
  bool noScheme = !regex_search(_input, uriSchemeRegex, match_continuous);

  if (noScheme)
    return fromPath(_input);

#endif

  return fromString(_input);
}
Example #5
0
std::string MedicManager::handleMessage(Message* message, std::string regexPattern)
{
    // Read the message out of the packet.
    BString tmp;
    message->getStringUnicode16(tmp);

    // If the string has no length the message is ill-formatted, send the
    // proper format to the client.
    if (!tmp.getLength())
        return "";

    // Convert the string to an ansi string for ease with the regex.
    tmp.convert(BSTRType_ANSI);
    std::string input_string(tmp.getAnsi());

    static const regex pattern(regexPattern);
    smatch result;

    regex_search(input_string, result, pattern);

    // Gather the results of the pattern for validation and use.
    std::string messageType(result[1]);
    if (messageType.length() > 0)
    {
        return messageType;
    }
    return "";
}
Example #6
0
bool CForbiddenPro::parse_condition_regex(const string& query, const vector<string>& condition_regex)const {

    for (size_t i = 0; i < condition_regex.size(); i++) {
        boost::regex regex(condition_regex[i].c_str());
        if (regex_search(query, regex)) {
            return true;
        }
    }
    return false;
}
Example #7
0
void CrowdinClient::HandleOAuthCallback(const std::string& uri)
{
    if (!m_authCallback)
        return;

    const regex re("access_token=([^&]+)&");
    smatch m;
    if (!regex_search(uri, m, re))
        return;

    SaveAndSetToken(m.str(1));

    m_authCallback();
    m_authCallback = nullptr;
}
// Reads from fin and returns a std::set with all the found authors, using the
// collection of patterns to find the authors in the stream.
template<typename T> set<string, custom_string_cmp>
get_authors(FILE* fin, const T& collection) {
  set<string, custom_string_cmp> authors;
  char buffer[BUFSIZ];
  while (fgets(buffer, sizeof(buffer), fin)) {
    for (const auto& regex_ : collection) {
      cmatch match;
      if (regex_search(buffer, match, regex_)) {
        string author_name = match.str(1);
        authors.emplace(*strip_in_place(&author_name, " \n\r\t."));
        break;  // Go to next line on first match
      }
    }
  }
  return authors;
}
Example #9
0
bool CForbiddenPro::parse_condition_regex(const string& query, 
        const vector<string>& condition_regex, string& forbidden_word)const {

	std::string tmp_query = query;
	// toUpper
	std::transform(tmp_query.begin(), tmp_query.end(), tmp_query.begin(), ::toupper);

    for (size_t i = 0; i < condition_regex.size(); i++) {
        boost::regex regex(condition_regex[i].c_str());
        if (regex_search(tmp_query, regex)) {
            forbidden_word = condition_regex[i];
            return true;
        }
    }
    return false;
}
Example #10
0
int main(int argc, char** argv)
{
	Configuration configuration = Configuration(argc, argv);
	
	vector<string>* lines = getLines(*configuration.filename);
	
	// run test
	
	string patternString;
	if(configuration.withOrOperator)
	{
		patternString = "\\d+|\\w+\\d+\\.";
	}
	else
	{
		patternString = "\\d+";
	}
	
	regex pattern(patternString);
	
	int countMatches = 0;
	if(configuration.dryRun)
	{
		for(vector<string>::const_iterator iterator = lines->begin(); iterator != lines->end(); iterator++);
	}
	else
	{
		for(vector<string>::const_iterator iterator = lines->begin(); iterator != lines->end(); iterator++)
		{
			if(regex_search(*iterator, pattern))
			{
				countMatches++;
			}
		}
	}
	
	//cout << countMatches << endl;
	
	delete lines;
	
	//cout << "done" << endl;
	return 0;
}
Example #11
0
void printMatches(const string & textToSearch, const string & regularExpression) {
	using std::endl;
	using std::cout;
	using boost::regex;
	using boost::regex_search;
	using boost::smatch;

	cout << endl << "Looking for " << regularExpression << " in " << textToSearch << endl;

	string::const_iterator start = textToSearch.begin();
	string::const_iterator end = textToSearch.end();
	regex expression(regularExpression);
	smatch result;

	cout << "\tMatched: ";
	while(regex_search(start,end,result,expression)) {
		cout << result << " ";
		start = result[0].second;
	}
	cout << endl;
}
Example #12
0
/**
Is Paraview at this location.
@return TRUE if determined to be present.
*/
bool isParaviewHere(const QString& location)
{
  using boost::regex;
  using boost::regex_search;

  bool found = false;
  if(!location.isEmpty())
  {
    QDirIterator it(location, QDirIterator::NoIteratorFlags);
    while (it.hasNext())
    {
      it.next();
      QString file =it.fileName();
      regex expression("^(paraview.exe)", boost::regex::icase);
      if(regex_search(file.toStdString(), expression) && it.fileInfo().isFile())
      {
        found = true;
        break;
      }
    }
  }
  return found;
}
Example #13
0
GncNumeric::GncNumeric(const std::string& str, bool autoround)
{
    static const std::string numer_frag("(-?[0-9]+)");
    static const std::string denom_frag("([0-9]+)");
    static const std::string hex_frag("(0x[a-f0-9]+)");
    static const std::string slash( "[ \\t]*/[ \\t]*");
    /* The llvm standard C++ library refused to recognize the - in the
     * numer_frag patter with the default ECMAScript syntax so we use the awk
     * syntax.
     */
    static const regex numeral(numer_frag);
    static const regex hex(hex_frag);
    static const regex numeral_rational(numer_frag + slash + denom_frag);
    static const regex hex_rational(hex_frag + slash + hex_frag);
    static const regex hex_over_num(hex_frag + slash + denom_frag);
    static const regex num_over_hex(numer_frag + slash + hex_frag);
    static const regex decimal(numer_frag + "[.,]" + denom_frag);
    smatch m;
/* The order of testing the regexes is from the more restrictve to the less
 * restrictive, as less-restrictive ones will match patterns that would also
 * match the more-restrictive and so invoke the wrong construction.
 */
    if (str.empty())
        throw std::invalid_argument("Can't construct a GncNumeric from an empty string.");
    if (regex_search(str, m, hex_rational))
    {
        GncNumeric n(stoll(m[1].str(), nullptr, 16),
                     stoll(m[2].str(), nullptr, 16));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    if (regex_search(str, m, hex_over_num))
    {
        GncNumeric n(stoll(m[1].str(), nullptr, 16),
                     stoll(m[2].str()));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    if (regex_search(str, m, num_over_hex))
    {
        GncNumeric n(stoll(m[1].str()),
                     stoll(m[2].str(), nullptr, 16));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    if (regex_search(str, m, numeral_rational))
    {
        GncNumeric n(stoll(m[1].str()), stoll(m[2].str()));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    if (regex_search(str, m, decimal))
    {
        GncInt128 high(stoll(m[1].str()));
        GncInt128 low(stoll(m[2].str()));
        int64_t d = powten(m[2].str().length());
        GncInt128 n = high * d + (high > 0 ? low : -low);
        if (!autoround && n.isBig())
        {
            std::ostringstream errmsg;
            errmsg << "Decimal string " << m[1].str() << "." << m[2].str()
                   << "can't be represented in a GncNumeric without rounding.";
            throw std::overflow_error(errmsg.str());
        }
        while (n.isBig() && d > 0)
        {
            n >>= 1;
            d >>= 1;
        }
        if (n.isBig()) //Shouldn't happen, of course
        {
            std::ostringstream errmsg;
            errmsg << "Decimal string " << m[1].str() << "." << m[2].str()
            << " can't be represented in a GncNumeric, even after reducing denom to " << d;
            throw std::overflow_error(errmsg.str());
        }
        GncNumeric gncn(static_cast<int64_t>(n), d);
        m_num = gncn.num();
        m_den = gncn.denom();
        return;
    }
    if (regex_search(str, m, hex))
    {
        GncNumeric n(stoll(m[1].str(), nullptr, 16),INT64_C(1));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    if (regex_search(str, m, numeral))
    {
        GncNumeric n(stoll(m[1].str()), INT64_C(1));
        m_num = n.num();
        m_den = n.denom();
        return;
    }
    std::ostringstream errmsg;
    errmsg << "String " << str << " contains no recognizable numeric value.";
    throw std::invalid_argument(errmsg.str());
}
Example #14
0
bool
Filesystem::parse_pattern(const char* pattern_, int framepadding_override,
                          std::string& normalized_pattern,
                          std::string& framespec)
{
    std::string pattern(pattern_);

    // The pattern is either a range (e.g., "1-15#"), a
    // set of hash marks (e.g. "####"), or a printf-style format
    // string (e.g. "%04d").
#define ONERANGE_SPEC "[0-9]+(-[0-9]+((x|y)-?[0-9]+)?)?"
#define MANYRANGE_SPEC ONERANGE_SPEC "(," ONERANGE_SPEC ")*"
#define SEQUENCE_SPEC                                                          \
    "(" MANYRANGE_SPEC ")?"                                                    \
    "((#|@)+|(%[0-9]*d))"
    static regex sequence_re(SEQUENCE_SPEC);
    // std::cout << "pattern >" << (SEQUENCE_SPEC) << "<\n";
    match_results<std::string::const_iterator> range_match;
    if (!regex_search(pattern, range_match, sequence_re)) {
        // Not a range
        static regex all_views_re("%[Vv]");
        if (regex_search(pattern, all_views_re)) {
            normalized_pattern = pattern;
            return true;
        }

        return false;
    }

    // It's a range. Generate the names by iterating through the numbers.
    std::string thematch(range_match[0].first, range_match[0].second);
    std::string thesequence(range_match[1].first, range_match[1].second);
    std::string thehashes(range_match[9].first, range_match[9].second);
    std::string theformat(range_match[11].first, range_match[11].second);
    std::string prefix(range_match.prefix().first, range_match.prefix().second);
    std::string suffix(range_match.suffix().first, range_match.suffix().second);

    // std::cout << "theformat: " << theformat << "\n";

    std::string fmt;
    if (theformat.length() > 0) {
        fmt = theformat;
    } else {
        // Compute the amount of padding desired
        int padding = 0;
        for (int i = (int)thematch.length() - 1; i >= 0; --i) {
            if (thematch[i] == '#')
                padding += 4;
            else if (thematch[i] == '@')
                padding += 1;
        }
        if (framepadding_override > 0)
            padding = framepadding_override;
        fmt = Strutil::sprintf("%%0%dd", padding);
    }

    // std::cout << "Format: '" << fmt << "'\n";

    normalized_pattern = prefix + fmt + suffix;
    framespec          = thesequence;

    return true;
}
Example #15
0
////////////////////////////////////////////////////////////////////////////////
// 'isAbbreviation' returns true if the string ends on an
// abbreviation, false otherwise.
////////////////////////////////////////////////////////////////////////////////
bool
isAbbreviation(const StringXML& line) {
  const StringXML abbreviations=
  "(\\d+[-\\.:]+(\\d+[-\\.:]+)+$)|"
   "(\\.\\.+$)|"
   "(_\\s*$)|"
   "(\\w+\\.+(\\w+\\.+)+$)|"
   "(A\\.$)|"
   "(Adm\\.$)|"
   "(Ark\\.$)|"
   "(Apr\\.$)|"
   "(Aug\\.$)|"
   "(B\\.$)|"
   "(C\\.$)|"
   "(Calif\\.$)|"
   "(Capt\\.$)|"
   "(Cmdr\\.$)|"
   "(Co\\.$)|"
   "(Conn\\.$)|"
   "(Cpl\\.$)|"
   "(D\\.$)|"
   "(Dec\\.$)|"
   "(Dr\\.$)|"
   "(E\\.$)|"
   "(F\\.$)|"
   "(F\\.W\\.$)|"
   "(Feb\\.$)|"
   "(Fla\\.$)|"
   "(G\\.$)|"
   "(Ga\\.$)|"
   "(Gen\\.$)|"
   "(Gov\\.$)|"
   "(H\\.$)|"
   "(I\\.$)|"
   "(Iraq-U\\.N\\.$)|"
   "(Inc\\.$)|"
   "(J\\.$)|"
   "(Jan\\.$)|"
   "(Jun\\.$)|"
   "(Jul\\.$)|"
   "(Jr\\.$)|"
   "(K\\.$)|"
   "(Ky\\.$)|"
   "(L\\.$)|"
   "(La\\.$)|"
   "(Lt\\.$)|"
   "(Ltd\\.$)|"
   "(m\\.$)|"
   "(M\\.$)|"
   "(Maj\\.$)|"
   "(Mar\\.$)|"
   "(May\\.$)|"
   "(Md\\.$)|"
   "(Mo\\.$)|"
   "(Mr\\.$)|"
   "(Ms\\.$)|"
   "(Mt\\.$)|"
   "(N\\.$)|"
   "(Neb\\.$)|"
   "(Nev\\.$)|"
   "(No\\.$)|"
   "(\\(No\\.$)|"
   "(Nov\\.$)|"
   "(O\\.$)|"
   "(Oct\\.$)|"
   "(Ore\\.$)|"
   "(P\\.$)|"
   "(Pa\\.$)|"
   "(Q\\.$)|"
   "(R\\.$)|"
   "(Rep\\.$)|"
   "(Rev\\.$)|"
   "(R.I\\.$)|"
   "(S\\.$)|"
   "(SA\\.$)|"
   "(Sen\\.$)|"
   "(Sep\\.$)|"
   "(Sgt\\.$)|"
   "(St\\.$)|"
   "(ST\\.$)|"
   "(T\\.$)|"
   "(Tenn\\.$)|"
   "(U\\.$)|"
   "(V\\.$)|"
   "(Va\\.$)|"
   "(vs\\.$)|"
   "(W\\.$)|"
   "(X\\.$)|"
   "(Y\\.$)|"
   "(Z\\.$)";
  static const regex abbrRegex(abbreviations);
  return regex_search(line, abbrRegex);
}
Example #16
0
bool
Filesystem::scan_for_matching_filenames(const std::string& pattern,
                                        const std::vector<string_view>& views,
                                        std::vector<int>& frame_numbers,
                                        std::vector<string_view>& frame_views,
                                        std::vector<std::string>& filenames)
{
    static regex format_re("%0([0-9]+)d");
    static regex all_views_re("%[Vv]"), view_re("%V"), short_view_re("%v");

    frame_numbers.clear();
    frame_views.clear();
    filenames.clear();
    if (regex_search(pattern, all_views_re)) {
        if (regex_search(pattern, format_re)) {
            // case 1: pattern has format and view
            std::vector<std::pair<std::pair<int, string_view>, std::string>>
                matches;
            for (const auto& view : views) {
                if (view.empty())
                    continue;

                const string_view short_view = view.substr(0, 1);
                std::vector<int> view_numbers;
                std::vector<std::string> view_filenames;

                std::string view_pattern = pattern;
                view_pattern = Strutil::replace(view_pattern, "%V", view, true);
                view_pattern = Strutil::replace(view_pattern, "%v", short_view,
                                                true);

                if (!scan_for_matching_filenames(view_pattern, view_numbers,
                                                 view_filenames))
                    continue;

                for (int j = 0, f = view_numbers.size(); j < f; ++j) {
                    matches.push_back(
                        std::make_pair(std::make_pair(view_numbers[j], view),
                                       view_filenames[j]));
                }
            }

            std::sort(matches.begin(), matches.end());

            for (auto& m : matches) {
                frame_numbers.push_back(m.first.first);
                frame_views.push_back(m.first.second);
                filenames.push_back(m.second);
            }

        } else {
            // case 2: pattern has view, but no format
            std::vector<std::pair<string_view, std::string>> matches;
            for (const auto& view : views) {
                const string_view short_view = view.substr(0, 1);
                std::string view_pattern     = pattern;
                view_pattern = Strutil::replace(view_pattern, "%V", view, true);
                view_pattern = Strutil::replace(view_pattern, "%v", short_view,
                                                true);
                if (exists(view_pattern))
                    matches.push_back(std::make_pair(view, view_pattern));
            }

            std::sort(matches.begin(), matches.end());
            for (auto& m : matches) {
                frame_views.push_back(m.first);
                filenames.push_back(m.second);
            }
        }
        return true;

    } else {
        // case 3: pattern has format, but no view
        return scan_for_matching_filenames(pattern, frame_numbers, filenames);
    }

    return true;
}
Example #17
0
bool
Filesystem::scan_for_matching_filenames(const std::string& pattern_,
                                        std::vector<int>& numbers,
                                        std::vector<std::string>& filenames)
{
    numbers.clear();
    filenames.clear();
    std::string pattern = pattern_;
    // Isolate the directory name (or '.' if none was specified)
    std::string directory = Filesystem::parent_path(pattern);
    if (directory.size() == 0) {
        directory = ".";
#ifdef _WIN32
        pattern = ".\\\\" + pattern;
#else
        pattern = "./" + pattern;
#endif
    }

    if (!exists(directory))
        return false;

    // build a regex that matches the pattern
    static regex format_re("%0([0-9]+)d");
    match_results<std::string::const_iterator> format_match;
    if (!regex_search(pattern, format_match, format_re))
        return false;

    std::string thepadding(format_match[1].first, format_match[1].second);
    std::string prefix(format_match.prefix().first,
                       format_match.prefix().second);
    std::string suffix(format_match.suffix().first,
                       format_match.suffix().second);

    std::string pattern_re_str = prefix + "([0-9]{" + thepadding + ",})"
                                 + suffix;
    std::vector<std::pair<int, std::string>> matches;

    // There are some corner cases regex that could be constructed here that
    // are badly structured and might throw an exception.
    try {
        regex pattern_re(pattern_re_str);

        filesystem::directory_iterator end_it;
        for (filesystem::directory_iterator it(u8path(directory)); it != end_it;
             ++it) {
            const std::string f = pathstr(it->path());
            if (is_regular(f)) {
                match_results<std::string::const_iterator> frame_match;
                if (regex_match(f, frame_match, pattern_re)) {
                    std::string thenumber(frame_match[1].first,
                                          frame_match[1].second);
                    int frame = Strutil::stoi(thenumber);
                    matches.push_back(std::make_pair(frame, f));
                }
            }
        }

    } catch (...) {
        // Botched regex. Just fail.
        return false;
    }

    // filesystem order is undefined, so return sorted sequences
    std::sort(matches.begin(), matches.end());

    for (auto& m : matches) {
        numbers.push_back(m.first);
        filenames.push_back(m.second);
    }

    return true;
}
Example #18
0
static bool
grep_file(const std::string& filename, regex& re,
          bool ignore_nonimage_files = false)
{
    if (!Filesystem::exists(filename)) {
        std::cerr << "igrep: " << filename << ": No such file or directory\n";
        return false;
    }

    if (Filesystem::is_directory(filename)) {
        if (!recursive)
            return false;
        if (print_dirs) {
            std::cout << "(" << filename << "/)\n";
            std::cout.flush();
        }
        bool r = false;
        std::vector<std::string> directory_entries;
        Filesystem::get_directory_entries(filename, directory_entries);
        for (const auto& d : directory_entries)
            r |= grep_file(d, re, true);
        return r;
    }

    auto in = ImageInput::open(filename);
    if (!in.get()) {
        if (!ignore_nonimage_files)
            std::cerr << geterror() << "\n";
        return false;
    }
    ImageSpec spec = in->spec();

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

    bool found   = false;
    int subimage = 0;
    do {
        if (!all_subimages && subimage > 0)
            break;
        for (auto&& 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 = 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";
                    }
                }
            }
        }
    } while (in->seek_subimage(++subimage, 0, spec));

    if (invert_match) {
        found = !found;
        if (found)
            std::cout << filename << "\n";
    }
    return found;
}