/* $Id: OutputFilterNormalizeLinebreaks.h 403 2008-07-30 18:22:01Z rbock $ */ #ifndef UTILITY_INPUT_FILTER_NORMALIZE_LINEBREAKS_H #define UTILITY_INPUT_FILTER_NORMALIZE_LINEBREAKS_H // Binaries includes #include // output_filter_tag #include // put namespace Utility { /** OutputFilterNormalizeLinebreaks * * Input filter for boost::iostreams::filtering_ostream * * Used for normalization of different linebreaks: * * \n (unix) * \r\n (dos/windows) * \r (mac) * \r\r\n (result of binary upload and ascii download of dos encoded data via a dos/windows ftp server) * * Everything is normalized to * * \n * * Usage like this: * * boost::iostreams::filtering_ostream correctedOutputStream; * correctedOutputStream.push(OutputFilterNormalizeLinebreaks()); * correctedOutputStream.push(originalOutputStream); * */ class OutputFilterNormalizeLinebreaks { public: typedef char char_type; typedef boost::iostreams::output_filter_tag category; OutputFilterNormalizeLinebreaks() : hasCarriageReturn_(false) { } template bool put(Sink& sink, const char c) { if (c == '\r' || c == '\n') { if (hasCarriageReturn_ == false) { hasCarriageReturn_ = true; boost::iostreams::flush(sink); return boost::iostreams::put(sink, '\n'); } return true; } hasCarriageReturn_ = false; return boost::iostreams::put(sink, c); } template void close(Sink&); private: bool hasCarriageReturn_; }; } // namespace #endif