Boost logo

Boost Users :

From: Stefan Arentz (stefan.arentz_at_[hidden])
Date: 2008-02-04 08:03:35


I've written a really simple aggregate_filter that runs the world
famous and highly secret ROT13 encryption. This filter works fine when
I use a file_sink and file_source. But I really just want to run this
on a bunch of string buffers. Unfortunately it does not work, the
destination string is always empty. Does anyone understand why?

Here's the code, tested against Boost 1.34.1 on OS X 10.5.1:

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

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

template< typename Ch, typename Alloc = std::allocator<Ch> >
class basic_rot13_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_rot13_filter()
      {
      }

   private:

      typedef typename base_type::vector_type vector_type;

      void do_filter(const vector_type& src, vector_type& dest)
      {
         for (size_t i = 0; i < src.size(); ++i) {
            dest.push_back(src[i] + 13);
         }
      }
};

typedef basic_rot13_filter<char> rot13_filter;
typedef basic_rot13_filter<wchar_t> wrot13_filter;

int main()
{
   std::string src = "SourceText";
   boost::iostreams::filtering_istream in;
   in.push(boost::make_iterator_range(src));

   std::string dst;
   boost::iostreams::filtering_ostream out;
   out.push(rot13_filter());
   out.push(boost::iostreams::back_inserter(dst));

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

   std::cout << dst << std::endl;

   return 0;
}


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