forked from boostorg/iostreams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_comments_filter.hpp
222 lines (194 loc) · 5.79 KB
/
shell_comments_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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// (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.
// Adapted from an example of James Kanze, with suggestions from Peter Dimov.
// See https://web.archive.org/web/20041222094942/http://www.gabi-soft.fr/codebase-en.html.
#ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED
#define BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED
#include <cassert>
#include <cstdio> // EOF.
#include <iostream> // cin, cout.
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/detail/ios.hpp> // BOOST_IOS.
#include <boost/iostreams/filter/stdio.hpp>
#include <boost/iostreams/operations.hpp>
namespace boost { namespace iostreams { namespace example {
class shell_comments_stdio_filter : public stdio_filter {
public:
explicit shell_comments_stdio_filter(char comment_char = '#')
: comment_char_(comment_char)
{ }
private:
void do_filter()
{
bool skip = false;
int c;
while ((c = std::cin.get()) != EOF) {
skip = c == comment_char_ ?
true :
c == '\n' ?
false :
skip;
if (!skip)
std::cout.put(c);
}
}
char comment_char_;
};
class shell_comments_input_filter : public input_filter {
public:
explicit shell_comments_input_filter(char comment_char = '#')
: comment_char_(comment_char), skip_(false)
{ }
template<typename Source>
int get(Source& src)
{
int c;
while (true) {
if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
break;
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (!skip_)
break;
}
return c;
}
template<typename Source>
void close(Source&) { skip_ = false; }
private:
char comment_char_;
bool skip_;
};
class shell_comments_output_filter : public output_filter {
public:
explicit shell_comments_output_filter(char comment_char = '#')
: comment_char_(comment_char), skip_(false)
{ }
template<typename Sink>
bool put(Sink& dest, int c)
{
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (skip_)
return true;
return iostreams::put(dest, c);
}
template<typename Source>
void close(Source&) { skip_ = false; }
private:
char comment_char_;
bool skip_;
};
class shell_comments_dual_use_filter : public dual_use_filter {
public:
explicit shell_comments_dual_use_filter(char comment_char = '#')
: comment_char_(comment_char), skip_(false)
{ }
template<typename Source>
int get(Source& src)
{
int c;
while (true) {
if ((c = boost::iostreams::get(src)) == EOF || c == WOULD_BLOCK)
break;
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (!skip_)
break;
}
return c;
}
template<typename Sink>
bool put(Sink& dest, int c)
{
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (skip_)
return true;
return iostreams::put(dest, c);
}
template<typename Device>
void close(Device&, BOOST_IOS::openmode) { skip_ = false; }
private:
char comment_char_;
bool skip_;
};
class shell_comments_multichar_input_filter : public multichar_input_filter {
public:
explicit shell_comments_multichar_input_filter(char comment_char = '#')
: comment_char_(comment_char), skip_(false)
{ }
template<typename Source>
std::streamsize read(Source& src, char* s, std::streamsize n)
{
for (std::streamsize z = 0; z < n; ++z) {
int c;
while (true) {
if ((c = boost::iostreams::get(src)) == EOF)
return z != 0 ? z : -1;
else if (c == WOULD_BLOCK)
return z;
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (!skip_)
break;
}
s[z] = c;
}
return n;
}
template<typename Source>
void close(Source&) { skip_ = false; }
private:
char comment_char_;
bool skip_;
};
class shell_comments_multichar_output_filter : public multichar_output_filter {
public:
explicit shell_comments_multichar_output_filter(char comment_char = '#')
: comment_char_(comment_char), skip_(false)
{ }
template<typename Sink>
std::streamsize write(Sink& dest, const char* s, std::streamsize n)
{
std::streamsize z;
for (z = 0; z < n; ++z) {
int c = s[z];
skip_ = c == comment_char_ ?
true :
c == '\n' ?
false :
skip_;
if (skip_)
continue;
if (!iostreams::put(dest, c))
break;
}
return z;
}
template<typename Source>
void close(Source&) { skip_ = false; }
private:
char comment_char_;
bool skip_;
};
} } } // End namespaces example, iostreams, boost.
#endif // #ifndef BOOST_IOSTREAMS_SHELL_COMMENTS_FILTER_HPP_INCLUDED