File: filters.hpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (185 lines) | stat: -rw-r--r-- 5,919 bytes parent folder | download | duplicates (13)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2004-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.

// Contains the definitions of several constants used by the test program.

#ifndef BOOST_IOSTREAMS_TEST_FILTERS_HPP_INCLUDED
#define BOOST_IOSTREAMS_TEST_FILTERS_HPP_INCLUDED

#include <boost/config.hpp>                 
#include <algorithm>                            // min.
#include <cctype>                               // to_upper, to_lower.
#include <cstdlib>                              // to_upper, to_lower (VC6).
#include <cstddef>                              // ptrdiff_t.
#include <vector>
#include <boost/iostreams/char_traits.hpp>
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/constants.hpp>
#include <boost/iostreams/detail/iostream.hpp>  // seekdir, streamsize.
#include <boost/iostreams/detail/streambuf.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/pipeline.hpp>
#include <boost/type_traits/is_convertible.hpp>
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::toupper; using ::tolower; }
#endif

// Must come last.
#include <boost/iostreams/detail/config/disable_warnings.hpp>

namespace boost { namespace iostreams { namespace test {

struct toupper_filter : public input_filter {
    template<typename Source>
    int get(Source& s) 
    { 
        int c = boost::iostreams::get(s);
        return c != EOF && c != WOULD_BLOCK ?
            std::toupper((unsigned char) c) :
            c;
    }
};
BOOST_IOSTREAMS_PIPABLE(toupper_filter, 0)

struct tolower_filter : public output_filter {
    template<typename Sink>
    bool put(Sink& s, char c)
    { 
        return boost::iostreams::put(
                   s, (char) std::tolower((unsigned char) c)
               ); 
    }
};
BOOST_IOSTREAMS_PIPABLE(tolower_filter, 0)

struct toupper_multichar_filter : public multichar_input_filter {
    template<typename Source>
    std::streamsize read(Source& s, char* buf, std::streamsize n)
    {
        std::streamsize result = boost::iostreams::read(s, buf, n);
        if (result == -1)
            return -1;
        for (int z = 0; z < result; ++z)
            buf[z] = (char) std::toupper((unsigned char) buf[z]);
        return result;
    }
};
BOOST_IOSTREAMS_PIPABLE(toupper_multichar_filter, 0)

struct tolower_multichar_filter : public multichar_output_filter {
    template<typename Sink>
    std::streamsize write(Sink& s, const char* buf, std::streamsize n)
    {
        std::streamsize result;
        for (result = 0; result < n; ++result) {
            char c = (char) std::tolower((unsigned char) buf[result]);
            if (!boost::iostreams::put(s, c))
                break;
        }
        return result;
    }
};
BOOST_IOSTREAMS_PIPABLE(tolower_multichar_filter, 0)

struct padding_filter : dual_use_filter {
    explicit padding_filter(char pad_char) 
        : pad_char_(pad_char), use_pad_char_(false), eof_(false)
        { }

    template<typename Source>
    int get(Source& src) 
    { 
        int result;
        if (use_pad_char_) {
            result = eof_ ? EOF : pad_char_;
            use_pad_char_ = false;
        } else {
            result = boost::iostreams::get(src);
            if (result != EOF && result != WOULD_BLOCK)
                use_pad_char_ = true;
            eof_ = result == EOF;
        }
        return result;
    }

    template<typename Sink>
    bool put(Sink& s, char c) 
    { 
        if (use_pad_char_) {
            if (!boost::iostreams::put(s, pad_char_))
                return false;
            use_pad_char_ = false;
        } 
        if (!boost::iostreams::put(s, c))
            return false;
        if (!boost::iostreams::put(s, pad_char_))
            use_pad_char_ = true;
        return true;
    }

    char  pad_char_;
    bool  use_pad_char_;
    bool  eof_;
};
BOOST_IOSTREAMS_PIPABLE(padding_filter, 0)

struct flushable_output_filter {
    typedef char char_type;
    struct category 
        : output_filter_tag, 
          flushable_tag
        { };
    template<typename Sink>
    bool put(Sink&, char c) 
    { 
        buf_.push_back(c); 
        return true; 
    }
    template<typename Sink>
    bool flush(Sink& s) 
    { 
        if (!buf_.empty()) {
            boost::iostreams::write(s, &buf_[0], (std::streamsize) buf_.size());
            buf_.clear();
        }
        return true;
    }
    std::vector<char> buf_;
};
BOOST_IOSTREAMS_PIPABLE(flushable_output_filter, 0)

struct identity_seekable_filter : filter<seekable> {
    template<typename Source>
    int get(Source& s) { return boost::iostreams::get(s); }

    template<typename Sink>
    bool put(Sink& s, char c) { return boost::iostreams::put(s, c); }

    template<typename Device>
    std::streampos seek(Device& d, stream_offset off, BOOST_IOS::seekdir way)
    { return boost::iostreams::seek(d, off, way); }
};
BOOST_IOSTREAMS_PIPABLE(identity_seekable_filter, 0)

struct identity_seekable_multichar_filter : multichar_filter<seekable> {
    template<typename Source>
    std::streamsize read(Source& s, char* buf, std::streamsize n)
    { return boost::iostreams::read(s, buf, n); }
    template<typename Sink>
    std::streamsize write(Sink& s, const char* buf, std::streamsize n)
    { return boost::iostreams::write(s, buf, n); }
    template<typename Device>
    std::streampos seek(Device& d, stream_offset off, BOOST_IOS::seekdir way)
    { return boost::iostreams::seek(d, off, way); }
};
BOOST_IOSTREAMS_PIPABLE(identity_seekable_multichar_filter, 0)

} } } // End namespaces detail, iostreams, boost.

#include <boost/iostreams/detail/config/enable_warnings.hpp>

#endif // #ifndef BOOST_IOSTREAMS_TEST_FILTERS_HPP_INCLUDED