1 | // |
---|
2 | // Base64Encoder.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Base64Encoder.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: Base64 |
---|
9 | // |
---|
10 | // Definition of class Base64Encoder. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Base64Encoder_INCLUDED |
---|
40 | #define Foundation_Base64Encoder_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/UnbufferedStreamBuf.h" |
---|
45 | #include <ostream> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf |
---|
52 | /// This streambuf base64-encodes all data written |
---|
53 | /// to it and forwards it to a connected |
---|
54 | /// ostream. |
---|
55 | { |
---|
56 | public: |
---|
57 | Base64EncoderBuf(std::ostream& ostr); |
---|
58 | ~Base64EncoderBuf(); |
---|
59 | |
---|
60 | int close(); |
---|
61 | /// Closes the stream buffer. |
---|
62 | |
---|
63 | void setLineLength(int lineLength); |
---|
64 | /// Specify the line length. |
---|
65 | /// |
---|
66 | /// After the given number of characters have been written, |
---|
67 | /// a newline character will be written. |
---|
68 | /// |
---|
69 | /// Specify 0 for an unlimited line length. |
---|
70 | |
---|
71 | int getLineLength() const; |
---|
72 | /// Returns the currently set line length. |
---|
73 | |
---|
74 | private: |
---|
75 | int writeToDevice(char c); |
---|
76 | |
---|
77 | unsigned char _group[3]; |
---|
78 | int _groupLength; |
---|
79 | int _pos; |
---|
80 | int _lineLength; |
---|
81 | std::ostream& _ostr; |
---|
82 | |
---|
83 | static const unsigned char OUT_ENCODING[64]; |
---|
84 | |
---|
85 | friend class Base64DecoderBuf; |
---|
86 | |
---|
87 | Base64EncoderBuf(const Base64EncoderBuf&); |
---|
88 | Base64EncoderBuf& operator = (const Base64EncoderBuf&); |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | class Foundation_API Base64EncoderIOS: public virtual std::ios |
---|
93 | /// The base class for Base64Encoder. |
---|
94 | /// |
---|
95 | /// This class is needed to ensure the correct initialization |
---|
96 | /// order of the stream buffer and base classes. |
---|
97 | { |
---|
98 | public: |
---|
99 | Base64EncoderIOS(std::ostream& ostr); |
---|
100 | ~Base64EncoderIOS(); |
---|
101 | int close(); |
---|
102 | Base64EncoderBuf* rdbuf(); |
---|
103 | |
---|
104 | protected: |
---|
105 | Base64EncoderBuf _buf; |
---|
106 | |
---|
107 | private: |
---|
108 | Base64EncoderIOS(const Base64EncoderIOS&); |
---|
109 | Base64EncoderIOS& operator = (const Base64EncoderIOS&); |
---|
110 | }; |
---|
111 | |
---|
112 | |
---|
113 | class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream |
---|
114 | /// This ostream base64-encodes all data |
---|
115 | /// written to it and forwards it to |
---|
116 | /// a connected ostream. |
---|
117 | /// Always call close() when done |
---|
118 | /// writing data, to ensure proper |
---|
119 | /// completion of the encoding operation. |
---|
120 | { |
---|
121 | public: |
---|
122 | Base64Encoder(std::ostream& ostr); |
---|
123 | ~Base64Encoder(); |
---|
124 | |
---|
125 | private: |
---|
126 | Base64Encoder(const Base64Encoder&); |
---|
127 | Base64Encoder& operator = (const Base64Encoder&); |
---|
128 | }; |
---|
129 | |
---|
130 | |
---|
131 | } // namespace Poco |
---|
132 | |
---|
133 | |
---|
134 | #endif // Foundation_Base64Encoder_INCLUDED |
---|