Detecting start of a MIME document's body

Most of the time, x::mime::newline_iter's output iterator is a x::mime::bodystart_iter which is an output iterator over a sequence of ints. x::mime::bodystart_iter's template parameter is an output iterator class. It iterates over the same sequence as x::mime::bodystart_iter iterates over, unchanged, except that the first time that x::mime::bodystart_iter iterates over two x::mime::newline_start/x::mime::newline_end tuples in a row, x::mime::bodystart_iter adds an x::mime::body_start value to the output sequence, after the second tuple. This marks the start of a MIME document's body, following the first empty line:

#include <x/mime/newlineiter.H>
#include <x/mime/bodystartiter.H>
#include <vector>
#include <iterator>
#include <iostream>

int main()
{
	std::vector<int> seq;

	typedef std::back_insert_iterator<std::vector<int>> ins_iter_t;

	typedef x::mime::bodystart_iter<ins_iter_t> bodystart_iter_t;

	typedef x::mime::newline_iter<bodystart_iter_t> newline_iter_t;

	auto iter=std::copy(std::istreambuf_iterator<char>(std::cin),
			    std::istreambuf_iterator<char>(),
			    newline_iter_t::create
			    (bodystart_iter_t::create
			     (ins_iter_t(seq))));

	iter.get()->eof();

	ins_iter_t value=iter.get()->iter.get()->iter;

	for (int c:seq)
	{
		if (x::mime::nontoken(c))
			std::cout << (char)c;
		else
			std::cout << '<' << c << '>';
	}
	std::cout << std::endl << std::flush;
	return 0;
}

Instantiating a x::mime::bodystart_iter results in an output iterator, but x::mime::bodystart_iter gets instantiated by create() like a reference-counted object (because, internally, it is). Like x::mime::newline_iter, the output iterator's get() method returns a reference to the underlying reference-counted method, with an iter class member giving the current value of the underlying output iterator. In this example, std::copy returns the new value of the output iterator, newline_iter_t, a.k.a. x::mime::newline_iter<bodystart_iter_t>. Its get()->iter returns its underlying output iterator, bodystart_iter_t, a.k.a. x::mime::bodystart_iter<ins_iter_t>, and its own get()->iter returns the underlying ins_iter_t. Sample output from this example:


$ cat bodystart.txt
Subject: test

test
$ ./bodystart <bodystart.txt
Subject: test<256>
<257><256>
<257><258>test<256>
<257><-1>
      

x::mime::body_start, or 258 gets inserted into the output sequence after the second consecutive newline sequence, marking the start the MIME section's body. If two consecutive newline sequences do not appear in the sequence, a x::mime::body_start still gets inserted into the output sequence before the x::mime::eof:


$ cat bodystart.txt
Subject: test
$ ./bodystart <bodystart.txt
Subject: test<256>
<257><258><-1>