Created
November 26, 2014 00:18
-
-
Save dgodfrey206/d9fef49e912e7480e679 to your computer and use it in GitHub Desktop.
line_inserter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <fstream> | |
class insertbuf : public std::streambuf | |
{ | |
public: | |
insertbuf(std::ostream& os, std::string const& data) | |
: m_stream(os) | |
, msg(data) | |
{ } | |
int_type overflow(int_type c) | |
{ | |
static bool newline(true); | |
if (c != traits_type::eof() && newline) | |
{ | |
m_stream.write(msg.data(), msg.size()) | |
? traits_type::not_eof(c) : traits_type::eof(); | |
} | |
newline = traits_type::eq(traits_type::to_char_type(c), '\n'); | |
return m_stream.put(traits_type::to_char_type(c)) | |
? traits_type::not_eof(c) : traits_type::eof(); | |
} | |
int sync() { return m_stream.rdbuf()->pubsync() ? 0 : -1; } | |
private: | |
std::ostream& m_stream; | |
std::string msg; | |
}; | |
class line_inserter : private insertbuf, public std::ostream | |
{ | |
public: | |
line_inserter(std::ostream& os, std::string const& data) | |
: insertbuf(os, data) | |
, std::ostream(this) | |
{ } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment