Atlas-C++
|
00001 // This file may be redistributed and modified only under the terms of 00002 // the GNU Lesser General Public License (See COPYING for details). 00003 // Copyright (C) 2000-2001 Michael Day, Stefanus Du Toit 00004 00005 // $Id$ 00006 00007 #ifndef ATLAS_FILTER_H 00008 #define ATLAS_FILTER_H 00009 00010 #include <iostream> 00011 #include <string> 00012 00013 namespace Atlas { 00014 00029 class Filter 00030 { 00031 public: 00032 00033 Filter(Filter* = 0); 00034 virtual ~Filter(); 00035 00036 virtual void begin() = 0; 00037 virtual void end() = 0; 00038 00039 virtual std::string encode(const std::string&) = 0; 00040 virtual std::string decode(const std::string&) = 0; 00041 00042 enum Type 00043 { 00044 CHECKSUM, 00045 COMPRESSION, 00046 ENCRYPTION 00047 }; 00048 00049 protected: 00050 00051 Filter* m_next; 00052 }; 00053 00054 typedef int int_type; 00055 00056 class filterbuf : public std::streambuf { 00057 00058 public: 00059 00060 filterbuf(std::streambuf& buffer, 00061 Filter& filter) 00062 : m_streamBuffer(buffer), m_filter(filter) 00063 { 00064 setp(m_outBuffer, m_outBuffer + (m_outBufferSize - 1)); 00065 setg(m_inBuffer + m_inPutback, m_inBuffer + m_inPutback, 00066 m_inBuffer + m_inPutback); 00067 } 00068 00069 virtual ~filterbuf(); 00070 00071 protected: 00072 static const int m_outBufferSize = 10; 00073 char m_outBuffer[m_outBufferSize]; 00074 00075 static const int m_inBufferSize = 10; 00076 static const int m_inPutback = 4; 00077 char m_inBuffer[m_inBufferSize]; 00078 00079 int flushOutBuffer() 00080 { 00081 int num = pptr() - pbase(); 00082 std::string encoded = m_filter.encode(std::string(pbase(), pptr())); 00083 m_streamBuffer.sputn(encoded.c_str(), (long) encoded.size()); 00084 pbump(-num); 00085 return num; 00086 } 00087 00088 virtual int_type overflow(int_type c); 00089 virtual int_type underflow(); 00090 virtual int sync(); 00091 00092 private: 00093 00094 std::streambuf& m_streamBuffer; 00095 Filter& m_filter; 00096 }; 00097 00098 } // Atlas namespace 00099 00100 #endif
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.