bool grep_file(const char *file, const char *regex, ...) { bool done, found; int fd; va_list ap; atf_dynstr_t formatted; va_start(ap, regex); RE(atf_dynstr_init_ap(&formatted, regex, ap)); va_end(ap); done = false; found = false; ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1); do { atf_dynstr_t line; RE(atf_dynstr_init(&line)); done = read_line(fd, &line); if (!done) found = grep_string(&line, atf_dynstr_cstring(&formatted)); atf_dynstr_fini(&line); } while (!found && !done); close(fd); atf_dynstr_fini(&formatted); return found; }
/** Searches for a regexp in a file. * * \param regex The regexp to look for. * \param file The file in which to look for the expression. * \param ... Positional parameters to the regex. * * \return True if there is a match; false otherwise. */ bool atf_utils_grep_file(const char *regex, const char *file, ...) { int fd; va_list ap; atf_dynstr_t formatted; atf_error_t error; va_start(ap, file); error = atf_dynstr_init_ap(&formatted, regex, ap); va_end(ap); ATF_REQUIRE(!atf_is_error(error)); ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1); bool found = false; char *line = NULL; while (!found && (line = atf_utils_readline(fd)) != NULL) { found = grep_string(atf_dynstr_cstring(&formatted), line); free(line); } close(fd); atf_dynstr_fini(&formatted); return found; }
bool grep_collection(const std::string& regexp, const Collection& collection) { for (typename Collection::const_iterator iter = collection.begin(); iter != collection.end(); ++iter) { if (grep_string(regexp, *iter)) return true; } return false; }
static void capture_stream_fini (void *v) { struct capture_stream *s = v; switch (s->m_base.m_type) { case stdout_type: ATF_CHECK (grep_string (&s->m_msg, "stdout: msg")); ATF_CHECK (!grep_string (&s->m_msg, "stderr: msg")); break; case stderr_type: ATF_CHECK (!grep_string (&s->m_msg, "stdout: msg")); ATF_CHECK (grep_string (&s->m_msg, "stderr: msg")); break; default: UNREACHABLE; } atf_dynstr_fini (&s->m_msg); atf_process_stream_fini (&s->m_base.m_sb); }
ATF_TC_BODY(grep_string, tc) { atf_dynstr_t str; atf_dynstr_init_fmt(&str, "a string - aaaabbbb"); ATF_CHECK(grep_string(&str, "a string")); ATF_CHECK(grep_string(&str, "^a string")); ATF_CHECK(grep_string(&str, "aaaabbbb$")); ATF_CHECK(grep_string(&str, "aa.*bb")); ATF_CHECK(!grep_string(&str, "foo")); ATF_CHECK(!grep_string(&str, "bar")); ATF_CHECK(!grep_string(&str, "aaaaa")); atf_dynstr_fini(&str); }
/** Searches for a regexp in a string. * * \param regex The regexp to look for. * \param str The string in which to look for the expression. * \param ... Positional parameters to the regex. * * \return True if there is a match; false otherwise. */ bool atf_utils_grep_string(const char *regex, const char *str, ...) { bool res; va_list ap; atf_dynstr_t formatted; atf_error_t error; va_start(ap, str); error = atf_dynstr_init_ap(&formatted, regex, ap); va_end(ap); ATF_REQUIRE(!atf_is_error(error)); res = grep_string(atf_dynstr_cstring(&formatted), str); atf_dynstr_fini(&formatted); return res; }
grep_it(string file,string exp) { int current_line; string lines; current_line=0; matched=0; while((lines=read_file(file,current_line,SIZE))!=0) { grep_string(lines,exp,file); current_line+=SIZE; } if (current_line==0) write("File(s) not found.\n"); else if(moreflag == 1) more(implode(output, "")); return 1; }
bool grep_file(const char* name, const char* regex) { std::ifstream is(name); ATF_REQUIRE(is); bool found = false; std::string line; std::getline(is, line); while (!found && is.good()) { if (grep_string(line, regex)) found = true; else std::getline(is, line); } return found; }