FBB::IFilterStreambuf(3bobcat)

Filtering Input Stream Buffer
(libbobcat-dev_4.08.03-x.tar.gz)

2005-2018

NAME

FBB::IFilterStreambuf - Filtering stream buffer initialized by a std::istream object

SYNOPSIS

#include <bobcat/ifilterstreambuf>
Linking option: -lbobcat

DESCRIPTION

FBB::IFilterStreambuf objects may be used as a std::streambuf for std::istream objects, filtering the information produced by those objects.

The class IFilterStreambuf was designed with the openSSL BIO (cf. bio(3ssl)) in mind. Since the BIO concept was developed in the context of the C programming language, BIOs do not support C++ streams. Nonetheless, the concept of a filtering device is an attractive one, and is offered by the FBB::IFilterStreambuf class.

In addition to filtering, IFilterStreambuf objects use split buffers, and thus, depending on the (configurable) size of buffer that is maintained by IFilterStreambuf objects, usually multiple characters read from the IFilterStreambuf can be pushed back again.

The class IFilterStreambuf is an abstract base class. It is used via classes that are derived from IFilterStreambuf, implementing its pure virtual load member (see below at PRIVATE VIRTUAL MEMBER FUNCTIONS).

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

std::streambuf

MEMBER FUNCTIONS

All members of std::streambuf are available, as IFilterStreambuf inherits from this class. Some of the std::streambuf's member are overridden by IFilterStreambuf, see the next section.

Overloaded move and/or copy assignment operators are not available.

PROTECTED CONSTRUCTOR

Copy- and move constructors are not available.

PROTECTED MEMBER FUNCTION

PRIVATE VIRTUAL MEMBER FUNCTIONS

The final attribute was added to the above three members to give IFilterStreambuf objects full control over their own buffers.

EXAMPLE

Here is a class, derived from IFilterStreambuf, filtering out a predefined set of characters. It is used twice to filter digits and vowels, to illustrate chaining of IFilterStreambuf objects.

#include <iostream>
#include <istream>
#include <string>

#include <bobcat/ifilterstreambuf>

class CharFilterStreambuf: public FBB::IFilterStreambuf
{
    std::istream &d_in;         // stream to read from
    std::string d_rmChars;      // chars to rm
    std::string d_buffer;       // locally buffered chars
    size_t const d_maxSize = 100;

    public:
        CharFilterStreambuf(std::istream &in, std::string const &rmChars);

    private:
        bool filter(char const **srcBegin, 
                    char const **srcEnd) override;
};

CharFilterStreambuf::CharFilterStreambuf(std::istream &in, 
                                         std::string const &rmChars)
:
    d_in(in),
    d_rmChars(rmChars)
{
    setBuffer();        // required if peek() must return the 1st
}                       // available character right from the start

bool CharFilterStreambuf::filter(char const **srcBegin, 
                                 char const **srcEnd)
{
    d_buffer.clear();

    while (d_buffer.size() != d_maxSize)
    {
        char ch;
        if (not d_in.get(ch))
            break;
        if (d_rmChars.find(ch) != std::string::npos) // found char to rm
            continue;
        d_buffer.push_back(ch);
    }

    if (d_buffer.empty())
        return false;

    *srcBegin = d_buffer.data();    
    *srcEnd = d_buffer.data() + d_buffer.size();

    return true;
}

int main()
{
    CharFilterStreambuf buf1(std::cin, "1234567890");
    std::istream in1(&buf1);

    CharFilterStreambuf buf2(in1, "AEIOUaeiou");
    std::istream in2(&buf2);

    std::cout << in2.rdbuf();
}





FILES

bobcat/ifdstreambuf - defines the class interface

SEE ALSO

bobcat(7), isymcryptstreambuf(3bobcat), ibase64streambuf(3bobcat), ofilterstreambuf(3bobcat). std::streambuf

BUGS

None reported.

DISTRIBUTION FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).