Main Page Class Hierarchy Alphabetical List Compound List Examples

other_codecs.h
1/***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: other_codecs.h,v 1.13 2008-10-07 11:06:26 tat Exp $
6 ***************************************************************************/
7#ifndef _MIMETIC_CODEC_OTHER_CODECS_H_
8#define _MIMETIC_CODEC_OTHER_CODECS_H_
9#include <mimetic/codec/codec_base.h>
10
11 namespace mimetic
12{
13
14/// Pass through codec. Copies input to output
15/*!
16
17 \sa encode decode
18 */
19 struct NullCodec: public unbuffered_codec, public chainable_codec<NullCodec>
20{
21 template<typename OutIt>
22 void process(char c, OutIt& out)
23 {
24 *out = c; ++out;
25 }
26 const char* name() const
27 {
28 return "NullCodec";
29 }
30};
31
32/// Converts input chars to upper case
33/*!
34
35 \sa encode decode
36 */
37 struct ToUpperCase: public unbuffered_codec, public chainable_codec<ToUpperCase>
38{
39 template<typename OutIt>
40 void process(char c, OutIt& out)
41 {
42 enum { offset = 'A' - 'a' };
43 if(c >= 'a' && c <= 'z')
44 c += offset;
45 *out = c;
46 ++out;
47 }
48 const char* name() const
49 {
50 return "ToUpperCase";
51 }
52};
53
54/// Converts input chars to lower case
55/*!
56
57 \sa encode decode
58 */
59 struct ToLowerCase: public unbuffered_codec, public chainable_codec<ToLowerCase>
60{
61 template<typename OutIt>
62 void process(char c, OutIt& out)
63 {
64 enum { offset = 'a' - 'A' };
65 if(c >= 'A' && c <= 'Z')
66 c += offset;
67 *out = c;
68 ++out;
69 }
70 const char* name() const
71 {
72 return "ToLowerCase";
73 }
74};
75
76
77/// Converts any LF (\\n) to CRLF (\\r\\n)
78/*!
79
80 \sa encode decode
81 */
82 struct Lf2CrLf: public unbuffered_codec, public chainable_codec<Lf2CrLf>
83{
84 template<typename OutIt>
85 void process(char c, OutIt& out)
86 {
87 enum { LF = 0xA, CR = 0xD };
88 if(c == LF)
89 {
90 *out = CR; ++out;
91 *out = LF; ++out;
92 } else
93 *out = c; ++out;
94 }
95 const char* name() const
96 {
97 return "Lf2CrLf";
98 }
99};
100
101/// Inserts a new line if the input line is too long
102/*!
103
104 \sa encode decode
105 */
106 struct MaxLineLen: public unbuffered_codec, public chainable_codec<MaxLineLen>
107{
108 MaxLineLen()
109 : m_max(0), m_written(0)
110 {
111 }
112 MaxLineLen(uint m)
113 : m_max(m), m_written(0)
114 {
115 }
116 template<typename OutIt>
117 void process(char c, OutIt& out)
118 {
119 enum { cr = 0xD, lf = 0xA };
120 if(m_max && m_written++ == m_max)
121 {
122 *out = cr; ++out;
123 *out = lf; ++out;
124 m_written = 1;
125 }
126 *out = c;
127 ++out;
128 }
129 const char* name() const
130 {
131 return "MaxLineLen";
132 }
133 private:
134 unsigned int m_max, m_written;
135};
136
137// internal
138 template<typename OutIt>
139 struct oiterator_wrapper:
140 public unbuffered_codec,
141 public chainable_codec<oiterator_wrapper<OutIt> >
142{
143 oiterator_wrapper(): m_pOut(0)
144 {
145 }
146 oiterator_wrapper(OutIt& out): m_pOut(&out)
147 {
148 }
149 template<typename OutIt2>
150 void process(char c, OutIt2& out)
151 {
152 **m_pOut = c; ++(*m_pOut);
153 *out = c; ++out;
154 }
155 const char* name() const
156 {
157 return "oiterator_wrapper";
158 }
159 private:
160 OutIt* m_pOut;
161};
162
163
164}
165#endif
mimetic
Definition body.h:18
mimetic::Lf2CrLf
Converts any LF (\n) to CRLF (\r\n).
Definition other_codecs.h:83
mimetic::NullCodec
Pass through codec. Copies input to output.
Definition other_codecs.h:20
mimetic::ToLowerCase
Converts input chars to lower case.
Definition other_codecs.h:60
mimetic::ToUpperCase
Converts input chars to upper case.
Definition other_codecs.h:38
mimetic::unbuffered_codec
Base class for unbuffered codecs.
Definition codec_base.h:38