Boost logo

Boost Users :

From: Stefan Arentz (stefan.arentz_at_[hidden])
Date: 2008-02-04 12:33:18


I'm looking for some help with the following code where I try to
implement an aggregate filter and a sink. This super simple example is
supposed to print the xorred input but instead it prints:

The result is:
do_filter
write: 01+x1+x,*!x76=

Does anyone understand why the filtering and sink.write are not done
as part of the copy operation? The last two lines should be printed as
part of the copy process! It seems they are executed as part of
destructing these objects instead?

This stuff should be so simple :-/

 S.

#include <cctype>
#include <iostream>
#include <memory>
#include <string>

#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/aggregate.hpp>
#include <boost/iostreams/filtering_stream.hpp>

///

template< typename Ch, typename Alloc = std::allocator<Ch> >
class basic_xor_filter : public boost::iostreams::aggregate_filter<Ch, Alloc>
{
   private:

      typedef boost::iostreams::aggregate_filter<Ch, Alloc>
     base_type;

   public:

      typedef typename base_type::char_type char_type;
      typedef typename base_type::category category;
      typedef std::basic_string<Ch> string_type;

   public:

      basic_xor_filter(Ch key)
         : key_(key)
      {
      }

   private:

      typedef typename base_type::vector_type vector_type;

      void do_filter(const vector_type& src, vector_type& dest)
      {
         std::cout << "do_filter" << std::endl;

         for (size_t i = 0; i < src.size(); ++i) {
            dest.push_back(src[i] ^ key_);
         }
      }

   private:

      Ch key_;
};

BOOST_IOSTREAMS_PIPABLE(basic_xor_filter, 1)

typedef basic_xor_filter<char> xor_filter;
typedef basic_xor_filter<wchar_t> wxor_filter;

///

class my_string_sink : public boost::iostreams::sink
{
   public:

      my_string_sink(std::string& buffer)
         : buffer_(buffer)
      {
      }

   public:

      std::string const& str()
      {
         return buffer_;
      }

   public:

      std::streamsize write(const char* s, std::streamsize n)
      {
         std::cout << "write: " << std::string(s,n) << std::endl;
         buffer_.append(s, n);
         return n;
      }

   private:

      std::string& buffer_;
};

///

int main()
{
   { // This does not work
      boost::iostreams::filtering_istream in;
      in.push(boost::make_iterator_range("This is try one"));

      std::string result;
      boost::iostreams::filtering_ostream out;
      out.push(xor_filter('X'));
      out.push(my_string_sink(result));

      boost::iostreams::copy(in, out);

      std::cout << "The result is: " << result << std::endl;
   }
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net