#include #include #include int main(int argc, char* argv[]) { std::istream& inputstream = std::cin; boost::regex regex(argv[1]); std::size_t buffersize = 4; // for demonstration, normally use something like 4096 std::size_t used_buffer = 0; // use unique pointer instead of "plain" pointer so that there is no memory leak in case of exception std::unique_ptr buffer(new char[buffersize]); do { inputstream.read(buffer.get() + used_buffer, buffersize - used_buffer); char const* const buffer_end = buffer.get() + used_buffer + inputstream.gcount(); char const* buffer_handled_until = buffer.get(); // the whole following thing could be much easier by using a bidirectional input iterator here, but: // do not do this because input "file" could be a named pipe, stream or similar (no backwards iterating would be possible!) // so you have to manage the buffer (and release parts of it) yourself // for a more efficient solution (hopefully in the near future) see boost::cregex_iterator curr_match(buffer.get(), buffer_end, regex, boost::match_default | boost::match_partial); // add element to set when ... while (curr_match != boost::cregex_iterator() && curr_match->begin()->matched && // ... match is a full match and ... (!inputstream || // ... when file is at end or ... // (see next line) when match does not touch end of buffer (otherwise element could be longer, e. g. partial match) !boost::regex_match(curr_match->begin()->first, buffer_end, regex, boost::match_default | boost::match_partial))) { std::cout << curr_match->str() << "\n"; ++curr_match; } /* while (curr_match != boost::cregex_iterator()) { std::cout << curr_match->str() << " " << curr_match->size() << " " << (*curr_match)[1].matched << " " << ((*curr_match)[1].second - (*curr_match)[1].first) << "\n"; ++curr_match; } */ // the last match is always a partial match except full match touches buffer end (or buffer is empty) // so mark begin of last match as new begin of buffer when filling it up in next round of do-while-loop buffer_handled_until = (curr_match != boost::cregex_iterator() ? curr_match->begin()->first : buffer_end); used_buffer = buffer_end - buffer_handled_until; if (buffer_handled_until == buffer.get()) { // if current element fills the whole buffer, buffer is too small and thus doubled buffersize *= 2; std::unique_ptr new_buffer(new char[buffersize]); std::memmove(new_buffer.get(), buffer_handled_until, used_buffer); buffer = std::move(new_buffer); } else { // move the rest of new element (buffer_handled_until) to beginning of buffer and mark it as used std::memmove(buffer.get(), buffer_handled_until, used_buffer); } } while (inputstream); }