Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

other_codecs.h

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