+class LineLimitedTextOutputStream : public TextOutputStream
+{
+ TextOutputStream& outputStream;
+ std::size_t count;
+public:
+ LineLimitedTextOutputStream(TextOutputStream& outputStream, std::size_t count)
+ : outputStream(outputStream), count(count)
+ {
+ }
+ std::size_t write(const char* buffer, std::size_t length)
+ {
+ if(count != 0)
+ {
+ const char* p = buffer;
+ const char* end = buffer+length;
+ for(;;)
+ {
+ p = std::find(p, end, '\n');
+ if(p == end)
+ {
+ break;
+ }
+ ++p;
+ if(--count == 0)
+ {
+ length = p - buffer;
+ break;
+ }
+ }
+ outputStream.write(buffer, length);
+ }
+ return length;
+ }
+};
+