-
Notifications
You must be signed in to change notification settings - Fork 118
/
unix2dos_filter.hpp
99 lines (83 loc) · 2.53 KB
/
unix2dos_filter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2005-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
// See http://www.boost.org/libs/iostreams for documentation.
#ifndef BOOST_IOSTREAMS_UNIX2DOS_FILTER_FILTER_HPP_INCLUDED
#define BOOST_IOSTREAMS_UNIX2DOS_FILTER_FILTER_HPP_INCLUDED
#include <cassert>
#include <cstdio> // EOF.
#include <iostream> // cin, cout.
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/filter/stdio.hpp>
#include <boost/iostreams/operations.hpp>
namespace boost { namespace iostreams { namespace example {
class unix2dos_stdio_filter : public stdio_filter {
private:
void do_filter()
{
int c;
while ((c = std::cin.get()) != EOF) {
if (c == '\n')
std::cout.put('\r');
std::cout.put(c);
}
}
};
class unix2dos_input_filter : public input_filter {
public:
unix2dos_input_filter() : has_linefeed_(false) { }
template<typename Source>
int get(Source& src)
{
if (has_linefeed_) {
has_linefeed_ = false;
return '\n';
}
int c;
if ((c = iostreams::get(src)) == '\n') {
has_linefeed_ = true;
return '\r';
}
return c;
}
template<typename Source>
void close(Source&) { has_linefeed_ = false; }
private:
bool has_linefeed_;
};
class unix2dos_output_filter : public output_filter {
public:
unix2dos_output_filter() : has_linefeed_(false) { }
template<typename Sink>
bool put(Sink& dest, int c)
{
if (c == '\n')
return has_linefeed_ ?
put_char(dest, '\n') :
put_char(dest, '\r') ?
this->put(dest, '\n') :
false;
return iostreams::put(dest, c);
}
template<typename Sink>
void close(Sink&) { has_linefeed_ = false; }
private:
template<typename Sink>
bool put_char(Sink& dest, int c)
{
bool result;
if ((result = iostreams::put(dest, c)) == true) {
has_linefeed_ =
c == '\r' ?
true :
c == '\n' ?
false :
has_linefeed_;
}
return result;
}
bool has_linefeed_;
};
} } } // End namespaces example, iostreams, boost.
#endif // #ifndef BOOST_IOSTREAMS_UNIX2DOS_FILTER_FILTER_HPP_INCLUDED