Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/iostreams/example/unix2dos_filter.hpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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