Line | |
---|
1 | #include "commentstream.h" |
---|
2 | |
---|
3 | namespace utillib { |
---|
4 | |
---|
5 | class commentstreambuf : public std::streambuf { |
---|
6 | std::streambuf * const m_sbuf; |
---|
7 | char m_buf[512]; |
---|
8 | const int m_start; |
---|
9 | const int m_end; |
---|
10 | bool m_comment; |
---|
11 | public: |
---|
12 | commentstreambuf(std::streambuf *, char start, char end); |
---|
13 | |
---|
14 | int underflow(); |
---|
15 | }; |
---|
16 | |
---|
17 | commentstreambuf::commentstreambuf(std::streambuf *sb, char start, char end) |
---|
18 | : m_sbuf(sb), m_start(static_cast<unsigned char>(start)), |
---|
19 | m_end(static_cast<unsigned char>(end)), m_comment(false) |
---|
20 | { |
---|
21 | setg(m_buf, m_buf, m_buf); |
---|
22 | } |
---|
23 | |
---|
24 | int commentstreambuf::underflow() |
---|
25 | { |
---|
26 | char *p = m_buf; |
---|
27 | int ch; |
---|
28 | while (p != m_buf + sizeof m_buf && (ch = m_sbuf->sgetc()) != EOF) { |
---|
29 | m_sbuf->sbumpc(); |
---|
30 | |
---|
31 | if (ch == m_start) m_comment = true; |
---|
32 | else if (ch == m_end) m_comment = false; |
---|
33 | |
---|
34 | if (!m_comment) |
---|
35 | *p++ = ch; |
---|
36 | } |
---|
37 | |
---|
38 | if (p == m_buf) |
---|
39 | return EOF; |
---|
40 | |
---|
41 | setg(m_buf, m_buf, p); |
---|
42 | return *m_buf; |
---|
43 | } |
---|
44 | |
---|
45 | commentstream::commentstream(std::istream &s, char start, char end) |
---|
46 | : std::basic_istream(new commentstreambuf(s.rdbuf(), start, end)) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | commentstream::~commentstream() |
---|
51 | { |
---|
52 | delete rdbuf(); |
---|
53 | } |
---|
54 | |
---|
55 | } // namespace utillib |
---|
Note: See
TracBrowser
for help on using the repository browser.