Atlas-C++
Filter.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000-2001 Michael Day, Stefanus Du Toit
4 
5 // $Id$
6 
7 #ifndef ATLAS_FILTER_H
8 #define ATLAS_FILTER_H
9 
10 #include <iostream>
11 #include <string>
12 
13 namespace Atlas {
14 
29 class Filter
30 {
31  private:
32  Filter(const Filter &); // unimplemented
33  Filter & operator=(const Filter &); // unimplemented
34  public:
35 
36  Filter(Filter* = 0);
37  virtual ~Filter();
38 
39  virtual void begin() = 0;
40  virtual void end() = 0;
41 
42  virtual std::string encode(const std::string&) = 0;
43  virtual std::string decode(const std::string&) = 0;
44 
45  enum Type
46  {
47  CHECKSUM,
48  COMPRESSION,
49  ENCRYPTION
50  };
51 
52  protected:
53 
54  Filter* m_next;
55 };
56 
57 typedef int int_type;
58 
59 class filterbuf : public std::streambuf {
60 
61 public:
62 
63  filterbuf(std::streambuf& buffer,
64  Filter& filter)
65  : m_streamBuffer(buffer), m_filter(filter)
66  {
67  setp(m_outBuffer, m_outBuffer + (m_outBufferSize - 1));
68  setg(m_inBuffer + m_inPutback, m_inBuffer + m_inPutback,
69  m_inBuffer + m_inPutback);
70  }
71 
72  virtual ~filterbuf();
73 
74 protected:
75  static const int m_outBufferSize = 10;
76  char m_outBuffer[m_outBufferSize];
77 
78  static const int m_inBufferSize = 10;
79  static const int m_inPutback = 4;
80  char m_inBuffer[m_inBufferSize];
81 
82  int flushOutBuffer()
83  {
84  int num = pptr() - pbase();
85  std::string encoded = m_filter.encode(std::string(pbase(), pptr()));
86  m_streamBuffer.sputn(encoded.c_str(), (long) encoded.size());
87  pbump(-num);
88  return num;
89  }
90 
91  virtual int_type overflow(int_type c);
92  virtual int_type underflow();
93  virtual int sync();
94 
95 private:
96 
97  std::streambuf& m_streamBuffer;
98  Filter& m_filter;
99 };
100 
101 } // Atlas namespace
102 
103 #endif
Atlas
The Atlas namespace.
Definition: Bridge.h:20
Atlas::Filter
Atlas stream filter.
Definition: Filter.h:30
Atlas::filterbuf
Definition: Filter.h:59

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.