Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

other_codecs.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : [email protected]
00004 
00005     $Id: other__codecs_8h-source.html,v 1.4 2006-03-12 12:28:31 tat Exp $
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_CODEC_OTHER_CODECS_H_
00017 #define _MIMETIC_CODEC_OTHER_CODECS_H_
00018 #include <mimetic/codec/codec_base.h>
00019 
00020 namespace mimetic
00021 {
00022 
00023 /// Pass through codec. Copies input to output
00024 /*!
00025 
00026  \sa encode decode
00027  */
00028 struct NullCodec: public unbuffered_codec, public chainable_codec<NullCodec>
00029 {
00030     template<typename OutIt>
00031     void process(char c, OutIt& out)
00032     {
00033         *out = c; ++out;    
00034     }
00035     const char* name() const 
00036     {    
00037         return "NullCodec"; 
00038     }
00039 };
00040 
00041 /// Converts input chars to upper case
00042 /*!
00043 
00044  \sa encode decode
00045  */
00046 struct ToUpperCase: public unbuffered_codec, public chainable_codec<ToUpperCase>
00047 {
00048     template<typename OutIt>
00049     void process(char c, OutIt& out)
00050     {
00051         enum { offset = 'A' - 'a' };
00052         if(c >= 'a' && c <= 'z')
00053             c += offset;
00054         *out = c;
00055         ++out;
00056     }
00057     const char* name() const
00058     {    
00059         return "ToUpperCase"; 
00060     }
00061 };
00062 
00063 /// Converts input chars to lower case
00064 /*!
00065 
00066  \sa encode decode
00067  */
00068 struct ToLowerCase: public unbuffered_codec, public chainable_codec<ToLowerCase>
00069 {
00070     template<typename OutIt>
00071     void process(char c, OutIt& out)
00072     {
00073         enum { offset = 'a' - 'A' };
00074         if(c >= 'A' && c <= 'Z')
00075             c += offset;
00076         *out = c;
00077         ++out;
00078     }
00079     const char* name() const 
00080     {    
00081         return "ToLowerCase";
00082     }
00083 };
00084 
00085 
00086 /// Converts any LF (\\n) to CRLF (\\r\\n)
00087 /*!
00088 
00089  \sa encode decode
00090  */
00091 struct Lf2CrLf: public unbuffered_codec, public chainable_codec<Lf2CrLf>
00092 {
00093     template<typename OutIt>
00094     void process(char c, OutIt& out)
00095     {
00096         enum { LF = 0xA, CR = 0xD };
00097         if(c == LF)
00098         {
00099             *out = CR; ++out;
00100             *out = LF; ++out; 
00101         } else
00102             *out = c; ++out;
00103     }
00104     const char* name() const
00105     {    
00106         return "Lf2CrLf"; 
00107     }
00108 };
00109 
00110 /// Inserts a new line if the input line is too long
00111 /*!
00112 
00113  \sa encode decode
00114  */
00115 struct MaxLineLen: public unbuffered_codec, public chainable_codec<MaxLineLen>
00116 {
00117     MaxLineLen()
00118     : m_max(0), m_written(0)
00119     {
00120     }
00121     MaxLineLen(uint m)
00122     : m_max(m), m_written(0)
00123     {
00124     }
00125     template<typename OutIt>
00126     void process(char c, OutIt& out)
00127     {
00128         enum { cr = 0xD, lf = 0xA };
00129         if(m_max && m_written++ == m_max)
00130         {
00131             *out = cr; ++out;
00132             *out = lf; ++out;
00133             m_written = 1;
00134         }
00135         *out = c;
00136         ++out;
00137     }
00138     const char* name() const
00139     {    
00140         return "MaxLineLen"; 
00141     }
00142 private:
00143     unsigned int m_max, m_written;
00144 };
00145 
00146 // internal
00147 template<typename OutIt>
00148 struct oiterator_wrapper: 
00149     public unbuffered_codec, 
00150     public chainable_codec<oiterator_wrapper<OutIt> >
00151 {
00152     oiterator_wrapper(): m_pOut(0)
00153     {
00154     }
00155     oiterator_wrapper(OutIt& out): m_pOut(&out)
00156     {
00157     }
00158     template<typename OutIt2>
00159     void process(char c, OutIt2& out)
00160     {
00161         **m_pOut = c; ++(*m_pOut);
00162         *out = c; ++out;
00163     }
00164     const char* name() const
00165     {    
00166         return "oiterator_wrapper"; 
00167     }
00168 private:
00169     OutIt* m_pOut;    
00170 };
00171 
00172 
00173 }
00174 #endif